Example #1
0
        private void AddUV(Vector2 vertex)
        {
            var uv = Vector2.Divide(vertex - BoundingBox.Min.Xy, BoundingBox.Max.Xy - BoundingBox.Min.Xy);

            uv.Y *= -1;
            UVs.Add(uv);
        }
Example #2
0
 public void CreateDefaultUVs()
 {
     while (UVs.Count < Vertices.Count)
     {
         UVs.Add(Vector2.zero);
     }
 }
Example #3
0
        // =========================================

        // ============= Build by Shape ============
        public void BuildFromExtents(MeshExtents box)
        {
            Verts.Add(new Vector3(box.Min.X, box.Min.Y, box.Max.Z));
            Verts.Add(new Vector3(box.Max.X, box.Min.Y, box.Max.Z));
            Verts.Add(new Vector3(box.Max.X, box.Max.Y, box.Max.Z));
            Verts.Add(new Vector3(box.Min.X, box.Max.Y, box.Max.Z));
            Verts.Add(new Vector3(box.Min.X, box.Min.Y, box.Min.Z));
            Verts.Add(new Vector3(box.Max.X, box.Min.Y, box.Min.Z));
            Verts.Add(new Vector3(box.Max.X, box.Max.Y, box.Min.Z));
            Verts.Add(new Vector3(box.Min.X, box.Max.Y, box.Min.Z));

            UVs.Add(new Vector2(0.0001f, 0.9999f));
            UVs.Add(new Vector2(0.9999f, 0.9999f));
            UVs.Add(new Vector2(0.9999f, 0.9999f));
            UVs.Add(new Vector2(0.0001f, 0.9999f));
            UVs.Add(new Vector2(0.0001f, 0.0001f));
            UVs.Add(new Vector2(0.9999f, 0.0001f));
            UVs.Add(new Vector2(0.9999f, 0.0001f));
            UVs.Add(new Vector2(0.0001f, 0.0001f));

            AddFace(3, 4, 0);
            AddFace(0, 2, 3);
            AddFace(0, 1, 2);
            AddFace(1, 5, 6);
            AddFace(1, 6, 2);
            AddFace(3, 7, 4);
            AddFace(2, 7, 3);
            AddFace(2, 6, 7);
            AddFace(0, 5, 1);
            AddFace(4, 7, 6);
            AddFace(0, 4, 5);
            AddFace(4, 6, 5);
        }
Example #4
0
        /// <summary>
        /// Add OBJ model
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public Mesh AddOBJ(string Path)
        {
            FileStream fileStream = new FileStream(Path, FileMode.Open);

            using (StreamReader reader = new StreamReader(fileStream))
            {
                string line;
                Mesh   tmp = new Mesh();

                while ((line = reader.ReadLine()) != null)
                {
                    string[] d = line.Split(" ");

                    //Vertex
                    if (d.Length == 4 && d[0].Equals("v"))
                    {
                        tmp.Positions.Add(new Vector3(float.Parse(d[1], NumberStyles.Any, CultureInfo.InvariantCulture), float.Parse(d[2], NumberStyles.Any, CultureInfo.InvariantCulture), float.Parse(d[3], NumberStyles.Any, CultureInfo.InvariantCulture)));
                    }
                    //UV
                    if (d.Length == 3 && d[0].Equals("vt"))
                    {
                        tmp.UVs.Add(new Vector2(float.Parse(d[1], NumberStyles.Any, CultureInfo.InvariantCulture), float.Parse(d[2], NumberStyles.Any, CultureInfo.InvariantCulture)));
                    }
                    //Normal
                    if (d.Length == 4 && d[0].Equals("vn"))
                    {
                        tmp.Normals.Add(new Vector3(float.Parse(d[1], NumberStyles.Any, CultureInfo.InvariantCulture), float.Parse(d[2], NumberStyles.Any, CultureInfo.InvariantCulture), float.Parse(d[3], NumberStyles.Any, CultureInfo.InvariantCulture)));
                    }
                    //Face
                    if (d.Length == 4 && d[0].Equals("f"))
                    {
                        string[] tri1 = d[1].Split("/");
                        string[] tri2 = d[2].Split("/");
                        string[] tri3 = d[3].Split("/");

                        Positions.Add(tmp.Positions[int.Parse(tri1[0]) - 1]);
                        Positions.Add(tmp.Positions[int.Parse(tri2[0]) - 1]);
                        Positions.Add(tmp.Positions[int.Parse(tri3[0]) - 1]);

                        UVs.Add(tmp.UVs[int.Parse(tri1[1]) - 1]);
                        UVs.Add(tmp.UVs[int.Parse(tri2[1]) - 1]);
                        UVs.Add(tmp.UVs[int.Parse(tri3[1]) - 1]);

                        Normals.Add(tmp.Normals[int.Parse(tri1[2]) - 1]);
                        Normals.Add(tmp.Normals[int.Parse(tri2[2]) - 1]);
                        Normals.Add(tmp.Normals[int.Parse(tri3[2]) - 1]);

                        for (int i = 0; i < 3; i++)
                        {
                            Indices.Add(Indices.Count);
                        }
                    }
                }
            }

            return(this);
        }
Example #5
0
        // =========================================

        public int AddUV(Vector2 uv)
        {
            if (!UVs.Contains(uv))
            {
                UVs.Add(uv);
            }

            return(UVs.IndexOf(uv));
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        public void SetUV(float s, float t, int set = 0)
        {
            while (UVs.Count <= set)
            {
                UVs.Add(new Vector2());
            }

            UVs[set] = new Vector2(s, t);
        }
Example #7
0
 protected override void SetUVs()
 {
     for (int z = 0; z <= resolutionZ; z++)
     {
         for (int x = 0; x <= resolutionX; x++)
         {
             UVs.Add(new Vector2(x / (UVScale * resolutionX), z / (UVScale * resolutionZ)));
         }
     }
 }
 private void UpdateVertsAndUVs()
 {
     Mesh.Clear();
     for (int i = 0; i < Config.VertsPerLoop; i++)
     {
         Verts.Add(LoopOrientMatrix * LoopVerts[i]);
         UVs.Add(new Vector2((float)i / (Config.VertsPerLoop - 1), Mathf.Clamp01(currentSegmentLength / Config.SegmentLength)));
     }
     Mesh.SetVertices(Verts);
     Mesh.SetUVs(0, UVs);
 }
Example #9
0
        internal Mesh(UnityEngine.Mesh mesh)
        {
            Triangles = new List <int>(mesh.triangles);

            foreach (var v in mesh.vertices)
            {
                Vertices.Add(v.Map());
            }
            foreach (var u in mesh.uv)
            {
                UVs.Add(u.Map());
            }
        }
Example #10
0
        public void Optimise()
        {
            List <C2Vertex> points = new List <C2Vertex>();
            List <Vector3>  verts  = new List <Vector3>();
            List <Vector2>  uvs    = new List <Vector2>();

            for (int i = 0; i < Verts.Count; i++)
            {
                C2Vertex p = new C2Vertex(Verts[i], UVs[i]);

                //Console.WriteLine("Vert " + i);
                int newID = points.IndexOf(p);
                //int newID = verts.IndexOf(Verts[i]);

                if (newID == -1)
                {
                    //Console.WriteLine("Adding " + p.ToString());
                    points.Add(p);
                    verts.Add(Verts[i]);
                    uvs.Add(UVs[i]);
                    newID = verts.Count - 1;
                }

                for (int j = 0; j < Faces.Count; j++)
                {
                    Faces[j].ReplaceVertID(i, newID);
                }
            }

            //Console.WriteLine("Reduced Vert count from " + Verts.Count + " to " + verts.Count);
            //Console.WriteLine("Reduced UV count from " + UVs.Count + " to " + uvs.Count);

            Verts.Clear();
            for (int i = 0; i < verts.Count; i++)
            {
                Verts.Add(verts[i]);
            }
            UVs.Clear();
            for (int i = 0; i < uvs.Count; i++)
            {
                UVs.Add(uvs[i]);
            }
        }
Example #11
0
        public void AddFace(Vector3 v1, Vector3 v2, Vector3 v3, Vector2 uv1, Vector2 uv2, Vector2 uv3, int matID)
        {
            int iv1, iv2, iv3, iuv1, iuv2, iuv3;

            Verts.Add(v1);
            iv1 = Verts.Count - 1;
            Verts.Add(v2);
            iv2 = Verts.Count - 1;
            Verts.Add(v3);
            iv3 = Verts.Count - 1;

            UVs.Add(uv1);
            iuv1 = UVs.Count - 1;
            UVs.Add(uv2);
            iuv2 = UVs.Count - 1;
            UVs.Add(uv3);
            iuv3 = UVs.Count - 1;

            AddFace(iv1, iv2, iv3, iuv1, iuv2, iuv3, matID);
        }
Example #12
0
        public MyClass(Vertex[] vertices)
        {
            this.vertices = vertices;

            Name = "aaa";

            UVs.Add(new UV(0, 0));
            UVs.Add(new UV(0, 1));
            UVs.Add(new UV(1, 1));
            UVs.Add(new UV(1, 0));

            int i = 0;

            foreach (Vertex vertex in vertices)
            {
                Vertices.Add(new SharpGL.SceneGraph.Vertex(vertex.x, vertex.y, vertex.z));
                Face face = new Face();
                face.Indices.Add(new Index(i++, i % 4));
                Faces.Add(face);
            }
        }
Example #13
0
 public void addUV(UV uv)
 {
     UVs.Add(uv);
 }
Example #14
0
        /// <summary>
        /// This function makes a simple cube shape.
        /// </summary>
        private void CreateCubeGeometry()
        {
            UVs.Add(new UV(0, 0));
            UVs.Add(new UV(0, 1));
            UVs.Add(new UV(1, 1));
            UVs.Add(new UV(1, 0));

            //	Add the vertices.
            Vertices.Add(new Vertex(-1, -1, -1));            // 0
            Vertices.Add(new Vertex(1, -1, -1));             // 1
            Vertices.Add(new Vertex(1, -1, 1));              // 2
            Vertices.Add(new Vertex(-1, -1, 1));             // 3
            Vertices.Add(new Vertex(-1, 1, -1));             // 4
            Vertices.Add(new Vertex(1, 1, -1));              // 5
            Vertices.Add(new Vertex(1, 1, 1));               // 6
            Vertices.Add(new Vertex(-1, 1, 1));              // 7

            //	Add the faces.
            Face face = new Face();             //	bottom

            face.Indices.Add(new Index(1, 0));
            face.Indices.Add(new Index(2, 1));
            face.Indices.Add(new Index(3, 2));
            face.Indices.Add(new Index(0, 3));
            Faces.Add(face);

            face = new Face();                          //	top
            face.Indices.Add(new Index(7, 0));
            face.Indices.Add(new Index(6, 1));
            face.Indices.Add(new Index(5, 2));
            face.Indices.Add(new Index(4, 3));
            Faces.Add(face);

            face = new Face();                          //	right
            face.Indices.Add(new Index(5, 0));
            face.Indices.Add(new Index(6, 1));
            face.Indices.Add(new Index(2, 2));
            face.Indices.Add(new Index(1, 3));
            Faces.Add(face);

            face = new Face();                          //	left
            face.Indices.Add(new Index(7, 0));
            face.Indices.Add(new Index(4, 1));
            face.Indices.Add(new Index(0, 2));
            face.Indices.Add(new Index(3, 3));
            Faces.Add(face);

            face = new Face();                          // front
            face.Indices.Add(new Index(4, 0));
            face.Indices.Add(new Index(5, 1));
            face.Indices.Add(new Index(1, 2));
            face.Indices.Add(new Index(0, 3));
            Faces.Add(face);

            face = new Face();                          //	back
            face.Indices.Add(new Index(6, 0));
            face.Indices.Add(new Index(7, 1));
            face.Indices.Add(new Index(3, 2));
            face.Indices.Add(new Index(2, 3));
            Faces.Add(face);
        }
Example #15
0
        /// <summary>
        /// Adds a face to the meshes vertices, textures and normals
        /// </summary>
        /// <param name="vertPalette">all of the vertices usable. for a cube it might be 24 long</param>
        /// <param name="uvPalette">all of the texture coordinates</param>
        /// <param name="a" >vertex x index</param>
        /// <param name="aT">texture x index (u)</param>
        /// <param name="b" >vertex y index</param>
        /// <param name="bT">texture y index (v)</param>
        /// <param name="c" >vertex z index</param>
        /// <param name="cT">texture z index (w)</param>
        /// <param name="is3dTexture">says whether the face will have a 3d texture</param>
        public void AddFace(List <float> vertPalette,
                            List <float> uvPalette,
                            int a,
                            int aT,
                            int b,
                            int bT,
                            int c,
                            int cT,
                            bool is3dTexture)
        {
            if (a > 0 && b > 0 && c > 0)
            {
                if (aT > 0 && bT > 0 && cT > 0)
                {
                    // say...

                    /*          a = 5
                     *          b = 3
                     *          c = 1
                     *
                     *          aT = 1
                     *          bT = 2
                     *          cT = 3
                     *
                     *          now they're:
                     *
                     *          a = 4
                     *          b = 2
                     *          c = 0
                     *          aT = 0
                     *          bT = 1
                     *          cT = 2
                     */
                    a  -= 1; b -= 1; c -= 1;
                    aT -= 1; bT -= 1; cT -= 1;
                    int[] vertexIndices = new int[3] {
                        a, b, c
                    };
                    int[] textureIndices = new int[3] {
                        aT, bT, cT
                    };
                    Vector3 vertex1 = vertPalette.GetVertexFromList(a * 3);
                    Vector3 vertex2 = vertPalette.GetVertexFromList(b * 3);
                    Vector3 vertex3 = vertPalette.GetVertexFromList(c * 3);
                    Vector3 normals = (vertex2 - vertex1).Cross(vertex3 - vertex1).Normalised();

                    for (int i = 0; i < 3; i++)
                    {
                        int vertIndex = vertexIndices[i];
                        int textIndex = textureIndices[i];
                        if (vertIndex < (vertPalette.Count / 3))
                        {
                            Vertices.Add(vertPalette[(vertIndex * 3)]);
                            Vertices.Add(vertPalette[(vertIndex * 3) + 1]);
                            Vertices.Add(vertPalette[(vertIndex * 3) + 2]);
                            if (uvPalette.Count > 0)
                            {
                                if (is3dTexture)
                                {
                                    if (textIndex < (uvPalette.Count / 3))
                                    {
                                        UVs.Add(uvPalette[(textIndex * 3)]);
                                        UVs.Add(uvPalette[(textIndex * 3) + 1]);
                                        UVs.Add(uvPalette[(textIndex * 3) + 2]);
                                    }
                                }
                                else
                                {
                                    if (textIndex < (uvPalette.Count / 2))
                                    {
                                        UVs.Add(uvPalette[(textIndex * 2)]);
                                        UVs.Add(uvPalette[(textIndex * 2) + 1]);
                                    }
                                }
                            }
                            else
                            {
                                UVs.Add(0.0f);
                                UVs.Add(0.0f);
                            }
                            Normals.Add(normals.X);
                            Normals.Add(normals.Y);
                            Normals.Add(normals.Z);
                        }
                    }
                }
            }
        }
Example #16
0
        /// <summary>
        /// Abstract method. It will be called to finalize the mesh, such as close the mesh at the end.
        /// </summary>
        override protected void FinalizeMesh()
        {
            if (closeFront)
            {
                int a = frontVertices [0],
                    b = frontVertices [1],
                    c = frontVertices [2],
                    d = frontVertices [3];

                // Dup vertices
                Vector3 pa = vertices [a];
                Vector3 pb = vertices [b];
                Vector3 pc = vertices [c];
                Vector3 pd = vertices [d];

                vertices.Add(pa);
                UVs.Add(new Vector2(0, 0));
                normals.Add(frontNormal);
                a = vertices.Count - 1;
                vertices.Add(pb);
                UVs.Add(new Vector2(1, 0));
                normals.Add(frontNormal);
                b = vertices.Count - 1;
                vertices.Add(pc);
                UVs.Add(new Vector2(1, 1));
                normals.Add(frontNormal);
                c = vertices.Count - 1;
                vertices.Add(pd);
                UVs.Add(new Vector2(0, 1));
                normals.Add(frontNormal);
                d = vertices.Count - 1;

                AddQuad3(c, b, d, a);
            }

            if (closeBack)
            {
                int a = backVertices [0],
                    b = backVertices [1],
                    c = backVertices [2],
                    d = backVertices [3];

                // Dup vertices
                Vector3 pa = vertices [a];
                Vector3 pb = vertices [b];
                Vector3 pc = vertices [c];
                Vector3 pd = vertices [d];

                vertices.Add(pa);
                UVs.Add(new Vector2(0, 0));
                normals.Add(backNormal);
                a = vertices.Count - 1;
                vertices.Add(pb);
                UVs.Add(new Vector2(1, 0));
                normals.Add(backNormal);
                b = vertices.Count - 1;
                vertices.Add(pc);
                UVs.Add(new Vector2(1, 1));
                normals.Add(backNormal);
                c = vertices.Count - 1;
                vertices.Add(pd);
                UVs.Add(new Vector2(0, 1));
                normals.Add(backNormal);
                d = vertices.Count - 1;

                AddQuad3(a, b, d, c);
            }
        }
Example #17
0
        /// <summary>
        /// Generates a single mesh part.
        /// </summary>
        /// <param name="piece">The piece. For each piece, this method is called.</param>
        /// <param name="position">The current position on the path.</param>
        /// <param name="direction">The current direction at the position on the path.</param>
        /// <param name="xd">This vector indicates an ortogonal direction to the path direction.</param>
        /// <param name="yd">This vector indicates an ortogonal direction to the direction and the xd vector.
        /// Use xd and yd as a plane ortogonal to the direction.</param>
        override protected void GenerateMeshPart(int piece, Vector3 position, Vector3 direction, Vector3 xd, Vector3 yd)
        {
            float r;

            if (widthType == WidthType.Constant)
            {
                r = width;
            }
            else
            {
                float minTime = float.MaxValue;
                float maxTime = float.MinValue;

                for (int i = 0; i < widthCurve.length; i++)
                {
                    float time = widthCurve.keys [i].time;
                    if (time > maxTime)
                    {
                        maxTime = time;
                    }
                    if (time < minTime)
                    {
                        minTime = time;
                    }
                }

                float timeSpan = maxTime - minTime;
                float evalTime = minTime + ((float)piece / (float)pieces) * timeSpan;

                r = widthCurve.Evaluate(evalTime);
            }

            // Generate first Vertex
            Vector3 point = position + r / 2 * xd - height * Vector3.up;

            vertices.Add(transform.InverseTransformPoint(point));

            // Generate UVs
            UVs.Add(new Vector2(0, piece * 1f / (float)(pieces)));

            // Generate normals
            Vector3 normal = flipped ? xd : -xd;

            normals.Add(normal);

            if (piece == 0)
            {
                frontVertices.Add(vertices.Count - 1);
                frontNormal = flipped ? direction : -direction;
            }
            else if (piece == pieces)
            {
                backVertices.Add(vertices.Count - 1);
                backNormal = flipped ? -direction : direction;
            }

            // Generate second vertex
            point = position + r / 2 * xd;
            vertices.Add(transform.InverseTransformPoint(point));

            // Generate UVs
            UVs.Add(new Vector2(1, piece * 1f / (float)(pieces)));

            // Generate normals
            normal = flipped ? xd : -xd;
            normals.Add(normal);

            if (piece == 0)
            {
                frontVertices.Add(vertices.Count - 1);
                frontNormal = flipped ? direction : -direction;
            }
            else if (piece == pieces)
            {
                backVertices.Add(vertices.Count - 1);
                backNormal = flipped ? -direction : direction;
            }

            for (int j = 0; j <= sections; j++)
            {
                // Generate vertex
                point = position + (r / 2f - (float)j / (float)sections * r) * xd;
                vertices.Add(transform.InverseTransformPoint(point));

                // Generate UVs
                UVs.Add(new Vector2((float)j / (float)sections, piece * 1f / (float)(pieces)));

                // Generate normals
                normal = flipped ? yd : -yd;
                normals.Add(normal);
            }

            // Generate first vertex
            point = position - r / 2 * xd;
            vertices.Add(transform.InverseTransformPoint(point));

            // Generate UVs
            UVs.Add(new Vector2(1, piece * 1f / (float)(pieces)));

            // Generate normals
            normal = flipped ? -xd : xd;
            normals.Add(normal);

            if (piece == 0)
            {
                frontVertices.Add(vertices.Count - 1);
                frontNormal = flipped ? direction : -direction;
            }
            else if (piece == pieces)
            {
                backVertices.Add(vertices.Count - 1);
                backNormal = flipped ? -direction : direction;
            }

            // Generate second Vertex
            point = position - r / 2 * xd - height * Vector3.up;
            vertices.Add(transform.InverseTransformPoint(point));

            // Generate UVs
            UVs.Add(new Vector2(0, piece * 1f / (float)(pieces)));

            // Generate normals
            normal = flipped ? -xd : xd;
            normals.Add(normal);

            if (piece == 0)
            {
                frontVertices.Add(vertices.Count - 1);
                frontNormal = flipped ? direction : -direction;
            }
            else if (piece == pieces)
            {
                backVertices.Add(vertices.Count - 1);
                backNormal = flipped ? -direction : direction;
            }

            // Add triangles
            if (piece > 0)
            {
                int baseIndex = piece * (sections + 5);

                int a = baseIndex;
                int b = baseIndex - (sections + 5);
                int c = baseIndex + 1;
                int d = baseIndex + 1 - (sections + 5);

                AddQuad2(a, b, c, d);

                for (int j = 2; j < sections + 2; j++)
                {
                    a = baseIndex + j;
                    b = baseIndex + j - (sections + 5);
                    c = baseIndex + j + 1;
                    d = baseIndex + j + 1 - (sections + 5);

                    AddQuad1(a, b, c, d);
                }

                a = baseIndex + sections + 3;
                b = baseIndex + sections + 3 - (sections + 5);
                c = baseIndex + sections + 3 + 1;
                d = baseIndex + sections + 3 + 1 - (sections + 5);

                AddQuad2(a, b, c, d);
            }
        }
Example #18
0
        /// <summary>
        /// Generates a single mesh part.
        /// </summary>
        /// <param name="piece">The piece. For each piece, this method is called.</param>
        /// <param name="position">The current position on the path.</param>
        /// <param name="direction">The current direction at the position on the path.</param>
        /// <param name="xd">This vector indicates an ortogonal direction to the path direction.</param>
        /// <param name="yd">This vector indicates an ortogonal direction to the direction and the xd vector.
        /// Use xd and yd as a plane ortogonal to the direction.</param>
        override protected void GenerateMeshPart(int piece, Vector3 position, Vector3 direction, Vector3 xd, Vector3 yd)
        {
            float r;

            if (radiusType == RadiusType.Constant)
            {
                r = radius;
            }
            else
            {
                float minTime = float.MaxValue;
                float maxTime = float.MinValue;

                for (int i = 0; i < radiusCurve.length; i++)
                {
                    float time = radiusCurve.keys [i].time;
                    if (time > maxTime)
                    {
                        maxTime = time;
                    }
                    if (time < minTime)
                    {
                        minTime = time;
                    }
                }

                float timeSpan = maxTime - minTime;
                float evalTime = minTime + ((float)piece / (float)pieces) * timeSpan;

                r = radiusCurve.Evaluate(evalTime);
            }

            for (int j = 0; j <= sections; j++)
            {
                float angle = Mathf.Deg2Rad * angleOffset + fTwist + j * 2 * Mathf.PI / sections;

                // Generate vertex
                Vector3 point = position + r * Mathf.Sin(angle) * xd + r * Mathf.Cos(angle) * yd;
                vertices.Add(transform.InverseTransformPoint(point));

                // Generate UVs
                UVs.Add(new Vector2(j * 1f / (float)(sections), piece * 1f / (float)(pieces)));

                // Generate normals
                Vector3 normal = flipped ? (position - point).normalized : (point - position).normalized;
                normals.Add(normal);

                if (piece == 0)
                {
                    frontVertices.Add(vertices.Count - 1);
                    frontNormal   = flipped ? direction : -direction;
                    frontPosition = transform.InverseTransformPoint(position);
                }
                else if (piece == pieces)
                {
                    backVertices.Add(vertices.Count - 1);
                    backNormal   = flipped ? -direction : direction;
                    backPosition = transform.InverseTransformPoint(position);
                }
            }

            fTwist += twist / 100;

            // Add triangles
            if (piece > 0)
            {
                for (int j = 0; j < sections; j++)
                {
                    int baseIndex = piece * (sections + 1);

                    int a = baseIndex + j;
                    int b = baseIndex + j - (sections + 1);
                    int c = baseIndex + j + 1;
                    int d = baseIndex + j + 1 - (sections + 1);

                    AddQuad1(a, b, c, d);
                }
            }
        }
Example #19
0
        /// <summary>
        /// Abstract method. It will be called to finalize the mesh, such as close the mesh at the end.
        /// </summary>
        override protected void FinalizeMesh()
        {
            if (closeFront)
            {
                for (int j = 0; j < frontVertices.Count - 1; j++)
                {
                    int a = frontVertices [0],
                        b = frontVertices [j + 1],
                        c = frontVertices [j];

                    // Dup vertices
                    Vector3 pa = vertices [a];
                    Vector3 pb = vertices [b];
                    Vector3 pc = vertices [c];

                    vertices.Add(pa);
                    UVs.Add(pa - frontPosition);
                    normals.Add(frontNormal);
                    a = vertices.Count - 1;
                    vertices.Add(pb);
                    UVs.Add(pb - frontPosition);
                    normals.Add(frontNormal);
                    b = vertices.Count - 1;
                    vertices.Add(pc);
                    UVs.Add(pc - frontPosition);
                    normals.Add(frontNormal);
                    c = vertices.Count - 1;

                    if (flipped)
                    {
                        triangles2.Add(c);
                        triangles2.Add(b);
                        triangles2.Add(a);
                    }
                    else
                    {
                        triangles2.Add(a);
                        triangles2.Add(b);
                        triangles2.Add(c);
                    }
                }
            }

            if (closeBack)
            {
                for (int j = 0; j < backVertices.Count - 1; j++)
                {
                    int a = backVertices [0],
                        b = backVertices [j + 1],
                        c = backVertices [j];

                    // Dup vertices
                    Vector3 pa = vertices [a];
                    Vector3 pb = vertices [b];
                    Vector3 pc = vertices [c];

                    vertices.Add(pa);
                    UVs.Add(pa - backPosition);
                    normals.Add(backNormal);
                    a = vertices.Count - 1;
                    vertices.Add(pb);
                    UVs.Add(pb - backPosition);
                    normals.Add(backNormal);
                    b = vertices.Count - 1;
                    vertices.Add(pc);
                    UVs.Add(pc - backPosition);
                    normals.Add(backNormal);
                    c = vertices.Count - 1;

                    if (flipped)
                    {
                        triangles2.Add(a);
                        triangles2.Add(b);
                        triangles2.Add(c);
                    }
                    else
                    {
                        triangles2.Add(c);
                        triangles2.Add(b);
                        triangles2.Add(a);
                    }
                }
            }
        }
Example #20
0
        internal override Dictionary <string, OBJObjectBuilder> LoadBuilderDictionaries(StreamReader reader)
        {
            Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>();
            OBJObjectBuilder currentBuilder  = null;
            string           currentMaterial = "default";

            //lists for face data
            //prevents excess GC
            List <int> vertexIndices = new List <int>();
            List <int> normalIndices = new List <int>();
            List <int> uvIndices     = new List <int>();

            //helper func
            Action <string> setCurrentObjectFunc = (string objectName) =>
            {
                if (!builderDict.TryGetValue(objectName, out currentBuilder))
                {
                    currentBuilder          = new OBJObjectBuilder(objectName, this);
                    builderDict[objectName] = currentBuilder;
                }
            };

            //create default object
            setCurrentObjectFunc.Invoke("default");

            //var buffer = new DoubleBuffer(reader, 256 * 1024);
            var buffer = new CharWordReader(reader, 4 * 1024);

            //do the reading
            while (true)
            {
                if (buffer.currentChar == char.MinValue)
                {
                    //Debug.Log("");
                }

                buffer.SkipWhitespaces();

                if (buffer.endReached == true)
                {
                    break;
                }

                buffer.ReadUntilWhiteSpace();

                //comment or blank
                if (buffer.Is("#"))
                {
                    buffer.SkipUntilNewLine();
                    continue;
                }

                if (Materials == null && buffer.Is("mtllib"))
                {
                    buffer.SkipWhitespaces();
                    buffer.ReadUntilNewLine();
                    string mtlLibPath = buffer.GetString();
                    LoadMaterialLibrary(mtlLibPath);
                    continue;
                }

                if (buffer.Is("v"))
                {
                    Vertices.Add(buffer.ReadVector());
                    continue;
                }

                //normal
                if (buffer.Is("vn"))
                {
                    Normals.Add(buffer.ReadVector());
                    continue;
                }

                //uv
                if (buffer.Is("vt"))
                {
                    UVs.Add(buffer.ReadVector());
                    continue;
                }

                //new material
                if (buffer.Is("usemtl"))
                {
                    buffer.SkipWhitespaces();
                    buffer.ReadUntilNewLine();
                    string materialName = buffer.GetString();
                    currentMaterial = materialName;

                    if (SplitMode == SplitMode.Material)
                    {
                        setCurrentObjectFunc.Invoke(materialName);
                    }
                    continue;
                }

                //new object
                if ((buffer.Is("o") || buffer.Is("g")) && SplitMode == SplitMode.Object)
                {
                    buffer.ReadUntilNewLine();
                    string objectName = buffer.GetString(1);
                    setCurrentObjectFunc.Invoke(objectName);
                    continue;
                }

                //face data (the fun part)
                if (buffer.Is("f"))
                {
                    //loop through indices
                    while (true)
                    {
                        bool newLinePassed;
                        buffer.SkipWhitespaces(out newLinePassed);
                        if (newLinePassed == true)
                        {
                            break;
                        }

                        int vertexIndex = int.MinValue;
                        int normalIndex = int.MinValue;
                        int uvIndex     = int.MinValue;

                        vertexIndex = buffer.ReadInt();
                        if (buffer.currentChar == '/')
                        {
                            buffer.MoveNext();
                            if (buffer.currentChar != '/')
                            {
                                uvIndex = buffer.ReadInt();
                            }
                            if (buffer.currentChar == '/')
                            {
                                buffer.MoveNext();
                                normalIndex = buffer.ReadInt();
                            }
                        }

                        //"postprocess" indices
                        if (vertexIndex > int.MinValue)
                        {
                            if (vertexIndex < 0)
                            {
                                vertexIndex = Vertices.Count - vertexIndex;
                            }
                            vertexIndex--;
                        }
                        if (normalIndex > int.MinValue)
                        {
                            if (normalIndex < 0)
                            {
                                normalIndex = Normals.Count - normalIndex;
                            }
                            normalIndex--;
                        }
                        if (uvIndex > int.MinValue)
                        {
                            if (uvIndex < 0)
                            {
                                uvIndex = UVs.Count - uvIndex;
                            }
                            uvIndex--;
                        }

                        //set array values
                        vertexIndices.Add(vertexIndex);
                        normalIndices.Add(normalIndex);
                        uvIndices.Add(uvIndex);
                    }


                    //push to builder
                    currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices);

                    //clear lists
                    vertexIndices.Clear();
                    normalIndices.Clear();
                    uvIndices.Clear();

                    continue;
                }

                buffer.SkipUntilNewLine();
            }

            return(builderDict);
        }
Example #21
0
 public void AddListUV(Vector2 uv)
 {
     UVs.Add(uv);
 }
Example #22
0
 public void AddVertex(Vector3 position, Vector3 normal, Vector2 textureCoord)
 {
     Vertices.Add(position);
     Normals.Add(normal);
     UVs.Add(textureCoord);
 }
Example #23
0
 public void AddListUV(float u, float v)
 {
     UVs.Add(new Vector2(u, v));
 }
Example #24
0
            /// <summary>
            /// Initializes a new instance of the <see cref="PRTMRawDataMetaChunk"/> class.
            /// </summary>
            /// <param name="meta">The meta.</param>
            /// <remarks></remarks>
            public PRTMRawDataMetaChunk(ref Meta meta)
            {
                BinaryReader BR = new BinaryReader(meta.MS);

                BR.BaseStream.Position = 176;
                int tempc = BR.ReadInt32();
                int tempr = BR.ReadInt32() - meta.Map.SecondaryMagic - meta.offset;

                RawDataChunkInfo = new RawDataOffsetChunk[tempc];
                for (int x = 0; x < tempc; x++)
                {
                    RawDataChunkInfo[x]           = new RawDataOffsetChunk();
                    BR.BaseStream.Position        = tempr + (x * 16) + 6;
                    RawDataChunkInfo[x].ChunkSize = 56;
                    RawDataChunkInfo[x].Size      = BR.ReadInt32();
                    if (RawDataChunkInfo[x].ChunkSize == 0)
                    {
                        RawDataChunkInfo[x].ChunkSize = RawDataChunkInfo[x].Size;
                    }

                    RawDataChunkInfo[x].Offset     = BR.ReadInt32();
                    RawDataChunkInfo[x].ChunkCount = RawDataChunkInfo[x].Size / RawDataChunkInfo[x].ChunkSize;
                }

                BR.BaseStream.Position = 136;
                VerticeCount           = BR.ReadInt32();
                tempr = BR.ReadInt32() - meta.Map.SecondaryMagic - meta.offset;
                BR.BaseStream.Position = tempr;
                for (int x = 0; x < VerticeCount; x++)
                {
                    Vector3 vec = new Vector3();

                    vec.X = BR.ReadSingle();
                    vec.Y = BR.ReadSingle();
                    vec.Z = BR.ReadSingle();
                    Vertices.Add(vec);

                    Vector3 vec2 = new Vector3();
                    vec2.X = BR.ReadSingle();
                    vec2.Y = BR.ReadSingle();
                    vec2.Z = BR.ReadSingle();
                    Normals.Add(vec2);

                    Vector3 vec3 = new Vector3();
                    vec3.X = BR.ReadSingle();
                    vec3.Y = BR.ReadSingle();
                    vec3.Z = BR.ReadSingle();
                    Binormals.Add(vec3);

                    Vector3 vec4 = new Vector3();
                    vec4.X = BR.ReadSingle();
                    vec4.Y = BR.ReadSingle();
                    vec4.Z = BR.ReadSingle();
                    Tangents.Add(vec4);

                    Vector2 vec5 = new Vector2();
                    vec5.X = BR.ReadSingle();
                    vec5.Y = BR.ReadSingle();

                    UVs.Add(vec5);
                }

                BR.BaseStream.Position = 168;
                HeaderSize             = BR.ReadInt32() + 8;

                BR.BaseStream.Position = 144;
                IndiceCount            = BR.ReadInt32();
                tempr = BR.ReadInt32() - meta.Map.SecondaryMagic - meta.offset;
                BR.BaseStream.Position = tempr;
                FaceCount = IndiceCount / 3;

                this.Indices = new short[IndiceCount];
                for (int x = 0; x < IndiceCount; x++)
                {
                    Indices[x] = (short)BR.ReadUInt16();
                }

                SubMeshInfo = new ModelSubMeshInfo[1];
                for (int x = 0; x < 1; x++)
                {
                    SubMeshInfo[x]              = new ModelSubMeshInfo();
                    BR.BaseStream.Position      = HeaderSize + RawDataChunkInfo[0].Offset + (x * 72) + 4;
                    SubMeshInfo[x].ShaderNumber = 0;
                    SubMeshInfo[x].IndiceStart  = 0;
                    SubMeshInfo[x].IndiceCount  = IndiceCount;
                }
            }