最好的办法是制作一个屏幕对齐的容器,其原点和比例设置为您提供像素对齐和您选择的零零位置。
你的构建块是 sceneView.unprojectPoint 和 sceneView.frame
import Cocoa // (or UIKit for iOS)
import SceneKit
import PlaygroundSupport;
// create a scene view with an empty scene
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
var scene = SCNScene()
sceneView.scene = scene
// have it show in your playground
PlaygroundPage.current.liveView = sceneView;
// a camera
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(0, 0, 3)
scene.rootNode.addChildNode(cameraNode)
var origin = sceneView.unprojectPoint(SCNVector3(0,0,0))
var viewWidth = sceneView.frame.width;
var topRight = sceneView.unprojectPoint(SCNVector3(sceneView.frame.width, 0, 0));
var scale = 2 * (topRight.x - origin.x) / sceneView.frame.width
var container = SCNNode()
origin.z *= -1
origin.x = -2 * topRight.x;
origin.y = -2 * topRight.y;
container.position = origin;
container.scale = SCNVector3(scale, -scale, scale)
cameraNode.addChildNode(container);
func addBox(w:CGFloat, h:CGFloat, x:CGFloat, y:CGFloat) {
let box = SCNNode(geometry: SCNPlane(width: w, height: h))
box.geometry?.firstMaterial?.diffuse.contents = NSColor.red
//SCNPlane geometry is still center-origin
box.position = SCNVector3(x + w / 2, y + h / 2,0)
container.addChildNode(box)
}
addBox(w: 10, h:10, x: 290, y:290)
addBox(w: 10, h:10, x: 0, y:0)
addBox(w: 10, h:10, x: 0, y:290)
addBox(w: 10, h:10, x: 290, y:0)