场景套件 – 如何以编程方式调整'Sun elevation'参数?



我已经使用以下代码将SceneKit合并到我的应用程序中的SwiftUI视图中:

import SwiftUI
import SceneKit
struct ContentView: View {
let scene = SCNScene(named: "art.scnassets/ship.scn")!
let cameraNode = SCNNode()
let lightNode = SCNNode()
let ambientLightNode = SCNNode()
@State var ship: SCNNode = SCNNode()

var body: some View {
ZStack {
SceneView(
scene: scene,
pointOfView: cameraNode,
options: [.autoenablesDefaultLighting, .allowsCameraControl ]
)                
Text("Hello, world!")
.padding()    
}
.edgesIgnoringSafeArea(.all)
.onAppear {
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
lightNode.light = SCNLight()
lightNode.light!.type = .omni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = .ambient
ambientLightNode.light!.color = UIColor.darkGray

scene.rootNode.addChildNode(ambientLightNode)

ship = scene.rootNode.childNode(withName: "ship", 
recursively: true)!
ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0,
y: 0.1,
z: 0,
duration: 1)))
}            
}
}

我知道您可以使用scene.fogEndDistance和其他排列来使用Swift以编程方式调整雾。我在玩场景背景,但找不到任何可以改变太阳高度的方法。

如何使用Swift以编程方式更改SceneKit场景的太阳高程?

您可以同时使用MDL Sky Box进行场景照明和BG:

sceneView.scene?.background.contents = MDLSkyCubeTexture(name: "procedural sky",
channelEncoding: .float32,
textureDimensions: vector_int2(512,512),
turbidity: 0.12,
sunElevation: 1.00,
upperAtmosphereScattering: 0.05,
groundAlbedo: 0.32)
sceneView.scene?.lightingEnvironment.contents = sceneView.scene?.background.contents
sceneView.scene?.lightingEnvironment.intensity = 2.0

最新更新