Ejemplo n.º 1
0
        void DumpOBJ(TelescopeStructure structure, string filename)
        {
            using (StreamWriter file = new StreamWriter(filename))
            {
                // First write all vertices.
                int        vertOffset    = 1;
                List <int> vertexOffsets = new List <int>();
                // Write all vertices for all junctures
                foreach (TelescopeJuncture j in structure.junctures)
                {
                    // Record what index the first vertex should be
                    vertexOffsets.Add(vertOffset);

                    // Write all vertices
                    foreach (Vector3 vLocal in j.JunctureMesh.vertices)
                    {
                        // Write in world space
                        Vector3 v = j.transform.rotation * vLocal + j.transform.position;
                        file.WriteLine("v " + -v.x + " " + v.y + " " + v.z);
                    }

                    // Increment the offset by the vertex count we just added
                    vertOffset += j.JunctureMesh.vertexCount;
                }

                // Write all vertices for all shells
                foreach (TelescopeSegment seg in structure.segments)
                {
                    foreach (TelescopeShell s in seg.shells)
                    {
                        // Record index of first vertex
                        vertexOffsets.Add(vertOffset);

                        // Write all vertices
                        foreach (Vector3 vLocal in s.mesh.vertices)
                        {
                            // World space
                            Vector3 v = s.transform.rotation * vLocal + s.transform.position;
                            file.WriteLine("v " + -v.x + " " + v.y + " " + v.z);
                        }

                        // Increment offset
                        vertOffset += s.mesh.vertexCount;
                    }
                }

                // Now write all faces.
                int currentElement = 0;

                // Write faces for each juncture
                foreach (TelescopeJuncture j in structure.junctures)
                {
                    // Read which vertex we start numbering from
                    int startIndex   = vertexOffsets[currentElement];
                    int numTriangles = j.JunctureMesh.triangles.Length / 3;
                    // Write the faces
                    for (int f = 0; f < numTriangles; f++)
                    {
                        int v1 = j.JunctureMesh.triangles[3 * f + 0] + startIndex;
                        int v2 = j.JunctureMesh.triangles[3 * f + 1] + startIndex;
                        int v3 = j.JunctureMesh.triangles[3 * f + 2] + startIndex;

                        file.WriteLine("f " + v1 + " " + v2 + " " + v3);
                    }

                    currentElement++;
                }

                // Write all faces for shells
                foreach (TelescopeSegment seg in structure.segments)
                {
                    foreach (TelescopeShell s in seg.shells)
                    {
                        int startIndex   = vertexOffsets[currentElement];
                        int numTriangles = s.mesh.triangles.Length / 3;
                        // Write faces
                        for (int f = 0; f < numTriangles; f++)
                        {
                            int v1 = s.mesh.triangles[3 * f + 0] + startIndex;
                            int v2 = s.mesh.triangles[3 * f + 1] + startIndex;
                            int v3 = s.mesh.triangles[3 * f + 2] + startIndex;

                            file.WriteLine("f " + v1 + " " + v2 + " " + v3);
                        }

                        currentElement++;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void TICToTelescope()
        {
            // Create a telescope structure empty object
            GameObject structureObj = new GameObject();

            structureObj.name = "TelescopingStructure";
            TelescopeStructure structure = structureObj.AddComponent <TelescopeStructure>();

            structure.transform.parent = transform;

            // Create dictionary to record created telescope junctures
            Dictionary <DCurveBulb, TelescopeJuncture> bulbDict = new Dictionary <DCurveBulb, TelescopeJuncture>();

            // Create all of the junctures, and hash the original abstract intersections to them
            foreach (DCurveBulb dcb in tiBulbs)
            {
                TelescopeJuncture junct = TelescopeJuncture.CreateJuncture(dcb.transform.position, dcb.radius * Mathf.Sqrt(2));
                bulbDict.Add(dcb, junct);
                structure.junctures.Add(junct);
                junct.transform.parent = structure.transform;
            }

            // Now we need to loop over all telescope segments,
            // create the telescopes, and attach them to the correct junctions.
            foreach (TorsionImpulseCurve tic in tiCurves)
            {
                // Set radius to match the radii at endpoints
                float startRadius = tic.NumSegments * Constants.WALL_THICKNESS + 0.2f
                                    + (tic.ArcLength * Constants.TAPER_SLOPE);

                TelescopeSegment seg;

                // Need to address four cases, corresponding to whether or not
                // the segment is connected to a juncture at the beginning and end.
                // In this step, we just create telescopes with the appropriate radii.
                if (tic.StartJuncture && tic.EndJuncture)
                {
                    if (tic.EndJuncture.radius > tic.StartJuncture.radius)
                    {
                        //startRadius = tic.EndJuncture.radius;
                        startRadius = FindStartRadius(tic.StartJuncture.radius, tic.ArcLength, tic.NumSegments);
                        seg         = tic.MakeTelescope(startRadius, reverse: true);
                    }
                    else
                    {
                        //startRadius = tic.StartJuncture.radius;
                        startRadius = FindStartRadius(tic.EndJuncture.radius, tic.ArcLength, tic.NumSegments);
                        seg         = tic.MakeTelescope(startRadius);
                    }
                }
                else if (tic.StartJuncture && !tic.EndJuncture)
                {
                    startRadius = tic.StartJuncture.radius;
                    seg         = tic.MakeTelescope(startRadius);
                }
                else if (!tic.StartJuncture && tic.EndJuncture)
                {
                    startRadius = tic.EndJuncture.radius;
                    seg         = tic.MakeTelescope(startRadius, reverse: true);
                }
                else
                {
                    seg = tic.MakeTelescope(startRadius);
                }

                seg.transform.parent = structure.transform;
                structure.segments.Add(seg);

                seg.ExtendImmediate(1);

                // Now we actually connect the telescopes.
                // We look up which instantiated telescope object
                // the current segment should be connected to.
                if (tic.StartJuncture)
                {
                    TelescopeJuncture bulb = bulbDict[tic.StartJuncture];
                    seg.keepLocalPositionOnStart = true;
                    seg.SetParent(bulb);

                    bulb.childSegments.Add(seg);
                }
                if (tic.EndJuncture)
                {
                    TelescopeJuncture bulb = bulbDict[tic.EndJuncture];
                    bulb.keepLocalPositionOnStart = true;
                    bulb.SetParentToSegmentEnd(seg);
                }

                // Turn off all of the old objects.
                foreach (DCurveBulb oldBulb in tiBulbs)
                {
                    oldBulb.gameObject.SetActive(false);
                }

                foreach (TorsionImpulseCurve oldTc in tiCurves)
                {
                    oldTc.gameObject.SetActive(false);
                }
            }

            ActiveStructure = structure;
            stage           = CanvasStage.Telescope;
        }