- Home /
Create grid of points inside irregular quad.
Hi! I've been working on creating a grid of vertices to fill an irregular quad that can be rotated/ skewed in a lot of different manners. To do this I am passing the script 4 points (these are always flat on the Y axis). Here is what I have so far :
http://answers.unity3d.com/storage/attachments/6280-irregularquad.jpg
(In case you are wonder what the use for this is, the camera is being used to project a quad onto the Y axis. This will then be tessellated to create an endless ocean). The yellow points are the current vertices, I now need to alter their Z axis to form a grid.
The current code I am using for calculating the line is this :
for (int x = 0; x < vCount; x++) {
for (int z = 0; z < hCount; z++) {
vertices[index] = new Vector3(
ClipCorners[1].x + (((ClipCorners[2].x - ClipCorners[1].x) / (vCount-1)) * x),
clipHeight,
ClipCorners[1].z + (((ClipCorners[2].z - ClipCorners[1].z) / (vCount-1)) * x)
);
index++;
}
};
ClipCorners are the 4 corners of the quad in world space, vCount & hCount are dynamically increased to compensate for stretching of the quad. I know some of you are geometry math nuts so if there is a better way to be doing this please let me know! All that is important is that the grid is a.) in order for triangulation, b.) procedural creation (the pink quad is made stretching an existing model).
Thanks!
[1]: http://answers.unity3d.com/storage/attachments/6280-irregularquad.jpg
Answer by MylesLambert · Jan 02, 2013 at 11:58 PM
I made some progress with this (turns out sleep is the best answer), I just need to sort out some issues - not sure how to fix this slight over extension. Here is my updated code :)
for (int x = 0; x < vCount; x++) {
for (int z = 0; z < hCount; z++) {
vertices[index] = ClipCorners[1] +
(((ClipCorners[3] - ClipCorners[1]) / (hCount-1)) * z)
+ (((((ClipCorners[0] - ClipCorners[2]) / (hCount-1)) * z) / (vCount-1)) * x);
vertices[index]+= (((ClipCorners[2] - ClipCorners[1]) / (vCount-1)) * x);
vertices[index].y = clipHeight;
index++;
}
};
Your answer
![](https://koobas.hobune.stream/wayback/20220613090234im_/https://answers.unity.com/themes/thub/images/avi.jpg)