// Use this for initialization void Start() { // Getting the mesh of THIS object (the plane). this.sourceMesh = GetComponent <MeshFilter>().mesh; this.precomputedEdges = MeshExtrusion.BuildManifoldEdges(this.sourceMesh); // Defining transformations to extrude the mesh. Matrix4x4[] sections = new Matrix4x4[2]; // Starting point (where the mesh already is, but its needed so the new mesh does not generate with a hole). sections[0] = this.transform.localToWorldMatrix; // The end point. sections[1] = this.transform.worldToLocalMatrix * Matrix4x4.TRS(new Vector3(0, 1, 1), Quaternion.Euler(0, 45, 0), new Vector3(0.75f, 2, 1)); // Get the mesh again (the script need a different reference to the same mesh for some reason?). Mesh mesh = GetComponent <MeshFilter>().mesh; // Actually perform the extrusion, the parameter [invertFaces] is extremely important in this case but its kind of hard to programatically tell when it is needed. // I would guess you would need to somehow see the direction of the normals before even creating the face. MeshExtrusion.ExtrudeMesh(this.sourceMesh, mesh, sections, true); // Preparing CSG objects for substraction. // Again, passing a new refference to the same mesh is important for some reason. CSG plane = CSG.fromMesh(GetComponent <MeshFilter>().mesh, this.transform); CSG cube = CSG.fromMesh(this.cubeCutter.GetComponent <MeshFilter>().mesh, this.cubeCutter.transform); // Save the operation, this does not affect neither meshes yet. CSG result = plane.subtract(cube); // Replace the mesh of THIS object with the mesh representation of the operation. this.GetComponent <MeshFilter>().mesh = result.toMesh(); // Hide the cube to visualize the result. this.cubeCutter.SetActive(false); }
void UpdateMesh() { // get points Vector3[] vertices = new Vector3[transform.childCount]; for (int i = 0; i < transform.childCount; i++) { vertices[i] = transform.GetChild(i).localPosition; } // triangulate Vector3[] transformedVertices = new Vector3[vertices.Length]; for (int i = 0; i < vertices.Length; i++) { transformedVertices[i] = triangulationPlane ? triangulationPlane.InverseTransformPoint(vertices[i]): vertices[i]; } int[] triangles = DelaunayTriangulator.Triangulator.ConvertToVertexIndexList(DelaunayTriangulator.Triangulator.Triangulate(transformedVertices)); // generate colors Color[] colors = new Color[transform.childCount]; for (int i = 0; i < colors.Length; i++) { colors[i] = color; } MeshExtrusion.Edge[] edges = MeshExtrusion.BuildManifoldEdges(vertices.Length, mesh.triangles); // get edges to color them differently for (int i = 0; i < edges.Length; i++) { int vertId0 = edges[i].vertexIndex[0]; // one of the two points of the edge colors[vertId0] = edgeColor; } // update mesh mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.colors = colors; mesh.RecalculateBounds(); }
private GameObject getZObject() { ExtractFaces extractZFace = new ExtractFaces(this.form, ExtractFaces.FACE.Z); int[] verticesInCenter; Mesh planeMesh = extractZFace.getFace(out verticesInCenter); GameObject go = new GameObject("FormForZPlane"); MeshFilter meshFilter = go.AddComponent <MeshFilter>(); MeshCollider meshCollider = go.AddComponent <MeshCollider>(); go.AddComponent <MeshRenderer>(); meshFilter.sharedMesh = planeMesh; meshCollider.sharedMesh = planeMesh; MeshExtrusion.Edge[] falseEdges = MeshExtrusion.BuildManifoldEdges(planeMesh); MeshExtrusion.Edge[] realEdges = VertexClassifier.getRealEdges(falseEdges, verticesInCenter); // Defining transformations to extrude the mesh. Matrix4x4[] sections = new Matrix4x4[2]; // Starting point (where the mesh already is, but its needed so the new mesh does not generate with a hole). sections[0] = go.transform.localToWorldMatrix; // The end point. sections[1] = go.transform.worldToLocalMatrix * Matrix4x4.TRS(new Vector3(0, 0, 1), Quaternion.Euler(0, 0, 0), new Vector3(1, 1, 1)); // Get the mesh again (the script need a different reference to the same mesh for some reason?). Mesh deepMesh = new Mesh(); // Actually perform the extrusion, the parameter [invertFaces] is extremely important in this case but its kind of hard to programatically tell when it is needed. // I would guess you would need to somehow see the direction of the normals before even creating the face. MeshExtrusion.ExtrudeMesh(planeMesh, deepMesh, sections, realEdges, true); asignNewMesh(deepMesh, go); return(go); }
void Start() { srcMesh = GetComponent <MeshFilter>().sharedMesh; precomputedEdges = MeshExtrusion.BuildManifoldEdges(srcMesh); }
// Use this for initialization void Start() { int nvertex = 4; float extrude_length = 5.0f; Vector2[] pos = new Vector2[nvertex]; pos[0] = new Vector2(-1, -1); pos[1] = new Vector2(1, -1); pos[2] = new Vector2(1, 1); pos[3] = new Vector2(-1, 1); // Use triangulator to get indices for creating triangles Triangulator tr = new Triangulator(pos); int[] indices = tr.Triangulate(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[pos.Length]; for (int j = 0; j < vertices.Length; j++) { vertices[j] = new Vector3(pos[j].x, 0, pos[j].y); } // Create the mesh Mesh msh = new Mesh(); msh.vertices = vertices; msh.triangles = indices; msh.RecalculateNormals(); msh.RecalculateBounds(); float[] length = new float[vertices.Length]; float net_length = 0; for (int j = 0; j < vertices.Length; j++) { if (j < vertices.Length - 1) { length[j] = (vertices[j] - vertices[j + 1]).magnitude; net_length += length[j]; } else { length[j] = (vertices[j] - vertices[0]).magnitude; net_length += length[j]; } } Vector3[] tvertices = msh.vertices; Vector2[] uvs = new Vector2[tvertices.Length]; float cummulated_length = 0; for (int j = 0; j < uvs.Length; j++) { //uvs[j] = new Vector2 (vertices[j].x, vertices[j].z); uvs[j] = new Vector2(cummulated_length / net_length, 0); cummulated_length += length[j]; } msh.uv = uvs; Matrix4x4[] finalSections = new Matrix4x4[2]; finalSections[0] = Matrix4x4.identity; finalSections[1] = Matrix4x4.TRS(new Vector3(0, extrude_length, 0), Quaternion.identity, Vector3.one); MeshExtrusion.Edge[] precomputedEdges = MeshExtrusion.BuildManifoldEdges(msh); MeshExtrusion.ExtrudeMesh(msh, msh, finalSections, precomputedEdges, true); // Set up game object with mesh; GameObject extruded_object = new GameObject(); MeshFilter filter = extruded_object.AddComponent <MeshFilter>(); filter.mesh = msh; MeshRenderer mr = extruded_object.AddComponent <MeshRenderer>(); mr.material = mtl; MeshCollider mc = extruded_object.AddComponent <MeshCollider>(); //mc.sharedMesh = null; mc.sharedMesh = msh; }
public void SimpleExtrude(float extrude_length) { selectedPlane = planeProperties.SearchSelectedPlane(); sketchToExtude = selectedPlane.transform.GetChild(1).GetChild(selectedPlane.transform.GetChild(1).childCount - 1).gameObject; sketchToExtude.transform.GetChild(0).gameObject.GetComponent <Canvas>().enabled = false; sketchLineRenderer = sketchToExtude.GetComponent <LineRenderer>(); Vector2[] pos = new Vector2[sketchLineRenderer.positionCount]; for (int j = 0; j < pos.Length; j++) { pos[j] = new Vector2(selectedPlane.transform.InverseTransformPoint(sketchLineRenderer.GetPosition(j)).x, selectedPlane.transform.InverseTransformPoint(sketchLineRenderer.GetPosition(j)).z); //Debug.Log(pos[j]); } // Use triangulator to get indices for creating triangles Triangulator tr = new Triangulator(pos); int[] indices = tr.Triangulate(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[pos.Length]; for (int j = 0; j < vertices.Length; j++) { vertices[j] = new Vector3(pos[j].x, pos[j].y, 0); //XY Plane // vertices[j] = new Vector3(pos[j].x, 0, pos[j].y); //XZ Plane // vertices[j] = new Vector3(0, pos[j].x, pos[j].y); //YZ Plane } // Create the mesh Mesh msh = new Mesh(); msh.vertices = vertices; msh.triangles = indices; msh.RecalculateNormals(); msh.RecalculateBounds(); float[] length = new float[vertices.Length]; float net_length = 0; for (int j = 0; j < vertices.Length; j++) { if (j < vertices.Length - 1) { length[j] = (vertices[j] - vertices[j + 1]).magnitude; net_length += length[j]; } else { length[j] = (vertices[j] - vertices[0]).magnitude; net_length += length[j]; } } Vector3[] tvertices = msh.vertices; Vector2[] uvs = new Vector2[tvertices.Length]; float cummulated_length = 0; for (int j = 0; j < uvs.Length; j++) { //uvs[j] = new Vector2 (vertices[j].x, vertices[j].z); uvs[j] = new Vector2(cummulated_length / net_length, 0); cummulated_length += length[j]; } msh.uv = uvs; Matrix4x4[] finalSections = new Matrix4x4[2]; if (extrude_length >= 0) { finalSections[0] = Matrix4x4.TRS(selectedPlane.transform.position, Quaternion.LookRotation(-selectedPlane.transform.up, selectedPlane.transform.forward), Vector3.one); } else { finalSections[0] = Matrix4x4.TRS(selectedPlane.transform.position + selectedPlane.transform.up * extrude_length, Quaternion.LookRotation(-selectedPlane.transform.up, selectedPlane.transform.forward), Vector3.one); } Debug.Log(Quaternion.LookRotation(selectedPlane.transform.up, selectedPlane.transform.forward)); // finalSections[1] = Matrix4x4.TRS(new Vector3(extrude_length,0,0), Quaternion.identity, Vector3.one); // finalSections[1] = Matrix4x4.TRS(new Vector3(0, extrude_length, 0), Quaternion.identity, Vector3.one); if (extrude_length >= 0) { finalSections[1] = Matrix4x4.TRS(selectedPlane.transform.position + selectedPlane.transform.up * extrude_length, Quaternion.LookRotation(-selectedPlane.transform.up, selectedPlane.transform.forward), Vector3.one); } else { finalSections[1] = Matrix4x4.TRS(selectedPlane.transform.position, Quaternion.LookRotation(-selectedPlane.transform.up, selectedPlane.transform.forward), Vector3.one); } MeshExtrusion.Edge[] precomputedEdges = MeshExtrusion.BuildManifoldEdges(msh); MeshExtrusion.ExtrudeMesh(msh, msh, finalSections, precomputedEdges, true); // Set up game object with mesh; extruded_object = new GameObject(); // extruded_object.name = "extrude1"; MeshFilter filter = extruded_object.AddComponent <MeshFilter>(); filter.mesh = msh; MeshRenderer mr = extruded_object.AddComponent <MeshRenderer>(); mr.material = mtl; mr.material.shader = Shader.Find("Custom/VertexColor"); MeshCollider mc = extruded_object.AddComponent <MeshCollider>(); //mc.sharedMesh = null; mc.sharedMesh = msh; extruded_object.transform.SetParent(sketchToExtude.transform); extruded_object.name = "Extrusion"; extruded_object.AddComponent <extrusionProperties>(); extruded_object.GetComponent <extrusionProperties>().extrude_length = extrude_length; }
void Update() { while (sections.Count < geomBuffer) { Restock(); collisionNeedsUpdate = true; } if (sections[1].point.x - player.transform.position.x < 1) { Matrix4x4 localSpaceTransform = transform.localToWorldMatrix; Vector3 upDir = sections[sections.Count - 1].upDir; newHeightData.Set(sections[sections.Count - 1].point.x, randomizer.Next(heightMin, heightMax), sections[sections.Count - 1].point.z); if (sections[sections.Count - 1].usesOverride) { newHeightData.Set(sections[sections.Count - 1].point.x, sections[sections.Count - 1].heightOverride, sections[sections.Count - 1].point.z); } else { newHeightData.Set(sections[sections.Count - 1].point.x, randomizer.Next(heightMin, heightMax), sections[sections.Count - 1].point.z); } vertices.Add(localSpaceTransform.MultiplyPoint(sections[sections.Count - 1].point)); vertices.Add(localSpaceTransform.MultiplyPoint(newHeightData + upDir * height)); float u = 0.0f; for (int i = 0; i < 2; i++) { if (i != 0) { u = Mathf.Clamp01((Time.time - sections[sections.Count - 1].time) / time); } uv.Add(new Vector2(u, 0)); uv.Add(new Vector2(u, 1)); } for (int i = 0; i < 4; i++) { if (i == 0 || i == 1) { vertices.RemoveAt(i); } uv.RemoveAt(i); } sections.RemoveAt(0); } triangles.Clear(); ResetTris(); mesh.Clear(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uv.ToArray(); mesh.RecalculateNormals(); edges = MeshExtrusion.BuildManifoldEdges(mesh); extrusion.Clear(); extrusion.Add(transform.worldToLocalMatrix * Matrix4x4.TRS(depthPoints[0].point, extrudeRotation, Vector3.one)); extrusion.Add(transform.worldToLocalMatrix * Matrix4x4.TRS(depthPoints[1].point, extrudeRotation, Vector3.one)); MeshExtrusion.ExtrudeMesh(mesh, out_mesh, extrusion.ToArray(), edges, invertFaces); if (collisionNeedsUpdate == true) { meshc.sharedMesh = null; meshc.sharedMesh = out_mesh; if (floorPhysMat != null) { meshc.sharedMaterial = null; meshc.sharedMaterial = floorPhysMat; } collisionNeedsUpdate = false; } }