public const float MaximumHeight = 350; /* heighest point in Netherlands */

    public static Mesh CreateMesh(Stream pStream, String pName)
    {
        try
        {
            var       decoder         = new QuantizedMeshFormatDecoder(pStream, false);
            Vector3[] terrainVertices = new Vector3[decoder.VertexList.Length];
            for (int index = 0; index < terrainVertices.Length; index++)
            {
                float heightInMeter    = decoder.GetHeightInMeter(index);
                float normalizedHeight = Mathf.InverseLerp(MinimumHeight, MaximumHeight, heightInMeter);

                terrainVertices[index] = new Vector3(
                    (float)QuantizedMeshFormatDecoder.Normalize(decoder.VertexList[index].u),
                    normalizedHeight,
                    (float)QuantizedMeshFormatDecoder.Normalize(decoder.VertexList[index].v));
            }

            int[] terrainTriangles = new int[decoder.TriangleIndices.Length];
            for (int index = 0; index < terrainTriangles.Length / 3; index++)
            {
                // Inverse traingle direction (frontface / backface)
                int offset = (index * 3);
                terrainTriangles[offset + 0] = (int)decoder.TriangleIndices[offset + 0];
                terrainTriangles[offset + 1] = (int)decoder.TriangleIndices[offset + 2];
                terrainTriangles[offset + 2] = (int)decoder.TriangleIndices[offset + 1];
            }

            Vector2[] terrainUV = new Vector2[decoder.VertexList.Length];
            for (int index = 0; index < terrainVertices.Length; index++)
            {
                terrainUV[index] = new Vector2(
                    (float)QuantizedMeshFormatDecoder.Normalize(decoder.VertexList[index].u),
                    (float)QuantizedMeshFormatDecoder.Normalize(decoder.VertexList[index].v));
            }

            // Replace quad mesh with own mesh

            Mesh terreinMesh = new Mesh()
            {
                name      = pName,
                vertices  = terrainVertices,
                triangles = terrainTriangles,
                uv        = terrainUV
            };


            terreinMesh.RecalculateBounds();
            terreinMesh.RecalculateNormals(); // Could also be extracted from terrain file; but this is easer

            return(terreinMesh);
        }
        catch (Exception ex)
        {
            Mesh terreinMesh = CreateEmptyQuad(pName + "_corruptstream");
            return(terreinMesh);
        }
    }
예제 #2
0
 static void Main(string[] args)
 {
     //var decoder = new QuantizedMeshFormatDecoder(new System.IO.FileInfo(@"C:\\development\\quantized-mesh-decoder-master\\src\\assets\\tile-with-extensions.terrain", false));
     var decoder = new QuantizedMeshFormatDecoder(new System.IO.FileInfo(@"C:\dem\DenHaag\output\14\8385\10979.terrain"), false);
 }