I've managed to incorporate SceneKit into a SwiftUI view in my app using the following code:
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)))
}
}
}
I know that you can use scene.fogEndDistance and other permutations to programmatically adjust the fog using Swift. I was playing around with scene.background but can't find any method exposed that allows me to change the sun's elevation.
How do I use Swift to programmatically change the sun's elevation for my SceneKit scene?
CodePudding user response:
You can simultaneously use MDL Sky Box for scene lighting and for 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