void GenerateMesh() { mesh.Clear(); // Vertices List <Vector3> verts = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); for (int ring = 0; ring < edgeRingCount; ring++) { float t = ring / (edgeRingCount - 1f); OrientedPoint op = GetBezierOP(t); for (int i = 0; i < shape2D.VertexCount; i++) { verts.Add(op.LocalToWorldPos(shape2D.vertices[i].point)); normals.Add(op.LocalToWorldVec(shape2D.vertices[i].normal)); } } // Triangles List <int> triIndices = new List <int>(); for (int ring = 0; ring < edgeRingCount - 1; ring++) { int rootIndex = ring * shape2D.VertexCount; int rootIndexNext = (ring + 1) * shape2D.VertexCount; for (int line = 0; line < shape2D.LineCount; line += 2) { int lineIndexA = shape2D.lineIndices[line]; int lineIndexB = shape2D.lineIndices[line + 1]; int currentA = rootIndex + lineIndexA; int currentB = rootIndex + lineIndexB; int nextA = rootIndexNext + lineIndexA; int nextB = rootIndexNext + lineIndexB; triIndices.Add(currentA); triIndices.Add(nextA); triIndices.Add(nextB); triIndices.Add(currentA); triIndices.Add(nextB); triIndices.Add(currentB); } } mesh.SetVertices(verts); mesh.SetNormals(normals); mesh.SetTriangles(triIndices, 0); }
/// <summary> /// Smesh goes brrr /// </summary> /// <param name="uv"></param> private void GenerateMesh(Vector2 uv) { // On vide nos listes Mesh.Clear(); vertices.Clear(); triangles.Clear(); normals.Clear(); uv0.Clear(); uv1.Clear(); CubicBezierCurve bezier = GetBezierRepresentation(Space.Self); float curveArcLegth = bezier.GetArcLength(); float tiling = GetTextureAspectRatio(); int edgeCount = Mathf.Max(2, Mathf.RoundToInt(curveArcLegth * RoadChain.trianglesPerSegment)); // 2 triangles minimum sinon kaboom // Generation vertices normales & map texture for (int i = 0; i < edgeCount; ++i) { float t = i / (edgeCount - 1f); OrientedPoint op = bezier.GetOrientedPoint(t); float uv0V = t * tiling; float uv1U = Mathf.Lerp(uv.x, uv.y, t); for (int j = 0; j < RoadChain.mesh2D.VertexCount; ++j) { vertices.Add(op.LocalToWorldPos(RoadChain.mesh2D.vertices[j].point)); normals.Add(op.LocalToWorldVec(RoadChain.mesh2D.vertices[j].normal)); uv0.Add(new Vector2(RoadChain.mesh2D.vertices[j].u, uv0V)); uv1.Add(new Vector2(uv1U, 0)); } } // Generation des triangles for (int i = 0; i < edgeCount - 1; ++i) { int ifv = i * RoadChain.mesh2D.VertexCount; int ipofv = (i + 1) * RoadChain.mesh2D.VertexCount; for (int j = 0; j < RoadChain.mesh2D.LineCount; j += 2) { int t = RoadChain.mesh2D.triangles[j]; int tpo = RoadChain.mesh2D.triangles[j + 1]; triangles.Add(ifv + t); triangles.Add(ipofv + t); triangles.Add(ipofv + tpo); triangles.Add(ifv + t); triangles.Add(ipofv + tpo); triangles.Add(ifv + tpo); } } Mesh.SetVertices(vertices); Mesh.SetTriangles(triangles, 0); Mesh.SetNormals(normals); Mesh.SetUVs(0, uv0); Mesh.SetUVs(1, uv1); Mesh.RecalculateBounds(); MeshCollider collider = GetComponent <MeshCollider>(); collider.sharedMesh = Mesh; }
//Generate the mesh of the road void GenerateMesh(Vector3[] pts) { //Clear the mesh for good measure mesh.Clear(); //Verts, normals and graph nodes verts = new List <Vector3>(); normals = new List <Vector3>(); graphhNodesPos = new List <Vector3>(); //Loop through every segment ring for (int ring = 0; ring < edgeRingCount; ring++) { float t = ring / (edgeRingCount - 1f); //Get a point given the points and ring position in segment OrientedPoint op = GetPoint(pts, t); RaycastHit hit; // Does the ray intersect any objects excluding the player layer if (Physics.Raycast(op.position + Vector3.up * 100f, Vector3.down, out hit, Mathf.Infinity, layerMask)) { op.position = hit.point; } //Add graphnodes at segment position graphhNodesPos.Add(op.position); //Looping through each vertex in shape2D, adding vertecies and normals on the position that the ring is for (int i = 0; i < shape2D.VertexCount(); i++) { verts.Add(op.LocalToWorldPos(shape2D.vertices[i].point)); normals.Add(op.LocalToWorldDir(shape2D.vertices[i].normal)); } } triangleIndices = new List <int>(); //Loops through each segment connecting the vertexes for (int ring = 0; ring < edgeRingCount - 1; ring++) { int rootIndex = ring * shape2D.VertexCount(); int rootIndexNext = (ring + 1) * shape2D.VertexCount(); for (int line = 0; line < shape2D.LineCount(); line += 2) { int lineIndexA = shape2D.lineIndices[line]; int lineIndexB = shape2D.lineIndices[line + 1]; int currentA = rootIndex + lineIndexA; int currentB = rootIndex + lineIndexB; int nextA = rootIndexNext + lineIndexA; int nextB = rootIndexNext + lineIndexB; triangleIndices.Add(currentA); triangleIndices.Add(nextA); triangleIndices.Add(nextB); triangleIndices.Add(currentA); triangleIndices.Add(nextB); triangleIndices.Add(currentB); } } mesh.SetVertices(verts); mesh.SetNormals(normals); mesh.SetTriangles(triangleIndices, 0); }