/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// ClipPoly /// # Return clip poly for actual poly /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// public static GenericPoly ClipPoly(GenericPoly actualPoly, Plane plane) { //Debug.Log ("ClipPoly"); bool[] positive = new bool[vertexNumber]; int positiveCount = 0; for (int i = 0; i < actualPoly.vertexList.Count; i++) { positive [i] = !plane.GetSide(actualPoly.vertexList [i]); if (positive [i]) { positiveCount++; } } if (positiveCount == 0) { return(null); } if (positiveCount == actualPoly.vertexList.Count) { return(actualPoly); } GenericPoly temporalPoly = new GenericPoly(); for (int i = 0; i < actualPoly.vertexList.Count; i++) { int next = i + 1; next %= actualPoly.vertexList.Count; if (positive [i]) { temporalPoly.vertexList.Add(actualPoly.vertexList [i]); } if (positive [i] != positive [next]) { Vector3 v1 = actualPoly.vertexList [next]; Vector3 v2 = actualPoly.vertexList [i]; Vector3 v = BasicFunctions.LineCast(plane, v1, v2); temporalPoly.vertexList.Add(v); } } return(temporalPoly); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// AddPoly /// # Add a poly /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// private static void AddPoly(GenericPoly poly, Vector3 normal) { int ind1 = AddVertex(poly.vertexList [0], normal); for (int i = 1; i < poly.vertexList.Count - 1; i++) { int ind2 = AddVertex(poly.vertexList [i], normal); int ind3 = AddVertex(poly.vertexList [i + 1], normal); indexBufferList.Add(ind1); indexBufferList.Add(ind2); indexBufferList.Add(ind3); } }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// BuildDecalForObject /// # Build a decal for one of the affected objects /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// public static void BuildDecalForObject(GenericMeshDecal decal, GameObject affectedObject) { if (affectedObject.isStatic) { Debug.Log("NOTE: static objects are not supported by realtime mesh decals, we are trying to make it possible soon"); } else { Mesh affectedMesh = affectedObject.GetComponent <MeshFilter> ().sharedMesh; if (affectedMesh == null) { return; } float angleLimit = decal.angleLimit; 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 = vertexBufferList.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) >= angleLimit) { continue; } GenericPoly poly = new GenericPoly(v1, v2, v3); poly = GenericPoly.ClipPoly(poly, right); if (poly == null) { continue; } poly = GenericPoly.ClipPoly(poly, left); if (poly == null) { continue; } poly = GenericPoly.ClipPoly(poly, top); if (poly == null) { continue; } poly = GenericPoly.ClipPoly(poly, bottom); if (poly == null) { continue; } poly = GenericPoly.ClipPoly(poly, front); if (poly == null) { continue; } poly = GenericPoly.ClipPoly(poly, back); if (poly == null) { continue; } AddPoly(poly, normal); } GenerateTexCoords(startVertexCount, decal.sprite); } }