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);
}
|