static void AddPolygon(DecalPolygon 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); int ind1 = AddVertex(poly.vertices[0], normal); bufIndices.Add(ind1); bufIndices.Add(ind2); bufIndices.Add(ind3); } }
public static DecalPolygon ClipPolygon(DecalPolygon 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); // полностью перед плоскостью } DecalPolygon tempPolygon = new DecalPolygon(); 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 CreateDecalMeshStatic() { int startVertexCount = bufVertices.Count; Matrix4x4 matrix = decal.transform.worldToLocalMatrix * affectedObject.transform.localToWorldMatrix; for (int i = 0; i < triangles.Length; i += 3) { Vector3 v1 = matrix.MultiplyPoint(vertices[triangles[i]]); Vector3 v2 = matrix.MultiplyPoint(vertices[triangles[i + 1]]); Vector3 v3 = matrix.MultiplyPoint(vertices[triangles[i + 2]]); Vector3 normal = Vector3.Cross(v2 - v1, v3 - v1).normalized; if (!FacingNormal(triangles[i], triangles[i + 1], triangles[i + 2])) { continue; } DecalPolygon poly = new DecalPolygon(v1, v2, v3); poly = DecalPolygon.ClipPolygon(poly, DecalPolygon.right); if (poly == null) { continue; } poly = DecalPolygon.ClipPolygon(poly, DecalPolygon.left); if (poly == null) { continue; } poly = DecalPolygon.ClipPolygon(poly, DecalPolygon.top); if (poly == null) { continue; } poly = DecalPolygon.ClipPolygon(poly, DecalPolygon.bottom); if (poly == null) { continue; } poly = DecalPolygon.ClipPolygon(poly, DecalPolygon.front); if (poly == null) { continue; } poly = DecalPolygon.ClipPolygon(poly, DecalPolygon.back); if (poly == null) { continue; } AddPolygon(poly, normal); } GenerateTexCoords(startVertexCount, decal.decalDefinition.sprite); decalMesh = StaticCreateMesh(); decal.SetMesh(decalMesh); }