如何将变换矩阵应用于 SceneKit 中的向量 [英] How to apply a transformation matrix to a vector in SceneKit

查看:53
本文介绍了如何将变换矩阵应用于 SceneKit 中的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遗漏了什么吗?在各种向量/矩阵函数中 此处,我看不到任何函数可以简单地将 SCNMatrix4 变换应用于 SCNVector4.我当然可以通过直接写出矩阵/向量乘法来做到这一点,但肯定不需要吗?

Am I missing something here? Amongst the various vector/matrix functions here, I can't see any function to simply apply an SCNMatrix4 transform to an SCNVector4. I could of course do it by writing out the matrix/vector multiplication longhand, but surely that can't be needed?

这样做的原因是我分两步更新相机的投影变换.首先,我需要对原始变换应用一个变换,然后我需要将此变换应用到一个向量以计算出一个数字,然后在该数字的基础上我需要在顶部应用进一步的变换.

The reason for this is that I'm updating the projection transform for a camera in two steps. First I need to apply one transform to the original transform, then I need to apply this transform to a vector in order to work out a number, then on the basis of that number I need to apply a further transform on top.

由于调用 setProjectionTransform 似乎没有立即效果(我假设它发生在当前事务提交时),因此我无法调用 projectPoint 以在其中间状态应用转换.因此,我一直在考虑构建转换矩阵并手动应用它.

Since it appears that calling setProjectionTransform has no immediate effect (I assume it happens when the current transaction is committed), I can't call projectPoint to apply the transformation in its intermediate state. I've therefore been looking at building the transformation matrix and applying it manually.

这里肯定有一个函数来做你想用矩阵和向量做的最基本的事情???

Surely there must be a function in here to do the single most basic thing you could ever want to do with a matrix and a vector???

推荐答案

在我看来,Apple 还没有时间将他们的 Game Kit 类型转换为 simd,但因为他们知道他们会这样做,所以他们没有没有提供您对 Scene Kit 的期望.就目前而言,我认为正确的做法是让我们处理初始化程序的存储库.所有工作都将使用 simd 类型完成;必要时会进行转换.

It seems to me that Apple hasn't yet had the time to transition their Game Kit types over to simd, but because they know that they will, they haven't provided what you're expecting out of Scene Kit. For now, I believe the right thing to do is for us to work on a repository of initializers. All work will be done with simd types; conversion will happen when necessary.

extension float4x4 {
    init(_ matrix: SCNMatrix4) {
        self.init([
            float4(matrix.m11, matrix.m12, matrix.m13, matrix.m14),
            float4(matrix.m21, matrix.m22, matrix.m23, matrix.m24),
            float4(matrix.m31, matrix.m32, matrix.m33, matrix.m34),
            float4(matrix.m41, matrix.m42, matrix.m43, matrix.m44)
        ])
    }
}

extension float4 {
    init(_ vector: SCNVector4) {
        self.init(vector.x, vector.y, vector.z, vector.w)
    }
}

extension SCNVector4 {
    init(_ vector: float4) {
        self.init(x: vector.x, y: vector.y, z: vector.z, w: vector.w)
    }
}

这篇关于如何将变换矩阵应用于 SceneKit 中的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆