当前位置:   article > 正文

Unity中Shader旋转矩阵(四维旋转矩阵)_ue shader 旋转变换

ue shader 旋转变换


前言

在上篇文章中,我们推算出了Shader物体旋转所使用的二维旋转矩阵。

在这篇文章中,我们来推算得到四维旋转矩阵。


一、围绕X轴旋转

围绕X轴旋转代表,物体顶点的X轴不变。

1、可以使用上篇文章中,同样的方法推导得出围绕X轴旋转的点阵。

在这里插入图片描述

  • 我们把P2增加一维且分量为1

2、求Mrotate

  • Mrotate * P1 = P2
  • Mrotate = P2* P1-1
    在这里插入图片描述
  • 最后得到Mrotate
    在这里插入图片描述

二、围绕Y轴旋转

围绕Y轴旋转代表,物体顶点的Y轴不变。

1、可以使用上篇文章中,同样的方法推导得出围绕Y轴旋转的点阵。

在这里插入图片描述

  • 我们把P2增加一维且分量为1

2、求Mrotate

  • Mrotate * P1 = P2
  • Mrotate = P2* P1-1
    在这里插入图片描述
  • 最后得到Mrotate
    在这里插入图片描述

三、围绕Z轴旋转

围绕Z轴旋转代表,物体顶点的Z轴不变。

1、可以使用上篇文章中,同样的方法推导得出围绕Z轴旋转的点阵。

在这里插入图片描述

  • 我们把P2增加一维且分量为1

2、求Mrotate

  • Mrotate * P1 = P2
  • Mrotate = P2* P1-1
    在这里插入图片描述
  • 最后得到Mrotate
    在这里插入图片描述

可以修改sin函数前面的负号位置实现顺时针还是逆时针。这篇文章中是顺时针


四、在Shader实现

1、在属性面板定义四维变量,用xyz控制XYZ轴上的旋转

_Rotation(“Rotation(XYZ)”,Vector) = (0,0,0,0)

2、在常量缓冲区申明该变量

CBUFFER_START(UnityPerMaterial)
float4 _Rotation;
CBUFFER_END

3、在 顶点着色器 定义旋转矩阵

float4x4 M_rotateX = float4x4
(
1,0,0,0,
0,cos(_Rotation.x),sin(_Rotation.x),0,
0,-sin(_Rotation.x),cos(_Rotation.x),0,
0,0,0,1
);
float4x4 M_rotateY = float4x4
(
cos(_Rotation.y),0,sin(_Rotation.y),0,
0,1,0,0,
-sin(_Rotation.y),0,cos(_Rotation.y),0,
0,0,0,1
);
float4x4 M_rotateZ = float4x4
(
cos(_Rotation.z),sin(_Rotation.z),0,0,
-sin(_Rotation.z),cos(_Rotation.z),0,0,
0,0,1,0,
0,0,0,1
);

4、使用旋转矩阵与模型顶点相乘输出

v.vertexOS = mul(M_rotateX,v.vertexOS);
v.vertexOS = mul(M_rotateY,v.vertexOS);
v.vertexOS = mul(M_rotateZ,v.vertexOS);

5、最终效果

请添加图片描述


五、最终测试代码

//平移变换
//缩放变换
//旋转变换(四维)
Shader "MyShader/URP/P3_5_7"
{
    Properties
    {
        _Translate("Translate(XYZ)",Vector) = (0,0,0,0)
        _Scale("Scale(XYZ)",Vector)= (1,1,1,1)
        _Rotation("Rotation(XYZ)",Vector) = (0,0,0,0)
    }
    SubShader
    {
        Tags
        {
            "PenderPipeline"="UniversalPipeline"
            "RenderType"="Opaque"
            "Queue"="Geometry"
        }
        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            struct Attribute
            {
                float4 vertexOS : POSITION;
            };

            struct Varying
            {
                float4 vertexCS : SV_POSITION;
            };

            CBUFFER_START(UnityPerMaterial)
            float4 _Translate;
            float4 _Scale;
            float4 _Rotation;
            CBUFFER_END
            Varying vert (Attribute v)
            {
                Varying o;
                //平移变换
                float4x4 M_Translate = float4x4
                    (
                    1,0,0,_Translate.x,
                    0,1,0,_Translate.y,
                    0,0,1,_Translate.z,
                    0,0,0,1
                    );
                v.vertexOS = mul(M_Translate,v.vertexOS);
                //缩放交换
                float4x4 M_Scale = float4x4
                    (
                    _Scale.x,0,0,0,
                    0,_Scale.y,0,0,
                    0,0,_Scale.z,0,
                    0,0,0,1
                    );
                v.vertexOS = mul(M_Scale,v.vertexOS);
                //旋转变换
                float4x4 M_rotateX = float4x4
                    (
                    1,0,0,0,
                    0,cos(_Rotation.x),sin(_Rotation.x),0,
                    0,-sin(_Rotation.x),cos(_Rotation.x),0,
                    0,0,0,1
                    );
                float4x4 M_rotateY = float4x4
                    (
                    cos(_Rotation.y),0,sin(_Rotation.y),0,
                    0,1,0,0,
                    -sin(_Rotation.y),0,cos(_Rotation.y),0,
                    0,0,0,1
                    );
                float4x4 M_rotateZ = float4x4
                    (
                        cos(_Rotation.z),sin(_Rotation.z),0,0,
                        -sin(_Rotation.z),cos(_Rotation.z),0,0,
                        0,0,1,0,
                        0,0,0,1
                    );
                v.vertexOS = mul(M_rotateX,v.vertexOS);
                v.vertexOS = mul(M_rotateY,v.vertexOS);
                v.vertexOS = mul(M_rotateZ,v.vertexOS);
                o.vertexCS = TransformObjectToHClip(v.vertexOS.xyz);
                return o;
            }

            half4 frag (Varying i) : SV_Target
            {
                return 1;
            }
            ENDHLSL
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/110890
推荐阅读
相关标签
  

闽ICP备14008679号