Posts 유니티 쉐이더 & 함수 모음
Post
Cancel

유니티 쉐이더 & 함수 모음

Functions


트랜스폼에서 회전 행렬만 추출하기

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
// 트랜스폼의 회전 행렬 추출
float4x4 GetModelRotationMatrix()
{
    float4x4 rotationMatrix;

    vector sx = vector(unity_ObjectToWorld._m00, unity_ObjectToWorld._m10, unity_ObjectToWorld._m20, 0);
    vector sy = vector(unity_ObjectToWorld._m01, unity_ObjectToWorld._m11, unity_ObjectToWorld._m21, 0);
    vector sz = vector(unity_ObjectToWorld._m02, unity_ObjectToWorld._m12, unity_ObjectToWorld._m22, 0);

    float scaleX = length(sx);
    float scaleY = length(sy);
    float scaleZ = length(sz);

    rotationMatrix[0] = float4(unity_ObjectToWorld._m00 / scaleX, unity_ObjectToWorld._m01 / scaleY, unity_ObjectToWorld._m02 / scaleZ, 0);
    rotationMatrix[1] = float4(unity_ObjectToWorld._m10 / scaleX, unity_ObjectToWorld._m11 / scaleY, unity_ObjectToWorld._m12 / scaleZ, 0);
    rotationMatrix[2] = float4(unity_ObjectToWorld._m20 / scaleX, unity_ObjectToWorld._m21 / scaleY, unity_ObjectToWorld._m22 / scaleZ, 0);
    rotationMatrix[3] = float4(0, 0, 0, 1);

    return rotationMatrix;
}

// 위치 벡터 회전
float3 RotatePosObjectToWorld(float4x4 rotationMatrix, float3 pos)
{
    return mul(rotationMatrix, pos).xyz;
}

float3 RotatePosWorldToObject(float4x4 rotationMatrix, float3 pos)
{
    return mul(pos, rotationMatrix).xyz;
}
            
// 방향 벡터 회전
float3 RotateDirObjectToWorld(float4x4 rotationMatrix, float3 dir)
{
    return mul((float3x3)rotationMatrix, dir);
}

float3 RotateDirWorldToObject(float4x4 rotationMatrix, float3 dir)
{
    return mul(dir, (float3x3)rotationMatrix);
}

// 활용 예시
fixed4 frag (v2f i) : SV_Target
{
    float4x4 rotMatrix = GetModelRotationMatrix();

    // Object Light Direction
    float3 L = normalize(_WorldSpaceLightPos0.xyz);
    L = RotateDirWorldToObject(rotMatrix, L);
}


Render Depth


  • 단순히 뎁스만 색상으로 보여주는 쉐이더

image

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
Shader "Render Depth"
{
    Properties
    {
        _Multiplier("Multiplier", Float) = 50
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        Pass 
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct v2f 
            {
                float4 pos : SV_POSITION;
                float depth : TEXCOORD0;
            };

            float _Multiplier;

            v2f vert(appdata_base v) 
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.depth = length(mul(UNITY_MATRIX_MV, v.vertex)) * _ProjectionParams.w;
                return o;
            }

            half4 frag(v2f i) : SV_Target
            {
                return fixed4(i.depth, i.depth, i.depth, 1) * _Multiplier;
            }
            ENDCG
        }
    }
}


References


This post is licensed under CC BY 4.0 by the author.