Example #1
0
        /// <summary>
        /// construct a new bsp tree from unity mesh
        /// </summary>
        /// <param name="mesh">unity mesh</param>
        /// <param name="meshTransform">transformation</param>
        /// <param name="id">id of the mesh</param>
        public void Construct(Mesh mesh, Transform meshTransform, int id)
        {
            // cache mesh data
            var trianglesNum  = mesh.triangles.Length;
            var meshTriangles = mesh.triangles;
            var meshVertices  = mesh.vertices;
            var meshNormals   = mesh.normals;
            var meshUV        = mesh.uv;

            Polygons = new List <CSGPolygon>(trianglesNum / 3);

            for (var i = 0; i < trianglesNum; i += 3)
            {
                var id0 = meshTriangles[i];
                var id1 = meshTriangles[i + 1];
                var id2 = meshTriangles[i + 2];

                Polygons.Add(new CSGPolygon(id, new CSGVertex(meshTransform.TransformPoint(meshVertices[id0]), meshTransform.TransformDirection(meshNormals[id0]), meshUV[id0]),
                                            new CSGVertex(meshTransform.TransformPoint(meshVertices[id1]), meshTransform.TransformDirection(meshNormals[id1]), meshUV[id1]),
                                            new CSGVertex(meshTransform.TransformPoint(meshVertices[id2]), meshTransform.TransformDirection(meshNormals[id2]), meshUV[id2])));
            }

            root = new CSGNode();
            root.Build(Polygons);
        }
Example #2
0
	    /// <summary>
	    /// construct a new bsp tree from unity mesh
	    /// </summary>
	    /// <param name="mesh">unity mesh</param>
	    /// <param name="meshTransform">transformation</param>
	    /// <param name="id">id of the mesh</param>
	    public void Construct(Mesh mesh, Transform meshTransform, int id)
        {
            // cache mesh data
            var trianglesNum = mesh.triangles.Length;
            var meshTriangles = mesh.triangles;
            var meshVertices = mesh.vertices;
            var meshNormals = mesh.normals;
            var meshUV = mesh.uv;

            Polygons = new List<CSGPolygon>(trianglesNum/3);

            for (int i = 0; i < trianglesNum; i+=3)
            {
                var id0 = meshTriangles[i];
                var id1 = meshTriangles[i + 1];
                var id2 = meshTriangles[i + 2];

                Polygons.Add(new CSGPolygon(id, new CSGVertex(meshTransform.TransformPoint(meshVertices[id0]), meshTransform.TransformDirection(meshNormals[id0]), meshUV[id0]),
                                             new CSGVertex(meshTransform.TransformPoint(meshVertices[id1]), meshTransform.TransformDirection(meshNormals[id1]), meshUV[id1]),
                                             new CSGVertex(meshTransform.TransformPoint(meshVertices[id2]), meshTransform.TransformDirection(meshNormals[id2]), meshUV[id2])));
            }

            root = new CSGNode();
            root.Build(Polygons);
        }
Example #3
0
 /// <summary>
 /// copy constructor
 /// </summary>
 /// <param name="instance">copy instance</param>
 public CSG(CSG instance)
 {
     Polygons = new List<CSGPolygon>(instance.Polygons.Count);
     foreach (var polygon in instance.Polygons)
     {
         Polygons.Add(new CSGPolygon(polygon));
     }
     root = new CSGNode();
     root.Build(Polygons);
 }
Example #4
0
 /// <summary>
 /// copy constructor
 /// </summary>
 /// <param name="instance">copy instance</param>
 public CSG(CSG instance)
 {
     Polygons = new List <CSGPolygon>(instance.Polygons.Count);
     foreach (var polygon in instance.Polygons)
     {
         Polygons.Add(new CSGPolygon(polygon));
     }
     root = new CSGNode();
     root.Build(Polygons);
 }
Example #5
0
        /// <summary>
        /// build new bsp tree from polygons
        /// </summary>
        /// <param name="polygons">input polygons</param>
        public void Build(List <CSGPolygon> polygons)
        {
            if (polygons.Count == 0)
            {
                return;
            }

            if (Plane == null)
            {
                Plane = new CSGPlane(polygons[0].Plane);
            }

            var frontList = new List <CSGPolygon>();
            var backList  = new List <CSGPolygon>();

            foreach (var polygon in polygons)
            {
                Plane.Split(polygon, Polygons, Polygons, frontList, backList);
            }

            if (frontList.Count > 0)
            {
                if (FrontChild == null)
                {
                    FrontChild = new CSGNode();
                }
                FrontChild.Build(frontList);
            }

            if (backList.Count > 0)
            {
                if (BackChild == null)
                {
                    BackChild = new CSGNode();
                }
                BackChild.Build(backList);
            }
        }