Exemplo n.º 1
0
    /// <summary>
    /// 利用网格信息绘制网格点
    /// </summary>
    /// <param name="meshData"></param>
    public static GameObject[] CreatePointsByMeshStruct(MeshStruct meshData, GameObject sprefab, bool boraderOnly = true)
    {
        List <Vector3>    pointsTemp = new List <Vector3>();
        List <GameObject> sprites    = new List <GameObject>();

        List <Vector3> newPoints = new List <Vector3>();
        float          maxx      = 0;
        float          maxy      = 0;
        float          maxz      = 0;//只创建边线上

        foreach (var item in meshData.vertices)
        {
            if (!pointsTemp.Contains(item))
            {
                newPoints.Add(item);
                maxx = maxx > item.x ? maxx : item.x;
                maxy = maxx > item.y ? maxy : item.y;
                maxz = maxx > item.z ? maxz : item.z;
                pointsTemp.Add(item);//防止重复记录
            }
        }

        foreach (var item in newPoints)
        {
            if (!boraderOnly || (Mathf.Abs(item.x) == maxx && Mathf.Abs(item.y) == maxy) || (Mathf.Abs(item.y) == maxy && Mathf.Abs(item.z) == maxz || ((Mathf.Abs(item.z) == maxz && Mathf.Abs(item.x) == maxx))))
            {
                GameObject sprite = GameObject.Instantiate(sprefab);
                sprite.SetActive(true);
                sprite.transform.position = item;
                sprites.Add(sprite);
            }
        }
        return(sprites.ToArray());
    }
Exemplo n.º 2
0
    static void CreateMeshGrid(Vector3Int _grid, BeamInfo binfo, ref MeshStruct _meshStruct)
    {
        vIndex = tIndex = 0;
        float xMin = -binfo.length * 0.5f;
        float xMax = binfo.length * 0.5f;
        float yMin = -binfo.height * 0.5f;
        float yMax = binfo.height * 0.5f;
        float zMin = -binfo.wigth * 0.5f;
        float zMax = binfo.wigth * 0.5f;

        CreateMeshPlane(_grid.x, _grid.z,//上
                        (i, j) => { return(Mathf.Clamp(i * binfo.gridSize + xMin, xMin, xMax)); },
                        (i, j) => { return(yMax); },
                        (i, j) => { return(Mathf.Clamp(-j * binfo.gridSize + zMax, zMin, zMax)); },
                        /*new Rect(0, 0.7083f, 0.9278f, 0.2917f)*/
                        (x, y, z) => { return(0 + ((x + xMax) / binfo.length) * 0.9278f); },
                        (x, y, z) => { return(0.7083f + ((z + zMax) / binfo.wigth) * 0.2917f); },
                        Vector3.up, ref _meshStruct, true);
        CreateMeshPlane(_grid.x, _grid.z,//下
                        (i, j) => { return(Mathf.Clamp(i * binfo.gridSize + xMin, xMin, xMax)); },
                        (i, j) => { return(yMin); },
                        (i, j) => { return(Mathf.Clamp(-j * binfo.gridSize + zMax, zMin, zMax)); },
                        //new Rect(0, 0, 0.9278f, 0.2917f),
                        (x, y, z) => { return(0 + ((x + xMax) / binfo.length) * 0.9278f); },
                        (x, y, z) => { return(0 + ((z + zMax) / binfo.wigth) * 0.2917f); },
                        Vector3.down, ref _meshStruct);
        CreateMeshPlane(_grid.z, _grid.y,//左
                        (i, j) => { return(xMin); },
                        (i, j) => { return(Mathf.Clamp(j * binfo.gridSize + yMin, yMin, yMax)); },
                        (i, j) => { return(Mathf.Clamp(-i * binfo.gridSize + zMax, zMin, zMax)); },
                        //new Rect(0.9278f, 0.2917f, 0.0722f, 0.4167f),
                        (x, y, z) => { return(0.9278f + ((z + zMax) / binfo.wigth) * 0.0722f); },
                        (x, y, z) => { return(0.2917f + ((y + yMax) / binfo.height) * 0.4167f); },
                        Vector3.left, ref _meshStruct);
        CreateMeshPlane(_grid.z, _grid.y,//右
                        (i, j) => { return(xMax); },
                        (i, j) => { return(Mathf.Clamp(j * binfo.gridSize + yMin, yMin, yMax)); },
                        (i, j) => { return(Mathf.Clamp(-i * binfo.gridSize + zMax, zMin, zMax)); },
                        //new Rect(0.9278f, 0.2917f, 0.0722f, 0.4167f),
                        (x, y, z) => { return(0.9278f + ((z + zMax) / binfo.wigth) * 0.0722f); },
                        (x, y, z) => { return(0.2917f + ((y + yMax) / binfo.height) * 0.4167f); },
                        Vector3.right, ref _meshStruct, true);
        CreateMeshPlane(_grid.x, _grid.y,//前
                        (i, j) => { return(Mathf.Clamp(i * binfo.gridSize + xMin, xMin, xMax)); },
                        (i, j) => { return(Mathf.Clamp(j * binfo.gridSize + yMin, yMin, yMax)); },
                        (i, j) => { return(zMin); },
                        //new Rect(0f, 0.2917f, 0.9278f, 0.4167f),
                        (x, y, z) => { return(0 + ((x + xMax) / binfo.length) * 0.9278f); },
                        (x, y, z) => { return(0.2917f + ((y + yMax) / binfo.height) * 0.4167f); },
                        Vector3.back, ref _meshStruct);
        CreateMeshPlane(_grid.x, _grid.y,//后
                        (i, j) => { return(Mathf.Clamp(i * binfo.gridSize + xMin, xMin, xMax)); },
                        (i, j) => { return(Mathf.Clamp(j * binfo.gridSize + yMin, yMin, yMax)); },
                        (i, j) => { return(zMax); },
                        //new Rect(0f, 0.2917f, 0.9278f, 0.4167f),
                        (x, y, z) => { return(0 + ((x + xMax) / binfo.length) * 0.9278f); },
                        (x, y, z) => { return(0.2917f + ((y + yMax) / binfo.height) * 0.4167f); },
                        Vector3.forward, ref _meshStruct, true);
    }
Exemplo n.º 3
0
 /// <summary>
 /// 利用网格信息得到线集合
 /// </summary>
 /// <param name="meshData"></param>
 /// <returns></returns>
 public static void UpdatePointLines(MeshStruct meshData, ref PointLines pointLine)
 {
     for (int i = 0; i < meshData.triangles.Length - 2; i += 3)
     {
         PointLine.GetPointLines(pointLine, i,
                                 new Vector3[] { meshData.vertices[meshData.triangles[i]], meshData.vertices[meshData.triangles[i + 1]], meshData.vertices[meshData.triangles[i + 2]] });
     }
 }
Exemplo n.º 4
0
 public MeshStruct(MeshStruct temp)
 {
     this.vertices  = new Vector3[temp.vertices.Length]; System.Array.Copy(temp.vertices, this.vertices, this.vertices.Length);
     this.triangles = new int[temp.triangles.Length]; System.Array.Copy(temp.triangles, this.triangles, this.triangles.Length);
     this.uvs       = new Vector2[temp.uvs.Length]; System.Array.Copy(temp.uvs, this.uvs, this.uvs.Length);
     this.normals   = new Vector3[temp.normals.Length]; System.Array.Copy(temp.normals, this.normals, this.normals.Length);
     this.tangents  = new Vector4[temp.tangents.Length]; System.Array.Copy(temp.tangents, this.tangents, this.tangents.Length);
     this.Colors32  = new Color32[temp.Colors32.Length]; System.Array.Copy(temp.Colors32, this.Colors32, this.Colors32.Length);
 }
Exemplo n.º 5
0
    /// <summary>
    /// 利用梁的基本信息绘制梁
    /// </summary>
    /// <param name="dir"></param>
    private void ResetBeamView(bool dir)
    {
        _meshStructStatic = MeshUtility.CreateBeamMesh(beamInfo);
        _meshStruct       = new MeshStruct(_meshStructStatic);

        _gridmeshStructStatic = MeshUtility.CreateBeamMesh(gridBeamInfo);
        _gridmeshStruct       = new MeshStruct(_gridmeshStructStatic);

        SetMeshStructToMesh();
    }
Exemplo n.º 6
0
 private void Start()
 {
     _meshStruct = new MeshStruct(m_meshFilter.mesh);
     for (int i = 0; i < _meshStruct.vertices.Length; i++)
     {
         _meshStruct.vertices[i] = new Vector3(_meshStruct.vertices[i].x, _meshStruct.vertices[i].z, _meshStruct.vertices[i].y);
     }
     transform.localRotation = Quaternion.identity;
     SetMeshStructToMesh();
     _meshStructStatic = new MeshStruct(_meshStruct);
 }
Exemplo n.º 7
0
    void copy(MeshStruct src, int srcOfs, MeshStruct dest, int destOfs, int length, IntVector3 chunkpos)
    {
        Vector3 cp = new Vector3(chunkpos.x << Block45Constants._shift, chunkpos.y << Block45Constants._shift, chunkpos.z << Block45Constants._shift);

        for (int i = 0; i < length; i++)
        {
            dest.vertices[destOfs + i] = src.vertices[srcOfs + i] + cp;
        }
        //Array.Copy(src.vertices,	srcOfs, dest.vertices,	destOfs, length);
        Array.Copy(src.uv, srcOfs, dest.uv, destOfs, length);
        Array.Copy(src.normals, srcOfs, dest.normals, destOfs, length);
    }
Exemplo n.º 8
0
    void addNewMesh()
    {
        MeshStruct newMesh = new MeshStruct();

        newMesh.vertices  = new Vector3[SurfExtractorsMan.c_vertsCntMax];
        newMesh.uv        = new Vector2[SurfExtractorsMan.c_vertsCntMax];
        newMesh.triangles = new int[SurfExtractorsMan.c_vertsCntMax];
        newMesh.normals   = new Vector3[SurfExtractorsMan.c_vertsCntMax];
        Array.Copy(Block45Kernel.indicesConst, 0, newMesh.triangles, 0, SurfExtractorsMan.c_vertsCntMax);

        reorganizedMeshes.Add(newMesh);
    }
Exemplo n.º 9
0
    void CreateMesh()
    {
        var filter = GetComponent <MeshFilter>();
        var mesh   = new Mesh();

        mesh.name   = "column_mesh";
        filter.mesh = mesh;
        mesh.Clear();

        var ring1 = createPoly(transform.position + Vector3.back, sides, radius * 0.5f);
        var ring2 = createPoly(transform.position, sides, radius);

        MeshStruct data = new MeshStruct();

        data.vertices = new Vector3[ring1.vertices.Length + ring2.vertices.Length];
        for (int i = 0; i < sides; i++)
        {
            data.vertices[i] = ring1.vertices[i];
        }

        for (int i = sides; i < sides * 2; i++)
        {
            data.vertices[i] = ring2.vertices[i - sides];
        }

        data.uv = new Vector2[data.vertices.Length];

        int[] triangles = new int[sides * 2 * 3];

        for (int t = 0, v = 0; v < sides - 1 /* x ringCount-1 */; t += 6, v++)
        {
            triangles[t]     = v;
            triangles[t + 1] = v + sides + 1;
            triangles[t + 2] = v + sides;
            triangles[t + 3] = v;
            triangles[t + 4] = v + 1;
            triangles[t + 5] = v + sides + 1;
        }

        // Final two triangles
        triangles[triangles.Length - 1] = sides - 1;
        triangles[triangles.Length - 2] = (sides * 2) - 1;
        triangles[triangles.Length - 3] = sides;
        triangles[triangles.Length - 4] = sides - 1;
        triangles[triangles.Length - 5] = sides;
        triangles[triangles.Length - 6] = 0;

        mesh.vertices  = data.vertices;
        mesh.uv        = data.uv;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
    }
Exemplo n.º 10
0
    /// <summary>
    /// 按要求创建网格体信息
    /// </summary>
    /// <param name="binfo"></param>
    /// <param name="size"></param>
    /// <returns></returns>
    public static MeshStruct CreateBeamMesh(BeamInfo binfo)
    {
        Vector3Int _grid = new Vector3Int();

        // 根据【单元格大小】计算【格子个数】
        _grid.x = Mathf.CeilToInt((float)binfo.length / binfo.gridSize) + 1;
        _grid.y = Mathf.CeilToInt((float)binfo.height / binfo.gridSize) + 1;
        _grid.z = Mathf.CeilToInt((float)binfo.wigth / binfo.gridSize) + 1;
        MeshStruct _meshStruct = AllocateMeshArrays(_grid);

        CreateMeshGrid(_grid, binfo, ref _meshStruct);
        return(_meshStruct);
    }
Exemplo n.º 11
0
 /// <summary>
 /// 按要求将网格,得到新的网格
 /// </summary>
 /// <param name="meshData"></param>
 /// <param name=""></param>
 /// <returns></returns>
 public static void CurveMeshStruct(HandleData handle, MeshStruct meshDataTemp, ref MeshStruct meshData)
 {
     for (int i = 0; i < meshData.vertices.Length; i++)
     {
         Vector3 item = meshData.vertices[i];
         //区域内部
         if (item.x > handle.LeftCenterPoint.x && item.x < handle.RightCenterPoint.x)
         {
             Vector3 newCenterPos = BezierUtility.CalculateBezierPoint((item.x - handle.LeftCenterPoint.x) / (handle.RightCenterPoint.x - handle.LeftCenterPoint.x), handle.LeftCenterPoint, handle.HandPoint1, handle.HandPoint2, handle.RightCenterPoint);
             meshData.vertices[i].y = meshDataTemp.vertices[i].y + newCenterPos.y;
         }
     }
 }
Exemplo n.º 12
0
        // Use this for initialization
        public static Mesh BuildMesh(string meshInfo)
        {
            MeshStruct newMesh = CreateMeshStruct(meshInfo);

            //populateMeshStruct(ref newMesh, meshInfo);
            //Debug.Log("t" +newMesh.triangles.Count);
            //Debug.Log("v"  + newMesh.vertices.Count);
            //Debug.Log("fd" +newMesh.faceData.Count);
            Vector3[] newVerts = new Vector3[newMesh.faceData.Count];
            Vector2[] newUVs   = new Vector2[newMesh.faceData.Count];
            //Vector3[] newNormals = new Vector3[newMesh.faceData.Length];

            //try
            //{
            //    int i = 0;
            //    /* The following foreach loops through the facedata and assigns the appropriate vertex, uv, or normal
            //        * for the appropriate Unity mesh array.
            //        */
            //    foreach (Vector3 v in newMesh.faceData)
            //    {
            //        if ((int)v.x <= newMesh.vertices.Count) {
            //            newVerts[i] = newMesh.vertices[(int)v.x];
            //            i++;
            //        }
            //        //if (v.y >= 1)
            //        //    //    newUVs[i] = newMesh.uv[(int)v.y - 1];
            //        //    //if (v.z >= 1)
            //        //    //    newNormals[i] = newMesh.normals[(int)v.z - 1];
            //        //}
            //    }
            //    Debug.Log("Loop Success");
            //}
            //catch (Exception e)
            //{
            //    Debug.Log(e.Message);
            //}

            Mesh mesh = new Mesh
            {
                vertices = newMesh.vertices.ToArray(),
                name     = newMesh.fileName,
                //mesh.uv = newUVs;
                //mesh.normals = newNormals;
                triangles = newMesh.triangles.ToArray()
            };

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            //Debug.Log("Submesh Generated");
            return(mesh);
        }
Exemplo n.º 13
0
        // Use this for initialization
        public static Mesh ImportFile(String filePath)
        {
            if (!File.Exists(filePath))
            {
                Debug.LogWarning($"Unable to load mesh from \"{filePath}\": no file existing there.");
                return(null);
            }
            MeshStruct newMesh = CreateMeshStruct(filePath);

            PopulateMeshStruct(ref newMesh);

            Vector3F[] newVerts   = new Vector3F[newMesh.FaceData.Length];
            Vector2F[] newUVs     = new Vector2F[newMesh.FaceData.Length];
            Vector3F[] newNormals = new Vector3F[newMesh.FaceData.Length];
            Int32      i          = 0;

            /* The following foreach loops through the face data and assigns the appropriate vertex, uv, or normal
             * for the appropriate Unity mesh array.
             */
            foreach (Vector3F v in newMesh.FaceData)
            {
                newVerts[i] = newMesh.Vertices[(Int32)v.X - 1];
                if (v.Y >= 1)
                {
                    newUVs[i] = newMesh.Uv[(Int32)v.Y - 1];
                }

                if (v.Z >= 1)
                {
                    newNormals[i] = newMesh.Normals[(Int32)v.Z - 1];
                }

                i++;
            }

            Mesh mesh = new Mesh
            {
                Vertices  = newVerts,
                UVs       = newUVs,
                Normals   = newNormals,
                Triangles = newMesh.Triangles,
                Name      = newMesh.FileName
            };

            mesh.Apply(false);

            //mesh.RecalculateBounds(); *cough*

            return(mesh);
        }
Exemplo n.º 14
0
            public static MeshStruct Read(BinaryReader br)
            {
                MeshStruct ret = new MeshStruct();

                ret.VertexCount        = br.ReadUInt16();
                ret.Padding            = br.ReadUInt16();
                ret.IndexCount         = br.ReadUInt32();
                ret.MaterialIndex      = br.ReadUInt16();
                ret.SubMeshIndex       = br.ReadUInt16();
                ret.SubMeshCount       = br.ReadUInt16();
                ret.BoneTableIndex     = br.ReadUInt16();
                ret.StartIndex         = br.ReadUInt32();
                ret.VertexBufferOffset = br.ReadStructures <UInt32>(3).ToArray();
                ret.VertexBufferStride = br.ReadBytes(3);
                ret.VertexStreamCount  = br.ReadByte();
                return(ret);
            }
Exemplo n.º 15
0
    MeshStruct createPoly(Vector3 centerPoint, int sides, float r)
    {
        MeshStruct result = new MeshStruct();

        result.vertices = ShapeFactory.UnitRegular(centerPoint, sides, r);
        result.uv       = new Vector2[result.vertices.Length];
        result.normals  = new Vector3[result.vertices.Length];

        for (int i = 0; i < result.vertices.Length; i++)
        {
            var v = result.vertices[i];
            result.uv[i]      = new Vector2(v.x, v.y);
            result.normals[i] = Vector3.back;
        }

        return(result);
    }
Exemplo n.º 16
0
        // Use this for initialization
        public static Mesh ImportFile(String filePath)
        {
            MeshStruct newMesh = CreateMeshStruct(filePath);

            PopulateMeshStruct(ref newMesh);

            Vector3[] newVerts   = new Vector3[newMesh.FaceData.Length];
            Vector2[] newUVs     = new Vector2[newMesh.FaceData.Length];
            Vector3[] newNormals = new Vector3[newMesh.FaceData.Length];
            Int32     i          = 0;

            /* The following foreach loops through the face data and assigns the appropriate vertex, uv, or normal
             * for the appropriate Unity mesh array.
             */
            foreach (Vector3 v in newMesh.FaceData)
            {
                newVerts[i] = newMesh.Vertices[(Int32)v.x - 1];
                if (v.y >= 1)
                {
                    newUVs[i] = newMesh.Uv[(Int32)v.y - 1];
                }

                if (v.z >= 1)
                {
                    newNormals[i] = newMesh.Normals[(Int32)v.z - 1];
                }

                i++;
            }

            Mesh mesh = new Mesh
            {
                vertices  = newVerts,
                uv        = newUVs,
                normals   = newNormals,
                triangles = newMesh.Triangles
            };


            mesh.RecalculateBounds();

            return(mesh);
        }
Exemplo n.º 17
0
    static MeshStruct AllocateMeshArrays(Vector3Int _grid)
    {
        int numVertices = 2 * (
            _grid.x * _grid.z +             //上下
            _grid.y * _grid.z +             //左右
            _grid.x * _grid.y);             // 前后
        int NumTriangle = 2 * 2 * 3 * (
            (_grid.x - 1) * (_grid.z - 1) + //上下
            (_grid.y - 1) * (_grid.z - 1) + //左右
            (_grid.x - 1) * (_grid.y - 1)); // 前后
        MeshStruct _meshStruct = new MeshStruct();

        _meshStruct.vertices  = new Vector3[numVertices];
        _meshStruct.normals   = new Vector3[numVertices];
        _meshStruct.tangents  = new Vector4[numVertices];
        _meshStruct.Colors32  = new Color32[numVertices];
        _meshStruct.uvs       = new Vector2[numVertices];
        _meshStruct.triangles = new int[NumTriangle];
        return(_meshStruct);
    }
Exemplo n.º 18
0
        // Use this for initialization
        public Mesh ImportFile(string filePath)
        {
            MeshStruct newMesh = createMeshStruct(filePath);

            populateMeshStruct(ref newMesh);

            Vector3[] newVerts   = new Vector3[newMesh.faceData.Length];
            Vector2[] newUVs     = new Vector2[newMesh.faceData.Length];
            Vector3[] newNormals = new Vector3[newMesh.faceData.Length];
            int       i          = 0;

            /* The following foreach loops through the facedata and assigns the appropriate vertex, uv, or normal
             * for the appropriate Unity mesh array.
             */
            foreach (Vector3 v in newMesh.faceData)
            {
                newVerts[i] = newMesh.vertices[(int)v.x - 1];
                if (v.y >= 1)
                {
                    newUVs[i] = newMesh.uv[(int)v.y - 1];
                }

                if (v.z >= 1)
                {
                    newNormals[i] = newMesh.normals[(int)v.z - 1];
                }
                i++;
            }

            Mesh mesh = new Mesh();

            mesh.vertices  = newVerts;
            mesh.uv        = newUVs;
            mesh.normals   = newNormals;
            mesh.triangles = newMesh.triangles;

            mesh.RecalculateBounds();
            mesh.Optimize();

            return(mesh);
        }
Exemplo n.º 19
0
    void Awake()
    {
        foreach (Renderer m in gameObject.GetComponentsInChildren <Renderer>())
        {
            MeshStruct nS = new MeshStruct();
            nS.mesh        = m;
            nS.originalMat = m.material;
            meshS.Add(nS);
        }

        //flashMaterial[0] = White
        flashMaterial.Add(Resources.Load <Material>("FlashMatWhite"));
        //flashMaterial[1] = Red
        flashMaterial.Add(Resources.Load <Material>("FlashMatRed"));
        //flashMaterial[2] = Yellow
        flashMaterial.Add(Resources.Load <Material>("FlashMatYellow"));
        //flashMaterial[3] = Green
        flashMaterial.Add(Resources.Load <Material>("FlashMatGreen"));
        //flashMaterial[4] = Blue
        flashMaterial.Add(Resources.Load <Material>("FlashMatBlue"));
    }
Exemplo n.º 20
0
 public void DeserializeDump(byte[] raw, int start)
 {
     Mesh      = new MeshStruct();
     readerpos = start;
     try
     {
         ReadBoundings(raw);
         ReadkDOPTree(raw);
         ReadRawTris(raw);
         ReadMaterials(raw);
         ReadVerts(raw);
         ReadBuffers(raw);
         ReadEdges(raw);
         UnknownPart(raw);
         ReadIndexBuffer(raw);
         ReadEnd(raw);
     }
     catch (Exception e)
     {
         DebugLog.PrintLn("StaticMesh #" + MyIndex + " Error: " + e.Message);
     }
 }
Exemplo n.º 21
0
    /// <summary>
    /// 创建出一个面的mesh数据
    /// </summary>
    /// <param name="axisX"></param>
    /// <param name="axisY"></param>
    /// <param name="vx"></param>
    /// <param name="vy"></param>
    /// <param name="vz"></param>
    /// <param name="uvstapX"></param>
    /// <param name="uvstapY"></param>
    /// <param name="normal"></param>
    static void CreateMeshPlane(int axisX, int axisY,
                                System.Func <int, int, float> vx,
                                System.Func <int, int, float> vy,
                                System.Func <int, int, float> vz,
                                System.Func <float, float, float, float> vuvx,
                                System.Func <float, float, float, float> vuvy,
                                Vector3 normal, ref MeshStruct _meshStruct, bool revers = false)
    {
        int start = vIndex;

        for (int j = 0; j < axisY; j++)
        {
            for (int i = 0; i < axisX; i++)
            {
                // Set vertices
                _meshStruct.vertices[vIndex].x = vx(i, j);
                _meshStruct.vertices[vIndex].y = vy(i, j);
                _meshStruct.vertices[vIndex].z = vz(i, j);

                // Set triangles
                if (j < axisY - 1 && i < axisX - 1)
                {
                    _meshStruct.triangles[tIndex + 0] = revers ? (j * axisX) + i + 1 + start : (j * axisX) + i + start;
                    _meshStruct.triangles[tIndex + 1] = ((j + 1) * axisX) + i + start;
                    _meshStruct.triangles[tIndex + 2] = revers ? (j * axisX) + i + start : (j * axisX) + i + 1 + start;

                    _meshStruct.triangles[tIndex + 3] = revers ? (j * axisX) + i + 1 + start : ((j + 1) * axisX) + i + start;
                    _meshStruct.triangles[tIndex + 4] = ((j + 1) * axisX) + i + 1 + start;
                    _meshStruct.triangles[tIndex + 5] = revers ? ((j + 1) * axisX) + i + start : (j * axisX) + i + 1 + start;

                    tIndex += 6;
                }
                float uvx = vuvx(_meshStruct.vertices[vIndex].x, _meshStruct.vertices[vIndex].y, _meshStruct.vertices[vIndex].z); //uvRect.x + i * uvstapX * uvRect.width;
                float uvy = vuvy(_meshStruct.vertices[vIndex].x, _meshStruct.vertices[vIndex].y, _meshStruct.vertices[vIndex].z); //uvRect.y + j * uvstapY * uvRect.height;
                SetOther(uvx, uvy, normal, ref _meshStruct);
                vIndex++;
            }
        }
    }
Exemplo n.º 22
0
    static void SetOther(float uvx, float uvy, Vector3 normal, ref MeshStruct _meshStruct)
    {
        //Color32 colorOne = new Color32(255, 255, 255, 255);
        bool    setTangents = true;
        Vector4 tangent     = new Vector4(1f, 0f, 0f, 1f);

        // Set UV
        _meshStruct.uvs[vIndex].x = uvx;
        _meshStruct.uvs[vIndex].y = uvy;

        //// Set colors
        //_meshStruct.Colors32[vIndex] = colorOne;

        // Set normals
        _meshStruct.normals[vIndex] = normal;

        if (setTangents)
        {
            // set tangents
            _meshStruct.tangents[vIndex] = tangent;
        }
    }
Exemplo n.º 23
0
        /// <summary>
        /// Imports a mesh from the raw string content of an obj mesh file.
        /// </summary>
        public static Mesh ImportMesh(string fileText)
        {
            MeshStruct newMesh = CreateMeshStruct(fileText);

            PopulateMeshStruct(ref newMesh, fileText);

            Vector3[] newVerts   = new Vector3[newMesh.faceData.Length];
            Vector2[] newUVs     = new Vector2[newMesh.faceData.Length];
            Vector3[] newNormals = new Vector3[newMesh.faceData.Length];
            int       i          = 0;

            foreach (Vector3 v in newMesh.faceData)
            {
                newVerts[i] = newMesh.vertices[(int)v.x - 1];
                if (v.y >= 1)
                {
                    newUVs[i] = newMesh.uv[(int)v.y - 1];
                }

                if (v.z >= 1)
                {
                    newNormals[i] = newMesh.normals[(int)v.z - 1];
                }
                i++;
            }

            Mesh mesh = new Mesh
            {
                vertices  = newVerts,
                uv        = newUVs,
                normals   = newNormals,
                triangles = newMesh.triangles
            };

            mesh.RecalculateBounds();

            return(mesh);
        }
Exemplo n.º 24
0
    public void truncateLastMesh()
    {
        MeshStruct lastMesh = reorganizedMeshes[curMeshIdx];

        Vector3[] truncatedVB  = new Vector3[destBufferIdx];
        Vector2[] truncatedUV  = new Vector2[destBufferIdx];
        Vector3[] truncatedNRM = new Vector3[destBufferIdx];
        int[]     truncatedIB  = new int[destBufferIdx];


        Array.Copy(lastMesh.vertices, 0, truncatedVB, 0, destBufferIdx);
        Array.Copy(lastMesh.uv, 0, truncatedUV, 0, destBufferIdx);
        Array.Copy(lastMesh.triangles, 0, truncatedIB, 0, destBufferIdx);

        Array.Copy(lastMesh.normals, 0, truncatedNRM, 0, destBufferIdx);

        lastMesh.vertices  = truncatedVB;
        lastMesh.uv        = truncatedUV;
        lastMesh.triangles = truncatedIB;
        lastMesh.normals   = truncatedNRM;

        reorganizedMeshes[curMeshIdx] = lastMesh;
    }
Exemplo n.º 25
0
        private static MeshStruct createMeshStruct(string filename)
        {
            int        triangles = 0;
            int        vertices  = 0;
            int        vt        = 0;
            int        vn        = 0;
            int        face      = 0;
            MeshStruct mesh      = new MeshStruct();

            mesh.fileName = filename;
            StreamReader stream     = File.OpenText(filename);
            string       entireText = stream.ReadToEnd();

            stream.Close();
            using (StringReader reader = new StringReader(entireText))
            {
                string   currentText     = reader.ReadLine();
                char[]   splitIdentifier = { ' ' };
                string[] brokenString;
                while (currentText != null)
                {
                    if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ") &&
                        !currentText.StartsWith("vn "))
                    {
                        currentText = reader.ReadLine();
                        if (currentText != null)
                        {
                            currentText = currentText.Replace("  ", " ");
                        }
                    }
                    else
                    {
                        currentText  = currentText.Trim();                          //Trim the current line
                        brokenString = currentText.Split(splitIdentifier, 50);      //Split the line into an array, separating the original line by blank spaces
                        switch (brokenString[0])
                        {
                        case "v":
                            vertices++;
                            break;

                        case "vt":
                            vt++;
                            break;

                        case "vn":
                            vn++;
                            break;

                        case "f":
                            face      = face + brokenString.Length - 1;
                            triangles = triangles + 3 * (brokenString.Length - 2);     /*brokenString.Length is 3 or greater since a face must have at least
                                                                                        * 3 vertices.  For each additional vertice, there is an additional
                                                                                        * triangle in the mesh (hence this formula).*/
                            break;
                        }
                        currentText = reader.ReadLine();
                        if (currentText != null)
                        {
                            currentText = currentText.Replace("  ", " ");
                        }
                    }
                }
            }
            mesh.triangles = new int[triangles];
            mesh.vertices  = new Vector3[vertices];
            mesh.uv        = new Vector2[vt];
            mesh.normals   = new Vector3[vn];
            mesh.faceData  = new Vector3[face];
            return(mesh);
        }
Exemplo n.º 26
0
        private static void populateMeshStruct(ref MeshStruct mesh)
        {
            StreamReader stream     = File.OpenText(mesh.fileName);
            string       entireText = stream.ReadToEnd();

            stream.Close();
            using (StringReader reader = new StringReader(entireText))
            {
                string currentText = reader.ReadLine();

                char[]   splitIdentifier  = { ' ' };
                char[]   splitIdentifier2 = { '/' };
                string[] brokenString;
                string[] brokenBrokenString;
                int      f   = 0;
                int      f2  = 0;
                int      v   = 0;
                int      vn  = 0;
                int      vt  = 0;
                int      vt1 = 0;
                int      vt2 = 0;
                while (currentText != null)
                {
                    if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ") &&
                        !currentText.StartsWith("vn ") && !currentText.StartsWith("g ") && !currentText.StartsWith("usemtl ") &&
                        !currentText.StartsWith("mtllib ") && !currentText.StartsWith("vt1 ") && !currentText.StartsWith("vt2 ") &&
                        !currentText.StartsWith("vc ") && !currentText.StartsWith("usemap "))
                    {
                        currentText = reader.ReadLine();
                        if (currentText != null)
                        {
                            currentText = currentText.Replace("  ", " ");
                        }
                    }
                    else
                    {
                        currentText  = currentText.Trim();
                        brokenString = currentText.Split(splitIdentifier, 50);
                        switch (brokenString[0])
                        {
                        case "g":
                            break;

                        case "usemtl":
                            break;

                        case "usemap":
                            break;

                        case "mtllib":
                            break;

                        case "v":
                            mesh.vertices[v] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
                                                           System.Convert.ToSingle(brokenString[3]));
                            v++;
                            break;

                        case "vt":
                            mesh.uv[vt] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
                            vt++;
                            break;

                        case "vt1":
                            mesh.uv[vt1] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
                            vt1++;
                            break;

                        case "vt2":
                            mesh.uv[vt2] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
                            vt2++;
                            break;

                        case "vn":
                            mesh.normals[vn] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
                                                           System.Convert.ToSingle(brokenString[3]));
                            vn++;
                            break;

                        case "vc":
                            break;

                        case "f":

                            int        j        = 1;
                            List <int> intArray = new List <int>();
                            while (j < brokenString.Length && ("" + brokenString[j]).Length > 0)
                            {
                                Vector3 temp = new Vector3();
                                brokenBrokenString = brokenString[j].Split(splitIdentifier2, 3);        //Separate the face into individual components (vert, uv, normal)
                                temp.x             = System.Convert.ToInt32(brokenBrokenString[0]);
                                if (brokenBrokenString.Length > 1)                                      //Some .obj files skip UV and normal
                                {
                                    if (brokenBrokenString[1] != "")                                    //Some .obj files skip the uv and not the normal
                                    {
                                        temp.y = System.Convert.ToInt32(brokenBrokenString[1]);
                                    }
                                    temp.z = System.Convert.ToInt32(brokenBrokenString[2]);
                                }
                                j++;

                                mesh.faceData[f2] = temp;
                                intArray.Add(f2);
                                f2++;
                            }
                            j = 1;
                            while (j + 2 < brokenString.Length)         //Create triangles out of the face data.  There will generally be more than 1 triangle per face.
                            {
                                mesh.triangles[f] = intArray[0];
                                f++;
                                mesh.triangles[f] = intArray[j];
                                f++;
                                mesh.triangles[f] = intArray[j + 1];
                                f++;

                                j++;
                            }
                            break;
                        }
                        currentText = reader.ReadLine();
                        if (currentText != null)
                        {
                            currentText = currentText.Replace("  ", " ");       //Some .obj files insert double spaces, this removes them.
                        }
                    }
                }
            }
        }
Exemplo n.º 27
0
 public void DeserializeDump(byte[] raw)
 {
     Mesh = new MeshStruct();
     readerpos = 0;
     try
     {
         ReadBoundings(raw);
         ReadkDOPTree(raw);
         ReadRawTris(raw);
         ReadMaterials(raw);
         ReadVerts(raw);
         ReadBuffers(raw);
         ReadEdges(raw);
         UnknownPart(raw);
         ReadIndexBuffer(raw);
         ReadEnd(raw);
     }
     catch (Exception e)
     {
         DebugOutput.PrintLn("StaticMesh Error: " + e.Message);
     }
 }
Exemplo n.º 28
0
        //private static MeshStruct createMeshStruct(string meshInfo)
        //{
        //    int triangles = 0;
        //    int vertices = 0;
        //    int vt = 0;
        //    int vn = 0;
        //    int face = 0;
        //    MeshStruct mesh = new MeshStruct();
        //    mesh.fileName = meshInfo.GetHashCode().ToString();
        //    using (StringReader reader = new StringReader(meshInfo))
        //    {
        //        string currentText = reader.ReadLine();
        //        char[] splitIdentifier = { ' ' };
        //        string[] brokenString;
        //        while (currentText != null)
        //        {
        //            if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ")
        //                && !currentText.StartsWith("vn "))
        //            {
        //                currentText = reader.ReadLine();
        //                if (currentText != null)
        //                {
        //                    currentText = currentText.Replace("  ", " ");
        //                }
        //            }
        //            else
        //            {
        //                currentText = currentText.Trim();                           //Trim the current line
        //                brokenString = currentText.Split(splitIdentifier);      //Split the line into an array, separating the original line by blank spaces
        //                switch (brokenString[0])
        //                {
        //                    case "v":
        //                        vertices++;
        //                        break;
        //                    case "vt":
        //                        vt++;
        //                        break;
        //                    //case "vn":
        //                    //    vn++;
        //                    //    break;
        //                    case "f":
        //                        face = face + brokenString.Length - 1;
        //                        triangles = triangles + 3 * (brokenString.Length - 2); /*brokenString.Length is 3 or greater since a face must have at least
        //                                                                             3 vertices.  For each additional vertice, there is an additional
        //                                                                             triangle in the mesh (hence this formula).*/
        //                        break;
        //                }
        //                currentText = reader.ReadLine();
        //                if (currentText != null)
        //                {
        //                    currentText = currentText.Replace("  ", " ");
        //                }
        //            }
        //        }
        //    }
        //    mesh.triangles = new int[triangles];
        //    mesh.vertices = new Vector3[vertices];
        //    mesh.uv = new Vector2[vt];
        //    //mesh.normals = new Vector3[vn];
        //    mesh.faceData = new Vector3[face];
        //    Debug.Log("Created Mesh Struct");
        //    return mesh;
        //}

        //private static void populateMeshStruct(ref MeshStruct mesh, string meshInfo)
        //{
        //    int faceSubtract = 0;
        //    using (StringReader reader = new StringReader(meshInfo))
        //    {
        //        string currentText = reader.ReadLine();

        //        char[] splitIdentifier = { ' ' };
        //        char[] splitIdentifier2 = { '/' };
        //        string[] brokenString;
        //        string[] brokenBrokenString;
        //        int f = 0;
        //        int f2 = 0;
        //        int v = 0;
        //        int vn = 0;
        //        int vt = 0;
        //        int vt1 = 0;
        //        int vt2 = 0;
        //        bool _isFirst = true;
        //        Debug.Log("Starting");
        //        while (currentText != null)
        //        {
        //            if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ") &&
        //                !currentText.StartsWith("vn ") && !currentText.StartsWith("g ") && !currentText.StartsWith("usemtl ") &&
        //                !currentText.StartsWith("mtllib ") && !currentText.StartsWith("vt1 ") && !currentText.StartsWith("vt2 ") &&
        //                !currentText.StartsWith("vc ") && !currentText.StartsWith("usemap "))
        //            {
        //                currentText = reader.ReadLine();
        //                if (currentText != null)
        //                {
        //                    currentText = currentText.Replace("  ", " ");
        //                }
        //            }
        //            else
        //            {
        //                currentText = currentText.Trim();
        //                brokenString = currentText.Split(splitIdentifier, 50);
        //                switch (brokenString[0])
        //                {
        //                    case "g":
        //                        break;
        //                    case "usemtl":
        //                        break;
        //                    case "usemap":
        //                        break;
        //                    case "mtllib":
        //                        break;
        //                    case "v":
        //                        mesh.vertices[v] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
        //                                                    System.Convert.ToSingle(brokenString[3]));
        //                        v++;
        //                        break;
        //                    case "vt":
        //                        mesh.uv[vt] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
        //                        vt++;
        //                        break;
        //                    case "vt1":
        //                        mesh.uv[vt1] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
        //                        vt1++;
        //                        break;
        //                    case "vt2":
        //                        mesh.uv[vt2] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
        //                        vt2++;
        //                        break;
        //                    //case "vn":
        //                    //    mesh.normals[vn] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
        //                    //                            System.Convert.ToSingle(brokenString[3]));
        //                        vn++;
        //                        break;
        //                    case "vc":
        //                        break;
        //                    case "f":
        //                        int j = 1;
        //                        List<int> intArray = new List<int>();
        //                        while (j < brokenString.Length && ("" + brokenString[j]).Length > 0)
        //                        {
        //                            Vector3 temp = new Vector3();
        //                            brokenBrokenString = brokenString[j].Split(splitIdentifier2, 3);    //Separate the face into individual components (vert, uv, normal)
        //                            temp.x = System.Convert.ToInt32(brokenBrokenString[0]) - faceSubtract;
        //                            if (_isFirst)
        //                            {
        //                                faceSubtract = System.Convert.ToInt32(temp.x) - 1;
        //                                temp.x = 1;
        //                                _isFirst = false;
        //                            }
        //                            if (brokenBrokenString.Length > 1)                                  //Some .obj files skip UV and normal
        //                            {
        //                                if (brokenBrokenString[1] != "")                                    //Some .obj files skip the uv and not the normal
        //                                {
        //                                    temp.y = System.Convert.ToInt32(brokenBrokenString[1]) - faceSubtract;
        //                                }
        //                                temp.z = System.Convert.ToInt32(brokenBrokenString[2]) - faceSubtract;
        //                            }
        //                            j++;

        //                            mesh.faceData[f2] = temp;
        //                            intArray.Add(f2);
        //                            f2++;
        //                        }
        //                        j = 1;
        //                        while (j + 2 < brokenString.Length)     //Create triangles out of the face data.  There will generally be more than 1 triangle per face.
        //                        {
        //                            mesh.triangles[f] = intArray[0];
        //                            f++;
        //                            mesh.triangles[f] = intArray[j];
        //                            f++;
        //                            mesh.triangles[f] = intArray[j + 1];
        //                            f++;

        //                            j++;
        //                        }
        //                        break;
        //                }
        //                currentText = reader.ReadLine();
        //                if (currentText != null)
        //                {
        //                    currentText = currentText.Replace("  ", " ");       //Some .obj files insert double spaces, this removes them.
        //                }
        //            }
        //        }

        //        Debug.Log("Generated");
        //    }


        //}

        private static MeshStruct CreateMeshStruct(string meshInfo)
        {
            MeshStruct mesh = new MeshStruct
            {
                vertices  = new List <Vector3>(),
                normals   = new List <Vector3>(),
                faceData  = new List <Vector3>(),
                triangles = new List <int>()
            };

            char[] splitidentifier = { ' ', '/' };
            bool   _isFirst        = true;
            int    f            = 0;
            int    faceSubtract = 0;

            //Debug.Log("Starting Reader");
            using (StringReader reader = new StringReader(meshInfo))
            {
                string line = reader.ReadLine();
                line = line.Trim();
                while (line != null || !string.IsNullOrEmpty(line))
                {
                    List <string> words = line.Split(splitidentifier).ToList();
                    if (words.Count > 0)
                    {
                        if (words.Count > 3)
                        {
                            switch (words[0])
                            {
                            case "v":
                            {
                                mesh.vertices.Add(new Vector3(Convert.ToSingle(words[1]), Convert.ToSingle(words[2]),
                                                              System.Convert.ToSingle(words[3])));
                                break;
                            }

                            case "vn":
                            {
                                mesh.normals.Add(new Vector3(Convert.ToSingle(words[1]), Convert.ToSingle(words[2]),
                                                             System.Convert.ToSingle(words[3])));
                                break;
                            }

                            case "f":
                            {
                                //Debug.Log("Face Word Count" + words.Count);
                                if (words.Count > 9)
                                {
                                    //Debug.Log(string.Join(",", words.ToArray()));
                                    if (_isFirst)
                                    {
                                        int first = Convert.ToInt32(words[1]);
                                        faceSubtract = first - 1;
                                        _isFirst     = false;
                                    }
                                    for (int i = 1; i < 8;)
                                    {
                                        // f// f// f//
                                        try
                                        {
                                            Vector3 a = new Vector3(Convert.ToInt32(words[i]) - faceSubtract,
                                                                    0,
                                                                    0);
                                            if (a.x <= mesh.vertices.Count)
                                            {
                                                mesh.faceData.Add(a);
                                                mesh.triangles.Add(f++);
                                                i += 3;
                                            }
                                        }
                                        catch (FormatException e)
                                        {
                                            Debug.Log("Exception here " + e.Message);
                                        }
                                    }
                                }

                                break;
                            }
                            }
                        }
                        line = reader.ReadLine();
                    }
                }
            }
            return(mesh);
        }
Exemplo n.º 29
0
        private static void PopulateMeshStruct(ref MeshStruct mesh)
        {
            StreamReader stream     = File.OpenText(mesh.FileName);
            String       entireText = stream.ReadToEnd();

            stream.Close();
            using (StringReader reader = new StringReader(entireText))
            {
                String currentText = reader.ReadLine();

                Char[] splitIdentifier  = { ' ' };
                Char[] splitIdentifier2 = { '/' };
                UInt32 f   = 0;
                UInt32 f2  = 0;
                UInt32 v   = 0;
                UInt32 vn  = 0;
                UInt32 vt  = 0;
                UInt32 vt1 = 0;
                UInt32 vt2 = 0;
                while (currentText != null)
                {
                    if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ") &&
                        !currentText.StartsWith("vn ") && !currentText.StartsWith("g ") && !currentText.StartsWith("usemtl ") &&
                        !currentText.StartsWith("mtllib ") && !currentText.StartsWith("vt1 ") && !currentText.StartsWith("vt2 ") &&
                        !currentText.StartsWith("vc ") && !currentText.StartsWith("usemap "))
                    {
                        currentText = reader.ReadLine();
                        currentText = currentText?.Replace("  ", " ");
                    }
                    else
                    {
                        currentText = currentText.Trim();
                        String[] brokenString = currentText.Split(splitIdentifier, 50);
                        // ReSharper disable once SwitchStatementMissingSomeCases
                        switch (brokenString[0])
                        {
                        case "g":
                            break;

                        case "usemtl":
                            break;

                        case "usemap":
                            break;

                        case "mtllib":
                            break;

                        case "v":
                            mesh.Vertices[v] = new Vector3F(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]),
                                                            Convert.ToSingle(brokenString[3]));
                            v++;
                            break;

                        case "vt":
                            mesh.Uv[vt] = /*Vector2F.Right - */ new Vector2F(1.0F - Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]));
                            vt++;
                            break;

                        case "vt1":
                            mesh.Uv[vt1] = /*Vector2F.Right - */ new Vector2F(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]));
                            vt1++;
                            break;

                        case "vt2":
                            mesh.Uv[vt2] = /*Vector2F.Right - */ new Vector2F(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]));
                            vt2++;
                            break;

                        case "vn":
                            mesh.Normals[vn] = new Vector3F(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]),
                                                            Convert.ToSingle(brokenString[3]));
                            vn++;

                            break;

                        case "vc":
                            break;

                        case "f":

                            Int32         j        = 1;
                            List <UInt32> intArray = new List <UInt32>();
                            while (j < brokenString.Length && ("" + brokenString[j]).Length > 0)
                            {
                                Vector3F temp = new Vector3F();
                                String[] brokenBrokenString = brokenString[j].Split(splitIdentifier2, 3);
                                temp.X = Convert.ToUInt32(brokenBrokenString[0]);
                                if (brokenBrokenString.Length > 1)                                      //Some .obj files skip UV and normal
                                {
                                    if (brokenBrokenString[1] != "")                                    //Some .obj files skip the uv and not the normal
                                    {
                                        temp.Y = Convert.ToInt32(brokenBrokenString[1]);
                                    }
                                    temp.Z = Convert.ToInt32(brokenBrokenString[2]);
                                }
                                j++;

                                mesh.FaceData[f2] = temp;
                                intArray.Add(f2);
                                f2++;
                            }
                            j = 1;
                            while (j + 2 < brokenString.Length)         //Create triangles out of the face data.  There will generally be more than 1 triangle per face.
                            {
                                mesh.Triangles[f] = intArray[0];
                                f++;
                                mesh.Triangles[f] = intArray[j];
                                f++;
                                mesh.Triangles[f] = intArray[j + 1];
                                f++;

                                j++;
                            }
                            break;
                        }
                        currentText = reader.ReadLine();
                        currentText = currentText?.Replace("  ", " ");       //Some .obj files insert Double spaces, this removes them.
                    }
                }
            }
        }
Exemplo n.º 30
0
        private static MeshStruct CreateMeshStruct(String filename)
        {
            Int32      triangles = 0;
            Int32      vertices  = 0;
            Int32      vt        = 0;
            Int32      vn        = 0;
            Int32      face      = 0;
            MeshStruct mesh      = new MeshStruct {
                FileName = filename
            };
            StreamReader stream     = File.OpenText(filename);
            String       entireText = stream.ReadToEnd();

            stream.Close();
            using (StringReader reader = new StringReader(entireText))
            {
                String currentText     = reader.ReadLine();
                Char[] splitIdentifier = { ' ' };
                while (currentText != null)
                {
                    if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ") &&
                        !currentText.StartsWith("vn "))
                    {
                        currentText = reader.ReadLine();
                        currentText = currentText?.Replace("  ", " ");
                    }
                    else
                    {
                        currentText = currentText.Trim();                           //Trim the current line
                        String[] brokenString = currentText.Split(splitIdentifier, 50);
                        // ReSharper disable once SwitchStatementMissingSomeCases
                        switch (brokenString[0])
                        {
                        case "v":
                            vertices++;
                            break;

                        case "vt":
                            vt++;
                            break;

                        case "vn":
                            vn++;
                            break;

                        case "f":
                            face       = face + brokenString.Length - 1;
                            triangles += 3 * (brokenString.Length - 2);     /*brokenString.Length is 3 or greater since a face must have at least
                                                                             *       3 vertices.  For each additional vertex, there is an additional
                                                                             *       triangle in the mesh (hence this formula).*/
                            break;
                        }
                        currentText = reader.ReadLine();
                        currentText = currentText?.Replace("  ", " ");
                    }
                }
            }
            mesh.Triangles = new UInt32[triangles];
            mesh.Vertices  = new Vector3F[vertices];
            mesh.Uv        = new Vector2F[vt];
            mesh.Normals   = new Vector3F[vn];
            mesh.FaceData  = new Vector3F[face];
            return(mesh);
        }