private void RoadQuadMaker(MeshGenerator meshGenerator, Vector3 pointPrevious, Vector3 pointCurrent, Vector3 pointNext, Vector3 offset, Vector3 targetOffset, int submesh) { Vector3 forward = (pointNext - pointCurrent).normalized; Vector3 forwardPrevious = (pointCurrent - pointPrevious).normalized; Quaternion perpendicularAngle = Quaternion.LookRotation(Vector3.Cross(forward, Vector3.up)); Quaternion perpendicularPrevious = Quaternion.LookRotation(Vector3.Cross(forwardPrevious, Vector3.up)); Vector3 topLeft = pointCurrent + (perpendicularPrevious * offset); Vector3 topRight = pointCurrent + (perpendicularPrevious * (offset + targetOffset)); Vector3 bottomLeft = pointNext + (perpendicularAngle * offset); Vector3 bottomRight = pointNext + (perpendicularAngle * (offset + targetOffset)); meshGenerator.BuildTriangle(topLeft, topRight, bottomLeft, submesh); meshGenerator.BuildTriangle(topRight, bottomRight, bottomLeft, submesh); perpendicularAngle = Quaternion.LookRotation(Vector3.Cross(-forward, Vector3.up)); perpendicularPrevious = Quaternion.LookRotation(Vector3.Cross(-forwardPrevious, Vector3.up)); topLeft = pointCurrent + (perpendicularPrevious * offset); topRight = pointCurrent + (perpendicularPrevious * (offset + targetOffset)); bottomLeft = pointNext + (perpendicularAngle * offset); bottomRight = pointNext + (perpendicularAngle * (offset + targetOffset)); meshGenerator.BuildTriangle(bottomLeft, bottomRight, topLeft, submesh); meshGenerator.BuildTriangle(bottomRight, topRight, topLeft, submesh); }
//A method to create the triangle void GenerateTriangle() { //The 3 points required to do the triangle by setting the x, y and z of the size of the triangle accordingly Vector3 pt0 = new Vector3(size.x, size.y, -size.z); Vector3 pt1 = new Vector3(-size.x, size.y, -size.z); Vector3 pt2 = new Vector3(-size.x, size.y, size.z); //Make the actual triangle by passing the points created before and assign the index to 0 to fetch the first material meshGenerator.BuildTriangle(pt0, pt1, pt2, 0); //Specify the MeshFilter generated by the MeshGenerator meshFilter.mesh = meshGenerator.MeshCreator(); //Calling the method to be able to populate the list IncludeMaterials(); //Converting the list of materials to an array and assign it to the materials in the MehshRenderer meshRenderer.materials = listOfMaterials.ToArray(); }
private void CreateCube(Vector3 center) { Vector3 t0 = new Vector3(center.x + halfSize.x, center.y + halfSize.y, center.z + halfSize.z); Vector3 t1 = new Vector3(center.x + halfSize.x, center.y + halfSize.y, center.z - halfSize.z); Vector3 t2 = new Vector3(center.x - halfSize.x, center.y + halfSize.y, center.z - halfSize.z); Vector3 t3 = new Vector3(center.x - halfSize.x, center.y + halfSize.y, center.z + halfSize.z); Vector3 b0 = new Vector3(center.x + halfSize.x, center.y - halfSize.y, center.z + halfSize.z); Vector3 b1 = new Vector3(center.x - halfSize.x, center.y - halfSize.y, center.z + halfSize.z); Vector3 b2 = new Vector3(center.x - halfSize.x, center.y - halfSize.y, center.z - halfSize.z); Vector3 b3 = new Vector3(center.x + halfSize.x, center.y - halfSize.y, center.z - halfSize.z); //top square mg.BuildTriangle(t0, t1, t2); mg.BuildTriangle(t0, t2, t3); //bottom square mg.BuildTriangle(b0, b1, b2); mg.BuildTriangle(b0, b2, b3); //front square mg.BuildTriangle(t1, b3, b2); mg.BuildTriangle(t1, b2, t2); //back square mg.BuildTriangle(t0, t3, b1); mg.BuildTriangle(t0, b1, b0); //left square mg.BuildTriangle(t3, t2, b2); mg.BuildTriangle(t3, b2, b1); //right square mg.BuildTriangle(t1, t0, b0); mg.BuildTriangle(t1, b0, b3); }
//A method to create the cube void GenerateCube() { //Top vertices Vector3 ttopLeft = new Vector3(-size.x, size.y, size.z); Vector3 ttopRight = new Vector3(size.x, size.y, size.z); Vector3 tbottomLeft = new Vector3(-size.x, size.y, -size.z); Vector3 tbottomRight = new Vector3(size.x, size.y, -size.z); //Bottom vertices Vector3 btopLeft = new Vector3(-size.x, -size.y, size.z); Vector3 btopRight = new Vector3(size.x, -size.y, size.z); Vector3 bbottomLeft = new Vector3(-size.x, -size.y, -size.z); Vector3 bbottomRight = new Vector3(size.x, -size.y, -size.z); //Top Square meshGenerator.BuildTriangle(ttopLeft, ttopRight, tbottomRight, 0); meshGenerator.BuildTriangle(ttopLeft, tbottomRight, tbottomLeft, 0); //Bottom Square meshGenerator.BuildTriangle(btopRight, btopLeft, bbottomLeft, 0); meshGenerator.BuildTriangle(btopRight, bbottomLeft, bbottomRight, 0); //Left-side Square meshGenerator.BuildTriangle(ttopLeft, tbottomLeft, bbottomLeft, 0); meshGenerator.BuildTriangle(ttopLeft, bbottomLeft, btopLeft, 0); //Right-side Square meshGenerator.BuildTriangle(tbottomRight, ttopRight, btopRight, 0); meshGenerator.BuildTriangle(tbottomRight, btopRight, bbottomRight, 0); //Front Square meshGenerator.BuildTriangle(tbottomLeft, tbottomRight, bbottomRight, 0); meshGenerator.BuildTriangle(tbottomLeft, bbottomRight, bbottomLeft, 0); //Back Square meshGenerator.BuildTriangle(ttopRight, ttopLeft, btopLeft, 0); meshGenerator.BuildTriangle(ttopRight, btopLeft, btopRight, 0); //Specify the MeshFilter generated by the MeshGenerator meshFilter.mesh = meshGenerator.MeshCreator(); //Calling the method to be able to populate the list IncludeMaterials(); //Converting the list of materials to an array and assign it to the materials in the MehshRenderer meshRenderer.materials = listOfMaterials.ToArray(); }
private void CreateSphere(int resolution) { float step = 1.0f / resolution; //Vector3 step3 = new Vector3(step, step, step); for (int face = 0; face < 6; ++face) { Vector3 origin = baseOrigin[face]; Vector3 right = constRight[face]; Vector3 up = constUp[face]; for (int j = 0; j < resolution + 1; ++j) { for (int i = 0; i < resolution + 1; ++i) { Vector3 p = origin + step * (i * right + j * up); //float noiseValue = PerlinNoise.Noise(MappingFunction(0, 100, (int)p.x * 5), MappingFunction(0, 100, (int)p.z * 5), 0); //Debug.Log(noiseValue); Vector3 p2 = new Vector3(p.x * p.x, p.y * p.y, p.z * p.z); Vector3 n = new Vector3(p.x * Mathf.Sqrt(1.0f - 0.5f * (p2.y + p2.z) + p2.y * p2.z / 3.0f), p.y * Mathf.Sqrt(1.0f - 0.5f * (p2.x + p2.z) + p2.x * p2.z / 3.0f), p.z * Mathf.Sqrt(1.0f - 0.5f * (p2.y + p2.x) + p2.x * p2.y / 3.0f)) * 5f; //float noiseValue = PerlinNoise.Noise(Random.Range(0,100), Random.Range(0, 100), 0); //Debug.Log(noiseValue); nvertices.Add(n); } } } //for (int i = 0; i < nvertices.Count; ++i) //{ // float noiseValue = PerlinNoise.Noise(MappingFunction(0, 100, i), Random.Range(0, 100), 0); // nvertices[i] += new Vector3(0, 0.9f * noiseValue, 0); //} int k = resolution + 1; Debug.Log(nvertices[0].y); for (int face = 0; face < 6; ++face) { for (int j = 0; j < resolution; ++j) { bool bottom = j < (resolution / 2); for (int i = 0; i < resolution; ++i) { bool left = i < (resolution / 2); int a = (face * k + j) * k + i; int b = (face * k + j) * k + i + 1; int c = (face * k + j + 1) * k + i; int d = (face * k + j + 1) * k + i + 1; mg.BuildTriangle(nvertices[b], nvertices[c], nvertices[a]); mg.BuildTriangle(nvertices[b], nvertices[d], nvertices[c]); } // } } } // Vector3 a = new Vector3(6f, 6f, 6f); //Vector3 b = new Vector3(6f, 7f, 7f); //Vector3 c = new Vector3(7f, 6f, 6f); //Vector3 d = new Vector3(7f, 7f, 8f); //mg.BuildTriangle(b, c, a); //mg.BuildTriangle(b, d, c); meshFilter.mesh = mg.CreateMesh(); }