@@ -25,6 +25,9 @@ public class SurfaceMeshVisualizer : MonoBehaviour
25
25
26
26
[ SerializeField ] private Vector3 offset ;
27
27
28
+ [ SerializeField ] private bool showNormals ;
29
+ [ SerializeField ] private float normalSize = 0.1f ;
30
+
28
31
private Mesh mesh ;
29
32
private int currentResolution ;
30
33
private Vector3 [ ] vertices ;
@@ -70,10 +73,23 @@ private void OnEnable()
70
73
Refresh ( ) ;
71
74
}
72
75
76
+ private void OnDrawGizmosSelected ( )
77
+ {
78
+ if ( showNormals && vertices != null )
79
+ {
80
+ Gizmos . color = Color . yellow ;
81
+ for ( int v = 0 ; v < vertices . Length ; v ++ )
82
+ {
83
+ Gizmos . DrawRay ( transform . TransformPoint ( vertices [ v ] ) , normals [ v ] * normalSize ) ;
84
+ }
85
+ }
86
+ }
87
+
73
88
[ ContextMenu ( "Refresh" ) ]
74
89
private void Refresh ( )
75
90
{
76
- if ( currentResolution != resolution || mesh == null )
91
+ if ( currentResolution != resolution || mesh == null || vertices == null ||
92
+ vertices . Length != ( ( resolution + 1 ) * ( resolution + 1 ) ) )
77
93
{
78
94
CreateMesh ( ) ;
79
95
}
@@ -83,8 +99,8 @@ private void Refresh()
83
99
var point01 = transform . TransformPoint ( new Vector3 ( - 0.5f , 0 , 0.5f ) ) + offset ;
84
100
var point11 = transform . TransformPoint ( new Vector3 ( 0.5f , 0 , 0.5f ) ) + offset ;
85
101
86
- // var minSample = float.MaxValue;
87
- // var maxSample = float.MinValue;
102
+ var minSample = float . MaxValue ;
103
+ var maxSample = float . MinValue ;
88
104
89
105
var stepSize = 1f / resolution ;
90
106
for ( int v = 0 , y = 0 ; y <= resolution ; y ++ )
@@ -95,16 +111,19 @@ private void Refresh()
95
111
{
96
112
var point = Vector3 . Lerp ( point0 , point1 , x * stepSize ) ;
97
113
var sample = Noise ( point ) ;
98
- //minSample = Mathf.Min(sample, minSample);
99
- //maxSample = Mathf.Max(sample, maxSample);
100
- vertices [ v ] . y = ( sample - 0.5f ) * strength ;
101
- colors [ v ] = coloring . Evaluate ( sample ) ;
114
+ minSample = Mathf . Min ( sample . value , minSample ) ;
115
+ maxSample = Mathf . Max ( sample . value , maxSample ) ;
116
+ vertices [ v ] . y = ( sample . value - 0.5f ) * strength ;
117
+ colors [ v ] = coloring . Evaluate ( sample . value ) ;
118
+ normals [ v ] = new Vector3 ( sample . derivative . x , 1 , 0 ) . normalized ;
102
119
}
103
120
}
104
121
122
+ //Debug.Log($"Min: {minSample} Max:{maxSample}");
123
+
105
124
mesh . vertices = vertices ;
125
+ mesh . normals = normals ;
106
126
mesh . colors = colors ;
107
- mesh . RecalculateNormals ( ) ;
108
127
}
109
128
110
129
[ ContextMenu ( "Force Refresh" ) ]
@@ -114,40 +133,58 @@ private void ForceRefresh()
114
133
Refresh ( ) ;
115
134
}
116
135
117
- private float Noise ( Vector3 point )
136
+ private NoiseSample Noise ( Vector3 point )
118
137
{
119
138
var v = 0f ;
139
+ var sample = new NoiseSample ( ) ;
120
140
switch ( dimension )
121
141
{
122
142
case Dimension . Value1D :
123
- v = ValueNoise . Fractal1D ( point . x * frequency , seed , frequency , octaves , lacunarity , persistence ) ;
143
+ //sample = ValueNoise.Sample1D(point.x, seed, frequency);
144
+ sample = ValueNoise . FractalSample1D ( point . x , seed : seed , frequency : frequency , octaves : octaves , lacunarity : lacunarity , persistence : persistence ) ;
145
+ //v = ValueNoise.Fractal1D(point.x * frequency, seed, frequency, octaves, lacunarity, persistence);
124
146
break ;
125
147
case Dimension . Value2D :
126
148
v = ValueNoise . Fractal2D ( point * frequency , seed , frequency , octaves , lacunarity , persistence ) ;
149
+ sample . value = v ;
127
150
break ;
128
151
case Dimension . Value3D :
129
152
v = ValueNoise . Fractal3D ( point * frequency , seed , frequency , octaves , lacunarity , persistence ) ;
153
+ sample . value = v ;
130
154
break ;
131
155
case Dimension . Perlin1D :
132
156
v = PerlinGradientNoise . Fractal1D ( point . x , seed , frequency , octaves , lacunarity , persistence ) ;
157
+ sample . value = v ;
133
158
break ;
134
159
case Dimension . Perlin2D :
135
160
v = PerlinGradientNoise . Fractal2D ( point . x , point . y , seed , frequency , octaves , lacunarity ,
136
161
persistence ) ;
162
+ sample . value = v ;
137
163
break ;
138
164
case Dimension . Perlin3D :
139
165
v = PerlinGradientNoise . Fractal3D ( point . x , point . y , point . z , seed , frequency , octaves , lacunarity ,
140
166
persistence ) ;
167
+ sample . value = v ;
141
168
break ;
142
169
}
143
170
144
- return v ;
171
+ return sample ;
145
172
}
146
173
147
174
private void CreateMesh ( )
148
175
{
149
176
currentResolution = resolution ;
150
- mesh . Clear ( ) ;
177
+
178
+ if ( mesh == null )
179
+ {
180
+ mesh = new Mesh ( ) ;
181
+ mesh . name = "Surface Mesh" ;
182
+ MeshFilter . mesh = mesh ;
183
+ }
184
+ else
185
+ {
186
+ mesh . Clear ( ) ;
187
+ }
151
188
152
189
vertices = new Vector3 [ ( resolution + 1 ) * ( resolution + 1 ) ] ;
153
190
colors = new Color [ vertices . Length ] ;
0 commit comments