void Start() { List <Vector3> vertices = new List <Vector3> { new Vector3(1, 0, 0), new Vector3(-1, 0, 0), new Vector3(0, 0, -1), new Vector3(0, 0, 1), new Vector3(0, 1, 0), new Vector3(0, -1, 0) }; List <int> index = new List <int> { 5, 2, 0, //Abajo Izquierda //3,2,1/*, //Medio 5, 1, 2, //Abajo Atrás 5, 3, 1, //Abajo Derecha 5, 0, 3, //Abajo Adelante 0, 2, 4, //Arriba Izquierda 2, 1, 4, //Arriba Atrás 1, 3, 4, //Arriba Derecha 3, 0, 4 //Arriba Adelante }; //De unity, no se puede modificar /* * Mesh mesh = new Mesh(); * triangleMesh.vertices = points; * triangleMesh.triangles = indices; */ //Un mesh de nosotros, que si se puede modificar myMesh = new LocalMesh(vertices, index); MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); //mr.sharedMaterial = new Material(Shader.Find("Standard")); MeshFilter mf = gameObject.AddComponent <MeshFilter>(); //mf.mesh = myMesh; mf.mesh.vertices = myMesh.points.ToArray(); mf.mesh.triangles = myMesh.faces.ToArray(); mf.mesh.RecalculateNormals(); //tessell(vertices); for (int i = 0; i <= 2; i++) { MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>(); myMesh = TessellateMesh(myMesh); meshFilter.mesh.vertices = myMesh.points.ToArray(); meshFilter.mesh.triangles = myMesh.faces.ToArray(); meshFilter.mesh.RecalculateNormals(); } }
// Update is called once per frame void Update() { if (Input.GetKeyDown("f")) { MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>(); myMesh = TessellateMesh(myMesh); meshFilter.mesh.vertices = myMesh.points.ToArray(); meshFilter.mesh.triangles = myMesh.faces.ToArray(); meshFilter.mesh.RecalculateNormals(); } }
void Start() { points = new List <Vector3> { new Vector3(0, 2, 0), //0 - C new Vector3(-2, 0, 0), //1 - D new Vector3(0, 0, 2), //2 - F new Vector3(2, 0, 0), //3 - B new Vector3(0, 0, -2), //4 - A new Vector3(0, -2, 0) //5 - E }; faces = new List <int> { 0, 2, 3, 0, 1, 2, 0, 3, 4, 0, 4, 1, 2, 1, 5, 3, 2, 5, //5, 2, 3, 4, 3, 5, //5, 3, 4, 1, 4, 5 //5, 4, 1 }; //Unity Mesh Quad myMesh = new LocalMesh(points, faces); MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = new Material(Shader.Find("Standard")); MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>(); meshFilter.mesh.vertices = myMesh.points.ToArray(); meshFilter.mesh.triangles = myMesh.faces.ToArray(); meshFilter.mesh.RecalculateNormals(); for (int i = 0; i < 4; i++) { meshFilter = gameObject.GetComponent <MeshFilter>(); myMesh = TessellateMesh(myMesh); meshFilter.mesh.vertices = myMesh.points.ToArray(); meshFilter.mesh.triangles = myMesh.faces.ToArray(); meshFilter.mesh.RecalculateNormals(); } Vector3[] vertices = meshFilter.mesh.vertices; Vector2[] uvs = new Vector2[vertices.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(vertices[i].x, vertices[i].z); } meshFilter.mesh.uv = uvs; meshRenderer.material = mineMaterial; }
void Start() { List <Vector3> vertices = new List <Vector3> { new Vector3(0.67832f, -6.11971f, -2.99274f), new Vector3(4.01266f, -6.11971f, -2.99274f), new Vector3(4.01266f, -2.78537f, -2.99274f), new Vector3(0.67832f, -2.78537f, -2.99274f), new Vector3(4.01266f, -6.11971f, -6.32708f), new Vector3(4.01266f, -2.78537f, -6.32708f), new Vector3(0.67832f, -6.11971f, -6.32708f), new Vector3(0.67832f, -2.78537f, -6.32708f) }; List <int> index = new List <int> { 0, 1, 2, 0, 2, 3, //FRONT 1, 4, 5, 1, 5, 2, //RIGHT 4, 6, 7, 4, 7, 5, //LEFT 6, 0, 3, 6, 3, 7, //BACK 3, 2, 5, 3, 5, 7, //TOP 1, 0, 6, 1, 6, 4 //BOTTOM }; myMesh = new LocalMesh(vertices, index); //triangleMesh.vertices = points; //triangleMesh.triangles = indices; MeshRenderer mr = gameObject.AddComponent <MeshRenderer>(); mr.sharedMaterial = new Material(Shader.Find("Standard")); MeshFilter mf = gameObject.AddComponent <MeshFilter>(); // mf.mesh = triangleMesh; mf.mesh.vertices = myMesh.points.ToArray(); mf.mesh.triangles = myMesh.faces.ToArray(); // mf.mesh.RecalculateNormals(); }
//private void updateTextureObjects(Mesh mesh3d) { // // iterating through every triangle // for (int i = 0; i < mesh3d.triangles.Length; i += 3) { // if (textureObjectsOnTriangles[i / 3] == null) { // continue; // } // // UV triangle from 3D model // Vector2 u1 = mesh3d.uv[mesh3d.triangles[i + 0]]; // Vector2 u2 = mesh3d.uv[mesh3d.triangles[i + 1]]; // Vector2 u3 = mesh3d.uv[mesh3d.triangles[i + 2]]; // foreach (DynamicObject obj in textureObjectsOnTriangles[i / 3]) { // obj.barycentric = new Barycentric(u1, u2, u3, obj.toVector2()); // } // } //} // recalculate positions of planar mesh verticles public void updateMesh(Mesh mesh3d) { //if (showingNormalization && normalizationStep >= normalizationStepMax) { // return; //} else if (showingNormalization) { Debug.Log("-------------- " + normalizationStep + " ----------------"); } else { Debug.Log("-------------------------------------"); } if (arranger.isDynamic()) { recalcTextureObjects(mesh3d); } List <Vector2> uvList = new List <Vector2>(); List <Vector3> vertList = new List <Vector3>(); int objectIndex = 0; // iterating through every triangle for (int i = 0; i < mesh3d.triangles.Length; i += 3) { // we should do operations only if there is at least one point on current triangle if (textureObjectsOnTriangles[i / 3] != null) { Neighbour neighbour = neighbours[i / 3]; // mesh of triangle with his neighbours LocalMesh localMesh = new LocalMesh( transform, mesh3d, neighbour, normalizationStrength, detectOverlappingOnAllTriangles, detectOverlappingOnAllEdges, useStrength, textureObjectsOnTriangles[i / 3]); if (showingNormalization || neighbour.usedTriangles == null || alwaysBuildBestMesh) { // calculations with selecting which triangles we should use // for optimization reasons normally choosed once localMesh.buildFirstUsedTriangles(); do { localMesh.makeEdges(); if (showingNormalization) { localMesh.normalizeFlatMesh(normalizationStep); } else { localMesh.normalizeFlatMesh(normalizationStepMax); } } while (!localMesh.checkForOutsiders()); neighbour.setUsedTriangles(localMesh.usedTriangles); } else { // we know which triangles we should use, but we need to normalize them again // 3D mesh is in motion localMesh.makeEdges(); localMesh.normalizeFlatMesh(normalizationStepMax); } // localMesh.printError(); // localMesh.objects.Reverse(); foreach (DynamicObject obj in localMesh.objects) { Vector3[] transformedVerticles = localMesh.getTransformedByObject(obj); Texture2D tex = createTextureForDebug(); foreach (int k in neighbour.usedTriangles) { Triangle3D triangle = new Triangle3D(transformedVerticles[localMesh.triangles[k + 0]], transformedVerticles[localMesh.triangles[k + 1]], transformedVerticles[localMesh.triangles[k + 2]]); uvList.Add(triangle.p1); uvList.Add(triangle.p2); uvList.Add(triangle.p3); vertList.Add(gridVertices[neighbour.index[k + 0]]); vertList.Add(gridVertices[neighbour.index[k + 1]]); vertList.Add(gridVertices[neighbour.index[k + 2]]); drawDebugTriangle(tex, triangle, k); } applyDebugTexture(tex, objectIndex); objectIndex++; } } } if (normalizationStep < normalizationStepMax) { normalizationStep++; } int[] triangles = new int[vertList.Count]; Vector3[] normals = new Vector3[vertList.Count]; Vector3[] vertices = vertList.ToArray(); // iterating through every point for (int i = 0; i < vertList.Count; i++) { // filling positions of points in triangle triangles[i] = i; // all normals will be fixed normals[i] = Vector3.forward; } mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; mesh.uv = uvList.ToArray(); }
LocalMesh TessellateMesh(LocalMesh inMesh) { List <Vector3> outPoints = new List <Vector3>(); List <int> outFaces = new List <int>(); for (int i = 0; i < inMesh.faces.Count; i += 3) { int inIndex0 = inMesh.faces[i + 0]; int inIndex1 = inMesh.faces[i + 1]; int inIndex2 = inMesh.faces[i + 2]; Vector3 v0 = inMesh.points[inIndex0]; //A Vector3 v1 = inMesh.points[inIndex1]; //B Vector3 v2 = inMesh.points[inIndex2]; //C Vector3 v3 = (0.5f * (v0 + v1)).normalized; //D Vector3 v4 = (0.5f * (v1 + v2)).normalized; //E Vector3 v5 = (0.5f * (v2 + v0)).normalized; //F int outIndex0 = outPoints.IndexOf(v0); if (outIndex0 == -1) { outIndex0 = outPoints.Count; outPoints.Add(v0); } int outIndex1 = outPoints.IndexOf(v1); if (outIndex1 == -1) { outIndex1 = outPoints.Count; outPoints.Add(v1); } int outIndex2 = outPoints.IndexOf(v2); if (outIndex2 == -1) { outIndex2 = outPoints.Count; outPoints.Add(v2); } int outIndex3 = outPoints.IndexOf(v3); if (outIndex3 == -1) { outIndex3 = outPoints.Count; outPoints.Add(v3); } int outIndex4 = outPoints.IndexOf(v4); if (outIndex4 == -1) { outIndex4 = outPoints.Count; outPoints.Add(v4); } int outIndex5 = outPoints.IndexOf(v5); if (outIndex5 == -1) { outIndex5 = outPoints.Count; outPoints.Add(v5); } outFaces.AddRange(new int[] { outIndex0, outIndex3, outIndex5 }); outFaces.AddRange(new int[] { outIndex3, outIndex4, outIndex5 }); outFaces.AddRange(new int[] { outIndex3, outIndex1, outIndex4 }); outFaces.AddRange(new int[] { outIndex5, outIndex4, outIndex2 }); } LocalMesh outMesh = new LocalMesh(outPoints, outFaces); return(outMesh); }