Ejemplo n.º 1
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="chunk"></param>
 /// <param name="device"></param>
 /// <param name="vertices"></param>
 /// <param name="indices"></param>
 public ChunkMesh(ReadOnlyChunk chunk, TrueCraftGame game, VertexPositionNormalColorTexture[] vertices, int[] indices)
     : base(game, 1, true)
 {
     Chunk = chunk;
     Vertices = vertices;
     SetSubmesh(0, indices);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="vertices"></param>
 /// <returns></returns>
 protected override BoundingBox RecalculateBounds(VertexPositionNormalColorTexture[] vertices)
 {
     return new BoundingBox(
         new Vector3(Chunk.X * TrueCraft.Core.World.Chunk.Width, 0, Chunk.Z * TrueCraft.Core.World.Chunk.Depth),
         new Vector3(Chunk.X * TrueCraft.Core.World.Chunk.Width
             + TrueCraft.Core.World.Chunk.Width, TrueCraft.Core.World.Chunk.Height,
             Chunk.Z * TrueCraft.Core.World.Chunk.Depth + TrueCraft.Core.World.Chunk.Depth));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="chunk"></param>
 /// <param name="device"></param>
 /// <param name="vertices"></param>
 /// <param name="opaqueIndices"></param>
 /// <param name="transparentIndices"></param>
 public ChunkMesh(ReadOnlyChunk chunk, TrueCraftGame game, VertexPositionNormalColorTexture[] vertices, int[] opaqueIndices, int[] transparentIndices)
     : base(game, 2, true)
 {
     Chunk = chunk;
     Vertices = vertices;
     SetSubmesh(0, opaqueIndices);
     SetSubmesh(1, transparentIndices);
 }
Ejemplo n.º 4
0
        public static VertexPositionNormalColorTexture[] CreateUniformCube(Vector3 offset, Vector2[] texture,
                                                                           VisibleFaces faces, int indiciesOffset, out int[] indicies, Color color, int[] lighting = null)
        {
            faces = VisibleFaces.All;             // Temporary
            if (lighting == null)
            {
                lighting = DefaultLighting;
            }

            var totalFaces = 0;
            var f          = (uint)faces;

            while (f != 0)
            {
                if ((f & 1) == 1)
                {
                    totalFaces++;
                }
                f >>= 1;
            }

            indicies = new int[6 * totalFaces];
            var verticies = new VertexPositionNormalColorTexture[4 * totalFaces];

            int[] _indicies;
            var   textureIndex = 0;
            var   sidesSoFar   = 0;

            for (var _side = 0; _side < 6; _side++)
            {
                if ((faces & VisibleForCubeFace[_side]) == 0)
                {
                    textureIndex += 4;
                    continue;
                }

                var lightColor = LightColor.ToVector3() * CubeBrightness[lighting[_side]];

                var side = (CubeFace)_side;
                var quad = CreateQuad(side, offset, texture, textureIndex % texture.Length, indiciesOffset,
                                      out _indicies, new Color(lightColor * color.ToVector3()));
                Array.Copy(quad, 0, verticies, sidesSoFar * 4, 4);
                Array.Copy(_indicies, 0, indicies, sidesSoFar * 6, 6);
                textureIndex += 4;
                sidesSoFar++;
            }

            return(verticies);
        }
Ejemplo n.º 5
0
 protected static VertexPositionNormalColorTexture[] CreateAngledQuad(int face, Vector3 offset, Vector2[] texture, int textureOffset,
     int indiciesOffset, out int[] indicies, Color color)
 {
     indicies = new[] { 0, 1, 3, 1, 2, 3 };
     for (int i = 0; i < indicies.Length; i++)
         indicies[i] += (face * 4) + indiciesOffset;
     var quad = new VertexPositionNormalColorTexture[4];
     var unit = QuadMesh[face];
     var normal = CubeNormals[face];
     for (int i = 0; i < 4; i++)
     {
         quad[i] = new VertexPositionNormalColorTexture(offset + unit[i], normal, color, texture[textureOffset + i]);
     }
     return quad;
 }
Ejemplo n.º 6
0
 protected VertexPositionNormalColorTexture[] RenderQuads(BlockDescriptor descriptor, Vector3 offset,
     Vector2[] textureMap, int indiciesOffset, out int[] indicies, Color color)
 {
     indicies = new int[6 * 4];
     var verticies = new VertexPositionNormalColorTexture[4 * 4];
     int[] _indicies;
     int textureIndex = 0;
     for (int side = 0; side < 4; side++)
     {
         var quad = CreateAngledQuad(side, offset, textureMap, textureIndex % textureMap.Length, indiciesOffset, out _indicies, color);
         Array.Copy(quad, 0, verticies, side * 4, 4);
         Array.Copy(_indicies, 0, indicies, side * 6, 6);
         textureIndex += 4;
     }
     return verticies;
 }
Ejemplo n.º 7
0
        protected static VertexPositionNormalColorTexture[] CreateAngledQuad(int face, Vector3 offset, Vector2[] texture, int textureOffset,
                                                                             int indiciesOffset, out int[] indicies, Color color)
        {
            indicies = new[] { 0, 1, 3, 1, 2, 3 };
            for (int i = 0; i < indicies.Length; i++)
            {
                indicies[i] += (face * 4) + indiciesOffset;
            }
            var quad   = new VertexPositionNormalColorTexture[4];
            var unit   = QuadMesh[face];
            var normal = CubeNormals[face];

            for (int i = 0; i < 4; i++)
            {
                quad[i] = new VertexPositionNormalColorTexture(offset + unit[i], normal, color, texture[textureOffset + i]);
            }
            return(quad);
        }
Ejemplo n.º 8
0
        protected VertexPositionNormalColorTexture[] CreateUniformCube(Vector3 offset, Vector2[] texture, int indiciesOffset, out int[] indicies, Color color)
        {
            indicies = new int[6 * 6];
            var verticies = new VertexPositionNormalColorTexture[4 * 6];

            int[] _indicies;
            int   textureIndex = 0;

            for (int _side = 0; _side < 6; _side++)
            {
                var side = (CubeFace)_side;
                var quad = CreateQuad(side, offset, texture, textureIndex % texture.Length, indiciesOffset, out _indicies, color);
                Array.Copy(quad, 0, verticies, _side * 4, 4);
                Array.Copy(_indicies, 0, indicies, _side * 6, 6);
                textureIndex += 4;
            }
            return(verticies);
        }
Ejemplo n.º 9
0
        protected VertexPositionNormalColorTexture[] RenderQuads(BlockDescriptor descriptor, Vector3 offset,
                                                                 Vector2[] textureMap, int indiciesOffset, out int[] indicies, Color color)
        {
            indicies = new int[6 * 4];
            var verticies = new VertexPositionNormalColorTexture[4 * 4];

            int[] _indicies;
            int   textureIndex = 0;

            for (int side = 0; side < 4; side++)
            {
                var quad = CreateAngledQuad(side, offset, textureMap, textureIndex % textureMap.Length, indiciesOffset, out _indicies, color);
                Array.Copy(quad, 0, verticies, side * 4, 4);
                Array.Copy(_indicies, 0, indicies, side * 6, 6);
                textureIndex += 4;
            }
            return(verticies);
        }
Ejemplo n.º 10
0
        protected static VertexPositionNormalColorTexture[] CreateQuad(CubeFace face, Vector3 offset,
                                                                       Vector2[] texture, int textureOffset, int indiciesOffset, out int[] indicies, Color color)
        {
            indicies = new[] { 0, 1, 3, 1, 2, 3 };
            for (int i = 0; i < indicies.Length; i++)
            {
                indicies[i] += ((int)face * 4) + indiciesOffset;
            }
            var quad      = new VertexPositionNormalColorTexture[4];
            var unit      = CubeMesh[(int)face];
            var normal    = CubeNormals[(int)face];
            var faceColor = new Color(FaceBrightness[(int)face] * color.ToVector3());

            for (int i = 0; i < 4; i++)
            {
                quad[i] = new VertexPositionNormalColorTexture(offset + unit[i], normal, faceColor, texture[textureOffset + i]);
            }
            return(quad);
        }
Ejemplo n.º 11
0
        public static VertexPositionNormalColorTexture[] CreateUniformCube(Vector3 offset, Vector2[] texture,
            VisibleFaces faces, int indiciesOffset, out int[] indicies, Color color, int[] lighting = null)
        {
            faces = VisibleFaces.All; // Temporary
            if (lighting == null)
                lighting = DefaultLighting;

            int totalFaces = 0;
            uint f = (uint)faces;
            while (f != 0)
            {
                if ((f & 1) == 1)
                    totalFaces++;
                f >>= 1;
            }

            indicies = new int[6 * totalFaces];
            var verticies = new VertexPositionNormalColorTexture[4 * totalFaces];
            int[] _indicies;
            int textureIndex = 0;
            int sidesSoFar = 0;
            for (int _side = 0; _side < 6; _side++)
            {
                if ((faces & VisibleForCubeFace[_side]) == 0)
                {
                    textureIndex += 4;
                    continue;
                }
                var lightColor = LightColor.ToVector3() * CubeBrightness[lighting[_side]];

                var side = (CubeFace)_side;
                var quad = CreateQuad(side, offset, texture, textureIndex % texture.Length, indiciesOffset,
                    out _indicies, new Color(lightColor * color.ToVector3()));
                Array.Copy(quad, 0, verticies, sidesSoFar * 4, 4);
                Array.Copy(_indicies, 0, indicies, sidesSoFar * 6, 6);
                textureIndex += 4;
                sidesSoFar++;
            }
            return verticies;
        }
Ejemplo n.º 12
0
        public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
                                                                  Tuple <int, int> textureMap, int indiciesOffset, out int[] indicies)
        {
            // Wheat is rendered by rendering the four vertical faces of a cube, then moving them
            // towards the middle. We also render a second set of four faces so that you can see
            // each face from the opposite side (to avoid culling)
            var texture = Textures[0];

            if (descriptor.Metadata < Textures.Length)
            {
                texture = Textures[descriptor.Metadata];
            }
            indicies = new int[4 * 2 * 6];
            var verticies = new VertexPositionNormalColorTexture[4 * 2 * 6];

            int[] _indicies;
            for (int _side = 0; _side < 4; _side++) // Y faces are the last two in the CubeFace enum, so we can just iterate to 4
            {
                var side = (CubeFace)_side;
                var quad = CreateQuad(side, Vector3.Zero, texture, 0, indiciesOffset, out _indicies, Color.White);
                if (side == CubeFace.NegativeX || side == CubeFace.PositiveX)
                {
                    for (int i = 0; i < quad.Length; i++)
                    {
                        quad[i].Position.X *= 0.5f;
                        quad[i].Position   += offset;
                    }
                }
                else
                {
                    for (int i = 0; i < quad.Length; i++)
                    {
                        quad[i].Position.Z *= 0.5f;
                        quad[i].Position   += offset;
                    }
                }
                Array.Copy(quad, 0, verticies, _side * 4, 4);
                Array.Copy(_indicies, 0, indicies, _side * 6, 6);
            }
            indiciesOffset += 4 * 6;
            for (int _side = 0; _side < 4; _side++)
            {
                var side = (CubeFace)_side;
                var quad = CreateQuad(side, Vector3.Zero, texture, 0, indiciesOffset, out _indicies, Color.White);
                if (side == CubeFace.NegativeX || side == CubeFace.PositiveX)
                {
                    for (int i = 0; i < quad.Length; i++)
                    {
                        quad[i].Position.X *= 0.5f;
                        quad[i].Position.X  = -quad[i].Position.X;
                        quad[i].Position   += offset;
                    }
                }
                else
                {
                    for (int i = 0; i < quad.Length; i++)
                    {
                        quad[i].Position.Z *= 0.5f;
                        quad[i].Position.Z  = -quad[i].Position.Z;
                        quad[i].Position   += offset;
                    }
                }
                Array.Copy(quad, 0, verticies, _side * 4 + 4 * 4, 4);
                Array.Copy(_indicies, 0, indicies, _side * 6 + 6 * 4, 6);
            }
            for (int i = 0; i < verticies.Length; i++)
            {
                verticies[i].Position.Y -= 1 / 16f;
            }
            return(verticies);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Recalculates the bounding box for this mesh.
 /// </summary>
 /// <param name="vertices">The vertices in this mesh.</param>
 /// <returns></returns>
 protected virtual BoundingBox RecalculateBounds(VertexPositionNormalColorTexture[] vertices)
 {
     return new BoundingBox(
         vertices.Select(v => v.Position).OrderBy(v => v.Length()).First(),
         vertices.Select(v => v.Position).OrderByDescending(v => v.Length()).First());
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a new mesh.
 /// </summary>
 public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices,
         int[] indices, bool recalculateBounds = true) : this(game, 1, recalculateBounds)
 {
     Vertices = vertices;
     SetSubmesh(0, indices);
 }
 public bool Equals(VertexPositionNormalColorTexture other)
 {
     return Position == other.Position && Normal == other.Normal &&
         Color == other.Color && Texture == other.Texture;
 }
Ejemplo n.º 16
0
 public bool Equals(VertexPositionNormalColorTexture other)
 {
     return(Position == other.Position && Normal == other.Normal &&
            Color == other.Color && Texture == other.Texture);
 }
Ejemplo n.º 17
0
 protected VertexPositionNormalColorTexture[] CreateUniformCube(Vector3 offset, Vector2[] texture, int indiciesOffset, out int[] indicies, Color color)
 {
     indicies = new int[6 * 6];
     var verticies = new VertexPositionNormalColorTexture[4 * 6];
     int[] _indicies;
     int textureIndex = 0;
     for (int _side = 0; _side < 6; _side++)
     {
         var side = (CubeFace)_side;
         var quad = CreateQuad(side, offset, texture, textureIndex % texture.Length, indiciesOffset, out _indicies, color);
         Array.Copy(quad, 0, verticies, _side * 4, 4);
         Array.Copy(_indicies, 0, indicies, _side * 6, 6);
         textureIndex += 4;
     }
     return verticies;
 }
Ejemplo n.º 18
0
 public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
     VisibleFaces faces, Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
 {
     // Wheat is rendered by rendering the four vertical faces of a cube, then moving them
     // towards the middle. We also render a second set of four faces so that you can see
     // each face from the opposite side (to avoid culling)
     var texture = Textures[0];
     if (descriptor.Metadata < Textures.Length)
         texture = Textures[descriptor.Metadata];
     indicies = new int[4 * 2 * 6];
     var verticies = new VertexPositionNormalColorTexture[4 * 2 * 6];
     int[] _indicies;
     for (int _side = 0; _side < 4; _side++) // Y faces are the last two in the CubeFace enum, so we can just iterate to 4
     {
         var side = (CubeFace)_side;
         var quad = CreateQuad(side, Vector3.Zero, texture, 0, indiciesOffset, out _indicies, Color.White);
         if (side == CubeFace.NegativeX || side == CubeFace.PositiveX)
         {
             for (int i = 0; i < quad.Length; i++)
             {
                 quad[i].Position.X *= 0.5f;
                 quad[i].Position += offset;
             }
         }
         else
         {
             for (int i = 0; i < quad.Length; i++)
             {
                 quad[i].Position.Z *= 0.5f;
                 quad[i].Position += offset;
             }
         }
         Array.Copy(quad, 0, verticies, _side * 4, 4);
         Array.Copy(_indicies, 0, indicies, _side * 6, 6);
     }
     indiciesOffset += 4 * 6;
     for (int _side = 0; _side < 4; _side++)
     {
         var side = (CubeFace)_side;
         var quad = CreateQuad(side, Vector3.Zero, texture, 0, indiciesOffset, out _indicies, Color.White);
         if (side == CubeFace.NegativeX || side == CubeFace.PositiveX)
         {
             for (int i = 0; i < quad.Length; i++)
             {
                 quad[i].Position.X *= 0.5f;
                 quad[i].Position.X = -quad[i].Position.X;
                 quad[i].Position += offset;
             }
         }
         else
         {
             for (int i = 0; i < quad.Length; i++)
             {
                 quad[i].Position.Z *= 0.5f;
                 quad[i].Position.Z = -quad[i].Position.Z;
                 quad[i].Position += offset;
             }
         }
         Array.Copy(quad, 0, verticies, _side * 4 + 4 * 4, 4);
         Array.Copy(_indicies, 0, indicies, _side * 6 + 6 * 4, 6);
     }
     for (int i = 0; i < verticies.Length; i++)
     {
         verticies[i].Position.Y -= 1 / 16f;
     }
     return verticies;
 }
Ejemplo n.º 19
0
 protected static VertexPositionNormalColorTexture[] CreateQuad(CubeFace face, Vector3 offset,
     Vector2[] texture, int textureOffset, int indiciesOffset, out int[] indicies, Color color)
 {
     indicies = new[] { 0, 1, 3, 1, 2, 3 };
     for (int i = 0; i < indicies.Length; i++)
         indicies[i] += ((int)face * 4) + indiciesOffset;
     var quad = new VertexPositionNormalColorTexture[4];
     var unit = CubeMesh[(int)face];
     var normal = CubeNormals[(int)face];
     var faceColor = new Color(FaceBrightness[(int)face] * color.ToVector3());
     for (int i = 0; i < 4; i++)
     {
         quad[i] = new VertexPositionNormalColorTexture(offset + unit[i], normal, faceColor, texture[textureOffset + i]);
     }
     return quad;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Creates a new mesh.
 /// </summary>
 public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices,
         int submeshes = 1, bool recalculateBounds = true) : this(game, submeshes, recalculateBounds)
 {
     Vertices = vertices;
 }