【发布时间】:2021-01-18 10:43:33
【问题描述】:
我是 Swift 和 SceneKit 的新手,我正在尝试实现一个解决方案,用户可以将 3D 对象添加到场景中,并将其放置在平面表面的边缘。
我尝试使用平面节点的边界框值来假设其边缘和对象的潜在位置,但由于平面是自定义几何体,因此对象通常最终会超出平面的表面。
最重要的是,如果平面的特定边缘是对角线,我还需要旋转对象,使其始终平行于平面的边缘。
这是我的代码的 sn-p 和 image illustrating the problem and the desired outcome:
//creating plane out of custom geometry values
let surfaceNode = SCNNode(geometry: surfaceGeometry)
//defining plane’s bounding box & center point
let minimum = SIMD3<Float>(surfaceNode.boundingBox.min)
let maximum = SIMD3<Float>(surfaceNode.boundingBox.max)
let translation = (maximum - minimum) * 0.5
//fixing pivot point
surfaceNode.pivot = SCNMatrix4MakeTranslation(surfaceNode.boundingBox.min.x + translation.x, 0, surfaceNode.boundingBox.min.z + translation.z)
//adding custom plane to the scene
surfaceNode.position = SCNVector3(0, 0 ,0)
sceneView.scene.rootNode.addChildNode(surfaceNode)
//creating object
let object = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
let objectNode = SCNNode(geometry: object)
objectNode.pivot = SCNMatrix4MakeTranslation(0, -0.1, 0)
//positioning object on top of the plane’s surface
objectNode.position = SCNVector3(surfaceNode.position.x, surfaceNode.position.y, surfaceNode.position.z - translation.z)
//adding object to scene
sceneView.scene.rootNode.addChildNode(objectNode)
【问题讨论】:
-
你试过把objectNode添加到surfaceNode吗?子节点将使用它的父节点的旋转和位置作为参考