回答編集履歴

1

回転軸追加版を追記

2019/05/16 01:19

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -159,3 +159,227 @@
159
159
 
160
160
 
161
161
  ```
162
+
163
+
164
+
165
+ **ZXY順オイラー角回転**
166
+
167
+ ```ShaderLab
168
+
169
+ // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
170
+
171
+
172
+
173
+ Shader "Skybox/Cubemap (3-Axis Rotation)" {
174
+
175
+ Properties {
176
+
177
+ _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
178
+
179
+ [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
180
+
181
+
182
+
183
+ // 回転プロパティをX、Y、Zの3つに増やす
184
+
185
+ _RotationX ("Rotation X", Range(0, 360)) = 0
186
+
187
+ _RotationY ("Rotation Y", Range(0, 360)) = 0
188
+
189
+ _RotationZ ("Rotation Z", Range(0, 360)) = 0
190
+
191
+
192
+
193
+ [NoScaleOffset] _Tex ("Cubemap (HDR)", Cube) = "grey" {}
194
+
195
+ }
196
+
197
+
198
+
199
+ SubShader {
200
+
201
+ Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
202
+
203
+ Cull Off ZWrite Off
204
+
205
+
206
+
207
+ Pass {
208
+
209
+
210
+
211
+ CGPROGRAM
212
+
213
+ #pragma vertex vert
214
+
215
+ #pragma fragment frag
216
+
217
+ #pragma target 2.0
218
+
219
+
220
+
221
+ #include "UnityCG.cginc"
222
+
223
+
224
+
225
+ samplerCUBE _Tex;
226
+
227
+ half4 _Tex_HDR;
228
+
229
+ half4 _Tint;
230
+
231
+ half _Exposure;
232
+
233
+
234
+
235
+ // プロパティに合わせて回転角変数も3つに増やす
236
+
237
+ float _RotationX;
238
+
239
+ float _RotationY;
240
+
241
+ float _RotationZ;
242
+
243
+
244
+
245
+ // 回転用関数の名前を変更、引数をfloatからfloat3に変更
246
+
247
+ float3 RotateAroundZXYInDegrees (float3 vertex, float3 degrees)
248
+
249
+ {
250
+
251
+ // サイン、コサインも3成分についてそれぞれ算出し...
252
+
253
+ float3 alpha = degrees * UNITY_PI / 180.0;
254
+
255
+ float3 sina, cosa;
256
+
257
+ sincos(alpha, sina, cosa);
258
+
259
+
260
+
261
+ // 各軸周りの回転行列を作成し...
262
+
263
+ float3x3 rx = float3x3(
264
+
265
+ 1.0, 0.0, 0.0,
266
+
267
+ 0.0, cosa.x, -sina.x,
268
+
269
+ 0.0, sina.x, cosa.x);
270
+
271
+ float3x3 ry = float3x3(
272
+
273
+ cosa.y, 0.0, sina.y,
274
+
275
+ 0.0, 1.0, 0.0,
276
+
277
+ -sina.y, 0.0, cosa.y);
278
+
279
+ float3x3 rz = float3x3(
280
+
281
+ cosa.z, -sina.z, 0.0,
282
+
283
+ sina.z, cosa.z, 0.0,
284
+
285
+ 0.0, 0.0, 1.0);
286
+
287
+
288
+
289
+ // Z軸、X軸、Y軸の順で回転を合成し...
290
+
291
+ float3x3 m = mul(ry, mul(rx, rz));
292
+
293
+
294
+
295
+ // 回転を適用して返す
296
+
297
+ return mul(m, vertex);
298
+
299
+ }
300
+
301
+
302
+
303
+ struct appdata_t {
304
+
305
+ float4 vertex : POSITION;
306
+
307
+ UNITY_VERTEX_INPUT_INSTANCE_ID
308
+
309
+ };
310
+
311
+
312
+
313
+ struct v2f {
314
+
315
+ float4 vertex : SV_POSITION;
316
+
317
+ float3 texcoord : TEXCOORD0;
318
+
319
+ UNITY_VERTEX_OUTPUT_STEREO
320
+
321
+ };
322
+
323
+
324
+
325
+ v2f vert (appdata_t v)
326
+
327
+ {
328
+
329
+ v2f o;
330
+
331
+ UNITY_SETUP_INSTANCE_ID(v);
332
+
333
+ UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
334
+
335
+
336
+
337
+ // 回転関数にX、Y、Z回転角を与える
338
+
339
+ float3 rotated = RotateAroundZXYInDegrees(v.vertex, float3(_RotationX, _RotationY, _RotationZ));
340
+
341
+
342
+
343
+ o.vertex = UnityObjectToClipPos(rotated);
344
+
345
+ o.texcoord = v.vertex.xyz;
346
+
347
+ return o;
348
+
349
+ }
350
+
351
+
352
+
353
+ fixed4 frag (v2f i) : SV_Target
354
+
355
+ {
356
+
357
+ half4 tex = texCUBE (_Tex, i.texcoord);
358
+
359
+ half3 c = DecodeHDR (tex, _Tex_HDR);
360
+
361
+ c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
362
+
363
+ c *= _Exposure;
364
+
365
+ return half4(c, 1);
366
+
367
+ }
368
+
369
+ ENDCG
370
+
371
+ }
372
+
373
+ }
374
+
375
+
376
+
377
+
378
+
379
+ Fallback Off
380
+
381
+
382
+
383
+ }
384
+
385
+ ```