teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

回転軸追加版を追記

2019/05/16 01:19

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -78,4 +78,116 @@
78
78
 
79
79
  }
80
80
 
81
+ ```
82
+
83
+ **ZXY順オイラー角回転**
84
+ ```ShaderLab
85
+ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
86
+
87
+ Shader "Skybox/Cubemap (3-Axis Rotation)" {
88
+ Properties {
89
+ _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
90
+ [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
91
+
92
+ // 回転プロパティをX、Y、Zの3つに増やす
93
+ _RotationX ("Rotation X", Range(0, 360)) = 0
94
+ _RotationY ("Rotation Y", Range(0, 360)) = 0
95
+ _RotationZ ("Rotation Z", Range(0, 360)) = 0
96
+
97
+ [NoScaleOffset] _Tex ("Cubemap (HDR)", Cube) = "grey" {}
98
+ }
99
+
100
+ SubShader {
101
+ Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
102
+ Cull Off ZWrite Off
103
+
104
+ Pass {
105
+
106
+ CGPROGRAM
107
+ #pragma vertex vert
108
+ #pragma fragment frag
109
+ #pragma target 2.0
110
+
111
+ #include "UnityCG.cginc"
112
+
113
+ samplerCUBE _Tex;
114
+ half4 _Tex_HDR;
115
+ half4 _Tint;
116
+ half _Exposure;
117
+
118
+ // プロパティに合わせて回転角変数も3つに増やす
119
+ float _RotationX;
120
+ float _RotationY;
121
+ float _RotationZ;
122
+
123
+ // 回転用関数の名前を変更、引数をfloatからfloat3に変更
124
+ float3 RotateAroundZXYInDegrees (float3 vertex, float3 degrees)
125
+ {
126
+ // サイン、コサインも3成分についてそれぞれ算出し...
127
+ float3 alpha = degrees * UNITY_PI / 180.0;
128
+ float3 sina, cosa;
129
+ sincos(alpha, sina, cosa);
130
+
131
+ // 各軸周りの回転行列を作成し...
132
+ float3x3 rx = float3x3(
133
+ 1.0, 0.0, 0.0,
134
+ 0.0, cosa.x, -sina.x,
135
+ 0.0, sina.x, cosa.x);
136
+ float3x3 ry = float3x3(
137
+ cosa.y, 0.0, sina.y,
138
+ 0.0, 1.0, 0.0,
139
+ -sina.y, 0.0, cosa.y);
140
+ float3x3 rz = float3x3(
141
+ cosa.z, -sina.z, 0.0,
142
+ sina.z, cosa.z, 0.0,
143
+ 0.0, 0.0, 1.0);
144
+
145
+ // Z軸、X軸、Y軸の順で回転を合成し...
146
+ float3x3 m = mul(ry, mul(rx, rz));
147
+
148
+ // 回転を適用して返す
149
+ return mul(m, vertex);
150
+ }
151
+
152
+ struct appdata_t {
153
+ float4 vertex : POSITION;
154
+ UNITY_VERTEX_INPUT_INSTANCE_ID
155
+ };
156
+
157
+ struct v2f {
158
+ float4 vertex : SV_POSITION;
159
+ float3 texcoord : TEXCOORD0;
160
+ UNITY_VERTEX_OUTPUT_STEREO
161
+ };
162
+
163
+ v2f vert (appdata_t v)
164
+ {
165
+ v2f o;
166
+ UNITY_SETUP_INSTANCE_ID(v);
167
+ UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
168
+
169
+ // 回転関数にX、Y、Z回転角を与える
170
+ float3 rotated = RotateAroundZXYInDegrees(v.vertex, float3(_RotationX, _RotationY, _RotationZ));
171
+
172
+ o.vertex = UnityObjectToClipPos(rotated);
173
+ o.texcoord = v.vertex.xyz;
174
+ return o;
175
+ }
176
+
177
+ fixed4 frag (v2f i) : SV_Target
178
+ {
179
+ half4 tex = texCUBE (_Tex, i.texcoord);
180
+ half3 c = DecodeHDR (tex, _Tex_HDR);
181
+ c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
182
+ c *= _Exposure;
183
+ return half4(c, 1);
184
+ }
185
+ ENDCG
186
+ }
187
+ }
188
+
189
+
190
+ Fallback Off
191
+
192
+ }
81
193
  ```