@@ -145,21 +145,21 @@ public static VoxelMeshData CreateMeshData(float[] heightMap, int width, int hei
145
145
146
146
public class VoxelMeshData
147
147
{
148
- private List < Vector3 > vertices = new List < Vector3 > ( ) ;
149
- private List < Vector2 > uv = new List < Vector2 > ( ) ;
150
- private List < int > triangles = new List < int > ( ) ;
148
+ private readonly List < Vector3 > _vertices = new List < Vector3 > ( ) ;
149
+ private readonly List < Vector2 > _uv = new List < Vector2 > ( ) ;
150
+ private readonly List < int > _triangles = new List < int > ( ) ;
151
151
152
- private int width ;
153
- private int height ;
154
- private int quadCount ;
155
- private Vector2Int offset ;
152
+ private readonly int _width ;
153
+ private readonly int _height ;
154
+ private int _quadCount ;
155
+ private readonly Vector2Int _offset ;
156
156
157
157
public VoxelMeshData ( int width , int height , Vector2Int offset )
158
158
{
159
- this . width = width ;
160
- this . height = height ;
161
- this . offset = offset ;
162
- quadCount = 0 ;
159
+ _width = width ;
160
+ _height = height ;
161
+ _offset = offset ;
162
+ _quadCount = 0 ;
163
163
}
164
164
165
165
/// <summary>
@@ -177,10 +177,10 @@ public void AddUpQuad(int x, int y, float elevation)
177
177
var topRight = new Vector3 ( x + 1 , elevation , y + 1 ) ;
178
178
var bottomRight = new Vector3 ( x + 1 , elevation , y ) ;
179
179
180
- vertices . Add ( bottomLeft ) ;
181
- vertices . Add ( topLeft ) ;
182
- vertices . Add ( topRight ) ;
183
- vertices . Add ( bottomRight ) ;
180
+ _vertices . Add ( bottomLeft ) ;
181
+ _vertices . Add ( topLeft ) ;
182
+ _vertices . Add ( topRight ) ;
183
+ _vertices . Add ( bottomRight ) ;
184
184
185
185
AddUV ( x , y ) ;
186
186
AddTriangles ( ) ;
@@ -201,10 +201,10 @@ public void AddLeftQuad(int x, int y, float elevation, float depth)
201
201
var topRight = new Vector3 ( x , elevation , y ) ;
202
202
var bottomRight = new Vector3 ( x , elevation - depth , y ) ;
203
203
204
- vertices . Add ( bottomLeft ) ;
205
- vertices . Add ( topLeft ) ;
206
- vertices . Add ( topRight ) ;
207
- vertices . Add ( bottomRight ) ;
204
+ _vertices . Add ( bottomLeft ) ;
205
+ _vertices . Add ( topLeft ) ;
206
+ _vertices . Add ( topRight ) ;
207
+ _vertices . Add ( bottomRight ) ;
208
208
209
209
AddUV ( x , y ) ;
210
210
AddTriangles ( ) ;
@@ -225,10 +225,10 @@ public void AddRightQuad(int x, int y, float elevation, float depth)
225
225
var topRight = new Vector3 ( x + 1 , elevation , y + 1 ) ;
226
226
var bottomRight = new Vector3 ( x + 1 , elevation - depth , y + 1 ) ;
227
227
228
- vertices . Add ( bottomLeft ) ;
229
- vertices . Add ( topLeft ) ;
230
- vertices . Add ( topRight ) ;
231
- vertices . Add ( bottomRight ) ;
228
+ _vertices . Add ( bottomLeft ) ;
229
+ _vertices . Add ( topLeft ) ;
230
+ _vertices . Add ( topRight ) ;
231
+ _vertices . Add ( bottomRight ) ;
232
232
233
233
AddUV ( x , y ) ;
234
234
AddTriangles ( ) ;
@@ -249,10 +249,10 @@ public void AddBackQuad(int x, int y, float elevation, float depth)
249
249
var topRight = new Vector3 ( x + 1 , elevation , y ) ;
250
250
var bottomRight = new Vector3 ( x + 1 , elevation - depth , y ) ;
251
251
252
- vertices . Add ( bottomLeft ) ;
253
- vertices . Add ( topLeft ) ;
254
- vertices . Add ( topRight ) ;
255
- vertices . Add ( bottomRight ) ;
252
+ _vertices . Add ( bottomLeft ) ;
253
+ _vertices . Add ( topLeft ) ;
254
+ _vertices . Add ( topRight ) ;
255
+ _vertices . Add ( bottomRight ) ;
256
256
257
257
AddUV ( x , y ) ;
258
258
AddTriangles ( ) ;
@@ -273,10 +273,10 @@ public void AddFrontQuad(int x, int y, float elevation, float depth)
273
273
var topRight = new Vector3 ( x , elevation , y + 1 ) ;
274
274
var bottomRight = new Vector3 ( x , elevation - depth , y + 1 ) ;
275
275
276
- vertices . Add ( bottomLeft ) ;
277
- vertices . Add ( topLeft ) ;
278
- vertices . Add ( topRight ) ;
279
- vertices . Add ( bottomRight ) ;
276
+ _vertices . Add ( bottomLeft ) ;
277
+ _vertices . Add ( topLeft ) ;
278
+ _vertices . Add ( topRight ) ;
279
+ _vertices . Add ( bottomRight ) ;
280
280
281
281
AddUV ( x , y ) ;
282
282
AddTriangles ( ) ;
@@ -291,43 +291,43 @@ public void AddFrontQuad(int x, int y, float elevation, float depth)
291
291
private void AddUV ( int x , int y , float padding = 0.1f )
292
292
{
293
293
//We need to add the offset so UVs are shifted to correct texture chunk
294
- var uvBottomLeft = new Vector2 ( ( x + offset . x + padding ) / width , ( y + offset . y + padding ) / height ) ;
295
- var uvTopLeft = new Vector2 ( ( x + offset . x + padding ) / width , ( y + offset . y + 1 - padding ) / height ) ;
296
- var uvTopRight = new Vector2 ( ( x + offset . x + 1 - padding ) / width , ( y + offset . y + 1 - padding ) / height ) ;
297
- var uvBottomRight = new Vector2 ( ( x + offset . x + 1 - padding ) / width , ( y + offset . y + padding ) / height ) ;
298
-
299
- uv . Add ( uvBottomLeft ) ;
300
- uv . Add ( uvTopLeft ) ;
301
- uv . Add ( uvTopRight ) ;
302
- uv . Add ( uvBottomRight ) ;
294
+ var uvBottomLeft = new Vector2 ( ( x + _offset . x + padding ) / _width , ( y + _offset . y + padding ) / _height ) ;
295
+ var uvTopLeft = new Vector2 ( ( x + _offset . x + padding ) / _width , ( y + _offset . y + 1 - padding ) / _height ) ;
296
+ var uvTopRight = new Vector2 ( ( x + _offset . x + 1 - padding ) / _width , ( y + _offset . y + 1 - padding ) / _height ) ;
297
+ var uvBottomRight = new Vector2 ( ( x + _offset . x + 1 - padding ) / _width , ( y + _offset . y + padding ) / _height ) ;
298
+
299
+ _uv . Add ( uvBottomLeft ) ;
300
+ _uv . Add ( uvTopLeft ) ;
301
+ _uv . Add ( uvTopRight ) ;
302
+ _uv . Add ( uvBottomRight ) ;
303
303
}
304
304
305
305
/// <summary>
306
306
/// Add triangles for current quad
307
307
/// </summary>
308
308
private void AddTriangles ( )
309
309
{
310
- int triangleIndex = quadCount * 4 ;
310
+ int triangleIndex = _quadCount * 4 ;
311
311
312
- triangles . Add ( triangleIndex + 0 ) ;
313
- triangles . Add ( triangleIndex + 1 ) ;
314
- triangles . Add ( triangleIndex + 2 ) ;
312
+ _triangles . Add ( triangleIndex + 0 ) ;
313
+ _triangles . Add ( triangleIndex + 1 ) ;
314
+ _triangles . Add ( triangleIndex + 2 ) ;
315
315
316
- triangles . Add ( triangleIndex + 0 ) ;
317
- triangles . Add ( triangleIndex + 2 ) ;
318
- triangles . Add ( triangleIndex + 3 ) ;
316
+ _triangles . Add ( triangleIndex + 0 ) ;
317
+ _triangles . Add ( triangleIndex + 2 ) ;
318
+ _triangles . Add ( triangleIndex + 3 ) ;
319
319
320
- quadCount ++ ;
320
+ _quadCount ++ ;
321
321
}
322
322
323
323
324
324
public Mesh CreateMesh ( )
325
325
{
326
326
var mesh = new Mesh
327
327
{
328
- vertices = vertices . ToArray ( ) ,
329
- triangles = triangles . ToArray ( ) ,
330
- uv = uv . ToArray ( )
328
+ vertices = _vertices . ToArray ( ) ,
329
+ triangles = _triangles . ToArray ( ) ,
330
+ uv = _uv . ToArray ( )
331
331
} ;
332
332
mesh . RecalculateNormals ( ) ;
333
333
return mesh ;
0 commit comments