void editSFMesh() { meshVertices.Clear(); meshNormals.Clear(); //Vertices for (int i = 0; i < xPoints; i++) { double theta = ((double)i / xPoints) * Math.PI; for (int k = 0; k < yPoints; k++) { double phi = ((double)k / (yPoints - 1)) * Math.PI * 2.0; SuperFormulaMeshPoint newPos = SuperFormula3D(m1, n11, n21, n31, a1, b1, theta, m2, n12, n22, n32, a2, b2, phi); lineRenderer.SetPosition(k + i * yPoints, newPos.vertex); meshVertices.Add(newPos.vertex); meshNormals.Add(newPos.normal); } } //last vertex double theta2 = Math.PI; double phi2 = Math.PI * 2.0; SuperFormulaMeshPoint newPos2 = SuperFormula3D(m1, n11, n21, n31, a1, b1, theta2, m2, n12, n22, n32, a2, b2, phi2); meshVertices.Add(newPos2.vertex); meshNormals.Add(newPos2.normal); sphereMesh.SetVertices(meshVertices); //Calculate Smooth Normals //Inigo Quilez method without division //(with normalization at the end of the accumulation phase). //This approach creates smooth normals without taking into accounts //smoothing groups and hard edges. //This seems to works in CPU while it creates bad artifacts when //ported on to compute shader -> Find out why! for (int i = 0; i < meshNormals.Count; i++) { meshNormals[i] = Vector3.zero; //resetting normals } sphereMesh.SetNormals(meshNormals); for (int i = 0; i < meshTriangles.Count; i += 3) { int ia = meshTriangles[i]; int ib = meshTriangles[i + 1]; int ic = meshTriangles[i + 2]; Vector3 e1 = meshVertices[ib] - meshVertices[ia]; Vector3 e2 = meshVertices[ic] - meshVertices[ia]; Vector3 norm = Vector3.Cross(e1, e2); meshNormals[ia] += norm; //accumulate face normal in the vertices meshNormals[ib] += norm; //that are part of the face meshNormals[ic] += norm; } for (int i = 0; i < meshNormals.Count; i++) { meshNormals[i].Normalize(); //finally normalize the accumulated normal } sphereMesh.SetNormals(meshNormals); //setting normals //sphereMesh.RecalculateNormals();//unity normal recalculation method }
void createSuperFormulaMesh() { int numVerticesCirc = nPoints; List <Vector3> meshVertices = new List <Vector3>(); List <Vector3> meshNormals = new List <Vector3>(); List <Vector2> meshUV = new List <Vector2>(); List <int> meshTriangles = new List <int>(); meshVertices.Add(new Vector3(0, 0, 0)); meshUV.Add(new Vector2(0.5f, 0.5f)); for (int i = 0; i < numVerticesCirc; i++) { double phi = ((double)i / nPoints) * Math.PI * 2.0; SuperFormulaMeshPoint newPos = SuperShape2D(m, n1, n2, n3, a, b, phi); lineRenderer.SetPosition(i, newPos.vertex); meshVertices.Add(newPos.vertex); meshUV.Add(newPos.uv); meshTriangles.Add(i + 1); meshTriangles.Add(0); meshTriangles.Add(i + 2 < numVerticesCirc + 1 ? i + 2 : 1); } circleMesh.SetVertices(meshVertices); circleMesh.SetTriangles(meshTriangles, 0); circleMesh.SetUVs(0, meshUV); }
void createSFMesh() { meshVertices.Clear(); meshNormals.Clear(); meshTriangles.Clear(); meshUV.Clear(); //Vertices, UV, Normals for (int i = 0; i < xPoints; i++) { double theta = ((double)i / xPoints) * Math.PI; for (int k = 0; k < yPoints; k++) { double phi = ((double)k / (yPoints - 1)) * Math.PI * 2.0; SuperFormulaMeshPoint newPos = SuperFormula3D(m1, n11, n21, n31, a1, b1, theta, m2, n12, n22, n32, a2, b2, phi); lineRenderer.SetPosition(k + i * yPoints, newPos.vertex); meshVertices.Add(newPos.vertex); meshNormals.Add(newPos.normal); meshUV.Add(new Vector2((float)k / yPoints, 1f - (float)i / xPoints)); } } //last point double theta2 = Math.PI; double phi2 = Math.PI * 2.0; SuperFormulaMeshPoint newPos2 = SuperFormula3D(m1, n11, n21, n31, a1, b1, theta2, m2, n12, n22, n32, a2, b2, phi2); meshVertices.Add(newPos2.vertex); meshNormals.Add(newPos2.normal); meshUV.Add(Vector2.zero); //assign buffers sphereMesh.SetVertices(meshVertices); sphereMesh.SetUVs(0, meshUV); sphereMesh.SetNormals(meshNormals); //Triangles for (int i = 0; i < xPoints - 1; i++) { for (int k = 0; k < yPoints - 1; k++) { int current = k + i * (yPoints); int next = current + yPoints; meshTriangles.Add(current); meshTriangles.Add(current + 1); meshTriangles.Add(next + 1); meshTriangles.Add(current); meshTriangles.Add(next + 1); meshTriangles.Add(next); } } //Bottom Cap for (int k = 0; k < yPoints; k++) { meshTriangles.Add(meshVertices.Count - 1); meshTriangles.Add(meshVertices.Count - (k + 2) - 1); meshTriangles.Add(meshVertices.Count - (k + 1) - 1); } sphereMesh.SetTriangles(meshTriangles, 0); //recalc normals //sphereMesh.RecalculateNormals(); }