金属旋转形状改变时

时间:2018-07-19 06:13:52

标签: swift textures metal metalkit

我刚刚使用四个金属顶点创建了一个矩形。我只需要旋转它。所以我使用模型指标。这是我的顶点着色器。

vertex VertexOutTexture vertex_shader_texture(const VertexInTexture vertices [[stage_in]],
                                              constant ModelConstant &modelConstants[[buffer(1)]],

    VertexOutTexture v;
    v.position =  modelConstants.modelMatrix*float4(vertices.position,1);
    v.color = vertices.color;
    v.textureCoordinates = vertices.textureCoordinates;

    return v;

}

它旋转。但是形状改变了。因此,我使用了投影变换,将节点的坐标从摄影机坐标转换为归一化坐标。 我创建投影矩阵:

var sceneConstants = ScenceConstants()

设置其值:

sceneConstants.projectionMatrix = matrix_float4x4(prespectiveDegreesFov:45, aspectRatio:Float(1.0),nearZ:0.1,farZ:100)

init mathod在math.h中的位置

init(prespectiveDegreesFov degreesFov:Float, aspectRatio:Float,nearZ:Float,farZ:Float){
       let  fov = radians(fromDegrees: degreesFov)

        let y = 1/tan(fov*0.5)
        let x = y/aspectRatio
        let z1 = farZ/(nearZ - farZ)
        let w = (z1*nearZ)

        columns = (
            float4(x,  0,  0,  0),
            float4(0,  y,  0,  0),
            float4(0,  0,  z1,-1),
            float4(0,  0,  w,  0)
        )
    }

将命令发送到GPU:

commandEncoder.setVertexBytes(&sceneConstants, length: MemoryLayout<ScenceConstants>.stride, index: 2)

更改我的顶点着色器:

v.position = sceneConstants.projectionMatrix* modelConstants.modelMatrix*float4(vertices.position ,1 );

但是没有用。

旋转前:enter image description here

旋转后:enter image description here

我在下面使用了atach数学函数。

func radians(fromDegrees degrees:Float) ->Float{
    return (degrees/100)*Float(Double.pi)
}
extension matrix_float4x4 {

    init(prespectiveDegreesFov degreesFov:Float, aspectRatio:Float,nearZ:Float,farZ:Float){
       let  fov = radians(fromDegrees: degreesFov)

        let y = 1/tan(fov*0.5)
        let x = y/aspectRatio
        let z1 = farZ/(nearZ - farZ)
        let w = (z1*nearZ)

        columns = (
            float4(x,  0,  0,  0),
            float4(0,  y,  0,  0),
            float4(0,  0,  z1,-1),
            float4(0,  0,  w,  0)
        )
    }



    mutating func scale(axis: float3){

        var result = matrix_identity_float4x4
        let x :Float  = axis.x
        let y :Float  = axis.y
        let z :Float  = axis.z


        result.columns = (

            float4(x,0,0,0),
            float4(0,y,0,0),
            float4(0,0,z,0),
            float4(0,0,0,1)
        )

        print("self:\(self)")
        self = matrix_multiply(self, result)

    }


    mutating func translate(direction: float3){

        var result = matrix_identity_float4x4

        let x :Float  = direction.x
        let y :Float  = direction.y
        let z :Float  = direction.z


        result.columns = (
            float4(1,0,0,0),
            float4(0,1,0,0),
            float4(0,0,1,0),
            float4(x,y,z,1)
        )
        print("self:\(self)")
        self = matrix_multiply(self, result)
    }


    mutating func rotate(angle: Float ,axis: float3){

        var result = matrix_identity_float4x4

        let x :Float  = axis.x
        let y :Float  = axis.y
        let z :Float  = axis.z

        let c: Float = cos(angle)
        let s:Float = sin(angle)

        let mc :Float = (1 - c)

        let r1c1: Float = x * x * mc + c
        let r2c1: Float = x * y * mc + z * s
        let r3c1: Float = x * z * mc - y * s
        let r4c1: Float = 0.0

        let r1c2: Float = y * x * mc - z * s
        let r2c2: Float = y * y * mc + c
        let r3c2: Float = y * z * mc + x * s
        let r4c2: Float = 0.0

        let r1c3: Float = z * x * mc + y * s
        let r2c3: Float = z * y * mc - x * s
        let r3c3: Float = z * z * mc + c
        let r4c3: Float = 0.0

        let r1c4: Float = 0.0
        let r2c4: Float = 0.0
        let r3c4: Float = 0.0
        let r4c4: Float = 1.0


        result.columns = (
            float4(r1c1,r2c1,r3c1,r4c1),
            float4(r1c2,r2c2,r3c2,r4c2),
            float4(r1c3,r2c3,r3c3,r4c3),
            float4(r1c4,r2c4,r3c4,r4c4)
        )
        print("Result:\(result)")
        self = matrix_multiply(self, result)
    }


}

如何解决此问题?有任何建议吗?

0 个答案:

没有答案