我从Sketchfab下载了两个不同的3D模型。我在ARKit 3D对象放置中导入了两个模型。在放置3D模型时,节点未与其中一个模型的操纵器正确对齐。 我附上了截图。
左侧是所选节点(视口)和操纵器正确对齐的位置。 但右侧所选节点(视口)和操纵器未正确对齐。 如何使节点和操纵器像左侧3D模型一样对齐中心。请指导我。谢谢
@Josh Robbins
我已经尝试过您的代码,但我仍然遇到同样的问题。
box.firstMaterial?.emission.contents = UIColor.green
box.firstMaterial?.shaderModifiers = [SCNShaderModifierEntryPoint.surface: sm]
box.firstMaterial?.isDoubleSided = true
let boxNode = SCNNode(geometry: box)
boxNode.position = SCNVector3(node.position.x,(node.position.y + Float((proheights * 0.0234)/2)),node.position.z)
let minimum = float3(treenode.boundingBox.min)
let maximum = float3(treenode.boundingBox.max)
let translation = (maximum - minimum) * 0.5
//3. Set The Pivot
treenode.pivot = SCNMatrix4MakeTranslation(translation.x, translation.y, translation.z)
boxNode.pivot = SCNMatrix4MakeTranslation(translation.x, translation.y, translation.z)
self.addChildNode(boxNode)
更新代码
let treenode = SCNNode()
let box = SCNBox(width: (prowidths * 0.0234), height: (proheights * 0.0234), length: (prolenght * 0.0234), chamferRadius: 0)
box.firstMaterial?.emission.contents = UIColor.green
box.firstMaterial?.shaderModifiers = [SCNShaderModifierEntryPoint.surface: sm]
box.firstMaterial?.isDoubleSided = true
let boxNode = SCNNode(geometry: box)
boxNode.position = SCNVector3(treenode.position.x,(treenode.position.y + Float((proheights * 0.0234)/2)),treenode.position.z)
let minimum = float3(treenode.boundingBox.min)
let maximum = float3(treenode.boundingBox.max)
let translation = (maximum - minimum) * 0.5
//3. Set The Pivot
treenode.pivot = SCNMatrix4MakeTranslation(translation.x, translation.y, translation.z)
boxNode.pivot = SCNMatrix4MakeTranslation(translation.x, translation.y, translation.z)
self.addChildNode(boxNode)
self.addChildNode(treenode)
return treenode
答案 0 :(得分:2)
这似乎是一个受欢迎的问题。几分钟前我刚刚回答了类似的问题。看看下面的这个功能是否有助于在不移动其位置的情况下使节点的枢轴居中。
set pivot of SCNNode to bounding volume center without changing the node's position
func centerPivotWithOutMoving(for node: SCNNode) -> SCNNode {
let initialPos = treenode.presentation.position
var min = SCNVector3Zero
var max = SCNVector3Zero
node.__getBoundingBoxMin(&min, max: &max)
node.pivot = SCNMatrix4MakeTranslation(
min.x + (max.x - min.x)/2,
min.y + (max.y - min.y)/2,
min.z + (max.z - min.z)/2
)
let correctX = Float(min.x + (max.x - min.x)/2)
let correctY = Float(min.y + (max.y - min.y)/2)
let correctZ = Float(min.z + (max.z - min.z)/2)
if node.convertVector(SCNVector3(0,0,1), from: parentNode).z < 0 {
// if blue local z-axis is pointing downwards
node.position = SCNVector3(initialPos.x - correctX, initialPos.y - correctY, initialPos.z - correctZ)
} else {
// if blue local z-axis is pointing upwards
node.position = SCNVector3(initialPos.x + correctX, initialPos.y + correctY, initialPos.z + correctZ)
}
return node
}
使用上面的函数执行此操作:
centerPivotWithOutMoving(for: treenode)
编辑: 我已经意识到上面的方法不是解决所有情况下解决centerPivotWithOutMoving的最有效方法,因为在某些情况下本地轴是您尝试将枢轴居中的子节点可能与ParentNode轴不共面(本地轴和父轴共享相同的平面)。更好的方法是在每个要将枢轴居中的节点周围放置一个wrapperNode。
类似这样的东西将包装每个节点,当轴不是共面时,修复该潜在问题。
for child in (self.parentNode?.childNodes)! {
let wrapperNode = SCNNode()
child.geometry?.firstMaterial?.lightingModel = .physicallyBased
wrapperNode.addChildNode(child)
wrapperNode.name = child.name! + "_wrapper"
self.parentNode.addChildNode(wrapperNode)
}
我暂时在点击场景时在所有childNodes上调用CenerPivotWithOutMoving。
for child in parentNode.childNodes {
let delayInSeconds = Double(0.1)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) {
self.centerPivotWithOutMoving(for: child)
}
}
这里是修改后的centerPivotWithOutMoving,其中添加了一些用于测试本地轴对父节点的代码,只是为了显示所有封装节点的1,1,1。没有包装节点,这些数字在大多数情况下从不是1,1,1。
func centerPivot(for node: SCNNode) -> SCNNode {
var min = SCNVector3Zero
var max = SCNVector3Zero
node.__getBoundingBoxMin(&min, max: &max)
node.pivot = SCNMatrix4MakeTranslation(
min.x + (max.x - min.x)/2,
min.y + (max.y - min.y)/2,
min.z + (max.z - min.z)/2
)
return node
}
func centerPivotWithOutMoving(for node: SCNNode) -> SCNNode {
let initialPos = node.presentation.position
var min = SCNVector3Zero
var max = SCNVector3Zero
node.__getBoundingBoxMin(&min, max: &max)
// corrective factor for each axis
let correctX = Float(min.x + (max.x - min.x)/2)
let correctY = Float(min.y + (max.y - min.y)/2)
let correctZ = Float(min.z + (max.z - min.z)/2)
var xAxis = node.convertVector(SCNVector3(1,0,0), from: parentNode).x
var yAxis = node.convertVector(SCNVector3(0,1,0), from: parentNode).y
var zAxis = node.convertVector(SCNVector3(0,0,1), from: parentNode).z
// check the local axis is coplanar with parentNode axis
if (xAxis == 1) && (yAxis == 1) && (zAxis == 1) {
print(node.name)
centerPivot(for: node)
node.position = SCNVector3(initialPos.x + correctX, initialPos.y + correctY, initialPos.z + correctZ)
} else {
print("node axis is not coplanar with parent")
}
return node
}
答案 1 :(得分:1)
查看您的模型,似乎枢轴设置在您树的左侧。
您可以使用`SCNNode的pivot属性更改编程的数据透视,这是SCNMatrix4MakeTranslation。
假设您的模型被称为树,您可以像这样更改它:
tree.pivot = SCNMatrix4MakeTranslation(0,0,0)
或者你可以试试这个:
//1. Get The Bounding Box Of The Tree
let minimum = float3(tree.boundingBox.min)
let maximum = float3(tree.boundingBox.max)
//2. Set The Translation To Be Half Way Between The Vector
let translation = (maximum - minimum) * 0.5
//3. Set The Pivot
tree.pivot = SCNMatrix4MakeTranslation(translation.x, translation.y, translation.z)
<强>更新强> 在Scenekit编辑器中,您还可以创建一个“EMPTY NODE&#39;”。然后将树集中放置在其中。如果前面提到的不起作用,这可能是你最好的选择。
答案 2 :(得分:0)
我用node.position = SCNVector3(0, 0, 0)
在我的代码中,看起来像这样:
func placeObject(with objectPath: String) {
// Create a new scene
guard let virtualObjectScene = SCNScene(named: objectPath)
else {
print("Unable to Generate " + objectPath)
return
}
let wrapperNode = SCNNode()
for child in virtualObjectScene.rootNode.childNodes {
child.geometry?.firstMaterial?.lightingModel = .constant
wrapperNode.addChildNode(child)
}
// Rotate the node
let rotationAction = SCNAction.rotateBy(x: 0, y: 0.5, z: 0, duration: 1)
let inifiniteAction = SCNAction.repeatForever(rotationAction)
wrapperNode.runAction(inifiniteAction)
// 0.08 from placed plane
wrapperNode.position = SCNVector3(0, 0.08, 0)
node.addChildNode(wrapperNode)
}
希望对您有帮助。