How to change SCNView pivot point

Viewed 54

Question

How does the SceneView/camera work out around which point to rotate, and how do I force the scene to rotate around the red sphere (-2, 0, 0)?

Demo

In this example SceneView I have placed

  • a camera at (-2, 0, 5)
  • a red sphere at (-2, 0, 0)
  • a blue sphere at (-1, 0, 0)
  • a white sphere at (0, 0, 0)

So the camera is in the same position along the x-axis as the red sphere

In the image you see the scene rotates around the blue sphere.

enter image description here

Playground example

Here is a working playground to produce the above example

import UIKit
import SceneKit
import PlaygroundSupport

var scene = SCNScene()

var sceneView: SCNView = {
    let s = SCNView(
        frame: CGRect(x: 0, y: 0, width: 600, height: 600)
    )
    s.scene = scene
    s.backgroundColor = UIColor.black
    s.allowsCameraControl = true
    return s
}()

PlaygroundPage.current.liveView = sceneView

let redXPosition: simd_float1 = -2.0
let blueXPosition: simd_float1 = -1.0
let originXPosition: simd_float1 = 0.0


// MARK: Scene Nodes
let cameraNode: SCNNode = {
    let n = SCNNode()
    n.camera = SCNCamera()
    n.camera?.contrast = 0.0
    n.camera?.wantsHDR = false
    return n
}()

cameraNode.simdPosition = simd_float3(redXPosition, 0, 5)
scene.rootNode.addChildNode(cameraNode)

let ambientLightNode: SCNNode = {
    let n = SCNNode()
    n.light = SCNLight()
    n.light!.type = SCNLight.LightType.ambient
    n.light!.color = UIColor(white: 0.75, alpha: 1.0)
    return n
}()

ambientLightNode.simdPosition = simd_float3(0,5,0)
scene.rootNode.addChildNode(ambientLightNode)
// MARK: Spheres

// MARK: Origin - White
let originNode: SCNNode = {
    let sphere = SCNSphere(radius: 0.5)
    let node = SCNNode(geometry: sphere)
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor.white
    sphere.materials = [mat]
    return node
}()

// MARK: Red
let redNode: SCNNode = {
    let sphere = SCNSphere(radius: 0.3)
    let node = SCNNode(geometry: sphere)
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor.red
    sphere.materials = [mat]
    return node
}()

// MARK: Blue
let blueNode: SCNNode = {
    let sphere = SCNSphere(radius: 0.3)
    let node = SCNNode(geometry: sphere)
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor.blue
    sphere.materials = [mat]
    return node
}()

// MARK: Place nodes in scene
originNode.simdPosition = simd_float3(originXPosition,0,0)
redNode.simdPosition = simd_float3(redXPosition,0,0)
blueNode.simdPosition = simd_float3(blueXPosition,0,0)


scene.rootNode.addChildNode(originNode)
scene.rootNode.addChildNode(redNode)
scene.rootNode.addChildNode(blueNode)
0 Answers
Related