gpt4 book ai didi

ios - 将 SCNNode 的枢轴设置为边界体积中心而不更改节点的位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:23:17 25 4
gpt4 key购买 nike

我正在寻找一种方法来将 SCNNodes 的枢轴(通过导入 Collada 文件获得)设置到它们的质心(或它们的边界框的中心)。我得到中心

 SCNVector3 center;
CGFloat radius;
[node getBoundingSphereCenter:&center radius:&radius];

但是,当我尝试将节点的枢轴(它是一个 4D 向量,而不是中心的 3D 向量)设置为中心时,节点的位置也会发生变化。

node.pivot = SCNMatrix4MakeTranslation(center.x,center.y,center.z);

我怎样才能抵消这种运动 - resp。我怎样才能只改变节点的枢轴而不影响它在空间中的位置?或者甚至有一种方法(不使用额外的空节点)在 Interface Builder 中做到这一点?

最佳答案

这里也问了同样的问题Is it normal for an SCNNode pivot to change the nodes position? ...但是在网上搜索了几个小时后,我找不到任何解决这个问题的编码方法。即如何在不移动节点的情况下使节点的轴心居中

所以最后,我今天大部分时间都在研究下面的这个编码答案。我发现,局部坐标轴的方向决定了您是减去还是添加校正因子。

骑下面这辆自行车。右侧踏板曲柄枢轴 z 轴(蓝色)指向上方,而在另一张照片中,左侧踏板曲柄枢轴 z 轴(蓝色)指向下方。这些轴不同的原因是当人们在 SketchUp 中绘制自行车模型时,他们只是复制并将第一个踏板曲柄翻转到另一侧。不幸的是,导入的 DAE 模型具有特定的枢轴方向和位置是很常见的!

enter image description here

enter image description here

 func centerPivotWithOutMoving(for node: SCNNode) -> SCNNode {

let initialPos = activeNode.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
}

我只在上面显示的两个不同的 z 轴方向上测试了上面的代码,完整的代码还必须使用这两个条件处理 x、y 轴。

  node.convertVector(SCNVector3(0,1,0), from: parentNode).z < 0

node.convertVector(SCNVector3(1,0,0), from: parentNode).z < 0

如果有更好的方法...请分享?

关于ios - 将 SCNNode 的枢轴设置为边界体积中心而不更改节点的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com