SceneKit – 如何以编程方式调整 'Sun elevation' 参数?
SceneKit – How can I programmatically adjust 'Sun elevation' parameter?
我已经使用以下代码将 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 调整雾。我在玩 scene.background 但找不到任何公开的方法可以让我改变太阳的高度。
如何使用 Swift 以编程方式更改我的 SceneKit 场景的太阳高度?
您可以同时使用 MDL 天空盒进行场景照明和 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
我已经使用以下代码将 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 调整雾。我在玩 scene.background 但找不到任何公开的方法可以让我改变太阳的高度。
如何使用 Swift 以编程方式更改我的 SceneKit 场景的太阳高度?
您可以同时使用 MDL 天空盒进行场景照明和 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