Beispiel #1
0
        private static void CreateFace()
        {
            if (Selection.objects.Length == 3)
            {
                var vertices = Utilities.Get3FromSelection <Vertex>();
                if (vertices.Item1 != null && vertices.Item2 != null && vertices.Item3 != null)
                {
                    var vertexArray = new[] { vertices.Item1, vertices.Item2, vertices.Item3 };
                    Utilities.ConnectUnconnectedVertices(vertexArray);
                    vertexArray = vertexArray.OrderBy(v => v.Index).ToArray();

                    Face3.Create(vertices.Item1.GeometryEditor, new Triangle(vertexArray[0], vertexArray[1], vertexArray[2]));
                }
            }
            else if (Selection.objects.Length == 4)
            {
                var vertices = Utilities.Get4FromSelection <Vertex>();
                if (vertices.Item1 != null && vertices.Item2 != null && vertices.Item3 != null && vertices.Item4 != null)
                {
                    var vertexArray = new[] { vertices.Item1, vertices.Item2, vertices.Item3, vertices.Item4 };
                    Utilities.ConnectUnconnectedVertices(vertexArray);
                    vertexArray = vertexArray.OrderBy(v => v.Index).ToArray();


                    Face4.Create(vertices.Item1.GeometryEditor, new Triangle(vertexArray[0], vertexArray[1], vertexArray[2]), new Triangle(vertexArray[3], vertexArray[2], vertexArray[1]));
                }
            }
        }
Beispiel #2
0
        public double faceArea(Face3 face, out Vector3 n)
        {
            n = doubleFaceArea(face);
            var area = n.Length / 2.0;

            n.Unitize();
            return(area);
        }
Beispiel #3
0
 public IntersectionInfo(float distance, Vector3 intersectionPoint, Face3 face, int faceIndex, Object3D o)
 {
     Distance          = distance;
     IntersectionPoint = intersectionPoint;
     Face      = face;
     FaceIndex = faceIndex;
     Object    = o;
 }
Beispiel #4
0
        /// <summary>
        /// determine the raw face normal whose length is double the area of the mesh face
        /// <param name="face">mesh face for which to calculate the area</param>
        /// <returns>normal vector to face whose length is double the face area</returns>
        /// </summary>
        public Vector3 doubleFaceArea(Face3 face)
        {
            var res = DoubleTriangleArea(face.A, face.B, face.C);

            if (face.IsQuad)
            {
                res += DoubleTriangleArea(face.A, face.C, face.D);
            }
            return(res.Convert());
        }
Beispiel #5
0
		/// <summary>
		/// Initializes a new instance of the <see cref="libtr.TR2.RoomData"/> struct.
		/// </summary>
		/// <param name="vertices">The vertices.</param>
		/// <param name="rectangles">The textured rectangles.</param>
		/// <param name="triangles">The textured triangles.</param>
		/// <param name="sprites">The sprites.</param>
		public RoomData (
			RoomVertex [] vertices, Face4 [] rectangles,
			Face3 [] triangles, RoomSprite [] sprites) : this () {
			VertexCount = (Int16) vertices.Length;
			RectangleCount = (Int16) rectangles.Length;
			TriangleCount = (Int16) triangles.Length;
			SpriteCount = (Int16) sprites.Length;
			Vertices = vertices;
			Rectangles = rectangles;
			Triangles = triangles;
			Sprites = sprites;
		}
Beispiel #6
0
 public static bool TryParse(string line, out IFace face)
 {
     if (Face3.TryParse(line, out face))
     {
         return(true);
     }
     if (Face4.TryParse(line, out face))
     {
         return(true);
     }
     return(false);
 }
        /// <summary>
        /// Analytically subdivide a face to the required detail level.
        /// </summary>
        /// <param name="face"></param>
        /// <param name="detail"></param>
        private void Subdivide(Face3 face, float detail)
        {
            var cols  = System.Math.Pow(2, detail);
            var cells = System.Math.Pow(4, detail);
            var a     = Prepare(this.Vertices[face.a]);
            var b     = Prepare(this.Vertices[face.b]);
            var c     = Prepare(this.Vertices[face.c]);

            var v = new List <List <Vector3> >();

            // Construct all of the vertices for this subdivision.

            for (var i = 0; i <= cols; i++)
            {
                v.Add(new List <Vector3>());

                var aj   = Prepare((Vector3)a.Clone()).Lerp(c, i / (float)cols);
                var bj   = Prepare((Vector3)b.Clone()).Lerp(c, i / (float)cols);
                var rows = cols - i;

                for (var j = 0; j <= rows; j++)
                {
                    if (j == 0 && i == cols)
                    {
                        v[i].Add(aj);
                    }
                    else
                    {
                        v[i].Add(Prepare(((Vector3)aj.Clone()).Lerp(bj, j / (float)rows)));
                    }
                }
            }

            // Construct all of the faces.

            for (var i = 0; i < cols; i++)
            {
                for (var j = 0; j < 2 * (cols - i) - 1; j++)
                {
                    var k = (int)System.Math.Floor(j / 2.0f);
                    if (j % 2 == 0)
                    {
                        Make(v[i][k + 1], v[i + 1][k], v[i][k]);
                    }
                    else
                    {
                        Make(v[i][k + 1], v[i + 1][k + 1], v[i + 1][k]);
                    }
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// calculate the normal of the face.
        /// works for triangles and quads?
        /// </summary>
        /// <param name="face"></param>
        /// <returns></returns>
        public Vector3 faceNormal(Face3 face)
        {
            var    p1  = Vertices[face.A];
            var    p2  = Vertices[face.B];
            var    p3  = Vertices[face.C];
            var    v1  = p2 - p1;
            var    v2  = p3 - p1;
            var    res = Vector3.CrossProduct(v1, v2);
            double l   = res.Length;

            if (l != 0.0)
            {
                res /= l;
            }
            return(res);
        }
Beispiel #9
0
        public void Render_DrawStepwiseMesh()
        {
            ShellSecState state = Utilities.MakeState_ElementBased();

            ShellSec_RendererInfo info = new ShellSec_RendererInfo_Force();

            info.DisplayResults = new Dictionary <ShellSecResult, bool> {
                { ShellSecResult.M_nn, true }
            };
            info.ScaleResults = new Dictionary <ShellSecResult, double> {
                { ShellSecResult.M_nn, 1.0 }
            };
            info.DisplaySmooth = false;
            info.DisplayMesh   = true;


            var expectedVertices = new List <Point3>()
            {
                new Point3(0, 0, 1),
                new Point3(0, 0, 0),
                new Point3(0.5, 0, 1),
                new Point3(0.5, 0, 0),
                new Point3(0.5, 0, 2),
                new Point3(0.5, 0, 0),
                new Point3(1, 0, 2),
                new Point3(1, 0, 0),
            };
            var expectedFaces = new Face3[]
            {
                new Face3(0, 1, 3, 2),
                new Face3(4, 5, 7, 6)
            };



            var sut = new ShellSec_MeshRenderer();

            sut.Render(state, info);
            List <IMesh> output = sut.RenderedMeshes;



            Assert.AreEqual(output.Count, 1);
            Assert.That(output[0].Vertices, Is.EqualTo(expectedVertices));
            Assert.That(output[0].Faces, Is.EqualTo(expectedFaces));
        }
Beispiel #10
0
        /// <summary>
        /// Approximate a curved face with recursively sub-divided triangles.
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <param name="v3"></param>
        private void Make(Vector3 v1, Vector3 v2, Vector3 v3)
        {
            var face = new Face3(((UserData)v1.UserData).index, ((UserData)v2.UserData).index, ((UserData)v3.UserData).index);

            face.VertexNormals.Add((Vector3)v1.Clone());
            face.VertexNormals.Add((Vector3)v2.Clone());
            face.VertexNormals.Add((Vector3)v3.Clone());
            this.Faces.Add(face);

            _centroid.Copy(v1).Add(v2).Add(v3).DivideScalar(3);

            var azi = Azimuth(_centroid);

            this.FaceVertexUvs[0].Add(new List <Vector2>
            {
                CorrectUV(((UserData)v1.UserData).uv, v1, azi),
                CorrectUV(((UserData)v2.UserData).uv, v2, azi),
                CorrectUV(((UserData)v2.UserData).uv, v3, azi)
            });
        }
Beispiel #11
0
        // Approximate a curved face with recursively sub-divided triangles.
        private void Make(VertexInfo v1, VertexInfo v2, VertexInfo v3)
        {
            var face = new Face3(v1.Index, v2.Index, v3.Index, v1.Position, v2.Position, v3.Position);

            faces.Add(face);

            var centroid = v1.Position;

            centroid.Add(v2.Position);
            centroid.Add(v3.Position);
            centroid.Divide(3);

            var azi     = Azimuth(centroid);
            var uvA     = CorrectUV(v1.UV, v1.Position, azi);
            var uvB     = CorrectUV(v2.UV, v2.Position, azi);
            var uvC     = CorrectUV(v3.UV, v3.Position, azi);
            var faceSet = new UVFaceSet(uvA, uvB, uvC);
            var uvs     = faceVertexUvs[0];

            uvs.Add(faceSet);
        }
Beispiel #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="vertices"></param>
        /// <param name="indices"></param>
        /// <param name="radius"></param>
        /// <param name="detail"></param>
        protected void Construct(IList <float> vertices, IList <int> indices, float radius, float detail)
        {
            var i = 0; var j = 0; var l = 0;

            for (i = 0; i < vertices.Count; i += 3)
            {
                this.Prepare(new Vector3(vertices[i], vertices[i + 1], vertices[i + 2]));
            }

            var faces = new List <Face3>();

            for (i = 0, j = 0, l = indices.Count; i < l; i += 3, j++)
            {
                var v1 = this.Vertices[indices[i + 0]];
                var v2 = this.Vertices[indices[i + 1]];
                var v3 = this.Vertices[indices[i + 2]];

                var face = new Face3(((UserData)v1.UserData).index, ((UserData)v2.UserData).index, ((UserData)v3.UserData).index);
                face.VertexNormals.Add((Vector3)v1.Clone());
                face.VertexNormals.Add((Vector3)v2.Clone());
                face.VertexNormals.Add((Vector3)v3.Clone());

                faces.Add(face);
            }

            _centroid = new Vector3();

            for (i = 0; i < faces.Count; i++)
            {
                Subdivide(faces[i], detail);
            }

            // Handle case when face straddles the seam

            for (i = 0; i < this.FaceVertexUvs[0].Count; i++)
            {
                var uvs = this.FaceVertexUvs[0][i];

                var x0 = uvs[0].X;
                var x1 = uvs[1].X;
                var x2 = uvs[2].X;

                var max = System.Math.Max(x0, System.Math.Max(x1, x2));
                var min = System.Math.Min(x0, System.Math.Min(x1, x2));

                if (max > 0.9 && min < 0.1)
                { // 0.9 is somewhat arbitrary
                    if (x0 < 0.2)
                    {
                        uvs[0].X += 1;
                    }
                    if (x1 < 0.2)
                    {
                        uvs[1].X += 1;
                    }
                    if (x2 < 0.2)
                    {
                        uvs[2].X += 1;
                    }
                }
            }

            // Apply radius

            for (i = 0; i < this.Vertices.Count; i++)
            {
                this.Vertices[i].MultiplyScalar(radius);
            }

            // Merge vertices

            this.MergeVertices();

            this.ComputeFaceNormals();

            this.BoundingSphere = new Sphere(new Vector3(), radius);
        }
Beispiel #13
0
        /// <summary>
        /// </summary>
        /// <param name="u"></param>
        /// <param name="v"></param>
        /// <param name="udir"></param>
        /// <param name="vdir"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="depth"></param>
        /// <param name="materialIndex"></param>
        private void BuildPlane(
            string u,
            string v,
            int udir,
            int vdir,
            float width,
            float height,
            float depth,
            int materialIndex)
        {
            var w          = string.Empty;
            var gridX      = this.widthSegments;
            var gridY      = this.heightSegments;
            var widthHalf  = width / 2;
            var heightHalf = height / 2;
            var offset     = this.Vertices.Count;

            if ((u == "x" && v == "y") || (u == "y" && v == "x"))
            {
                w = "z";
            }
            else if ((u == "x" && v == "z") || (u == "z" && v == "x"))
            {
                w     = "y";
                gridY = this.depthSegments;
            }
            else if ((u == "z" && v == "y") || (u == "y" && v == "z"))
            {
                w     = "x";
                gridX = this.depthSegments;
            }

            var gridX1        = gridX + 1;
            var gridY1        = gridY + 1;
            var segmentWidth  = width / gridX;
            var segmentHeight = height / gridY;
            var normal        = new Vector3().SetValue(w, depth > 0 ? 1 : -1);

            for (var iy = 0; iy < gridY1; iy++)
            {
                for (var ix = 0; ix < gridX1; ix++)
                {
                    var vector = new Vector3();
                    vector.SetValue(u, (ix * segmentWidth - widthHalf) * udir);
                    vector.SetValue(v, (iy * segmentHeight - heightHalf) * vdir);
                    vector.SetValue(w, depth);

                    this.Vertices.Add(vector);
                }
            }

            //if (this.FaceVertexUvs.Count < 1)
            //    this.FaceVertexUvs.Add(new List<List<Vector2>>());
            Debug.Assert(this.FaceVertexUvs.Count == 1, "Should only be 1 element at this stage");

            for (var iy = 0; iy < gridY; iy++)
            {
                for (var ix = 0; ix < gridX; ix++)
                {
                    var a = ix + gridX1 * iy;
                    var b = ix + gridX1 * (iy + 1);
                    var c = (ix + 1) + gridX1 * (iy + 1);
                    var d = (ix + 1) + gridX1 * iy;

                    var uva = new Vector2(ix / gridX, 1 - iy / gridY);
                    var uvb = new Vector2(ix / gridX, 1 - (iy + 1) / gridY);
                    var uvc = new Vector2((ix + 1) / gridX, 1 - (iy + 1) / gridY);
                    var uvd = new Vector2((ix + 1) / gridX, 1 - iy / gridY);

                    var face = new Face3(a + offset, b + offset, d + offset, new Vector3().One(), Color.White);
                    face.Normal = normal;
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.MaterialIndex = materialIndex;

                    this.Faces.Add(face);
                    {
                        var uvs = new List <Vector2> {
                            uva, uvb, uvd
                        };
                        this.FaceVertexUvs[0].Add(uvs);
                    }

                    face        = new Face3(b + offset, c + offset, d + offset, new Vector3().One(), Color.White);
                    face.Normal = normal;
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.MaterialIndex = materialIndex;

                    this.Faces.Add(face);
                    {
                        var uvs = new List <Vector2> {
                            (Vector2)uvb.Clone(), uvc, (Vector2)uvd.Clone()
                        };
                        this.FaceVertexUvs[0].Add(uvs);
                    }
                }
            }

            this.MergeVertices();
        }
Beispiel #14
0
        // Analytically subdivide a face to the required detail level.
        private void Subdivide(List <VertexInfo> vertexSet, Face3 face, int detail)
        {
            var cols  = Mathf.Pow(2, detail);
            var cells = Mathf.Pow(4, detail);
            var a     = Prepare(vertexSet[face.A].Position, vertexSet);
            var b     = Prepare(vertexSet[face.B].Position, vertexSet);
            var c     = Prepare(vertexSet[face.C].Position, vertexSet);
            var v     = new List <List <VertexInfo> >();

            // Construct all of the vertices for this subdivision.

            for (var i = 0; i <= cols; i++)
            {
                v.Add(new List <VertexInfo>());

                var offset = i / (float)cols;

                var aV = a.Position;
                aV.Lerp(c.Position, offset);
                var aj = Prepare(aV, vertexSet);

                var bV = b.Position;
                bV.Lerp(c.Position, offset);
                var bj = Prepare(bV, vertexSet);

                var rows = cols - i;

                for (var j = 0; j <= rows; j++)
                {
                    if (j == 0 && i == cols)
                    {
                        v[i].Add(aj);
                    }
                    else
                    {
                        var ajV = aj.Position;
                        ajV.Lerp(bj.Position, j / (float)rows);
                        v[i].Add(Prepare(ajV, vertexSet));
                    }
                }
            }

            // Construct all of the faces.
            for (var i = 0; i < cols; i++)
            {
                for (var j = 0; j < 2 * (cols - i) - 1; j++)
                {
                    var k = Mathf.Floor(j / 2);
                    if (j % 2 == 0)
                    {
                        Make(
                            v[i][k + 1],
                            v[i + 1][k],
                            v[i][k]
                            );
                    }
                    else
                    {
                        Make(
                            v[i][k + 1],
                            v[i + 1][k + 1],
                            v[i + 1][k]
                            );
                    }
                }
            }
        }
Beispiel #15
0
 public bool AddFace(Face3 face)
 {
     mesh.Faces.AddFace(face.A, face.B, face.C, face.D);
     return(true);
 }
Beispiel #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="radius"></param>
        /// <param name="widthSegments"></param>
        /// <param name="heightSegments"></param>
        /// <param name="phiStart"></param>
        /// <param name="phiLength"></param>
        /// <param name="thetaStart"></param>
        /// <param name="thetaLength"></param>
        public SphereGeometry(float radius = 50, int widthSegments = 8, int heightSegments = 6, float phiStart = 0, float phiLength = (float)Mat.PI2, float thetaStart = 0, float thetaLength = (float)Math.PI)
        {
            Debug.Assert(this.FaceVertexUvs.Count == 1, "Should only be 1 element at this stage");

            this.Radius = radius;

            this.WidthSegments  = widthSegments;
            this.HeightSegments = heightSegments;

            this.PhiStart  = phiStart;
            this.PhiLength = phiLength;

            this.ThetaStart  = thetaStart;
            this.ThetaLength = thetaLength;

            var uvs      = new List <List <Vector2> >();
            var vertices = new List <List <int> >();

            for (var y = 0; y <= heightSegments; y++)
            {
                var verticesRow = new List <int>();
                var uvsRow      = new List <Vector2>();

                for (var x = 0; x <= widthSegments; x++)
                {
                    var u = x / (float)widthSegments;
                    var v = y / (float)heightSegments;

                    var vertex = new Vector3();
                    vertex.X = -radius * (float)Math.Cos(phiStart + u * phiLength) * (float)Math.Sin(thetaStart + v * thetaLength);
                    vertex.Y = radius * (float)Math.Cos(thetaStart + v * thetaLength);
                    vertex.Z = radius * (float)Math.Sin(phiStart + u * phiLength) * (float)Math.Sin(thetaStart + v * thetaLength);

                    this.Vertices.Add(vertex);

                    verticesRow.Add(this.Vertices.Count - 1);
                    uvsRow.Add(new Vector2(u, 1 - v));
                }

                vertices.Add(verticesRow);
                uvs.Add(uvsRow);
            }

            for (var y = 0; y < heightSegments; y++)
            {
                for (var x = 0; x < widthSegments; x++)
                {
                    var v1 = vertices[y][x + 1];
                    var v2 = vertices[y][x];
                    var v3 = vertices[y + 1][x];
                    var v4 = vertices[y + 1][x + 1];

                    var n1 = ((Vector3)this.Vertices[v1].Clone()).Normalize();
                    var n2 = ((Vector3)this.Vertices[v2].Clone()).Normalize();
                    var n3 = ((Vector3)this.Vertices[v3].Clone()).Normalize();
                    var n4 = ((Vector3)this.Vertices[v4].Clone()).Normalize();

                    var uv1 = (Vector2)uvs[y][x + 1].Clone();
                    var uv2 = (Vector2)uvs[y][x].Clone();
                    var uv3 = (Vector2)uvs[y + 1][x].Clone();
                    var uv4 = (Vector2)uvs[y + 1][x + 1].Clone();

                    if (Math.Abs(this.Vertices[v1].Y) == radius)
                    {
                        uv1.X = (uv1.X + uv2.X) / 2;

                        var face = new Face3(v1, v3, v4);
                        face.VertexNormals.Add(n1);
                        face.VertexNormals.Add(n3);
                        face.VertexNormals.Add(n4);
                        this.Faces.Add(face);

                        this.FaceVertexUvs[0].Add(new List <Vector2> {
                            uv1, uv3, uv4
                        });
                    }
                    else if (Math.Abs(this.Vertices[v3].Y) == radius)
                    {
                        uv3.X = (uv3.X + uv4.X) / 2;

                        var face = new Face3(v1, v2, v3);
                        face.VertexNormals.Add(n1);
                        face.VertexNormals.Add(n2);
                        face.VertexNormals.Add(n3);
                        this.Faces.Add(face);

                        this.FaceVertexUvs[0].Add(new List <Vector2> {
                            uv1, uv2, uv3
                        });
                    }
                    else
                    {
                        var face = new Face3(v1, v2, v4);
                        this.Faces.Add(face);
                        face.VertexNormals.Add(n1);
                        face.VertexNormals.Add(n2);
                        face.VertexNormals.Add(n4);

                        this.FaceVertexUvs[0].Add(new List <Vector2> {
                            uv1, uv2, uv4
                        });

                        face = new Face3(v2, v3, v4);
                        this.Faces.Add(face);
                        face.VertexNormals.Add((Vector3)n2.Clone());
                        face.VertexNormals.Add(n3);
                        face.VertexNormals.Add((Vector3)n4.Clone());

                        this.FaceVertexUvs[0].Add(new List <Vector2> {
                            (Vector2)uv2.Clone(), uv3, (Vector2)uv4.Clone()
                        });
                    }
                }
            }

            this.ComputeFaceNormals();

            this.BoundingSphere = new Sphere(new Vector3(), radius);
        }
 public double CalculateAreaOfTopPyramid()
 {
     Console.WriteLine("I am inside Area of CalculateAreaOfPyramid Calculation");
     return(Face1.AreaOfTriangle() + Face2.AreaOfTriangle() + Face3.AreaOfTriangle() + Face4.AreaOfTriangle());
 }
Beispiel #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="radius"></param>
        /// <param name="tube"></param>
        /// <param name="radialSegments"></param>
        /// <param name="tubularSegments"></param>
        /// <param name="arc"></param>
        public TorusGeometry(float radius = 100, float tube = 40, int radialSegments = 8, int tubularSegments = 6, float arc = (float)Mat.PI2)
        {
            this.Radius          = radius;
            this.Tube            = tube;
            this.RadialSegments  = radialSegments;
            this.TubularSegments = tubularSegments;
            this.Arc             = arc;

            var center = new Vector3(); var uvs = new List <Vector2>(); var normals = new List <Vector3>();

            for (var j = 0; j <= radialSegments; j++)
            {
                for (var i = 0; i <= tubularSegments; i++)
                {
                    var u = i / (float)tubularSegments * arc;
                    var v = j / (float)radialSegments * System.Math.PI * 2;

                    center.X = radius * (float)System.Math.Cos(u);
                    center.Y = radius * (float)System.Math.Sin(u);

                    var vertex = new Vector3();
                    vertex.X = (radius + tube * (float)System.Math.Cos(v)) * (float)System.Math.Cos(u);
                    vertex.Y = (radius + tube * (float)System.Math.Cos(v)) * (float)System.Math.Sin(u);
                    vertex.Z = tube * (float)System.Math.Sin(v);

                    this.Vertices.Add(vertex);

                    uvs.Add(new Vector2(i / (float)tubularSegments, j / (float)radialSegments));
                    normals.Add(((Vector3)vertex.Clone()).Sub(center).Normalize());
                }
            }

            for (var j = 1; j <= radialSegments; j++)
            {
                for (var i = 1; i <= tubularSegments; i++)
                {
                    var a = (tubularSegments + 1) * j + i - 1;
                    var b = (tubularSegments + 1) * (j - 1) + i - 1;
                    var c = (tubularSegments + 1) * (j - 1) + i;
                    var d = (tubularSegments + 1) * j + i;

                    {
                        var face = new Face3(a, b, d);
                        face.VertexNormals.Add((Vector3)normals[a].Clone());
                        face.VertexNormals.Add((Vector3)normals[b].Clone());
                        face.VertexNormals.Add((Vector3)normals[d].Clone());
                        this.Faces.Add(face);
                        this.FaceVertexUvs[0].Add(new List <Vector2> {
                            (Vector2)uvs[a].Clone(), (Vector2)uvs[b].Clone(), (Vector2)uvs[d].Clone()
                        });
                    }

                    {
                        var face = new Face3(b, c, d);
                        face.VertexNormals.Add((Vector3)normals[b].Clone());
                        face.VertexNormals.Add((Vector3)normals[c].Clone());
                        face.VertexNormals.Add((Vector3)normals[d].Clone());
                        this.Faces.Add(face);
                        this.FaceVertexUvs[0].Add(new List <Vector2> {
                            (Vector2)uvs[b].Clone(), (Vector2)uvs[c].Clone(), (Vector2)uvs[d].Clone()
                        });
                    }
                }
            }

            this.ComputeFaceNormals();
        }
Beispiel #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="widthSegments"></param>
        /// <param name="heightSegments"></param>
        public PlaneGeometry(float width, float height, int widthSegments = 1, int heightSegments = 1)
        {
            Debug.Assert(this.FaceVertexUvs.Count == 1, "Should only be 1 element at this stage");

            var widthHalf     = width / 2;
            var heightHalf    = height / 2;
            var gridX         = widthSegments;
            var gridZ         = heightSegments;
            var gridX1        = gridX + 1;
            var gridZ1        = gridZ + 1;
            var segmentWidth  = width / gridX;
            var segmentHeight = height / gridZ;

            var normal = new Vector3(0, 0, 1);

            for (var iz = 0; iz < gridZ1; iz++)
            {
                var y = iz * segmentHeight - heightHalf;
                for (var ix = 0; ix < gridX1; ix++)
                {
                    var x = ix * segmentWidth - widthHalf;
                    this.Vertices.Add(new Vector3(x, -y, 0));
                }
            }

            for (var iz = 0; iz < gridZ; iz++)
            {
                for (var ix = 0; ix < gridX; ix++)
                {
                    var a = ix + gridX1 * iz;
                    var b = ix + gridX1 * (iz + 1);
                    var c = (ix + 1) + gridX1 * (iz + 1);
                    var d = (ix + 1) + gridX1 * iz;

                    var uva = new Vector2(ix / (float)gridX1, 1 - iz / gridZ);
                    var uvb = new Vector2(ix / (float)gridX1, 1 - (iz + 1) / gridZ);
                    var uvc = new Vector2((ix + 1) / (float)gridX1, 1 - (iz + 1) / gridZ);
                    var uvd = new Vector2((ix + 1) / (float)gridX1, 1 - iz / gridZ);

                    var face = new Face3(a, b, d, new Vector3().One(), Color.White);
                    face.Normal = normal;
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    this.Faces.Add(face);

                    this.FaceVertexUvs[0].Add(new List <Vector2> {
                        uva, uvb, uvd
                    });

                    face        = new Face3(b, c, d, new Vector3().One(), Color.White);
                    face.Normal = normal;
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    face.VertexNormals.Add((Vector3)normal.Clone());
                    this.Faces.Add(face);

                    this.FaceVertexUvs[0].Add(new List <Vector2> {
                        (Vector2)uvb.Clone(), uvc, (Vector2)uvd.Clone()
                    });
                }
            }
        }
        public static List <Face3> CreateFaces(int columns, int rows)
        {
            var faces = new List <Face3>();

            for (var x = 0; x < columns; x++)
            {
                for (var y = 0; y < rows; y++)
                {
                    var verticesInColumn = rows + 1;
                    var v0 = x * verticesInColumn + y;
                    var v1 = (x + 1) * verticesInColumn + y;
                    var v2 = (x + 1) * verticesInColumn + y + 1;
                    var v3 = x * verticesInColumn + y + 1;

                    Face3 f0;
                    Face3 f1;
                    if (y % 2 == 0)
                    {
                        if (x % 2 == 0)
                        {
                            f0 = new Face3 {
                                V0 = v0, V1 = v1, V2 = v2
                            };
                            f1 = new Face3 {
                                V0 = v0, V1 = v2, V2 = v3
                            };
                        }
                        else
                        {
                            f0 = new Face3 {
                                V0 = v0, V1 = v1, V2 = v3
                            };
                            f1 = new Face3 {
                                V0 = v1, V1 = v2, V2 = v3
                            };
                        }
                    }
                    else
                    {
                        if (x % 2 == 0)
                        {
                            f0 = new Face3 {
                                V0 = v0, V1 = v1, V2 = v3
                            };
                            f1 = new Face3 {
                                V0 = v1, V1 = v2, V2 = v3
                            };
                        }
                        else
                        {
                            f0 = new Face3 {
                                V0 = v0, V1 = v1, V2 = v2
                            };
                            f1 = new Face3 {
                                V0 = v0, V1 = v2, V2 = v3
                            };
                        }
                    }

                    faces.Add(f0);
                    faces.Add(f1);
                }
            }

            return(faces);
        }
Beispiel #21
0
        /// <summary>
        ///     Constructor
        /// </summary>
        public CylinderGeometry(float radiusTop = 20, float radiusBottom = 20, float height = 100, int radialSegments = 8, int heightSegments = 1, bool openEnded = false)
        {
            Debug.Assert(this.FaceVertexUvs.Count == 1, "Should only be 1 element at this stage");

            this.RadiusTop    = radiusTop;
            this.RadiusBottom = radiusBottom;
            this.Height       = height;

            this.RadialSegments = radialSegments;
            this.HeightSegments = heightSegments;

            this.OpenEnded = openEnded;

            var heightHalf = height / 2;

            var uvs      = new List <List <Vector2> >();
            var vertices = new List <List <int> >();

            for (var y = 0; y <= heightSegments; y++)
            {
                var verticesRow = new List <int>();
                var uvsRow      = new List <Vector2>();

                var v      = y / (float)heightSegments;
                var radius = v * (radiusBottom - radiusTop) + radiusTop;

                for (var x = 0; x <= radialSegments; x++)
                {
                    var u = x / (float)radialSegments;

                    var vertex = new Vector3();
                    vertex.X = radius * (float)Math.Sin(u * Math.PI * 2);
                    vertex.Y = -v * height + heightHalf;
                    vertex.Z = radius * (float)Math.Cos(u * Math.PI * 2);

                    this.Vertices.Add(vertex);

                    verticesRow.Add(this.Vertices.Count - 1);
                    uvsRow.Add(new Vector2(u, 1 - v));
                }

                vertices.Add(verticesRow);
                uvs.Add(uvsRow);
            }


            var tanTheta = (radiusBottom - radiusTop) / height;

            for (var x = 0; x < radialSegments; x++)
            {
                Vector3 na;
                Vector3 nb;

                if (radiusTop != 0)
                {
                    na = (Vector3)this.Vertices[vertices[0][x]].Clone();
                    nb = (Vector3)this.Vertices[vertices[0][x + 1]].Clone();
                }
                else
                {
                    na = (Vector3)this.Vertices[vertices[1][x]].Clone();
                    nb = (Vector3)this.Vertices[vertices[1][x + 1]].Clone();
                }

                na.Y = (float)Math.Sqrt(na.X * na.X + na.Z * na.Z) * tanTheta;
                nb.Y = (float)Math.Sqrt(nb.X * nb.X + nb.Z * nb.Z) * tanTheta;
                na.Normalize();
                nb.Normalize();

                for (var y = 0; y < heightSegments; y++)
                {
                    var v1 = vertices[y][x];
                    var v2 = vertices[y + 1][x];
                    var v3 = vertices[y + 1][x + 1];
                    var v4 = vertices[y][x + 1];

                    var n1 = (Vector3)na.Clone();
                    var n2 = (Vector3)na.Clone();
                    var n3 = (Vector3)nb.Clone();
                    var n4 = (Vector3)nb.Clone();

                    var uv1 = (Vector2)uvs[y][x].Clone();
                    var uv2 = (Vector2)uvs[y + 1][x].Clone();
                    var uv3 = (Vector2)uvs[y + 1][x + 1].Clone();
                    var uv4 = (Vector2)uvs[y][x + 1].Clone();

                    {
                        var face = new Face3(v1, v2, v4);
                        face.VertexNormals.Add(n1);
                        face.VertexNormals.Add(n2);
                        face.VertexNormals.Add(n4);
                        this.Faces.Add(face);
                    }

                    this.FaceVertexUvs[0].Add(new List <Vector2> {
                        uv1, uv2, uv4
                    });

                    {
                        var face = new Face3(v2, v3, v4);
                        face.VertexNormals.Add((Vector3)n2.Clone());
                        face.VertexNormals.Add(n3);
                        face.VertexNormals.Add((Vector3)n4.Clone());
                        this.Faces.Add(face);
                    }

                    this.FaceVertexUvs[0].Add(new List <Vector2> {
                        (Vector2)uv2.Clone(), uv3, (Vector2)uv4.Clone()
                    });
                }
            }

            // top cap

            if (openEnded == false && radiusTop > 0)
            {
                this.Vertices.Add(new Vector3(0, heightHalf, 0));

                for (var x = 0; x < radialSegments; x++)
                {
                    var v1 = vertices[0][x];
                    var v2 = vertices[0][x + 1];
                    var v3 = this.Vertices.Count - 1;

                    var n1 = new Vector3(0, 1, 0);
                    var n2 = new Vector3(0, 1, 0);
                    var n3 = new Vector3(0, 1, 0);

                    var uv1 = (Vector2)uvs[0][x].Clone();
                    var uv2 = (Vector2)uvs[0][x + 1].Clone();
                    var uv3 = new Vector2(uv2.X, 0);

                    var face = new Face3(v1, v2, v3);
                    face.VertexNormals.Add(n1);
                    face.VertexNormals.Add(n2);
                    face.VertexNormals.Add(n3);
                    this.Faces.Add(face);

                    this.FaceVertexUvs[0].Add(new List <Vector2> {
                        uv1, uv2, uv3
                    });
                }
            }

            // bottom cap

            if (openEnded == false && radiusBottom > 0)
            {
                this.Vertices.Add(new Vector3(0, -heightHalf, 0));

                var y = heightSegments;

                for (var x = 0; x < radialSegments; x++)
                {
                    var v1 = vertices[y][x + 1];
                    var v2 = vertices[y][x];
                    var v3 = this.Vertices.Count - 1;

                    var n1 = new Vector3(0, -1, 0);
                    var n2 = new Vector3(0, -1, 0);
                    var n3 = new Vector3(0, -1, 0);

                    var uv1 = (Vector2)uvs[y][x + 1].Clone();
                    var uv2 = (Vector2)uvs[y][x].Clone();
                    var uv3 = new Vector2(uv2.X, 1);

                    var face = new Face3(v1, v2, v3);
                    face.VertexNormals.Add(n1);
                    face.VertexNormals.Add(n2);
                    face.VertexNormals.Add(n3);
                    this.Faces.Add(face);

                    this.FaceVertexUvs[0].Add(new List <Vector2> {
                        uv1, uv2, uv3
                    });
                }
            }

            this.ComputeFaceNormals();
        }
Beispiel #22
0
        private void ParseModel(JObject json, Geometry geometry, float scale)
        {
            var verticesData = json["vertices"].Values <float>().ToArray();
            var facesData    = json["faces"].Values <int>().ToArray();
            var normalsData  = json["normals"].Values <float>().ToArray();
            var colorsData   = json["colors"].Values <uint>().ToArray();

            var uvsData = new List <List <float> >();

            foreach (var array in json["uvs"])
            {
                var uvData = array.Values <float>().ToArray();
                uvsData.Add(new List <float>(uvData));
            }

            var nUvLayers = 0;

            if (uvsData.Count > 0)
            {
                geometry.faceVertexUvs.Clear();
                // disregard empty arrays
                foreach (var row in uvsData)
                {
                    nUvLayers++;
                    geometry.faceVertexUvs.Add(new List <UVFaceSet>());
                }
            }


            var offset = 0;

            while (offset < verticesData.Length)
            {
                var x = verticesData[offset++] * scale;
                var y = verticesData[offset++] * scale;
                var z = verticesData[offset++] * scale;
                var v = new Vector3(x, y, z);
                geometry.vertices.Add(v);
            }

            offset = 0;
            while (offset < facesData.Length)
            {
                var type                = facesData[offset++];
                var isQuad              = IsBitSet(type, 0);
                var hasMaterial         = IsBitSet(type, 1);
                var hasFaceVertexUv     = IsBitSet(type, 3);
                var hasFaceNormal       = IsBitSet(type, 4);
                var hasFaceVertexNormal = IsBitSet(type, 5);
                var hasFaceColor        = IsBitSet(type, 6);
                var hasFaceVertexColor  = IsBitSet(type, 7);

                //Debug.WriteLine("type:{0} bits( IsQuad:{1} Material:{2} FaceVertexUv:{3} FaceNormal:{4} FaceVertexNormal:{5} FaceColor:{6} FaceVertexColor:{7}", type, isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);

                if (isQuad != 0)
                {
                    var a     = facesData[offset++];
                    var b     = facesData[offset++];
                    var c     = facesData[offset++];
                    var d     = facesData[offset++];
                    var faceA = new Face3(a, b, d);
                    var faceB = new Face3(b, c, d);

                    if (hasMaterial != 0)
                    {
                        var materialIndex = facesData[offset++];
                    }

                    if (hasFaceVertexUv != 0)
                    {
                        for (var i = 0; i < nUvLayers; i++)
                        {
                            var uvLayer = uvsData[i];

                            var uvIndexA = facesData[offset++];
                            var uvIndexB = facesData[offset++];
                            var uvIndexC = facesData[offset++];
                            var uvIndexD = facesData[offset++];

                            var uA  = uvLayer[uvIndexA * 2];
                            var vA  = uvLayer[uvIndexA * 2 + 1];
                            var uvA = new Vector2(uA, vA);

                            var uB  = uvLayer[uvIndexB * 2];
                            var vB  = uvLayer[uvIndexB * 2 + 1];
                            var uvB = new Vector2(uB, vB);

                            var uC  = uvLayer[uvIndexC * 2];
                            var vC  = uvLayer[uvIndexC * 2 + 1];
                            var uvC = new Vector2(uC, vC);

                            var uD  = uvLayer[uvIndexD * 2];
                            var vD  = uvLayer[uvIndexD * 2 + 1];
                            var uvD = new Vector2(uD, vD);


                            geometry.faceVertexUvs[i].Add(new UVFaceSet(uvA, uvB, uvD));
                            geometry.faceVertexUvs[i].Add(new UVFaceSet(uvB, uvC, uvD));
                        }
                    }

                    if (hasFaceNormal != 0)
                    {
                        var normalIndex = facesData[offset++] * 3;

                        var x = normalsData[normalIndex++];
                        var y = normalsData[normalIndex++];
                        var z = normalsData[normalIndex];
                        var n = new Vector3(x, y, z);;
                        faceA.NormalA = n;
                        faceA.NormalB = n;
                        faceA.NormalC = n;
                        faceB.NormalA = n;
                        faceB.NormalB = n;
                        faceB.NormalC = n;
                    }

                    if (hasFaceVertexNormal != 0)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            var normalIndex = facesData[offset++] * 3;
                            var x           = normalsData[normalIndex++];
                            var y           = normalsData[normalIndex++];
                            var z           = normalsData[normalIndex];
                            var n           = new Vector3(x, y, z);

                            switch (i)
                            {
                            case 0: faceA.NormalA = n; break;

                            case 1: faceA.NormalB = n; faceB.NormalA = n; break;

                            case 2: faceB.NormalB = n; break;

                            case 3: faceA.NormalC = n; faceB.NormalC = n; break;
                            }
                        }
                    }

                    if (hasFaceColor != 0)
                    {
                        var colorIndex = facesData[offset++];
                        var hex        = colorsData[colorIndex];
                        var color      = new Color(hex);
                        faceA.ColorA = color;
                        faceA.ColorB = color;
                        faceA.ColorC = color;
                        faceB.ColorA = color;
                        faceB.ColorB = color;
                        faceB.ColorC = color;
                    }

                    if (hasFaceVertexColor != 0)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            var colorIndex = facesData[offset++];
                            var hex        = colorsData[colorIndex];
                            var color      = new Color(hex);

                            switch (i)
                            {
                            case 0: faceA.ColorA = color; break;

                            case 1: faceA.ColorB = color; faceB.ColorA = color; break;

                            case 2: faceB.ColorB = color; break;

                            case 3: faceA.ColorC = color; faceB.ColorC = color; break;
                            }
                        }
                    }

                    geometry.faces.Add(faceA);
                    geometry.faces.Add(faceB);
                }
                else
                {
                    var a    = facesData[offset++];
                    var b    = facesData[offset++];
                    var c    = facesData[offset++];
                    var face = new Face3(a, b, c);

                    if (hasFaceVertexUv != 0)
                    {
                        for (var i = 0; i < nUvLayers; i++)
                        {
                            var uvLayer = uvsData[i];

                            var uvIndexA = facesData[offset++];
                            var uvIndexB = facesData[offset++];
                            var uvIndexC = facesData[offset++];

                            var uA  = uvLayer[uvIndexA * 2];
                            var vA  = uvLayer[uvIndexA * 2 + 1];
                            var uvA = new Vector2(uA, vA);

                            var uB  = uvLayer[uvIndexB * 2];
                            var vB  = uvLayer[uvIndexB * 2 + 1];
                            var uvB = new Vector2(uB, vB);

                            var uC  = uvLayer[uvIndexC * 2];
                            var vC  = uvLayer[uvIndexC * 2 + 1];
                            var uvC = new Vector2(uC, vC);


                            geometry.faceVertexUvs[i].Add(new UVFaceSet(uvA, uvB, uvC));
                        }
                    }

                    if (hasFaceNormal != 0)
                    {
                        var normalIndex = facesData[offset++] * 3;
                        var x           = normalsData[normalIndex++];
                        var y           = normalsData[normalIndex++];
                        var z           = normalsData[normalIndex];
                        var n           = new Vector3(x, y, z);
                        face.NormalA = n;
                        face.NormalB = n;
                        face.NormalC = n;
                    }

                    if (hasFaceVertexNormal != 0)
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            var normalIndex = facesData[offset++] * 3;
                            var x           = normalsData[normalIndex++];
                            var y           = normalsData[normalIndex++];
                            var z           = normalsData[normalIndex];
                            var n           = new Vector3(x, y, z);
                            switch (i)
                            {
                            case 0: face.NormalA = n; break;

                            case 1: face.NormalB = n; break;

                            case 2: face.NormalC = n; break;
                            }
                        }
                    }

                    if (hasFaceColor != 0)
                    {
                        var colorIndex = facesData[offset++];
                        var hex        = colorsData[colorIndex];
                        var color      = new Color(hex);
                        face.ColorA = color;
                        face.ColorB = color;
                        face.ColorC = color;
                    }

                    if (hasFaceVertexColor != 0)
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            var colorIndex = facesData[offset++];
                            var hex        = colorsData[colorIndex];
                            var color      = new Color(hex);
                            switch (i)
                            {
                            case 0: face.ColorA = color; break;

                            case 1: face.ColorB = color; break;

                            case 2: face.ColorC = color; break;
                            }
                        }
                    }

                    geometry.faces.Add(face);
                }
            }
        }