Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using TriangleNet.Geometry;
- using TriangleNet.Meshing;
- using UnityEngine;
- using Mesh = UnityEngine.Mesh;
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshRenderer))]
- [RequireComponent(typeof(MeshCollider))]
- public class CustomMesh : MonoBehaviour
- {
- private static readonly int BaseMap = Shader.PropertyToID("_BaseMap");
- public MeshFilter MeshFilter { get; private set; }
- public MeshRenderer MeshRenderer { get; private set; }
- public MeshCollider MeshCollider { get; private set; }
- public Mesh Mesh { get; set; }
- private void Awake()
- {
- MeshFilter = GetComponent<MeshFilter>();
- MeshRenderer = GetComponent<MeshRenderer>();
- MeshCollider = GetComponent<MeshCollider>();
- Mesh = new Mesh();
- MeshFilter.mesh = Mesh;
- }
- public void CreateCustomMesh(SerializedMesh serializedMesh)
- {
- if (serializedMesh.points.Count % 2 != 0)
- {
- Debug.LogError($"CreateCustomMesh - Cannot have odd points count ! ({serializedMesh.points.Count.ToString()} provided)");
- return;
- }
- List<Transform> childrenTransform = new List<Transform>();
- for (int i = 0; i < serializedMesh.children.Count; i++)
- {
- Transform child = SerializedManager.Instance.InstantiateSerializedGameObject(serializedMesh.children[i]);
- child.SetParent(transform);
- childrenTransform.Add(child);
- }
- int pointCount = serializedMesh.points.Count / 2;
- int vertCount = serializedMesh.points.Count + childrenTransform.Count * 8;
- Vector3[] vertices = new Vector3[vertCount];
- int[] triangles;
- // Create vertices
- for (int i = 0; i < serializedMesh.points.Count; i++)
- {
- vertices[i] = serializedMesh.points[i].ToVector3();
- }
- Plane frontFacePlane = new Plane(vertices[0], vertices[1], vertices[2]);
- Plane backFacePlane = new Plane(vertices[pointCount], vertices[pointCount+1], vertices[pointCount+2]);
- for (int i = 0; i < childrenTransform.Count; i++)
- {
- /*
- // World space
- MeshRenderer meshRenderer = childrenTransform[i].GetComponentInChildren<MeshRenderer>();
- if (meshRenderer == null)
- {
- Debug.LogError("CreateCustomMesh - Could not get mesh bounds of children !");
- continue;
- }
- Bounds bounds = meshRenderer.bounds;
- */
- // Local space
- Mesh mesh = childrenTransform[i].GetComponent<MeshFilter>().mesh;
- if (mesh == null)
- {
- Debug.LogError("CreateCustomMesh - Could not get MeshRenderer of children !");
- continue;
- }
- Bounds bounds = mesh.bounds;
- Vector3 v0 = new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z - bounds.extents.z);
- Vector3 v1 = new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z - bounds.extents.z);
- Vector3 v2 = new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z - bounds.extents.z);
- Vector3 v3 = new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z - bounds.extents.z);
- Vector3 v4 = new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z + bounds.extents.z);
- Vector3 v5 = new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z + bounds.extents.z);
- Vector3 v6 = new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z + bounds.extents.z);
- Vector3 v7 = new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z + bounds.extents.z);
- // This is probably one problem, I'm just trying values randomly...
- Vector3 childForward = childrenTransform[i].forward;
- childForward.x *= -90f;
- v0 = Quaternion.Euler(0, childForward.x, 0) * v0;
- v1 = Quaternion.Euler(0, childForward.x, 0) * v1;
- v2 = Quaternion.Euler(0, childForward.x, 0) * v2;
- v3 = Quaternion.Euler(0, childForward.x, 0) * v3;
- v4 = Quaternion.Euler(0, childForward.x, 0) * v4;
- v5 = Quaternion.Euler(0, childForward.x, 0) * v5;
- v6 = Quaternion.Euler(0, childForward.x, 0) * v6;
- v7 = Quaternion.Euler(0, childForward.x, 0) * v7;
- // Local space only ///////////////////////////////
- Vector3 childPos = childrenTransform[i].position;
- childPos.x -= bounds.center.x * 2;
- childPos.y -= bounds.center.y * 2;
- childPos.z -= bounds.center.z * 2;
- v0 += childPos;
- v1 += childPos;
- v2 += childPos;
- v3 += childPos;
- v4 += childPos;
- v5 += childPos;
- v6 += childPos;
- v7 += childPos;
- ////////////////////////////////////////////////////
- // This is so that the vertices of the holes don't "shrink" or expand the mesh, but rather stay on the same plane, it works
- v0 = frontFacePlane.ClosestPointOnPlane(v0);
- v1 = frontFacePlane.ClosestPointOnPlane(v1);
- v2 = frontFacePlane.ClosestPointOnPlane(v2);
- v3 = frontFacePlane.ClosestPointOnPlane(v3);
- v4 = backFacePlane.ClosestPointOnPlane(v4);
- v5 = backFacePlane.ClosestPointOnPlane(v5);
- v6 = backFacePlane.ClosestPointOnPlane(v6);
- v7 = backFacePlane.ClosestPointOnPlane(v7);
- /*
- // Create small sphere at each vert pos to help visualize order
- var go0 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go0.transform.localScale = Vector3.one * 0.1f;
- go0.transform.position = v0;
- go0.name = "v0";
- var go1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go1.transform.localScale = Vector3.one * 0.1f;
- go1.transform.position = v1;
- go1.name = "v1";
- var go2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go2.transform.localScale = Vector3.one * 0.1f;
- go2.transform.position = v2;
- go2.name = "v2";
- var go3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go3.transform.localScale = Vector3.one * 0.1f;
- go3.transform.position = v3;
- go3.name = "v3";
- var go4 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go4.transform.localScale = Vector3.one * 0.1f;
- go4.transform.position = v4;
- go4.name = "v4";
- var go5 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go5.transform.localScale = Vector3.one * 0.1f;
- go5.transform.position = v5;
- go5.name = "v5";
- var go6 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go6.transform.localScale = Vector3.one * 0.1f;
- go6.transform.position = v6;
- go6.name = "v6";
- var go7 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go7.transform.localScale = Vector3.one * 0.1f;
- go7.transform.position = v7;
- go7.name = "v7";
- */
- vertices[serializedMesh.points.Count + 8 * i] = v0;
- vertices[serializedMesh.points.Count + 8 * i + 1] = v1;
- vertices[serializedMesh.points.Count + 8 * i + 2] = v2;
- vertices[serializedMesh.points.Count + 8 * i + 3] = v3;
- vertices[serializedMesh.points.Count + 8 * i + 4] = v4;
- vertices[serializedMesh.points.Count + 8 * i + 5] = v5;
- vertices[serializedMesh.points.Count + 8 * i + 6] = v6;
- vertices[serializedMesh.points.Count + 8 * i + 7] = v7;
- #if UNITY_EDITOR
- // Draw bounding box of the holes
- Debug.DrawLine(v0, v1, Color.red, 1000);
- Debug.DrawLine(v1, v2, Color.red, 1000);
- Debug.DrawLine(v2, v3, Color.red, 1000);
- Debug.DrawLine(v3, v0, Color.red, 1000);
- Debug.DrawLine(v4, v5, Color.red, 1000);
- Debug.DrawLine(v4, v7, Color.red, 1000);
- Debug.DrawLine(v5, v6, Color.red, 1000);
- Debug.DrawLine(v6, v7, Color.red, 1000);
- Debug.DrawLine(v0, v4, Color.red, 1000);
- Debug.DrawLine(v1, v5, Color.red, 1000);
- Debug.DrawLine(v2, v6, Color.red, 1000);
- Debug.DrawLine(v3, v7, Color.red, 1000);
- #endif
- }
- // This is what doesn't work
- // If we can't predict the triangulation, use Delaunay
- if (serializedMesh.children.Count > 1 || serializedMesh.points.Count > 8)
- {
- List<int> meshTriangles = new List<int>();
- // Outer border
- for (int i = 0; i < pointCount - 1; i++)
- {
- int v0 = i + pointCount + 1;
- int v1 = i + 1;
- int v2 = i;
- int v3 = i + pointCount;
- meshTriangles.Add(v0);
- meshTriangles.Add(v1);
- meshTriangles.Add(v2);
- meshTriangles.Add(v2);
- meshTriangles.Add(v3);
- meshTriangles.Add(v0);
- }
- // Bridge last face of the outer border
- meshTriangles.Add(pointCount);
- meshTriangles.Add(0);
- meshTriangles.Add(pointCount-1);
- meshTriangles.Add(pointCount-1);
- meshTriangles.Add(2*pointCount-1);
- meshTriangles.Add(pointCount);
- ConstraintOptions options = new ConstraintOptions
- {
- ConformingDelaunay = true,
- SegmentSplitting = 2
- };
- /*
- QualityOptions quality = new QualityOptions
- {
- MaximumAngle = 25
- };
- */
- Polygon polygon = new Polygon();
- Vector3 faceNormal = TriangleNormal(serializedMesh.points[0].ToVector3(),serializedMesh.points[1].ToVector3(), serializedMesh.points[2].ToVector3());
- int dir = GetBoxDir(faceNormal);
- for (int i = 0; i < pointCount; i++)
- {
- Vector2 tmp = GetBoxUV(serializedMesh.points[i].ToVector3(), dir);
- Vertex vertex = new Vertex(tmp.x, tmp.y);
- polygon.Add(vertex);
- }
- for (int i = 0; i < polygon.Points.Count; i++)
- {
- polygon.Add(new Segment(polygon.Points[i], i == polygon.Points.Count - 1 ? polygon.Points[0] : polygon.Points[i+1]));
- }
- // TODO : DEBUG : try to build without holes and discard every triangle that share 3 index of the same hole ? no luck so far
- // Holes
- for (int i = 0; i < childrenTransform.Count; i++)
- {
- // Inner border of child i
- for (int j = 0; j < 3; j++)
- {
- meshTriangles.Add(j + pointCount*2 + 4 + 8*i);
- meshTriangles.Add(j + pointCount*2 + 8*i);
- meshTriangles.Add(j + pointCount*2 + 1 + 8*i);
- meshTriangles.Add(j + pointCount*2 + 1 + 8*i);
- meshTriangles.Add(j + pointCount*2 + 5 + 8*i);
- meshTriangles.Add(j + pointCount*2 + 4 + 8*i);
- }
- // Bridge last face of the inner border of child i
- meshTriangles.Add(pointCount*4 - 1 + 8*i);
- meshTriangles.Add(pointCount*3 - 1 + 8*i);
- meshTriangles.Add(pointCount*2 + 8*i);
- meshTriangles.Add(pointCount*2 + 8*i);
- meshTriangles.Add(pointCount*3 + 8*i);
- meshTriangles.Add(pointCount*4 - 1 + 8*i);
- Vector3 v0 = vertices[serializedMesh.points.Count + 8 * i];
- Vector3 v1 = vertices[serializedMesh.points.Count + 8 * i + 1];
- Vector3 v2 = vertices[serializedMesh.points.Count + 8 * i + 2];
- Vector3 v3 = vertices[serializedMesh.points.Count + 8 * i + 3];
- Vector2 v0tmp = GetBoxUV(v0, dir);
- Vector2 v1tmp = GetBoxUV(v1, dir);
- Vector2 v2tmp = GetBoxUV(v2, dir);
- Vector2 v3tmp = GetBoxUV(v3, dir);
- Vertex vertex0 = new Vertex(v0tmp.x, v0tmp.y);
- Vertex vertex1 = new Vertex(v1tmp.x, v1tmp.y);
- Vertex vertex2 = new Vertex(v2tmp.x, v2tmp.y);
- Vertex vertex3 = new Vertex(v3tmp.x, v3tmp.y);
- /*
- polygon.Add(vertex0);
- polygon.Add(vertex1);
- polygon.Add(vertex2);
- polygon.Add(vertex3);
- polygon.Add(new Segment(vertex0, vertex1));
- polygon.Add(new Segment(vertex1, vertex2));
- polygon.Add(new Segment(vertex2, vertex3));
- polygon.Add(new Segment(vertex3, vertex0));
- */
- List<Vertex> holeVertices = new List<Vertex>()
- {
- vertex0,
- vertex1,
- vertex2,
- vertex3
- };
- // This sometimes produces an error, because I'm unable to go from 2d to 3d, so points are overlapping or idk
- Contour hole = new Contour(holeVertices);
- polygon.Add(hole, true);
- }
- var triangulatedMesh = polygon.Triangulate(options) as TriangleNet.Mesh;
- if (triangulatedMesh == null)
- {
- Debug.LogError($"CreateCustomMesh - Triangulation error of {serializedMesh.name} !");
- return;
- }
- foreach(ITriangle triangle in triangulatedMesh.Triangles)
- {
- var v0 = triangle.GetVertexID(2);
- var v1 = triangle.GetVertexID(1);
- var v2 = triangle.GetVertexID(0);
- // Front face
- meshTriangles.Add(v0);
- meshTriangles.Add(v1);
- meshTriangles.Add(v2);
- // Back face
- meshTriangles.Add(v0 + pointCount);
- meshTriangles.Add(v2 + pointCount);
- meshTriangles.Add(v1 + pointCount);
- var t0 = triangle.GetVertex(2);
- var t1 = triangle.GetVertex(1);
- var t2 = triangle.GetVertex(0);
- /*
- Debug.DrawLine(new Vector3((float)t0.x, (float)t0.y), new Vector3((float)t1.x, (float)t1.y), Color.red, 1000);
- Debug.DrawLine(new Vector3((float)t1.x, (float)t1.y), new Vector3((float)t2.x, (float)t2.y), Color.red, 1000);
- Debug.DrawLine(new Vector3((float)t2.x, (float)t2.y), new Vector3((float)t0.x, (float)t0.y), Color.red, 1000);
- */
- }
- List<int> tmpTri = new List<int>();
- for (int i = 0; i < meshTriangles.Count; i++)
- {
- tmpTri.Add(meshTriangles[i]);
- }
- /* Tried a bit to build without holes, then discard according triangle, with no luck
- print("Before discarding : " + tmpTri.Count);
- int nbTriInHole;
- for (int i = 0; i < tmpTri.Count; i += 3)
- {
- nbTriInHole = 0;
- //print($"Triangle {i/3} is ({tmpTri[i]}, {tmpTri[i+1]}, {tmpTri[i+2]})");
- for (int j = 0; j < serializedMesh.children.Count; j++)
- {
- int tmp = pointCount * 2 + j * 8;
- if (tmpTri[i] == tmp || tmpTri[i] == tmp + 1 || tmpTri[i] == tmp + 2 ||
- tmpTri[i] == tmp + 3)
- {
- nbTriInHole++;
- }
- if (tmpTri[i+1] == tmp || tmpTri[i+1] == tmp + 1 || tmpTri[i+1] == tmp + 2 ||
- tmpTri[i+1] == tmp + 3)
- {
- nbTriInHole++;
- }
- if (tmpTri[i+2] == tmp || tmpTri[i+2] == tmp + 1 || tmpTri[i+2] == tmp + 2 ||
- tmpTri[i+2] == tmp + 3)
- {
- nbTriInHole++;
- }
- }
- if (nbTriInHole > 2)
- {
- print($"Discarding triangle {i/3} ({tmpTri[i]}, {tmpTri[i+1]}, {tmpTri[i+2]})");
- tmpTri.RemoveRange(i, 3);
- var t0 = vertices[tmpTri[i]];
- t0.z = 0;
- var t1 = vertices[tmpTri[i+1]];
- t1.z = 0;
- var t2 = vertices[tmpTri[i+2]];
- t2.z = 0;
- Debug.DrawLine(t0, t1, Color.blue, 4);
- Debug.DrawLine(t1, t2, Color.blue, 4);
- Debug.DrawLine(t2, t0, Color.blue, 4);
- }
- }
- print("After discarding : " + tmpTri.Count);
- */
- triangles = tmpTri.ToArray();
- }
- // Here are the no-hole / 1-hole hardcoded triangles, it works
- else
- {
- // Create triangles
- int triCount = (12 + childrenTransform.Count * 20) * 3;
- switch (childrenTransform.Count)
- {
- case 0:
- triangles = new [] {
- // Front face
- 0, 1, 2,
- 2, 3, 0,
- // Back face
- 7, 6, 5,
- 5, 4, 7,
- // Left face
- 4, 5, 1,
- 1, 0, 4,
- // Right face
- 3, 2, 6,
- 6, 7, 3,
- // Top face
- 1, 5, 6,
- 6, 2, 1,
- // Bottom face
- 4, 0, 3,
- 3, 7, 4
- };
- break;
- case 1:
- triangles = new [] {
- // Front left
- 0, 1, 9,
- 9, 8, 0,
- // Front top
- 9, 1, 2,
- 2, 10, 9,
- // Front right
- 11, 10, 2,
- 2, 3, 11,
- // Front bot
- 0, 8, 11,
- 11, 3, 0,
- // Back left
- 7, 6, 14,
- 14, 15, 7,
- // Back top
- 14, 6, 5,
- 5, 13, 14,
- // Back right
- 12, 13, 5,
- 5, 4, 12,
- // Back bot
- 7, 15, 12,
- 12, 4, 7,
- // Outer left
- 4, 5, 1,
- 1, 0, 4,
- // Outer top
- 1, 5, 6,
- 6, 2, 1,
- // Outer right
- 3, 2, 6,
- 6, 7, 3,
- // Outer bot
- 4, 0, 3,
- 3, 7, 4,
- // Inner left
- 8, 9, 13,
- 13, 12, 8,
- // Inner top
- 13, 9, 10,
- 10, 14, 13,
- // Inner right
- 14, 10, 11,
- 11, 15, 14,
- // Inner bot
- 15, 11, 8,
- 8, 12, 15
- };
- break;
- /*
- case 2:
- triangles = new [] {
- // Front left
- 0, 1, 9,
- 9, 8, 0,
- // Front top
- 9, 1, 2,
- 2, 10, 9,
- // Front right
- 11, 10, 2,
- 2, 3, 11,
- // Front bot left
- 0, 8, 17,
- 17, 16, 0,
- // Front bot top
- 17, 8, 11,
- 11, 18, 17,
- // Front bot right
- 19, 18, 11,
- 11, 3, 19,
- // Front bot bot
- 0, 16, 19,
- 19, 3, 0,
- // Back left
- 7, 6, 14,
- 14, 15, 7,
- // Back top
- 14, 6, 5,
- 5, 13, 14,
- // Back right
- 12, 13, 5,
- 5, 4, 12,
- // Back bot left
- 7, 15, 22,
- 22, 23, 7,
- // Back bot top
- 22, 15, 12,
- 12, 21, 22,
- // Back bot right
- 20, 21, 12,
- 12, 4, 20,
- // Back bot bot
- 7, 23, 20,
- 20, 4, 7,
- // Outer left
- 4, 5, 1,
- 1, 0, 4,
- // Outer top
- 1, 5, 6,
- 6, 2, 1,
- // Outer right
- 3, 2, 6,
- 6, 7, 3,
- // Outer bot
- 4, 0, 3,
- 3, 7, 4,
- // Inner left
- 8, 9, 13,
- 13, 12, 8,
- // Inner top
- 13, 9, 10,
- 10, 14, 13,
- // Inner right
- 14, 10, 11,
- 11, 15, 14,
- // Inner bot
- 15, 11, 8,
- 8, 12, 15,
- // Inner 2 left
- 20, 16, 17,
- 17, 21, 21,
- // Inner 2 top
- 21, 17, 18,
- 18, 22, 21,
- // Inner 2 right
- 22, 18, 19,
- 19, 23, 22,
- // Inner 2 bot
- 23, 19, 16,
- 16, 20, 23
- };
- break;
- */
- default:
- triangles = new int[0];
- break;
- }
- if (triangles.Length != triCount)
- {
- Debug.LogError($"CreateCustomMesh - Wrong triangles count : {triangles.Length.ToString()} (expected {triCount.ToString()})");
- return;
- }
- }
- Mesh.vertices = vertices;
- Mesh.triangles = triangles;
- Mesh.Optimize();
- Mesh.RecalculateNormals();
- CalculateUVs();
- Mesh.RecalculateTangents();
- Mesh.RecalculateBounds();
- MeshCollider.sharedMesh = Mesh;
- if(Mathf.Abs(serializedMesh.material.textureOffsetX) > 0.001f || Mathf.Abs(serializedMesh.material.textureOffsetY) > 0.001f)
- {
- MeshRenderer.material.SetTextureOffset(BaseMap, new Vector2(serializedMesh.material.textureOffsetX, serializedMesh.material.textureOffsetY));
- }
- }
- private void CalculateUVs()
- {
- Matrix4x4 matrix = transform.localToWorldMatrix;
- Vector3[] vertices = Mesh.vertices;
- Vector3[] normals = Mesh.normals;
- Vector3[] worldVertices = new Vector3[vertices.Length];
- for (int i = 0; i < worldVertices.Length; i++)
- {
- worldVertices[i] = matrix.MultiplyPoint(vertices[i]);
- }
- List<Vector3> newVerts = new List<Vector3>(vertices.Length);
- List<Vector3> newNormals = new List<Vector3>(vertices.Length);
- List<Vector2> newUVs = new List<Vector2>(vertices.Length);
- List<List<int>> newTris = new List<List<int>>();
- Dictionary<int,int[]> vertexMap = new Dictionary<int, int[]>();
- for (int i = -3; i <= 3; i++)
- {
- if (i == 0)
- continue;
- int[] vmap = new int[vertices.Length];
- for (int v = 0; v < vmap.Length; v++)
- {
- vmap[v] = -1;
- }
- vertexMap.Add(i,vmap);
- }
- // Compute triangle normal for each tri, and rebuild it with unique vertices
- for (int s = 0; s < Mesh.subMeshCount; s++)
- {
- int[] triangles = Mesh.GetTriangles(s);
- newTris.Add( new List<int>() );
- for (int t = 0; t < triangles.Length; t += 3)
- {
- int v0 = triangles[t];
- int v1 = triangles[t+1];
- int v2 = triangles[t+2];
- Vector3 triNormal = TriangleNormal(worldVertices[v0], worldVertices[v1], worldVertices[v2]);
- int boxDir = GetBoxDir(triNormal);
- // Remap triangle verts
- for (int i = 0; i < 3; i++)
- {
- int v = triangles[t+i];
- // If vertex doesn't already exist in boxDir vertex map,
- // we'll add a copy of it with the correct UV
- if (vertexMap[boxDir][v] < 0)
- {
- // Compute UV
- Vector2 vertexUV = GetBoxUV(worldVertices[v], boxDir);
- vertexMap[boxDir][v] = newVerts.Count;
- newVerts.Add(vertices[v]);
- newNormals.Add(normals[v]);
- newUVs.Add(vertexUV);
- }
- // Use remapped vertex index
- newTris[s].Add(vertexMap[boxDir][v]);
- }
- }
- }
- Mesh.vertices = newVerts.ToArray();
- Mesh.normals = newNormals.ToArray();
- Mesh.uv = newUVs.ToArray();
- for (int s = 0; s < newTris.Count; s++)
- {
- Mesh.SetTriangles(newTris[s].ToArray(), s);
- }
- }
- private static Vector3 TriangleNormal(Vector3 a, Vector3 b, Vector3 c)
- {
- return Vector3.Cross(b - a, c - a).normalized;
- }
- private static int GetBoxDir(Vector3 v)
- {
- float x = Mathf.Abs(v.x);
- float y = Mathf.Abs(v.y);
- float z = Mathf.Abs(v.z);
- // X - axis
- if (x > y && x > z)
- {
- return v.x < 0 ? -1 : 1;
- }
- // Y - axis
- if (y > z)
- {
- return v.y < 0 ? -2 : 2;
- }
- // Z - axis
- return v.z < 0 ? -3 : 3;
- }
- private static Vector2 GetBoxUV(Vector3 vertex, int boxDir)
- {
- switch (boxDir)
- {
- // X - axis
- case -1: case 1:
- return new Vector2(vertex.z * Mathf.Sign(boxDir), vertex.y);
- // Y - axis
- case -2: case 2:
- return new Vector2(vertex.x, vertex.z * Mathf.Sign(boxDir));
- // Z - axis
- default:
- return new Vector2(vertex.x * -Mathf.Sign(boxDir), vertex.y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement