Vector3[] GetVertices()
        {
            int numWallVertices = totalVertsPerCorner * outlines.Sum(outline => outline.Length);
            var vertices        = new Vector3[numWallVertices];

            int vertexIndex = 0;

            foreach (Vector3[] outline in outlines)
            {
                for (int i = 0; i < outline.Length; i++)
                {
                    Vector3 vertex             = outline[i];
                    Vector3 normal             = ComputeNormal(outline, i);
                    float   x                  = vertex.x;
                    float   z                  = vertex.z;
                    float   floorHeight        = floorHeightMap.GetHeight(x, z);
                    float   ceilingHeight      = ceilingHeightMap.GetHeight(x, z);
                    float   interpolationScale = 1 / (totalVertsPerCorner - 1f);

                    Vector3 ceilingVertex = new Vector3(x, ceilingHeight, z);
                    if (wallModule.AdjustCeilingCorners)
                    {
                        ceilingVertex = wallModule.GetAdjustedCorner(ceilingVertex, normal, floorHeight, ceilingHeight);
                        if (wallModule.AutoCorrectCornerHeights)
                        {
                            ceilingVertex.y = ceilingHeightMap.GetHeight(ceilingVertex.x, ceilingVertex.z);
                        }
                    }
                    vertices[vertexIndex++] = ceilingVertex;

                    for (int j = 0; j < extraVertsPerCorner; j++)
                    {
                        float interpolation = (j + 1) * interpolationScale;
                        vertex.y = Mathf.Lerp(ceilingHeight, floorHeight, interpolation);
                        vertex.y = interpolation * floorHeight + (1 - interpolation) * ceilingHeight;
                        vertices[vertexIndex++] = wallModule.GetAdjustedCorner(vertex, normal, floorHeight, ceilingHeight);
                    }

                    Vector3 floorVertex = new Vector3(x, floorHeight, z);
                    if (wallModule.AdjustFloorCorners)
                    {
                        floorVertex = wallModule.GetAdjustedCorner(floorVertex, normal, floorHeight, ceilingHeight);
                        if (wallModule.AutoCorrectCornerHeights)
                        {
                            floorVertex.y = floorHeightMap.GetHeight(floorVertex.x, floorVertex.z);
                        }
                    }
                    vertices[vertexIndex++] = floorVertex;
                }
            }
            return(vertices);
        }
예제 #2
0
        public IPhysicsObject CreateHeightmap(IHeightMap heightMapInfo, float scale, float shiftx, float shifty, float heightscale)
        {
            CollisionSkin collision = new CollisionSkin(null);
            Body          _body     = new Body();

            Array2D field = new Array2D(heightMapInfo.Size.X, heightMapInfo.Size.Y);

            for (int x = 0; x < field.Nx; x++)
            {
                for (int z = 0; z < field.Nz; z++)
                {
                    field.SetAt(x, z, heightscale * heightMapInfo.GetHeight(x, z));
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            _body.MoveTo(new Vector3(shiftx, 0, shifty), Matrix4.Identity);

            //collision.AddPrimitive(new Heightmap(field, shift.X, shift.Y, heightMapInfo.terrainScale, heightMapInfo.terrainScale), new MaterialProperties(0.7f, 0.7f, 0.6f));
            collision.AddPrimitive(new Heightmap(field, shiftx, shifty, scale, scale), new MaterialProperties(0.7f, 0.7f, 0.6f));

            _body.CollisionSkin = collision;
            _body.Immovable     = true;
            _body.EnableBody();

            return(new JigLibObject(_body));
        }
예제 #3
0
        Vector3[] GetVertices()
        {
            int numWallVertices = totalVertsPerCorner * outlines.Sum(outline => outline.Length);
            var vertices        = new Vector3[numWallVertices];
            var context         = new VertexContext(floorHeightMap, ceilingHeightMap, totalVertsPerCorner);

            int vertexIndex = 0;

            foreach (Vector3[] outline in outlines)
            {
                for (int i = 0; i < outline.Length; i++)
                {
                    Vector3 vertex             = outline[i];
                    Vector3 normal             = ComputeNormal(outline, i);
                    float   x                  = vertex.x;
                    float   z                  = vertex.z;
                    float   floorHeight        = floorHeightMap.GetHeight(x, z);
                    float   ceilingHeight      = ceilingHeightMap.GetHeight(x, z);
                    float   interpolationScale = 1 / (totalVertsPerCorner - 1f);

                    for (int j = 0; j < totalVertsPerCorner; j++)
                    {
                        float interpolation = j * interpolationScale;
                        vertex.y = Mathf.Lerp(ceilingHeight, floorHeight, interpolation);
                        vertex.y = interpolation * floorHeight + (1 - interpolation) * ceilingHeight;
                        context.Update(vertex, normal, j);
                        vertices[vertexIndex++] = wallModule.GetAdjustedCorner(context);
                    }
                }
            }
            return(vertices);
        }
 static void ApplyHeightMap(Vector3[] vertices, IHeightMap heightMap)
 {
     for (int i = 0; i < vertices.Length; i++)
     {
         Vector3 vertex = vertices[i];
         vertices[i].y = heightMap.GetHeight(vertex.x, vertex.z);
     }
 }
예제 #5
0
 void ApplyHeightMap()
 {
     if (heightMap != null)
     {
         for (int i = 0; i < mesh.vertices.Length; i++)
         {
             Vector3 vertex = mesh.vertices[i];
             mesh.vertices[i].y += heightMap.GetHeight(vertex.x, vertex.z);
         }
     }
 }
 void ApplyHeightMap()
 {
     if (!heightMap.IsSimple)
     {
         for (int i = 0; i < mesh.vertices.Length; i++)
         {
             Vector3 vertex = mesh.vertices[i];
             mesh.vertices[i].y -= heightMap.GetHeight(vertex.x, vertex.z);
         }
     }
 }
 void ApplyHeightMap()
 {
     if (!heightMap.IsSimple)
     {
         Vector3[] vertices = mesh.vertices;
         for (int i = 0; i < vertices.Length; i++)
         {
             Vector3 vertex = vertices[i];
             vertices[i].y += heightMap.GetHeight(vertex.x, vertex.z);
         }
     }
 }
        static Vector3[] GetVertices(List <Vector3[]> outlines, IHeightMap floorHeightMap, IHeightMap ceilingHeightMap)
        {
            int numWallVertices = 2 * outlines.Sum(outline => outline.Length);
            var vertices        = new Vector3[numWallVertices];

            int vertexIndex = 0;

            foreach (Vector3[] outline in outlines)
            {
                for (int i = 0; i < outline.Length; i++)
                {
                    Vector3 vertex = outline[i];
                    float   x      = vertex.x;
                    float   z      = vertex.z;
                    vertices[vertexIndex++] = new Vector3(x, ceilingHeightMap.GetHeight(x, z), z);
                    vertices[vertexIndex++] = new Vector3(x, floorHeightMap.GetHeight(x, z), z);
                }
            }
            return(vertices);
        }
        public static Mesh3V3N CreateFromHeightMap(int columns, int rows, IHeightMap heightMap)
        {
            var vertices = new List <Vertex3V3N>();

            for (var x = 0; x <= columns; x++)
            {
                for (var y = 0; y <= rows; y++)
                {
                    var height = heightMap.GetHeight(x, y);
                    vertices.Add(new Vertex3V3N
                    {
                        Position = new Vector3(x, (float)height, y),
                        Normal   = (Vector3)heightMap.GetNormal(x, y)
                    });
                }
            }

            var faces = CreateFaces(columns, rows);

            return(new Mesh3V3N(vertices, faces).Transformed(Matrix4.CreateTranslation(-columns / 2f, 0, -rows / 2f) * Matrix4.CreateScale((float)(1.0 / columns), 1, (float)(1.0 / rows))));
        }
예제 #10
0
            public Patch(IHeightMap map, int patchsize, int gridx, int gridy, int patchcount, float scale, float heightscale)
            {
                int   c = patchsize;
                float s = scale;

                VertexP3T2[] vtx = new VertexP3T2[c * c];

                int i = 0;

                for (int y = 0; y < c; ++y)
                {
                    for (int x = 0; x < c; ++x)
                    {
                        //x and y position
                        float px = (float)gridx * s + ((float)x / (float)(c - 1)) * s;
                        float py = (float)gridy * s + ((float)y / (float)(c - 1)) * s;
                        px -= s * patchcount / 2;
                        py -= s * patchcount / 2;

                        //z position (height)
                        float h = ((float)map.GetHeight(
                                       gridx * (patchsize - 1) + x,
                                       gridy * (patchsize - 1) + y)) * heightscale;

                        //add the vertex
                        vtx[i].position   = new Vector3(px, h, py);
                        vtx[i++].texture0 = new Vector2(
                            (float)(gridx * (c - 1) + x) / (float)(patchcount * (c - 1)),
                            (float)(gridy * (c - 1) + y) / (float)(patchcount * (c - 1))
                            );
                    }
                }

                Vertices        = Root.Instance.UserInterface.Renderer.CreateStaticVertexBuffer(vtx, Format.Size * vtx.Length);
                Vertices.Format = Format;
            }
예제 #11
0
            public Patch(IHeightMap map, int patchsize, int gridx, int gridy, int patchcount,float scale,float heightscale)
            {
                int c = patchsize;
                float s = scale;

                VertexP3T2[] vtx = new VertexP3T2[c * c];

                int i = 0;
                for (int y = 0; y < c; ++y)
                {
                    for (int x = 0; x < c; ++x)
                    {
                        //x and y position
                        float px = (float)gridx * s + ((float)x / (float)(c - 1)) * s;
                        float py = (float)gridy * s + ((float)y / (float)(c - 1)) * s;
                        px -= s * patchcount / 2;
                        py -= s * patchcount / 2;

                        //z position (height)
                        float h = ((float)map.GetHeight(
                            gridx * (patchsize - 1) + x,
                            gridy * (patchsize - 1) + y)) * heightscale;

                        //add the vertex
                        vtx[i].position=new Vector3(px, h, py);
                        vtx[i++].texture0=new Vector2(
                            (float)(gridx * (c - 1) + x) / (float)(patchcount * (c - 1)),
                            (float)(gridy * (c - 1) + y) / (float)(patchcount * (c - 1))
                            );
                    }
                }

                Vertices = Root.Instance.UserInterface.Renderer.CreateStaticVertexBuffer(vtx, Format.Size * vtx.Length);
                Vertices.Format = Format;
            }
예제 #12
0
 public float GetCeilingHeightAt(float x, float z)
 {
     return(ceilingHeightMap.GetHeight(x, z));
 }
예제 #13
0
 public float GetFloorHeightAt(float x, float z)
 {
     return(floorHeightMap.GetHeight(x, z));
 }
예제 #14
0
        public static Mesh3V3N CreateFromHeightMap(int columns, int rows, IHeightMap heightMap)
        {
            var vertices = new List<Vertex3V3N>();
            for (var x = 0; x <= columns; x++)
            {
                for (var y = 0; y <= rows; y++)
                {
                    var height = heightMap.GetHeight(x, y);
                    vertices.Add(new Vertex3V3N
                    {
                        Position = new Vector3(x, (float)height, y),
                        Normal = (Vector3)heightMap.GetNormal(x, y)
                    });
                }
            }

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

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

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

            return new Mesh3V3N(vertices, faces).Transformed(Matrix4.CreateTranslation(-columns / 2, 0, -rows / 2) * Matrix4.CreateScale((float)(1.0 / columns), 1, (float)(1.0 / rows)));
        }
예제 #15
0
        /// <summary>
        /// Generate a flat plane.
        /// </summary>
        /// <param name="size">Size of the plane.</param>
        /// <param name="vertexCount">Number of vertex per side.</param>
        /// <param name="textureRepeate">Number of texture repeate.</param>
        /// <returns>A flat plane.</returns>
        public static Model GeneratePlane(float size, int vertexCount, int textureRepeate, IHeightMap heightMap)
        {
            int count = vertexCount * vertexCount;

            Vertex[] vertecies = new Vertex[count];
            int[]    indices   = new int[6 * (vertexCount - 1) * (vertexCount - 1)];

            int vertexPointer = 0;

            for (int y = 0; y < vertexCount; y++)
            {
                for (int x = 0; x < vertexCount; x++)
                {
                    Vector2 vertexPosition = new Vector2((size / (vertexCount - 1)) * x, (size / (vertexCount - 1)) * y);
                    vertecies[vertexPointer] = new Vertex(new Vector3(vertexPosition.X, heightMap.GetHeight(x / (float)vertexCount, y / (float)vertexCount), vertexPosition.Y),
                                                          heightMap.GetNormal(x / (float)vertexCount, y / (float)vertexCount),
                                                          new Point2D(x / ((float)vertexCount - 1) * textureRepeate, (float)y / ((float)vertexCount - 1) * textureRepeate));
                    vertexPointer++;
                }
            }
            int pointer = 0;

            for (int y = 0; y < vertexCount - 1; y++)
            {
                for (int x = 0; x < vertexCount - 1; x++)
                {
                    int topLeft     = (y * vertexCount) + x;
                    int topRight    = topLeft + 1;
                    int bottomLeft  = ((y + 1) * vertexCount) + x;
                    int bottomRight = bottomLeft + 1;
                    indices[pointer++] = topLeft;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = topRight;
                    indices[pointer++] = topRight;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = bottomRight;
                }
            }

            return(new Model(vertecies, indices));
        }
예제 #16
0
        public IPhysicsObject CreateHeightmap(IHeightMap heightMapInfo, float scale, float shiftx, float shifty, float heightscale)
        {
            CollisionSkin collision = new CollisionSkin(null);
            Body _body = new Body();

            Array2D field = new Array2D(heightMapInfo.Size.X, heightMapInfo.Size.Y);

            for (int x = 0; x < field.Nx; x++)
            {
                for (int z = 0; z < field.Nz; z++)
                {
                    field.SetAt(x, z, heightscale * heightMapInfo.GetHeight(x,z));
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            _body.MoveTo(new Vector3(shiftx, 0, shifty), Matrix4.Identity);

            //collision.AddPrimitive(new Heightmap(field, shift.X, shift.Y, heightMapInfo.terrainScale, heightMapInfo.terrainScale), new MaterialProperties(0.7f, 0.7f, 0.6f));
            collision.AddPrimitive(new Heightmap(field, shiftx, shifty, scale, scale), new MaterialProperties(0.7f, 0.7f, 0.6f));

            _body.CollisionSkin = collision;
            _body.Immovable = true;
            _body.EnableBody();

            return new JigLibObject(_body);
        }