public TubeGeometry(Path path, int segments = 64, float radius = 1, int segmentsRadius = 8, bool closed = false)
            {
                this.path     = path;
                this.segments = segments;
                this.radius   = radius;
                //this.segmentsRadius = segmentsRadius;
                //this.closed = closed;

                numpoints = this.segments + 1;

                /*
                 * var frames = new THREE.TubeGeometry();
                 * frames.FrenetFrames(path, segments, closed);
                 */
                var frames = new THREE.TubeGeometry.FrenetFrames(path, segments, closed);
                //List<Vector3> tangents = frames.tangents;
                List <Vector3> normals   = frames.normals;
                List <Vector3> binormals = frames.binormals;

                // proxy internals
                //this.tangents = tangents;
                this.normals   = normals;
                this.binormals = binormals;

                geo = new ParametricGeometry(paramFunc, segments, segmentsRadius);
            }
Beispiel #2
0
        //void addShape(Shape shape, Option options ) {
        void addShape(ShapeAndHoleObject shapePoints, Option options)
        {
            Shape shape  = shapePoints.baseShape;
            int   amount = options.amount;

            float bevelThickness = options.bevelThickness;
            float bevelSize      = options.bevelSize;
            int   bevelSegments  = options.bevelSegments;
            bool  bevelEnabled   = options.bevelEnabled;
            int   curveSegments  = options.curveSegments;
            int   steps          = options.steps;

            Curve extrudePath = options.extrudePath;

            //Material material = options.material;
            //Material extrudeMaterial = options.extrudeMaterial;

            // Use default WorldUVGenerator if no UV generators are specified.

            WorldUVGenerator uvgen = options.UVGenerator;

            if (uvgen == null)
            {
                uvgen = new WorldUVGenerator();
            }

            List <Vector3> extrudePts    = new List <Vector3>();
            bool           extrudeByPath = false;

            THREE.TubeGeometry.FrenetFrames splineTube = null;
            Vector3 binormal, normal, position2;

            bool isClosePath             = false;

            if (extrudePath != null)
            {
                extrudePts    = extrudePath.getSpacedPoints(steps);
                extrudeByPath = true;
                bevelEnabled  = false;                                               // bevels not supported for path extrusion

                isClosePath = (extrudePath.GetType() == typeof(ClosedSplineCurve3)); // add inok

                // SETUP TNB variables
                // Reuse TNB from TubeGeomtry for now.
                splineTube = options.frames;
                if (splineTube == null)
                {
                    splineTube     = new THREE.TubeGeometry.FrenetFrames(extrudePath, steps, isClosePath);
                    options.frames = splineTube;
                }

                binormal  = new Vector3();
                normal    = new Vector3();
                position2 = new Vector3();
            }
            else
            {
                Debug.Log("no extrudePath");
            }

            // Safeguards if bevels are not enabled
            if (!bevelEnabled)
            {
                bevelSegments  = 0;
                bevelThickness = 0;
                bevelSize      = 0;
            }

            // Variables initalization

            //var ahole, h, hl; // looping of holes
            List <Vector3> ahole;

            //int shapesOffset = this.vertices.Count;
            int shapesOffset             = vertIndex;

//			ShapeAndHoleObject shapePoints = shape.extractPoints( curveSegments );
//
//			List<Vector3> vertices = shapePoints.shape;
//			List<List<Vector3>> holes = shapePoints.holes;
//
//			bool reverse = !Shape.Utils.isClockWise( vertices ) ;
//
//			if ( reverse ) {
//				vertices.Reverse();
//
//				for (int h = 0, hl = holes.Count; h < hl; h ++ ) {
//					ahole = holes[ h ];
//
//					if ( Shape.Utils.isClockWise( ahole ) ) {
//						ahole.Reverse();
//						holes[ h ] = ahole;
//					}
//				}
//			}
            List <Vector3>         vertices = shapePoints.shapeVertices;
            List <List <Vector3> > holes = shapePoints.holes;
            bool reverse = shapePoints.reverse;

            List <List <int> > faces = Shape.Utils.triangulateShape(vertices, holes);

            /* Vertices */
            //List<Vector3> contour = vertices; // vertices has all points but contour has only points of circumference
            List <Vector3> contour = new List <Vector3>();           // 上記だとholeが上手くいかない。改良の余地あり

            contour.AddRange(vertices);

            for (int h = 0, hl = holes.Count; h < hl; h++)
            {
                ahole = holes[h];
                vertices.AddRange(ahole);
            }


            float t;
            float z, bs;

            Vector3 vert;
            int     vlen = vertices.Count;
            //Face3 face;

            int flen = faces.Count;

            List <Vector3> contourMovements = new List <Vector3>();

            for (int i = 0, il = contour.Count, j = il - 1, k = i + 1; i < il; i++, j++, k++)
            {
                if (j == il)
                {
                    j = 0;
                }
                if (k == il)
                {
                    k = 0;
                }

                //  (j)---(i)---(k)
                Vector3 bevelVec = getBevelVec(contour[i], contour[j], contour[k]);
                contourMovements.Add(bevelVec);
                //contourMovements[ i ] = bevelVec;
            }

            List <List <Vector3> > holesMovements = new List <List <Vector3> >();
            List <Vector3>         oneHoleMovements;

            //verticesMovements = contourMovements.concat(); // TODO: Check /////////
            List <Vector3> verticesMovements = new List <Vector3>();

            verticesMovements.AddRange(contourMovements);               // COPY????

            //List<Vector3> ahole;
            for (int h = 0, hl = holes.Count; h < hl; h++)
            {
                ahole = holes[h];

                oneHoleMovements = new List <Vector3>();

                for (int i = 0, il = ahole.Count, j = il - 1, k = i + 1; i < il; i++, j++, k++)
                {
                    if (j == il)
                    {
                        j = 0;
                    }
                    if (k == il)
                    {
                        k = 0;
                    }

                    //  (j)---(i)---(k)
                    //oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
                    Vector3 bevelVec = getBevelVec(ahole[i], ahole[j], ahole[k]);
                    oneHoleMovements.Add(bevelVec);
                }

                holesMovements.Add(oneHoleMovements);
                verticesMovements.AddRange(oneHoleMovements);
            }

            // Loop bevelSegments, 1 for the front, 1 for the back
            for (int b = 0; b < bevelSegments; b++)
            {
                t = (float)b / bevelSegments;
                z = bevelThickness * (1 - t);

                bs = bevelSize * (Mathf.Sin(t * Mathf.PI / 2));                     // curved

                // contract shape
                for (int i = 0, il = contour.Count; i < il; i++)
                {
                    vert = scalePt2(contour[i], contourMovements[i], bs);

                    addVertex(vert.x, vert.y, -z);
                }

                // expand holes
                for (int h = 0, hl = holes.Count; h < hl; h++)
                {
                    ahole            = holes[h];
                    oneHoleMovements = holesMovements[h];

                    for (int i = 0, il = ahole.Count; i < il; i++)
                    {
                        vert = scalePt2(ahole[i], oneHoleMovements[i], bs);

                        addVertex(vert.x, vert.y, -z);
                    }
                }
            }

            bs = bevelSize;

            // Back facing vertices
            for (int i = 0; i < vlen; i++)
            {
                vert = bevelEnabled ? scalePt2(vertices[i], verticesMovements[i], bs) : vertices[i];

                if (!extrudeByPath)
                {
                    addVertex(vert.x, vert.y, 0);
                }
                else
                {
                    normal   = splineTube.normals[0] * (vert.x);
                    binormal = splineTube.binormals[0] * (vert.y);

                    position2 = (extrudePts[0]) + (normal) + (binormal);

                    addVertex(position2.x, position2.y, position2.z);
                }
            }

            // Add stepped vertices...
            // Including front facing vertices

            for (int s = 1; s <= steps; s++)
            {
                for (int i = 0; i < vlen; i++)
                {
                    vert = bevelEnabled ? scalePt2(vertices[i], verticesMovements[i], bs) : vertices[i];

                    if (!extrudeByPath)
                    {
                        addVertex(vert.x, vert.y, (float)amount / steps * s);
                    }
                    else
                    {
                        normal    = splineTube.normals[s] * (vert.x);
                        binormal  = (splineTube.binormals[s]) * (vert.y);
                        position2 = (extrudePts[s]) + (normal) + (binormal);

                        addVertex(position2.x, position2.y, position2.z);
                    }
                }
            }


            // Add bevel segments planes
            for (int b = bevelSegments - 1; b >= 0; b--)
            {
                t  = (float)b / bevelSegments;
                z  = bevelThickness * (1.0f - t);
                bs = bevelSize * Mathf.Sin(t * Mathf.PI / 2.0f);

                // contract shape
                for (int i = 0, il = contour.Count; i < il; i++)
                {
                    vert = scalePt2(contour[i], contourMovements[i], bs);
                    addVertex(vert.x, vert.y, (float)amount + z);
                }

                // expand holes
                for (int h = 0, hl = holes.Count; h < hl; h++)
                {
                    ahole            = holes[h];
                    oneHoleMovements = holesMovements[h];

                    for (int i = 0, il = ahole.Count; i < il; i++)
                    {
                        vert = scalePt2(ahole[i], oneHoleMovements[i], bs);

                        if (!extrudeByPath)
                        {
                            addVertex(vert.x, vert.y, (float)amount + z);
                        }
                        else
                        {
                            addVertex(vert.x, vert.y + extrudePts[steps - 1].y, extrudePts[steps - 1].x + z);
                        }
                    }
                }
            }


            /* Faces */
            faceIndex = 0;
            // Top and bottom faces
            if (!isClosePath)
            {
                buildLidFaces(vlen, faces, flen, steps, bevelSegments, bevelEnabled, shapesOffset, shape, uvgen, options, reverse);
            }
            // Sides faces
            buildSideFaces(vlen, contour, holes, steps, bevelSegments, shapesOffset, uvgen, shape, options, isClosePath, reverse);
        }
Beispiel #3
0
        public TubeGeometry(Curve path, int segments = 64, float radius = 1, int radialSegments = 8, bool closed = false)
        {
            //this.path = path;

            List <List <int> > grid = new List <List <int> > ();

            //Vector3 tangent;
            Vector3 normal;
            Vector3 binormal;

            int numpoints = segments + 1;

            //float x, y, z;
            //float tx, ty, tz;
            float u, v;

            float   cx, cy;
            Vector3 pos, pos2 = new Vector3();
            int     ip, jp;
            int     a, b, c, d;
            Vector2 uva, uvb, uvc, uvd;


            var frames = new THREE.TubeGeometry.FrenetFrames(path, segments, closed);

//			tangents = frames.tangents;
//			normals = frames.normals;
//			binormals = frames.binormals;

            // proxy internals
            this.tangents  = frames.tangents;
            this.normals   = frames.normals;
            this.binormals = frames.binormals;

//		function vert( x, y, z ) {
//
//			return scope.vertices.push( new THREE.Vector3( x, y, z ) ) - 1;
//
//		}

            // consruct the grid

            for (int i = 0; i < numpoints; i++)
            {
                //grid[ i ] = [];
                grid.Add(new List <int> ());

                u = (float)i / (numpoints - 1);

                pos = path.getPointAt(u);

                //tangent = tangents [i];
                normal   = normals [i];
                binormal = binormals [i];

                for (int j = 0; j < radialSegments; j++)
                {
                    v = (float)j / radialSegments * 2.0f * Mathf.PI;

                    cx = -radius *Mathf.Cos(v);                       // TODO: Hack: Negating it so it faces outside.

                    cy = radius * Mathf.Sin(v);

                    //pos2.copy( pos );
                    pos2    = (pos);
                    pos2.x += cx * normal.x + cy * binormal.x;
                    pos2.y += cx * normal.y + cy * binormal.y;
                    pos2.z += cx * normal.z + cy * binormal.z;

                    //grid[ i ][ j ] = vert( pos2.x, pos2.y, pos2.z );
                    grid [i].Add(vert(pos2.x, pos2.y, pos2.z));
                }
            }

            // construct the mesh

            for (int i = 0; i < segments; i++)
            {
                for (int j = 0; j < radialSegments; j++)
                {
                    ip = (closed) ? (i + 1) % segments : i + 1;
                    jp = (j + 1) % radialSegments;

                    a = grid[i][j];                                     // *** NOT NECESSARILY PLANAR ! ***
                    b = grid[ip][j];
                    c = grid[ip][jp];
                    d = grid[i][jp];

                    uva = new Vector2((float)i / segments, (float)j / radialSegments);
                    uvb = new Vector2((float)(i + 1) / segments, (float)j / radialSegments);
                    uvc = new Vector2((float)(i + 1) / segments, (float)(j + 1) / radialSegments);
                    uvd = new Vector2((float)i / segments, (float)(j + 1) / radialSegments);

                    this.faces.Add(new Face3(a, b, d));
                    this.faceVertexUvs.Add(new List <Vector2>(new Vector2[] { uva, uvb, uvd }));

                    this.faces.Add(new Face3(b, c, d));
                    this.faceVertexUvs.Add(new List <Vector2>(new Vector2[] { uvb, uvc, uvd }));
                }
            }

            //this.computeFaceNormals();
            //this.computeVertexNormals();
        }