//To Generate the mesh protected override void GenerateMesh() { base.GenerateMesh(); //Instantiate a new meshGenerator with the amount of the given submesh meshGenerator = new MeshGenerator(submeshCount); Vector3 t0 = new Vector3(0, size.y * 2, 0); //rotation of 0f so angle 0, Vector3 b0 = Quaternion.AngleAxis(0f, Vector3.up) * new Vector3(-size.x, 0, size.z); Vector3 b1 = Quaternion.AngleAxis(0f, Vector3.up) * new Vector3(size.x, 0, size.z); Vector3 b2 = Quaternion.AngleAxis(180f, Vector3.up) * new Vector3(-size.x, 0, size.z); Vector3 b3 = Quaternion.AngleAxis(180f, Vector3.up) * new Vector3(size.x, 0, size.z); //Creating BOTTOM face meshGenerator.AddTriangleToMesh(b0, b3, b2, 0); meshGenerator.AddTriangleToMesh(b0, b2, b1, 0); //Creating FRONT face meshGenerator.AddTriangleToMesh(b3, t0, b2, 1); //Creating BACK face meshGenerator.AddTriangleToMesh(b1, t0, b0, 2); //Creating LEFT face meshGenerator.AddTriangleToMesh(b0, t0, b3, 3); //Creating RIGHT face meshGenerator.AddTriangleToMesh(b2, t0, b1, 4); //Create the final mesh with the given triangles and set it to the meshFilter meshFilter.mesh = meshGenerator.GenerateMesh(); }
void GenerateSegmentPart(Vector3 pPrev, Vector3 pCurr, Vector3 pNext, Vector3 startingPartPos, Vector3 partWidth, int submesh) { //The direction the segment should face to make sense in the road Vector3 roadDirection = (pNext - pCurr).normalized; //The previous segment's direction Vector3 prevRoadDirection = (pCurr - pPrev).normalized; int[] directionsArray = new int[2] { 1, -1 }; foreach (int directionScale in directionsArray) { //How much the segment should be rotated to face the road direction Quaternion segmentAngle = Quaternion.LookRotation(Vector3.Cross(directionScale * roadDirection, Vector3.up)); //The previous segment's rotation Quaternion prevSegmentAngle = Quaternion.LookRotation(Vector3.Cross(directionScale * prevRoadDirection, Vector3.up)); //Defining the quad making up the segment part Vector3 v0 = pCurr + (prevSegmentAngle * startingPartPos); Vector3 v1 = pCurr + (prevSegmentAngle * (startingPartPos + partWidth)); Vector3 v2 = pNext + (segmentAngle * (startingPartPos + partWidth)); Vector3 v3 = pNext + (segmentAngle * startingPartPos); //Creating the triangles making up the quad if (directionScale == 1) { meshGenerator.AddTriangleToMesh(v0, v1, v2, submesh); meshGenerator.AddTriangleToMesh(v0, v2, v3, submesh); } else if (directionScale == -1) { meshGenerator.AddTriangleToMesh(v0, v2, v1, submesh); meshGenerator.AddTriangleToMesh(v0, v3, v2, submesh); } } }
//To Generate the mesh protected override void GenerateMesh() { base.GenerateMesh(); //Instantiate a new meshGenerator with the amount of the given submesh meshGenerator = new MeshGenerator(submeshCount); //To Store the Plane's vertices Vector3[,] planeVertices = new Vector3[planeWidth, planeHeight]; //Creating the Plane's vertices and storing them for (int x = 0; x < planeWidth; x++) { for (int z = 0; z < planeHeight; z++) { planeVertices[x, z] = new Vector3(size.x * x, 0, size.z * z); } } //Adding triangles depending on the vertices created for (int x = 0; x < planeWidth - 1; x++) { for (int y = 0; y < planeHeight - 1; y++) { Vector3 v0 = planeVertices[x, y]; Vector3 v1 = planeVertices[x + 1, y]; Vector3 v2 = planeVertices[x, y + 1]; Vector3 v3 = planeVertices[x + 1, y + 1]; meshGenerator.AddTriangleToMesh(v0, v2, v3, 0); meshGenerator.AddTriangleToMesh(v0, v3, v1, 0); } } //Create the final mesh with the given triangles and set it to the meshFilter meshFilter.mesh = meshGenerator.GenerateMesh(); }
//To Generate the mesh protected override void GenerateMesh() { base.GenerateMesh(); //Instantiate a new meshGenerator with 1 submesh meshGenerator = new MeshGenerator(1); //Setting the vertices of triangle Vector3 v0 = new Vector3(-1 * size.x, 0 * size.y, -1 * size.z); Vector3 v1 = new Vector3(-1 * size.x, 0 * size.y, 1 * size.z); Vector3 v2 = new Vector3(1 * size.x, 0 * size.y, -1 * size.z); //Adding a triangle with its submesh to the meshGenerator meshGenerator.AddTriangleToMesh(v0, v1, v2, 0); //Create the final mesh with the given triangles and set it to the meshFilter meshFilter.mesh = meshGenerator.GenerateMesh(); }
//To Generate the mesh protected override void GenerateMesh() { base.GenerateMesh(); //Instantiate a new meshGenerator with the amount of the given submesh meshGenerator = new MeshGenerator(submeshCount); //Bottom vertices Vector3 b0 = new Vector3(-1 * size.x, -1 * size.y, -1 * size.z); //bottom left vertex Vector3 b1 = new Vector3(-1 * size.x, -1 * size.y, 1 * size.z); //top left vertex Vector3 b2 = new Vector3(1 * size.x, -1 * size.y, 1 * size.z); //top right vertex Vector3 b3 = new Vector3(1 * size.x, -1 * size.y, -1 * size.z); //bottom right vertex //Top vertices Vector3 t0 = new Vector3(-1 * size.x, 1 * size.y, -1 * size.z); //bottom left vertex Vector3 t1 = new Vector3(-1 * size.x, 1 * size.y, 1 * size.z); //top left vertex Vector3 t2 = new Vector3(1 * size.x, 1 * size.y, 1 * size.z); //top right vertex Vector3 t3 = new Vector3(1 * size.x, 1 * size.y, -1 * size.z); //bottom right vertex //Creating BOTTOM face by adding triangles to the mesh meshGenerator.AddTriangleToMesh(b0, b3, b2, 0); meshGenerator.AddTriangleToMesh(b0, b2, b1, 0); //Creating FRONT face by adding triangles to the mesh meshGenerator.AddTriangleToMesh(t0, t3, b3, 1); meshGenerator.AddTriangleToMesh(t0, b3, b0, 1); //Creating TOP face by adding triangles to the mesh meshGenerator.AddTriangleToMesh(t0, t1, t2, 2); meshGenerator.AddTriangleToMesh(t0, t2, t3, 2); //Creating BACK face by adding triangles to the mesh meshGenerator.AddTriangleToMesh(t1, b2, t2, 3); meshGenerator.AddTriangleToMesh(t1, b1, b2, 3); //Creating LEFT face by adding triangles to the mesh meshGenerator.AddTriangleToMesh(t0, b1, t1, 4); meshGenerator.AddTriangleToMesh(t0, b0, b1, 4); //Creating RIGHT face by adding triangles to the mesh meshGenerator.AddTriangleToMesh(t3, t2, b2, 5); meshGenerator.AddTriangleToMesh(t3, b2, b3, 5); //Create the final mesh with the given triangles and set it to the meshFilter meshFilter.mesh = meshGenerator.GenerateMesh(); }