Skip to content

Commit a82b05e

Browse files
Keijiro TakahashiKeijiro Takahashi
Keijiro Takahashi
authored and
Keijiro Takahashi
committed
Improved effect controllers.
1 parent 761c098 commit a82b05e

File tree

8 files changed

+208
-137
lines changed

8 files changed

+208
-137
lines changed

Assets/CubeCluster/CubeClusterRenderer.cs

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ namespace UnityLogo
66
[ExecuteInEditMode]
77
public class CubeClusterRenderer : MonoBehaviour
88
{
9+
#region Exposed properties
10+
11+
[SerializeField, Range(0, 1)]
12+
float _phase;
13+
14+
[Space]
15+
16+
[SerializeField]
17+
float _transition = 0.1f;
18+
19+
[Space]
20+
921
[SerializeField, ColorUsage(false)]
1022
Color _color;
1123

@@ -21,25 +33,36 @@ public class CubeClusterRenderer : MonoBehaviour
2133
[SerializeField]
2234
float _textureScale = 1;
2335

24-
[SerializeField]
25-
Shader _shader;
36+
#endregion
37+
38+
#region Private resources
2639

40+
[SerializeField, HideInInspector] Shader _shader;
2741
Material _material;
2842

29-
Vector4 _switch1;
30-
Vector4 _switch2;
31-
32-
Vector4 RandomBinaryVector {
33-
get {
34-
return new Vector4(
35-
Random.value > 0.5f ? 1 : 0,
36-
Random.value > 0.5f ? 1 : 0,
37-
Random.value > 0.5f ? 1 : 0,
38-
Random.value > 0.5f ? 1 : 0
39-
);
40-
}
43+
#endregion
44+
45+
#region Private members
46+
47+
float[] _randomParams = new float[8];
48+
float _time;
49+
50+
#endregion
51+
52+
#region Public methods
53+
54+
public void ResetParams()
55+
{
56+
for (var i = 0; i < 8; i++)
57+
_randomParams[i] = Random.value > 0.66f ? 1 : 0;
58+
59+
_time = 0;
4160
}
4261

62+
#endregion
63+
64+
#region MonoBehaviour functions
65+
4366
void OnEnable()
4467
{
4568
_material = new Material(Shader.Find("UnityLogo/CubeCluster"));
@@ -52,31 +75,47 @@ void OnDisable()
5275
_material = null;
5376
}
5477

55-
IEnumerator Start()
78+
void Start()
5679
{
57-
while (true)
58-
{
59-
_switch1 = RandomBinaryVector;
60-
_switch2 = RandomBinaryVector;
61-
yield return new WaitForSeconds(18);
62-
}
80+
ResetParams();
6381
}
6482

6583
void Update()
6684
{
67-
_material.color = _color;
68-
_material.SetFloat("_Size", 1.0f / _mesh.columnCount);
69-
_material.mainTexture = _metallicMap;
85+
_material.SetColor("_Color", _color);
86+
_material.SetTexture("_MainTex", _metallicMap);
7087
_material.SetTexture("_BumpMap", _normalMap);
88+
_material.SetFloat("_BumpScale", 1);
89+
90+
_material.SetFloat("_Size", 1.0f / _mesh.columnCount);
7191
_material.SetFloat("_TextureScale", _textureScale);
7292

73-
_material.SetVector("_Switch1", _switch1);
74-
_material.SetVector("_Switch2", _switch2);
93+
_material.SetFloat("_RTime", _time);
94+
_material.SetFloat("_Phase", _phase);
95+
_material.SetFloat("_Transition", _transition);
96+
97+
_material.SetVector("_Params1", new Vector4(
98+
_randomParams[0],
99+
_randomParams[1],
100+
_randomParams[2],
101+
_randomParams[3]
102+
));
103+
104+
_material.SetVector("_Params2", new Vector4(
105+
_randomParams[4],
106+
_randomParams[5],
107+
_randomParams[6],
108+
_randomParams[7]
109+
));
75110

76111
Graphics.DrawMesh(
77112
_mesh.sharedMesh, transform.localToWorldMatrix, _material,
78113
gameObject.layer
79114
);
115+
116+
_time += Time.deltaTime;
80117
}
118+
119+
#endregion
81120
}
82121
}

Assets/CubeCluster/Shader/CubeCluster.shader

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@
2626
float _Size;
2727
float _TextureScale;
2828

29-
float4 _Switch1;
30-
float4 _Switch2;
29+
float _RTime;
30+
float _Phase;
31+
float _Transition;
3132

32-
struct Input {
33+
float4 _Params1;
34+
float4 _Params2;
35+
36+
struct Input
37+
{
3338
float2 uv_MainTex;
3439
float4 color : COLOR;
3540
};
@@ -85,57 +90,57 @@
8590
float3 uvw = v.texcoord1.xyz;
8691
float id = dot(uvw, float3(24.13, 5.87, 1)) / 30;
8792

88-
float time = frac(_Time.y / kCycle); // base time
89-
time *= 1 + kTransition * 2 / kCycle; // make transition margin
90-
time -= id * kTransition / kCycle; // bias with object id
91-
time = saturate(time); // clamp with 0-1
93+
float phase = _Phase;
94+
phase *= 1 + _Transition * 2; // make transition margin
95+
phase -= id * _Transition; // bias with object id
96+
phase = saturate(phase); // clamp with 0-1
9297

9398
const float eps = 1e-5;
94-
float dft1 = pow(max(eps, 1 - sin(time * UNITY_PI)), 2);
95-
float dft2 = pow(max(eps, 1 - sin(time * UNITY_PI)), 9);
99+
float dft1 = pow(max(eps, 1 - sin(phase * UNITY_PI)), 2);
100+
float dft2 = pow(max(eps, 1 - sin(phase * UNITY_PI)), 9);
96101

97102
// base animations
98103
float3 offs = _Size * 0.5 + uvw - 0.5;
99104
float scale = (dft2 + 1) / 2 * _Size;
100105
float4 rot = float4(0, 0, 0, 1);
101106

102107
{
103-
float dft = (1 - dft1) * (_Switch1.x > 0.9);
104-
float3 crd = uvw * 1.6 + float3(0, 0, _Time.y) * 1.2;
108+
float dft = (1 - dft1) * (_Params1.x > 0.9);
109+
float3 crd = uvw * 1.6 + float3(0, 0, _RTime) * 1.2;
105110
scale *= 1 + snoise(crd) * 0.4 * dft;
106111
}
107112
{
108-
float dft = (1 - dft1) * (_Switch1.y > 0.9);
109-
float3 crd = uvw * 1.4 + float3(0, 0, _Time.y) * 0.5;
113+
float dft = (1 - dft1) * (_Params1.y > 0.9);
114+
float3 crd = uvw * 1.4 + float3(0, 0, _RTime) * 0.5;
110115
offs += snoise_grad(crd) * 0.02 * dft;
111116
}
112117
{
113-
float dft = (1 - dft1) * (_Switch1.z > 0.9);
118+
float dft = (1 - dft1) * (_Params1.z > 0.9);
114119
float3 axis = random_point_on_sphere(id, 0);
115120
float angle = UVRand(id, 1) * 3 + 1;
116121
rot = qmul(rot, rotation_angle_axis(angle * dft, axis));
117122
}
118123
{
119-
float dft = (1 - dft1) * (_Switch1.w > 0.9);
124+
float dft = (1 - dft1) * (_Params1.w > 0.9);
120125
float3 axis = random_point_on_sphere(id, 13);
121-
float angle = snoise(uvw * 0.3 + float3(_Time.y * 0.3, 0, 0)) * 5;
126+
float angle = snoise(uvw * 0.3 + float3(_RTime * 0.3, 0, 0)) * 5;
122127
rot = qmul(rot, rotation_angle_axis(angle * dft, axis));
123128
}
124129
{
125-
float dft = saturate(_Switch1.x - dft1);
130+
float dft = saturate(_Params1.x - dft1);
126131
float3 pt = float3(UVRand(id, 6), UVRand(id, 7), UVRand(id, 8));
127132
offs = lerp(offs, pt * 0.8 - 0.4, dft);
128133
}
129134
{
130-
float dft = saturate(_Switch1.y - dft1);
135+
float dft = saturate(_Params1.y - dft1);
131136
float3 orbit = float3(UVRand(id, 2), UVRand(id, 3), UVRand(id, 4));
132-
orbit = sin((orbit + 1) * (_Time.y + 13)) * 0.4;
137+
orbit = sin((orbit + 1) * (_RTime + 13)) * 0.4;
133138
offs = lerp(offs, orbit, dft);
134139
}
135140
{
136-
float dft = saturate(_Switch1.z - dft1);
141+
float dft = saturate(_Params1.z - dft1);
137142
float sn, cs;
138-
float r = _Time.y * (0.5 + UVRand(id, 12) * 2);
143+
float r = _RTime * (0.5 + UVRand(id, 12) * 2);
139144
sincos(r, sn, cs);
140145
float3 pt = float3(offs.x, offs.y * cs - offs.z * sn, offs.y * sn + offs.z * cs);
141146
offs = lerp(offs, pt, dft);

Assets/Main.unity

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Light:
111111
serializedVersion: 6
112112
m_Type: 1
113113
m_Color: {r: 1, g: 0.9902637, b: 0.9117647, a: 1}
114-
m_Intensity: 0.3
114+
m_Intensity: 0.5
115115
m_Range: 10
116116
m_SpotAngle: 30
117117
m_CookieSize: 10
@@ -146,7 +146,7 @@ Transform:
146146
m_LocalEulerAnglesHint: {x: 11, y: -94, z: 0}
147147
m_Children: []
148148
m_Father: {fileID: 0}
149-
m_RootOrder: 5
149+
m_RootOrder: 4
150150
--- !u!1 &778849772
151151
GameObject:
152152
m_ObjectHideFlags: 0
@@ -498,6 +498,7 @@ GameObject:
498498
m_Component:
499499
- 4: {fileID: 1178461071}
500500
- 114: {fileID: 1178461070}
501+
- 95: {fileID: 1178461072}
501502
m_Layer: 0
502503
m_Name: Cube Cluster
503504
m_TagString: Untagged
@@ -516,6 +517,8 @@ MonoBehaviour:
516517
m_Script: {fileID: 11500000, guid: fc25ac8d039114093aab62baa8e91acb, type: 3}
517518
m_Name:
518519
m_EditorClassIdentifier:
520+
_phase: 0
521+
_transition: 0.1
519522
_color: {r: 1, g: 1, b: 1, a: 0}
520523
_mesh: {fileID: 11400000, guid: a4b2ab016fd8c4c6694c5ed8826f8316, type: 2}
521524
_metallicMap: {fileID: 2800000, guid: 1085d6f82e40b31428079b25ebee7d74, type: 3}
@@ -529,12 +532,30 @@ Transform:
529532
m_PrefabInternal: {fileID: 0}
530533
m_GameObject: {fileID: 1178461069}
531534
m_LocalRotation: {x: 0.5000001, y: -0.5, z: -0.5, w: 0.49999994}
532-
m_LocalPosition: {x: 0, y: 0, z: -0}
535+
m_LocalPosition: {x: 0, y: 0, z: 0}
533536
m_LocalScale: {x: 1, y: 1, z: 1}
534537
m_LocalEulerAnglesHint: {x: 0, y: -90, z: -90}
535-
m_Children: []
538+
m_Children:
539+
- {fileID: 1723420724}
536540
m_Father: {fileID: 0}
537-
m_RootOrder: 4
541+
m_RootOrder: 5
542+
--- !u!95 &1178461072
543+
Animator:
544+
serializedVersion: 3
545+
m_ObjectHideFlags: 0
546+
m_PrefabParentObject: {fileID: 0}
547+
m_PrefabInternal: {fileID: 0}
548+
m_GameObject: {fileID: 1178461069}
549+
m_Enabled: 1
550+
m_Avatar: {fileID: 0}
551+
m_Controller: {fileID: 9100000, guid: 86e8d568b60da4618aa1decf03cc3a12, type: 2}
552+
m_CullingMode: 0
553+
m_UpdateMode: 0
554+
m_ApplyRootMotion: 0
555+
m_LinearVelocityBlending: 0
556+
m_WarningMessage:
557+
m_HasTransformHierarchy: 1
558+
m_AllowConstantClipSamplingOptimization: 1
538559
--- !u!1 &1346240754
539560
GameObject:
540561
m_ObjectHideFlags: 0
@@ -815,12 +836,12 @@ Transform:
815836
m_PrefabParentObject: {fileID: 0}
816837
m_PrefabInternal: {fileID: 0}
817838
m_GameObject: {fileID: 1723420722}
818-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
839+
m_LocalRotation: {x: -0.5000001, y: 0.5, z: 0.5, w: 0.49999994}
819840
m_LocalPosition: {x: 0, y: 0, z: 0}
820841
m_LocalScale: {x: 1, y: 1, z: 1}
821-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
842+
m_LocalEulerAnglesHint: {x: -90, y: 90, z: 0}
822843
m_Children: []
823-
m_Father: {fileID: 1945796552}
844+
m_Father: {fileID: 1178461071}
824845
m_RootOrder: 0
825846
--- !u!1 &1748904341
826847
GameObject:
@@ -1034,50 +1055,3 @@ Transform:
10341055
- {fileID: 821748882}
10351056
m_Father: {fileID: 0}
10361057
m_RootOrder: 3
1037-
--- !u!1 &1945796550
1038-
GameObject:
1039-
m_ObjectHideFlags: 0
1040-
m_PrefabParentObject: {fileID: 0}
1041-
m_PrefabInternal: {fileID: 0}
1042-
serializedVersion: 4
1043-
m_Component:
1044-
- 4: {fileID: 1945796552}
1045-
- 95: {fileID: 1945796551}
1046-
m_Layer: 0
1047-
m_Name: Particle System
1048-
m_TagString: Untagged
1049-
m_Icon: {fileID: 0}
1050-
m_NavMeshLayer: 0
1051-
m_StaticEditorFlags: 0
1052-
m_IsActive: 1
1053-
--- !u!95 &1945796551
1054-
Animator:
1055-
serializedVersion: 3
1056-
m_ObjectHideFlags: 0
1057-
m_PrefabParentObject: {fileID: 0}
1058-
m_PrefabInternal: {fileID: 0}
1059-
m_GameObject: {fileID: 1945796550}
1060-
m_Enabled: 1
1061-
m_Avatar: {fileID: 0}
1062-
m_Controller: {fileID: 9100000, guid: 9215d52b657274962bdebd73a2ef7fc2, type: 2}
1063-
m_CullingMode: 0
1064-
m_UpdateMode: 0
1065-
m_ApplyRootMotion: 0
1066-
m_LinearVelocityBlending: 0
1067-
m_WarningMessage:
1068-
m_HasTransformHierarchy: 1
1069-
m_AllowConstantClipSamplingOptimization: 1
1070-
--- !u!4 &1945796552
1071-
Transform:
1072-
m_ObjectHideFlags: 0
1073-
m_PrefabParentObject: {fileID: 0}
1074-
m_PrefabInternal: {fileID: 0}
1075-
m_GameObject: {fileID: 1945796550}
1076-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
1077-
m_LocalPosition: {x: 0, y: 0, z: 0}
1078-
m_LocalScale: {x: 1, y: 1, z: 1}
1079-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1080-
m_Children:
1081-
- {fileID: 1723420724}
1082-
m_Father: {fileID: 0}
1083-
m_RootOrder: 6

Assets/Material/Skybox.mat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ Material:
101101
data:
102102
first:
103103
name: _Exposure
104-
second: 3
105-
data:
106-
first:
107-
name: _Glossiness
108-
second: 0.5
104+
second: 3.4
109105
data:
110106
first:
111107
name: _Metallic
@@ -114,6 +110,10 @@ Material:
114110
first:
115111
name: _BumpScale
116112
second: 1
113+
data:
114+
first:
115+
name: _Glossiness
116+
second: 0.5
117117
data:
118118
first:
119119
name: _OcclusionStrength

0 commit comments

Comments
 (0)