Ejemplo n.º 1
0
        public static CutResult Run(
            GameObject target,
            CuttingTemplate template
            )
        {
            Mesh          mesh = target.GetComponent <MeshFilter>().mesh;
            MeshPart      pos = new MeshPart(true), neg = new MeshPart(false);
            RingGenerator intersection_ring = new RingGenerator();

            Vector3[] vertices              = mesh.vertices;
            Vector3[] normals               = mesh.normals;
            int[]     triangles             = mesh.triangles;

            bool addNormals = normals.Length > 0;

            // Debug.Log(vertices.Length + " verts, " + triangles.Length / 3 + " tris, " + template.points.Count + "tpl. points");

            // divide mesh in half by vertices
            int i = 0;

            foreach (Vector3 vertex in vertices)
            {
                if (template.IsAbove(vertex))
                {
                    pos.indexMap.Add(i, pos.vertices.Count);
                    pos.vertices.Add(vertex);
                    if (addNormals)
                    {
                        pos.normals.Add(normals[i]);
                    }
                }
                else
                {
                    neg.indexMap.Add(i, neg.vertices.Count);
                    neg.vertices.Add(vertex);
                    if (addNormals)
                    {
                        neg.normals.Add(normals[i]);
                    }
                }
                i++;
            }

            // RingGen's are associated to first template point
            //   these define the rings used for generating strip-wise inner geometry
            // SortedDictionary are for connecting edges of strip-wise inner geometry
            //   Sorted by signed distance to template plane, so they get connected in the right order
            Dictionary <Vector3, Tuple <SortedDictionary <float, Vector3>, RingGen> > point_data = new Dictionary <Vector3, Tuple <SortedDictionary <float, Vector3>, RingGen> >();

            foreach (Vector3 point in template.points)
            {
                point_data.Add(point, new Tuple <SortedDictionary <float, Vector3>, RingGen>(new SortedDictionary <float, Vector3>(), new RingGen()));
            }

            IvSaver ivSaver = new IvSaver();

            // put triangles in correct mesh
            for (i = 0; i < triangles.Length; i += 3)
            {
                // find orignal indices
                int i_a = triangles[i],
                    i_b = triangles[i + 1],
                    i_c = triangles[i + 2];

                // find original verticies
                Vector3 a = vertices[i_a],
                        b = vertices[i_b],
                        c = vertices[i_c];

                // seperation check
                if (!ProcessTriangle(template, a, b, c, point_data, pos, neg, intersection_ring, ivSaver, addNormals))
                {
                    if (template.IsAbove(a))
                    {
                        // triangle above plane
                        pos.indices.Add(pos.indexMap[i_a]);
                        pos.indices.Add(pos.indexMap[i_b]);
                        pos.indices.Add(pos.indexMap[i_c]);
                    }
                    else
                    {
                        // triangle below plane
                        neg.indices.Add(neg.indexMap[i_a]);
                        neg.indices.Add(neg.indexMap[i_b]);
                        neg.indices.Add(neg.indexMap[i_c]);
                    }
                }
            }

            // Generate strip (inner) geometry
            SortedDictionary <float, Vector3> next_dist_map = point_data[template.points[template.isClosed ? 0 : template.points.Count - 1]].Item1;
            Vector3 next_point = template.isClosed ? template.points.First() : template.points.Last();

            for (i = template.points.Count - (template.isClosed ? 1 : 2); i >= 0; i--)
            {
                SortedDictionary <float, Vector3> dist_map = point_data[template.points[i]].Item1;
                Vector3 point = template.points[i];
                //Debugging.LogLine(template.points[i-1],template.normal);
                //Debugging.LogLine(template.points[i],template.normal);
                //Debugging.LogList(point_data[template.points[i-1]].Item1.Keys);
                //Debugging.LogList(point_data[template.points[i]].Item1.Keys);
                RingGen rg = point_data[template.points[i]].Item2;
                // Debug.Log("points: "+point+" -> "+next_point);
                //rg.MyDebugLog();
                rg.TemplateJoin(template, dist_map);
                rg.TemplateJoin(template, next_dist_map);
                //rg.MyDebugLog();
                foreach (Ring ring in rg.GetRings(false, false))
                {
                    //Debugging.DebugRing(ring.verts);
                    Vector3 normal = Vector3.Cross(template.normal, next_point - point);
                    GenerateRingMesh(ring.verts, pos, normal, true, null, addNormals);
                    //TmpGen(ring.verts,pos,normal);
                    GenerateRingMesh(ring.verts, neg, normal, false, null, addNormals);
                    //TmpGen(ring.verts,neg,normal,true);
                }
                // Debug.Log("---------------------------");
                next_dist_map = dist_map;
                next_point    = point;
            }

            // Debug.Log("template: "+template);

            return(new CutResult(target, Vector3.zero, intersection_ring.GetRings(false, false), pos, neg));
        }
Ejemplo n.º 2
0
 private void Start()
 {
     rg  = GameObject.Find("RingGen").GetComponent <RingGen>();
     uic = GameObject.Find("UIController").GetComponent <UIcontroller>();
 }