Ejemplo n.º 1
0
        internal SurfaceType GetSurfaceTypeByTriangle(int triangleIndex)
        {
            if (_mesh == null)
            {
                return(SurfaceType.Generic);
            }

            if (surfaceTypes.Length == 1)
            {
                return(surfaceTypes[0]);
            }

            //Get the sub mesh index of the supplied triangle index and find the surface type associated with it
            int subMeshIndex = MeshHelper.GetSubMeshIndexByTriangle(triangleIndex, _mesh.subMeshCount, _meshSubMeshTriangleRanges);

            return(subMeshIndex >= surfaceTypes.Length ? SurfaceType.Generic : surfaceTypes[subMeshIndex]);
        }
Ejemplo n.º 2
0
        internal void Project()
        {
            Initialize();

            if (_meshFilter.sharedMesh != null)
            {
                return;
            }

            this.transform.localScale = Vector3.one * _scale;
            // Randomly rotate the decal before projection
            this.transform.eulerAngles += new Vector3(180, 0, Random.Range(0f, 360f));

            RaycastHit raycastHit;
            bool       hit = Physics.Raycast(this.transform.position, this.transform.forward, out raycastHit, _scale);

            if (!hit)
            {
                Destroy(this.gameObject);
                return;
            }
            else if (raycastHit.collider.gameObject.GetComponent <Rigidbody>() != null)
            {
                Destroy(this.gameObject);
                return;
            }
            else if (raycastHit.collider.gameObject.layer != Globals.STRUCTURE_LAYER)
            {
                Destroy(this.gameObject);
                return;
            }
            UnityEngine.Mesh surfaceMesh = MeshHelper.GetObjectMesh(raycastHit.collider.gameObject);
            if (surfaceMesh == null)
            {
                Destroy(this.gameObject);
                return;
            }
            BooleanMesh(surfaceMesh, raycastHit);
        }
Ejemplo n.º 3
0
 private void Initialize()
 {
     _mesh = MeshHelper.GetObjectMesh(this.gameObject);
     _meshSubMeshTriangleRanges = MeshHelper.GetMeshSubMeshTriangleRanges(_mesh);
 }
Ejemplo n.º 4
0
        private void BooleanMesh(UnityEngine.Mesh surfaceMesh, RaycastHit raycastHit)
        {
            // Define the surfaces vertices and triangles
            Vector3[] vertices  = surfaceMesh.vertices;
            int[]     triangles = surfaceMesh.triangles;

            int startTriangle = 0;
            int endTriangle   = triangles.Length;

            if (_onlyBooleanHitSubMesh)
            {
                int[] subMeshRanges = MeshHelper.GetMeshSubMeshTriangleRanges(surfaceMesh);
                int   subMeshIndex  = MeshHelper.GetSubMeshIndexByTriangle(raycastHit.triangleIndex, surfaceMesh.subMeshCount, subMeshRanges);
                MeshHelper.GetSubMeshRange(subMeshRanges, subMeshIndex, out startTriangle, out endTriangle);
            }

            // Define clipping planes
            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);

            // Define vertex matrix
            Matrix4x4 matrix = this.transform.worldToLocalMatrix * raycastHit.collider.transform.localToWorldMatrix;

            for (int i = startTriangle; i < endTriangle; i += 3)
            {
                // Define indices
                int indice0 = triangles[i];
                int indice1 = triangles[i + 1];
                int indice2 = triangles[i + 2];

                // Define and offset verts to world space
                Vector3 vertice0 = matrix.MultiplyPoint(vertices[indice0]);
                Vector3 vertice1 = matrix.MultiplyPoint(vertices[indice1]);
                Vector3 vertice2 = matrix.MultiplyPoint(vertices[indice2]);

                Vector3 normal = GetTriangleNormal(vertice0, vertice1, vertice2);

                if (Vector3.Angle(-Vector3.forward, normal) >= _maxAngle)
                {
                    continue;
                }

                List <Vector3> poly = new List <Vector3> {
                    vertice0, vertice1, vertice2
                };

                ClipPoly(ref poly, right);
                if (poly.Count == 0)
                {
                    continue;
                }
                ClipPoly(ref poly, left);
                if (poly.Count == 0)
                {
                    continue;
                }

                ClipPoly(ref poly, top);
                if (poly.Count == 0)
                {
                    continue;
                }
                ClipPoly(ref poly, bottom);
                if (poly.Count == 0)
                {
                    continue;
                }

                ClipPoly(ref poly, front);
                if (poly.Count == 0)
                {
                    continue;
                }
                ClipPoly(ref poly, back);
                if (poly.Count == 0)
                {
                    continue;
                }

                AddPoly(poly, normal);
            }

            FinalizeMesh();
            CompositeMesh();
        }