コード例 #1
0
    public static bool ClipPolygon(ref DecalPolygon polygon, Plane plane)
    {
        ResetPositive();
        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(false);
        }
        if (positiveCount == polygon.vertices.Count)
        {
            return(true);
        }

        tempPolygon.Clear();
        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);
            }
        }
        polygon.CopyFrom(tempPolygon);
        return(true);
    }
コード例 #2
0
ファイル: DecalBuilder.cs プロジェクト: yangfan111/CsharpCode
    public static void BuildDecalForObject(GameObject decal, GameObject affectedObject, Texture2D texture)
    {
        Mesh         affectedMesh = null;
        MeshCollider mc           = affectedObject.GetComponent <MeshCollider>();

        if (mc != null)
        {
            affectedMesh = mc.sharedMesh;
        }
        if (affectedMesh == null)
        {
            affectedMesh = GenarateTerrainMesh(affectedObject);
        }

        if (affectedMesh == null)
        {
            MeshFilter mf = affectedObject.GetComponent <MeshFilter>();
            if (mf)
            {
                affectedMesh = mf.sharedMesh;
            }
        }
        if (affectedMesh == null)
        {
            return;
        }

        float maxAngle = /*decal.maxAngle*/ 90.0f;

        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;
            }

            poly.Clear();
            poly.SetVts(v1, v2, v3);

            //DecalPolygon poly = new DecalPolygon();
            //poly.SetVts(v1, v2, v3);

            if (!DecalPolygon.ClipPolygon(ref poly, right))
            {
                continue;
            }
            if (!DecalPolygon.ClipPolygon(ref poly, left))
            {
                continue;
            }

            if (!DecalPolygon.ClipPolygon(ref poly, top))
            {
                continue;
            }
            if (!DecalPolygon.ClipPolygon(ref poly, bottom))
            {
                continue;
            }

            if (!DecalPolygon.ClipPolygon(ref poly, front))
            {
                continue;
            }
            if (!DecalPolygon.ClipPolygon(ref poly, back))
            {
                continue;
            }
            AddPolygon(poly, normal);
        }
        GenerateTexCoords(startVertexCount, texture);
    }