예제 #1
0
        /**
         * Initialize a CSG_Model with the mesh of a gameObject.
         */
        public CSG_Model(GameObject go)
        {
            var mesh      = go.GetComponent <MeshFilter>().sharedMesh;
            var transform = go.GetComponent <Transform>();

            vertices = CSG_VertexUtility.GetVertices(mesh).Select(x => transform.TransformVertex(x)).ToList();
            indexes  = new List <int>(mesh.triangles);
        }
예제 #2
0
        /**
         * Converts a CSG_Model to a Unity mesh.
         */
        public Mesh ToMesh()
        {
            var mesh = new Mesh();

            CSG_VertexUtility.SetMesh(mesh, vertices);
            mesh.triangles = indexes.ToArray();
            return(mesh);
        }
예제 #3
0
        // Split `polygon` by this plane if needed, then put the polygon or polygon
        // fragments in the appropriate lists. Coplanar polygons go into either
        // `coplanarFront` or `coplanarBack` depending on their orientation with
        // respect to this plane. Polygons in front or in back of this plane go into
        // either `front` or `back`.
        public void SplitPolygon(CSG_Polygon polygon, List <CSG_Polygon> coplanarFront, List <CSG_Polygon> coplanarBack, List <CSG_Polygon> front, List <CSG_Polygon> back)
        {
            // Classify each point as well as the entire polygon into one of the above
            // four classes.
            EPolygonType        polygonType = 0;
            List <EPolygonType> types       = new List <EPolygonType>();

            for (int i = 0; i < polygon.vertices.Count; i++)
            {
                float        t    = Vector3.Dot(this.normal, polygon.vertices[i].position) - this.w;
                EPolygonType type = (t < -CSG.EPSILON) ? EPolygonType.Back : ((t > CSG.EPSILON) ? EPolygonType.Front : EPolygonType.Coplanar);
                polygonType |= type;
                types.Add(type);
            }

            // Put the polygon in the correct list, splitting it when necessary.
            switch (polygonType)
            {
            case EPolygonType.Coplanar:
            {
                if (Vector3.Dot(this.normal, polygon.plane.normal) > 0)
                {
                    coplanarFront.Add(polygon);
                }
                else
                {
                    coplanarBack.Add(polygon);
                }
            }
            break;

            case EPolygonType.Front:
            {
                front.Add(polygon);
            }
            break;

            case EPolygonType.Back:
            {
                back.Add(polygon);
            }
            break;

            case EPolygonType.Spanning:
            {
                List <CSG_Vertex> f = new List <CSG_Vertex>();
                List <CSG_Vertex> b = new List <CSG_Vertex>();

                for (int i = 0; i < polygon.vertices.Count; i++)
                {
                    int j = (i + 1) % polygon.vertices.Count;

                    EPolygonType ti = types[i], tj = types[j];

                    CSG_Vertex vi = polygon.vertices[i], vj = polygon.vertices[j];

                    if (ti != EPolygonType.Back)
                    {
                        f.Add(vi);
                    }

                    if (ti != EPolygonType.Front)
                    {
                        b.Add(vi);
                    }

                    if ((ti | tj) == EPolygonType.Spanning)
                    {
                        float t = (this.w - Vector3.Dot(this.normal, vi.position)) / Vector3.Dot(this.normal, vj.position - vi.position);

                        CSG_Vertex v = CSG_VertexUtility.Mix(vi, vj, t);

                        f.Add(v);
                        b.Add(v);
                    }
                }

                if (f.Count >= 3)
                {
                    front.Add(new CSG_Polygon(f));
                }

                if (b.Count >= 3)
                {
                    back.Add(new CSG_Polygon(b));
                }
            }
            break;
            }   // End switch(polygonType)
        }
 public CSG_Model(Mesh mesh)
 {
     vertices = CSG_VertexUtility.GetVertices(mesh).ToList();
     indexes  = new List <int>(mesh.triangles);
 }