private void BuildMesh() { int[] indices = TriangulationHelper.Triangulate2D(_polygonCollider2D.points); Vector3[] vertices = new Vector3[_polygonCollider2D.points.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = _polygonCollider2D.points[i] + _polygonCollider2D.offset; } if (_meshFilter.sharedMesh == null) { _meshFilter.sharedMesh = new Mesh(); } if (_meshFilter.sharedMesh.vertices != vertices) { _meshFilter.sharedMesh.vertices = vertices; _meshFilter.sharedMesh.triangles = indices; _meshFilter.sharedMesh.RecalculateNormals(); _meshFilter.sharedMesh.RecalculateBounds(); _meshRenderer.sortingOrder = _orderInLayer; _meshFilter.sharedMesh.name = "geometry"; _meshFilter.sharedMesh.hideFlags = HideFlags.HideAndDontSave; } }
private void OnDrawGizmos() { PolygonCollider2D polygonCollider2D = this.gameObject.GetComponent <PolygonCollider2D>(); int[] indices = TriangulationHelper.Triangulate2D(polygonCollider2D.points); Vector3[] vertices = new Vector3[polygonCollider2D.points.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = polygonCollider2D.points[i] + polygonCollider2D.offset; } MeshFilter meshFilter = this.gameObject.GetComponent <MeshFilter>(); MeshRenderer meshRenderer = this.gameObject.GetComponent <MeshRenderer>(); if (meshFilter.sharedMesh == null || meshFilter.sharedMesh.vertices.Length != vertices.Length) { meshFilter.sharedMesh = new Mesh(); } meshFilter.sharedMesh.vertices = vertices; meshFilter.sharedMesh.triangles = indices; meshFilter.sharedMesh.RecalculateNormals(); meshFilter.sharedMesh.RecalculateBounds(); meshRenderer.sortingOrder = _orderInLayer; meshFilter.sharedMesh.name = "geometry"; meshFilter.sharedMesh.hideFlags = HideFlags.HideAndDontSave; }
private void UpdateMesh(bool initialize = false) { if (!initialize) { if (!_meshRenderer.isVisible) { return; } if (_settleScale <= 0) { return; } } Vector2[] pathPoints = GetPathPoints(); _polygonCollider2D.points = pathPoints; int[] indices = TriangulationHelper.Triangulate2D(pathPoints); Vector3[] vertices = new Vector3[pathPoints.Length]; int vertexCount = vertices.Length; for (int i = 0; i < vertexCount; i++) { vertices[i] = pathPoints[i]; } Mesh mesh = _meshFilter.sharedMesh; // Update mesh if (mesh.vertexCount == 0 || mesh.vertexCount == vertexCount) { mesh.vertices = vertices; mesh.triangles = indices; mesh.RecalculateNormals(); mesh.RecalculateBounds(); } }