public static CausticDecalPolygon ClipPolygon(CausticDecalPolygon polygon, Plane plane) { bool[] positive = new bool[9]; int positiveCount = 0; for(int i = 0; i < polygon.vertices.Count; i++) { positive[i] = !plane.GetSide( polygon.vertices[i] ); if(positive[i]) positiveCount++; } if(positiveCount == 0) return null; // полностью за плоскостью if(positiveCount == polygon.vertices.Count) return polygon; // полностью перед плоскостью CausticDecalPolygon tempPolygon = new CausticDecalPolygon(); for(int i = 0; i < polygon.vertices.Count; i++) { int next = i + 1; next %= polygon.vertices.Count; if( positive[i] ) { tempPolygon.vertices.Add( polygon.vertices[i] ); } if( positive[i] != positive[next] ) { Vector3 v1 = polygon.vertices[next]; Vector3 v2 = polygon.vertices[i]; Vector3 v = LineCast(plane, v1, v2); tempPolygon.vertices.Add( v ); } } return tempPolygon; }
public static void BuildDecalForObject(CausticDecal decal, GameObject affectedObject) { Mesh affectedMesh = affectedObject.GetComponent<MeshFilter>().sharedMesh; if(affectedMesh == null) return; float maxAngle = decal.maxAngle; Plane right = new Plane( Vector3.right, Vector3.right/2f ); Plane left = new Plane( -Vector3.right, -Vector3.right/2f ); Plane top = new Plane( Vector3.up, Vector3.up/2f ); Plane bottom = new Plane( -Vector3.up, -Vector3.up/2f ); Plane front = new Plane( Vector3.forward, Vector3.forward/2f ); Plane back = new Plane( -Vector3.forward, -Vector3.forward/2f ); Vector3[] vertices = affectedMesh.vertices; int[] triangles = affectedMesh.triangles; int startVertexCount = bufVertices.Count; Matrix4x4 matrix = decal.transform.worldToLocalMatrix * affectedObject.transform.localToWorldMatrix; for(int i=0; i<triangles.Length; i+=3) { int i1 = triangles[i]; int i2 = triangles[i+1]; int i3 = triangles[i+2]; Vector3 v1 = matrix.MultiplyPoint( vertices[i1] ); Vector3 v2 = matrix.MultiplyPoint( vertices[i2] ); Vector3 v3 = matrix.MultiplyPoint( vertices[i3] ); Vector3 side1 = v2 - v1; Vector3 side2 = v3 - v1; Vector3 normal = Vector3.Cross(side1, side2).normalized; if( Vector3.Angle(-Vector3.forward, normal) >= maxAngle ) continue; CausticDecalPolygon poly = new CausticDecalPolygon( v1, v2, v3 ); poly = CausticDecalPolygon.ClipPolygon(poly, right); if(poly == null) continue; poly = CausticDecalPolygon.ClipPolygon(poly, left); if(poly == null) continue; poly = CausticDecalPolygon.ClipPolygon(poly, top); if(poly == null) continue; poly = CausticDecalPolygon.ClipPolygon(poly, bottom); if(poly == null) continue; poly = CausticDecalPolygon.ClipPolygon(poly, front); if(poly == null) continue; poly = CausticDecalPolygon.ClipPolygon(poly, back); if(poly == null) continue; AddPolygon( poly, normal ); } GenerateTexCoords(startVertexCount); }
private static void AddPolygon(CausticDecalPolygon poly, Vector3 normal) { int ind1 = AddVertex(poly.vertices[0], normal); for (int i = 1; i < poly.vertices.Count - 1; i++) { int ind2 = AddVertex(poly.vertices[i], normal); int ind3 = AddVertex(poly.vertices[i + 1], normal); bufIndices.Add(ind1); bufIndices.Add(ind2); bufIndices.Add(ind3); } }
public static CausticDecalPolygon ClipPolygon(CausticDecalPolygon polygon, Plane plane) { bool[] positive = new bool[9]; int positiveCount = 0; for (int i = 0; i < polygon.vertices.Count; i++) { positive[i] = !plane.GetSide(polygon.vertices[i]); if (positive[i]) { positiveCount++; } } if (positiveCount == 0) { return(null); // полностью за плоскостью } if (positiveCount == polygon.vertices.Count) { return(polygon); // полностью перед плоскостью } CausticDecalPolygon tempPolygon = new CausticDecalPolygon(); for (int i = 0; i < polygon.vertices.Count; i++) { int next = i + 1; next %= polygon.vertices.Count; if (positive[i]) { tempPolygon.vertices.Add(polygon.vertices[i]); } if (positive[i] != positive[next]) { Vector3 v1 = polygon.vertices[next]; Vector3 v2 = polygon.vertices[i]; Vector3 v = LineCast(plane, v1, v2); tempPolygon.vertices.Add(v); } } return(tempPolygon); }
private static void AddPolygon(CausticDecalPolygon poly, Vector3 normal) { int ind1 = AddVertex( poly.vertices[0], normal ); for(int i=1; i<poly.vertices.Count-1; i++) { int ind2 = AddVertex( poly.vertices[i], normal ); int ind3 = AddVertex( poly.vertices[i+1], normal ); bufIndices.Add( ind1 ); bufIndices.Add( ind2 ); bufIndices.Add( ind3 ); } }
public static void BuildDecalForObject(CausticDecal decal, GameObject affectedObject) { Mesh affectedMesh = affectedObject.GetComponent <MeshFilter>().sharedMesh; if (affectedMesh == null) { return; } float maxAngle = decal.maxAngle; Plane right = new Plane(Vector3.right, Vector3.right / 2f); Plane left = new Plane(-Vector3.right, -Vector3.right / 2f); Plane top = new Plane(Vector3.up, Vector3.up / 2f); Plane bottom = new Plane(-Vector3.up, -Vector3.up / 2f); Plane front = new Plane(Vector3.forward, Vector3.forward / 2f); Plane back = new Plane(-Vector3.forward, -Vector3.forward / 2f); Vector3[] vertices = affectedMesh.vertices; int[] triangles = affectedMesh.triangles; int startVertexCount = bufVertices.Count; Matrix4x4 matrix = decal.transform.worldToLocalMatrix * affectedObject.transform.localToWorldMatrix; for (int i = 0; i < triangles.Length; i += 3) { int i1 = triangles[i]; int i2 = triangles[i + 1]; int i3 = triangles[i + 2]; Vector3 v1 = matrix.MultiplyPoint(vertices[i1]); Vector3 v2 = matrix.MultiplyPoint(vertices[i2]); Vector3 v3 = matrix.MultiplyPoint(vertices[i3]); Vector3 side1 = v2 - v1; Vector3 side2 = v3 - v1; Vector3 normal = Vector3.Cross(side1, side2).normalized; if (Vector3.Angle(-Vector3.forward, normal) >= maxAngle) { continue; } CausticDecalPolygon poly = new CausticDecalPolygon(v1, v2, v3); poly = CausticDecalPolygon.ClipPolygon(poly, right); if (poly == null) { continue; } poly = CausticDecalPolygon.ClipPolygon(poly, left); if (poly == null) { continue; } poly = CausticDecalPolygon.ClipPolygon(poly, top); if (poly == null) { continue; } poly = CausticDecalPolygon.ClipPolygon(poly, bottom); if (poly == null) { continue; } poly = CausticDecalPolygon.ClipPolygon(poly, front); if (poly == null) { continue; } poly = CausticDecalPolygon.ClipPolygon(poly, back); if (poly == null) { continue; } AddPolygon(poly, normal); } GenerateTexCoords(startVertexCount); }