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

回答編集履歴

1

回転機能を追加

2019/05/13 21:55

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -12,6 +12,8 @@
12
12
  _Seed ("Seed", Float) = 0.0
13
13
  _NebulaColor1 ("Nebula Color 1", Color) = (0, 0, 0.25, 1)
14
14
  _NebulaColor2 ("Nebula Color 2", Color) = (0.25, 0, 0.25, 1)
15
+ _Rotation ("Rotation Angle", Range(0.0, 360.0)) = 0.0
16
+ _RotationAxis ("Rotation Axis", Vector) = (1.0, 0.0, 0.0, 0.0)
15
17
  }
16
18
  SubShader
17
19
  {
@@ -30,6 +32,7 @@
30
32
  CGPROGRAM
31
33
  #pragma vertex vert
32
34
  #pragma fragment frag
35
+ #include "UnityCG.cginc"
33
36
 
34
37
  struct appdata
35
38
  {
@@ -43,10 +46,25 @@
43
46
  float3 texcoord : TEXCOORD0;
44
47
  };
45
48
 
49
+ float _Rotation;
50
+ float3 _RotationAxis;
51
+
46
52
  v2f vert(appdata v)
47
53
  {
48
54
  v2f o;
55
+ float cosTheta;
56
+ float sinTheta;
57
+ sincos(_Rotation * UNITY_PI / 180.0, sinTheta, cosTheta);
58
+ float cosThetaI = 1.0 - cosTheta;
59
+ float3 u = normalize(_RotationAxis);
60
+ float3 u2 = u * u;
61
+ float3 us = u * u.yzx;
62
+ float3x3 r = float3x3(
63
+ cosTheta + u2.x * cosThetaI, -u.z * sinTheta + us.x * cosThetaI, u.y * sinTheta + us.z * cosThetaI,
64
+ u.z * sinTheta + us.x * cosThetaI, cosTheta + u2.y * cosThetaI, -u.x * sinTheta + us.y * cosThetaI,
65
+ -u.y * sinTheta + us.z * cosThetaI, u.x * sinTheta + us.y * cosThetaI, cosTheta + u2.z * cosThetaI
66
+ );
49
- o.vertex = UnityObjectToClipPos(v.vertex);
67
+ o.vertex = UnityObjectToClipPos(float4(mul(r, v.vertex.xyz), v.vertex.w));
50
68
  o.texcoord = v.texcoord;
51
69
  return o;
52
70
  }
@@ -144,6 +162,7 @@
144
162
  m1 = m1 * m1;
145
163
  return 49.0 * (dot(m0 * m0, float3(dot(p0, x0), dot(p1, x1), dot(p2, x2)))
146
164
  + dot(m1 * m1, float2(dot(p3, x3), dot(p4, x4))));
165
+
147
166
  }
148
167
 
149
168
  float _Seed;
@@ -173,4 +192,54 @@
173
192
  }
174
193
  }
175
194
  ```
176
- ![プロシージャル星空](548e6887bc54274906c0ee6f3723790d.gif)
195
+ ![プロシージャル星空](548e6887bc54274906c0ee6f3723790d.gif)
196
+
197
+ **回転させる例**
198
+ ```C#
199
+ using UnityEngine;
200
+
201
+ public class ProceduralStarfieldRotor : MonoBehaviour
202
+ {
203
+ private static readonly int RotationProperty = Shader.PropertyToID("_Rotation");
204
+ private static readonly int RotationAxisProperty = Shader.PropertyToID("_RotationAxis");
205
+
206
+ [SerializeField] private float angularSpeed = 15.0f;
207
+ [SerializeField] private Vector3 axis = Vector3.right;
208
+
209
+ private Material skybox;
210
+ private float angle;
211
+
212
+ private void Start()
213
+ {
214
+ this.skybox = RenderSettings.skybox;
215
+ if (this.skybox == null)
216
+ {
217
+ Debug.LogError("No Skybox.");
218
+ this.enabled = false;
219
+ return;
220
+ }
221
+
222
+ if (!this.skybox.HasProperty(RotationProperty))
223
+ {
224
+ Debug.LogError("Skybox does not have _Rotation.");
225
+ this.enabled = false;
226
+ return;
227
+ }
228
+
229
+ if (!this.skybox.HasProperty(RotationAxisProperty))
230
+ {
231
+ Debug.LogError("Skybox does not have _RotationAxis.");
232
+ this.enabled = false;
233
+ return;
234
+ }
235
+
236
+ this.skybox.SetVector(RotationAxisProperty, this.axis);
237
+ }
238
+
239
+ private void Update()
240
+ {
241
+ this.angle += this.angularSpeed * Time.deltaTime;
242
+ this.skybox.SetFloat(RotationProperty, this.angle);
243
+ }
244
+ }
245
+ ```