【问题标题】:Move SCNVector3 relative to SCNNode orientation相对于 SCNNode 方向移动 SCNVector3
【发布时间】:2018-03-02 02:33:10
【问题描述】:

在使用 Swift 的 SceneKit 中,我试图生成与场景视图的 pointOfView 节点的方向对齐的正方形的面,但我无法理解执行此操作所需的数学。

我需要做的就是获取节点的当前位置 (SCNVector3) 并生成这些相对于初始中心点且垂直于相机前进方向的点:

我有前进方向和当前位置:

let mat = self.sceneView.pointOfView.transform
let forwardDirection = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33)
let position = self.sceneView.pointOfView!.position

我只是无法弄清楚围绕这个的其余数学。

【问题讨论】:

    标签: ios swift scenekit


    【解决方案1】:

    pointOfView 节点基本上是活动相机。因此,您可以创建一个 SCNNode 来保存面部(通过创建其 SCNGeometry),根据需要定位节点,然后将该节点添加为相机的子节点。然后,如果您在相机周围移动,面部节点将跟随。

    根据您有限的描述,另一种可能更适合的方法是使用 spritekit 覆盖场景(因为它只是一张脸)。

    编辑:根据您在下面的评论,请找到包含以下代码(它在 OBJ C 中,但它完成了您似乎正在寻找的数学运算)。我用它来做类似的事情,与法线对齐(下面的轴变量是输入法线)。生成的矩阵 rotMat 应用于图像中的坐标应该可以解决问题。

        // Find a vector in the plane
        GLKVector3 tangent0 = [self crossVector:axis : GLKVector3Make(1, 0, 0)];
        if ([self dotVector:tangent0 : tangent0] < 0.001) {
            tangent0 = [self crossVector: axis: GLKVector3Make(0, 1, 0)];
        }
        tangent0 = [self normalizeVector: tangent0];
    
        // Find another vector in the plane
        GLKVector3 tangent1 = [self crossVector: axis: tangent0];
        tangent1 = [self normalizeVector: tangent1];
    
        //construct a 4x4 matrix using tangents and initial axis 
        GLKVector4 tangent0GLK4 = GLKVector4Make(tangent0.x, tangent0.y, tangent0.z, 0);
        GLKVector4 tangent1GLK4 = GLKVector4Make(tangent1.x, tangent1.y, tangent1.z, 0);
        GLKVector4 axisGLK4 = GLKVector4Make(axis.x, axis.y, axis.z, 0);
    
        GLKMatrix4 rotMat = GLKMatrix4MakeWithColumns(tangent0GLK4, tangent1GLK4, axisGLK4, GLKVector4Make(0, 0, 0, 1));
    

    【讨论】:

    • 谢谢,但我实际上是用它来绘制一个形状的几何图形,我希望在每次更新 pov 节点时添加这些顶点的几何图形上都有纹理,所以这赢了不适合我。
    • 好的,我在答案中添加了一些代码,我认为它可以满足您的数学需求,希望对您有所帮助!
    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2019-06-07
    • 2021-06-26
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2018-08-24
    • 2019-11-06
    相关资源
    最近更新 更多