Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a transformation matrix to a vector in SceneKit

Tags:

scenekit

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.

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???

like image 566
Tom Avatar asked Jul 01 '15 14:07

Tom


1 Answers

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)
    }
}
like image 92
Jessy Avatar answered Sep 19 '22 10:09

Jessy