public MeshAttributes Generate() { int numVerts = horizontal * vertical * 6; MeshAttributes mesh = new MeshAttributes { vertices = new Vector3[numVerts], uv = new Vector2[numVerts], triangles = Enumerable.Range(0, numVerts).ToArray <int>(), normals = new Vector3[horizontal + 1], normalIndex = new int[numVerts], tangent = new Vector3[] { Vector3.up }, tangentIndex = Enumerable.Repeat <int>(0, numVerts).ToArray <int>() }; //Generate Normals GenerateNormals(mesh); for (int y = 0; y < vertical; ++y) { for (int x = 0; x < horizontal; ++x) { int index = (y * horizontal + x) * 6; GenerateQuad(mesh, x, y, index); GenerateUv(mesh, x, y, index); GenerateNormalIndex(mesh, x, index); } } return(mesh); }
private void GenerateQuad(MeshAttributes mesh, int x, int y, int index) { //Angle of the leftmost vertex float angle = x * addTh; //I could've used a vector3 and added to another y vector3 //These two values are more easily memoizable than the positions. Vector2 positionXZ = GetXzPosition(angle); Vector2 nextPositionXZ = GetXzPosition(angle + addTh); float positionY = bottom + y * addY; float nextPositionY = bottom + (y + 1) * addY; Vector3 position = new Vector3(positionXZ.x, positionY, positionXZ.y); Vector3 nextX = new Vector3(nextPositionXZ.x, positionY, nextPositionXZ.y); Vector3 nextY = new Vector3(positionXZ.x, nextPositionY, positionXZ.y); Vector3 nextXY = new Vector3(nextPositionXZ.x, nextPositionY, nextPositionXZ.y); //Bottom triangle mesh.vertices[index] = position; mesh.vertices[index + 1] = nextY; mesh.vertices[index + 2] = nextX; //Top triangle mesh.vertices[index + 3] = nextXY; mesh.vertices[index + 4] = nextX; mesh.vertices[index + 5] = nextY; }
void GenerateNormals(MeshAttributes mesh) { for (int i = 0; i <= horizontal; ++i) { float t = ((float)i / (float)horizontal); mesh.normals[i] = normalMultiplier * new Vector3(Mathf.Sin(PI_2 * t), 0.0f, Mathf.Cos(PI_2 * t)); } }
private void GenerateNormalIndex(MeshAttributes mesh, int x, int index) { //Vertices 0,1 and 5 are to the left, and the rest are to the right for (int i = 0; i < 6; ++i) { //More to the right if (i >= 2 && i <= 4) { mesh.normalIndex[index + i] = x + 1; } //More to the left else { mesh.normalIndex[index + i] = x; } } }
//copied from PlaneGenerator private void GenerateUv(MeshAttributes mesh, int x, int y, int index) { Vector2 position = new Vector2(addUv.x * x, addUv.y * y); Vector2 nextU = position + addUv.x * Vector2.right; Vector2 nextV = position + addUv.y * Vector2.up; Vector2 nextUV = position + addUv; //Bottom triangle mesh.uv[index] = position; mesh.uv[index + 1] = nextV; mesh.uv[index + 2] = nextU; //Top triangle mesh.uv[index + 3] = nextUV; mesh.uv[index + 4] = nextU; mesh.uv[index + 5] = nextV; }
private void GenerateUv(MeshAttributes mesh, int x, int y, int index) { Vector2 texCoord = new Vector2(addUv.x * x, addUv.y * y); texCoord += textureLowerLeft; Vector2 nextU = texCoord + addUv.x * Vector2.right; Vector2 nextV = texCoord + addUv.y * Vector2.up; Vector2 nextUV = texCoord + addUv; //Bottom triangle mesh.uv[index] = texCoord; mesh.uv[index + 1] = nextV; mesh.uv[index + 2] = nextU; //Top triangle mesh.uv[index + 3] = nextUV; mesh.uv[index + 4] = nextU; mesh.uv[index + 5] = nextV; }
private void GenerateQuad(MeshAttributes mesh, int x, int y, int index) { Vector3 position = new Vector3(addPos.x * x, addPos.y * y, 0.0f); position += lowerLeft; Vector3 nextX = position + addPos.x * Vector3.right; Vector3 nextY = position + addPos.y * Vector3.up; Vector3 nextXY = position + addPos; //Bottom triangle mesh.vertices[index] = position; mesh.vertices[index + 1] = nextY; mesh.vertices[index + 2] = nextX; //Top triangle mesh.vertices[index + 3] = nextXY; mesh.vertices[index + 4] = nextX; mesh.vertices[index + 5] = nextY; }
public Mesh Modulate(MeshAttributes mesh) { Mesh result = new Mesh(); int length = mesh.vertices.Length; uvwAmplitudePos = new Vector3( uvDisplacement.x * uvDistPos.x, uvDisplacement.y * uvDistPos.y, amplitude ); uvAmplitudeTex = Vector2.Scale(uvDistTex, uvDisplacement); for (int i = 0; i < length; ++i) { Vector3 normal = mesh.normals[mesh.normalIndex[i]]; //AGAIN, TANGENT IS UP BECAUSE THIS ARE LEFT HANDED COORDINATES!! Vector3 tangent = mesh.tangent[mesh.tangentIndex[i]]; Vector3 bitangent = Vector3.Cross(normal, tangent); Vector3 noise = CalculateNoise(mesh.vertices[i]); float t = (mesh.vertices[i].y - vMin) / (vMax - vMin); float vFactor = Mathf.Sin(Mathf.PI * t); mesh.vertices[i] = ModulateVertex(mesh.vertices[i], noise, normal, bitangent, tangent, vFactor); mesh.uv[i] = ModulateUv(mesh.uv[i], noise, vFactor); } result.vertices = mesh.vertices; result.uv = mesh.uv; result.triangles = mesh.triangles; result.RecalculateNormals(); result.RecalculateTangents(); result.RecalculateBounds(); return(result); }