-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAZQuadGrid.cs
375 lines (279 loc) · 9.98 KB
/
AZQuadGrid.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using UnityEngine;
using System.Collections;
using System.IO;
[ExecuteInEditMode]
public class AZQuadGrid : MonoBehaviour
{
public struct AZQuadCell
{
public int x;
public int y;
public Vector3 center;
};
public Material lineMaterial;
public Material quadMaterial;
public bool renderQuads=true;
public bool renderLines=true;
[HideInInspector] [SerializeField] int numRows=3;
[ExposeProperty]
public int Rows
{
set{ numRows = Mathf.Clamp(value, 0, 100); rebuild();}
get{ return numRows;}
}
[HideInInspector] [SerializeField] int numColumns=3;
[ExposeProperty]
public int Columns
{
set{ numColumns = Mathf.Clamp(value, 0, 100); rebuild();}
get{ return numColumns;}
}
[HideInInspector] [SerializeField] float CellWidth=1.0f;
[ExposeProperty]
public float cellWidth
{
set{ CellWidth = Mathf.Abs(value); rebuild();}
get{ return CellWidth;}
}
[HideInInspector] [SerializeField] float CellHeight=1.0f;
[ExposeProperty]
public float cellHeight
{
set{ CellHeight = Mathf.Abs(value); rebuild();}
get{ return CellHeight;}
}
/**
* line width is substracted to cellwidth, cellheight (takes space from the cell)
*/
[HideInInspector] [SerializeField] float LineWidth=0.1f;
[ExposeProperty]
public float lineWidth
{
set{ LineWidth = Mathf.Clamp(value, 0.0f, Mathf.Min(CellWidth, CellHeight)); rebuild();}
get{ return LineWidth;}
}
private bool dorebuild=false;
private Mesh lineMesh;
private Mesh quadMesh;
private Vector3[] vertices;
//private Vector3[] normals;
//we need two uvsets, both of the same size(==vertices.size)
private Vector2[] lineUvs;
private Vector2[] quadUvs;
private int[] lineTriangles;
private int[] quadTriangles;
private BoxCollider boxcollider;
private AZQuadCell currCell;
public void rebuild()
{
dorebuild = true;
}
//--------------
// Use this for initialization
void Start ()
{
boxcollider = this.GetComponent<BoxCollider>();
_rebuild();
}
// Update is called once per frame
void Update () {
if(dorebuild) _rebuild ();
if(renderLines)
Graphics.DrawMesh(lineMesh, transform.localToWorldMatrix, lineMaterial, gameObject.layer);
//Graphics.DrawMesh(lineMesh, transform.position, transform.rotation, lineMaterial, gameObject.layer);
if(renderQuads)
Graphics.DrawMesh(quadMesh, transform.localToWorldMatrix, quadMaterial, gameObject.layer);
//Graphics.DrawMesh(quadMesh, transform.position, transform.rotation, quadMaterial, gameObject.layer);
}
//---------------------------
private void _rebuild()
{
//StreamWriter writer = new StreamWriter("C:\\debug.txt");
lineMesh = new Mesh();
quadMesh = new Mesh();
int numPoints = numRows*numColumns;
int numCorners = (numRows+1)*(numColumns+1);
int sizeVertexBuffer = numCorners * 16;//each point has 16 vertex
int numLines = numRows+1+numColumns+1; //needed lines to draw the grid
int sizeLineTriangleBuffer = numLines*2*3; //each line needs two triangles, each triangle three index
int sizeQuadTriangleBuffer = numPoints*2*3; //each quad needs two triangles, each triangle three index
vertices = new Vector3[sizeVertexBuffer];
//normals = new Vector3[sizeVertexBuffer];
quadUvs = new Vector2[sizeVertexBuffer];
lineUvs =new Vector2[sizeVertexBuffer];
lineTriangles = new int[sizeLineTriangleBuffer];
quadTriangles = new int[sizeQuadTriangleBuffer];
//mesh is centered in the parent transform
//we create for each point in the grid (each cell corner) a QUADLET (a small square of vertex)
//we need to iterate over an additional row and column, in order to create quadlets for the final
//bottm and right edges.
//LineWidth;
float lineWidth2 = LineWidth/2.0f;
float orix = -(CellWidth*numColumns)/2.0f;
float oriy = -(CellHeight*numRows)/2.0f;
float posx = orix;
float posy = oriy;
int row, col;
int i = 0;
for(row = 0; row<numRows+1;row++)
{
posx = orix;
for(col = 0; col<numColumns+1; col++)
{
//writer.WriteLine("cell "+row+","+col+" i "+ i + " pos "+ posx + ","+posy);
//posx, posy is the CENTER of the quadlet..ie
//vertex 0
vertices[i].x = posx-lineWidth2;
vertices[i].y = 0.0f;
vertices[i].z = posy-lineWidth2;
//writer.WriteLine((i)+": vertex 0: "+(posx-lineWidth2)+", "+ (posy-lineWidth2));
i++;
//vertex 1
vertices[i].x = posx+lineWidth2;
vertices[i].y = 0.0f;
vertices[i].z = posy-lineWidth2;
//writer.WriteLine((i)+": vertex 1: "+(posx+lineWidth2)+", "+ (posy-lineWidth2));
i++;
//vertex 2
vertices[i].x = posx+lineWidth2;
vertices[i].y = 0.0f;
vertices[i].z = posy+lineWidth2;
//writer.WriteLine((i)+": vertex 2: "+(posx+lineWidth2)+", "+ (posy+lineWidth2));
i++;
//vertex 3
vertices[i].x = posx-lineWidth2;
vertices[i].y = 0.0f;
vertices[i].z = posy+lineWidth2;
//writer.WriteLine((i)+": vertex 3: "+(posx-lineWidth2)+", "+ (posy+lineWidth2));
i++;
//after
posx+=CellWidth;
}
posy +=CellHeight;
}
//init normals
/*for(i=0;i<sizeVertexBuffer;i++)
{
normals[i].x= 0.0f;
normals[i].y=1.0f;
normals[i].z=0.0f;
}*/
/*NOW compute the triangle INDEXES for quads
each quad (cell) is formed by the local vertex 2, 7, 8, 13
in two triangles: 2,7,8 and 2, 8, 13
*/
int idquad = 0;
for(row = 0; row<numRows;row++)
{
for(col = 0; col<numColumns; col++)
{
//writer.WriteLine(" quad : "+col+", "+ row);
quadTriangles[idquad++] = _getVertexIndex(row, col)+2;
quadTriangles[idquad++] = _getVertexIndex(row+1, col+1);//+0;
quadTriangles[idquad++] = _getVertexIndex(row, col+1)+3;
//writer.WriteLine (_getVertexIndex(row, col)+","+_getVertexIndex(row, col+1)+","+_getVertexIndex(row+1, col+1));
quadTriangles[idquad++] = _getVertexIndex(row, col)+2;
quadTriangles[idquad++] = _getVertexIndex(row+1, col)+1;
quadTriangles[idquad++] = _getVertexIndex(row+1, col+1);//+0;
//writer.WriteLine (_getVertexIndex(row, col)+","+_getVertexIndex(row+1, col+1)+","+_getVertexIndex(row+1, col));
}
}
//now, build the triangle index for the lines
int idline=0;
for(row = 0; row<=numRows;row++)
{
//writer.WriteLine(" line row : "+row);
lineTriangles[idline++] = _getVertexIndex(row, 0);
lineTriangles[idline++] = _getVertexIndex(row, numColumns)+2;
lineTriangles[idline++] = _getVertexIndex(row, numColumns)+1;
//writer.WriteLine(" "+_getVertexIndex(row, 0)+ " to "+ _getVertexIndex(row, numColumns));
lineTriangles[idline++] = _getVertexIndex(row, 0);
lineTriangles[idline++] = _getVertexIndex(row, 0)+3;
lineTriangles[idline++] = _getVertexIndex(row, numColumns)+2;
//writer.WriteLine(" "+_getVertexIndex(row, 0)+ " to "+ _getVertexIndex(row, numColumns));
}
//idline=0;
for(col = 0; col<=numColumns;col++)
{
//writer.WriteLine(" line col : "+col);
lineTriangles[idline++] = _getVertexIndex(0,col);
lineTriangles[idline++] = _getVertexIndex(numRows,col)+2;
lineTriangles[idline++] = _getVertexIndex(0,col)+1;
//writer.WriteLine(" "+_getVertexIndex(0,col)+ " to "+ _getVertexIndex(numRows,col));
lineTriangles[idline++] = _getVertexIndex(0,col);
lineTriangles[idline++] = _getVertexIndex(numRows,col)+3;
lineTriangles[idline++] = _getVertexIndex(numRows,col)+2;
//writer.WriteLine(" "+_getVertexIndex(0,col)+ " to "+ _getVertexIndex(numRows,col));
}
/*quadUvs = new Vector2[sizeVertexBuffer];
lineUvs =new Vector2[sizeVertexBuffer];
lineTriangles = new int[sizeLineTriangleBuffer];
*/
lineMesh.vertices = vertices;
quadMesh.vertices = vertices;
//lineMesh.normals = normals;
//quadMesh.normals = normals;
lineMesh.RecalculateNormals();
quadMesh.RecalculateNormals();
lineMesh.uv = lineUvs;
quadMesh.uv = quadUvs;
lineMesh.triangles = lineTriangles;
quadMesh.triangles = quadTriangles;
//writer.Close();
//update collider
if(boxcollider != null)
{
Vector3 collsize = new Vector3();
collsize.x = cellWidth*numColumns;
collsize.y = 0.1f;
collsize.z = cellHeight*numRows;
boxcollider.size = collsize;
}
dorebuild = false;
}
//----------------------
private int _getVertexIndex(int row, int col)
{
return (row*(numColumns+1)*4) + ((col)*4);
}
//----------------------
public void DrawGizmo()
{
foreach(Vector3 point in vertices)
{
Gizmos.DrawLine(Vector3.zero, point);
}
}
//------------------------
public void OnMouseDown()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane plane = new Plane(transform.up, transform.position);
float distance = 0;
if(plane.Raycast(ray, out distance))
{
Vector3 point = ray.GetPoint(distance);
//Debug.DrawLine(ray.origin, point);
//compute the selected cell
//point = transform.worldToLocalMatrix*point;
point = transform.InverseTransformPoint(point);
float width = cellWidth*numColumns;
float width2 = width*0.5f;
float height = cellHeight*numRows;
float height2 = height*0.5f;
Debug.Log ("point: " + point.ToString() + " w "+width + " w2 "+ width2 + " h "+ height + " h2 "+ height2);
Debug.Log("div "+ ((point.x+width2) / cellWidth) + ". "+((point.z+height2)/cellHeight));
//currCell.x = Mathf.Clamp(Mathf.FloorToInt((point.x+width2) / cellWidth), 0, numColumns-1);
//currCell.y = Mathf.Clamp(Mathf.FloorToInt ((point.z+height2)/cellHeight), 0, numRows-1);
currCell.x = Mathf.FloorToInt((point.x+width2) / cellWidth);
currCell.y = Mathf.FloorToInt ((point.z+height2)/cellHeight);
currCell.center.x = (currCell.x * CellWidth)+(CellWidth*0.5f)-width2;
currCell.center.y = 0.0f;
currCell.center.z = (currCell.y * CellHeight)+(CellHeight*0.5f)-height2;
currCell.center = transform.TransformPoint(currCell.center);
this.SendMessage("OnTouchedCell", currCell , SendMessageOptions.DontRequireReceiver);
}
//Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
}
//----------------------------
}