回答編集履歴

1

回転機能を追加

2019/05/13 21:55

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -26,6 +26,10 @@
26
26
 
27
27
  _NebulaColor2 ("Nebula Color 2", Color) = (0.25, 0, 0.25, 1)
28
28
 
29
+ _Rotation ("Rotation Angle", Range(0.0, 360.0)) = 0.0
30
+
31
+ _RotationAxis ("Rotation Axis", Vector) = (1.0, 0.0, 0.0, 0.0)
32
+
29
33
  }
30
34
 
31
35
  SubShader
@@ -62,6 +66,8 @@
62
66
 
63
67
  #pragma fragment frag
64
68
 
69
+ #include "UnityCG.cginc"
70
+
65
71
 
66
72
 
67
73
  struct appdata
@@ -88,13 +94,43 @@
88
94
 
89
95
 
90
96
 
97
+ float _Rotation;
98
+
99
+ float3 _RotationAxis;
100
+
101
+
102
+
91
103
  v2f vert(appdata v)
92
104
 
93
105
  {
94
106
 
95
107
  v2f o;
96
108
 
109
+ float cosTheta;
110
+
111
+ float sinTheta;
112
+
113
+ sincos(_Rotation * UNITY_PI / 180.0, sinTheta, cosTheta);
114
+
115
+ float cosThetaI = 1.0 - cosTheta;
116
+
117
+ float3 u = normalize(_RotationAxis);
118
+
119
+ float3 u2 = u * u;
120
+
121
+ float3 us = u * u.yzx;
122
+
123
+ float3x3 r = float3x3(
124
+
125
+ cosTheta + u2.x * cosThetaI, -u.z * sinTheta + us.x * cosThetaI, u.y * sinTheta + us.z * cosThetaI,
126
+
127
+ u.z * sinTheta + us.x * cosThetaI, cosTheta + u2.y * cosThetaI, -u.x * sinTheta + us.y * cosThetaI,
128
+
129
+ -u.y * sinTheta + us.z * cosThetaI, u.x * sinTheta + us.y * cosThetaI, cosTheta + u2.z * cosThetaI
130
+
131
+ );
132
+
97
- o.vertex = UnityObjectToClipPos(v.vertex);
133
+ o.vertex = UnityObjectToClipPos(float4(mul(r, v.vertex.xyz), v.vertex.w));
98
134
 
99
135
  o.texcoord = v.texcoord;
100
136
 
@@ -290,6 +326,8 @@
290
326
 
291
327
  + dot(m1 * m1, float2(dot(p3, x3), dot(p4, x4))));
292
328
 
329
+
330
+
293
331
  }
294
332
 
295
333
 
@@ -349,3 +387,103 @@
349
387
  ```
350
388
 
351
389
  ![プロシージャル星空](548e6887bc54274906c0ee6f3723790d.gif)
390
+
391
+
392
+
393
+ **回転させる例**
394
+
395
+ ```C#
396
+
397
+ using UnityEngine;
398
+
399
+
400
+
401
+ public class ProceduralStarfieldRotor : MonoBehaviour
402
+
403
+ {
404
+
405
+ private static readonly int RotationProperty = Shader.PropertyToID("_Rotation");
406
+
407
+ private static readonly int RotationAxisProperty = Shader.PropertyToID("_RotationAxis");
408
+
409
+
410
+
411
+ [SerializeField] private float angularSpeed = 15.0f;
412
+
413
+ [SerializeField] private Vector3 axis = Vector3.right;
414
+
415
+
416
+
417
+ private Material skybox;
418
+
419
+ private float angle;
420
+
421
+
422
+
423
+ private void Start()
424
+
425
+ {
426
+
427
+ this.skybox = RenderSettings.skybox;
428
+
429
+ if (this.skybox == null)
430
+
431
+ {
432
+
433
+ Debug.LogError("No Skybox.");
434
+
435
+ this.enabled = false;
436
+
437
+ return;
438
+
439
+ }
440
+
441
+
442
+
443
+ if (!this.skybox.HasProperty(RotationProperty))
444
+
445
+ {
446
+
447
+ Debug.LogError("Skybox does not have _Rotation.");
448
+
449
+ this.enabled = false;
450
+
451
+ return;
452
+
453
+ }
454
+
455
+
456
+
457
+ if (!this.skybox.HasProperty(RotationAxisProperty))
458
+
459
+ {
460
+
461
+ Debug.LogError("Skybox does not have _RotationAxis.");
462
+
463
+ this.enabled = false;
464
+
465
+ return;
466
+
467
+ }
468
+
469
+
470
+
471
+ this.skybox.SetVector(RotationAxisProperty, this.axis);
472
+
473
+ }
474
+
475
+
476
+
477
+ private void Update()
478
+
479
+ {
480
+
481
+ this.angle += this.angularSpeed * Time.deltaTime;
482
+
483
+ this.skybox.SetFloat(RotationProperty, this.angle);
484
+
485
+ }
486
+
487
+ }
488
+
489
+ ```