Ejemplo n.º 1
0
    public override void BuildFace(Chunk chunk, Vector3[] vertices, Color32[] palette, ref BlockFace face, bool rotated)
    {
        bool backFace = DirectionUtils.IsBackface(face.side);

        var pools = Globals.WorkPool.GetPool(chunk.ThreadID);
        var verts = pools.Vector3ArrayPool.PopExact(4);
        var cols  = pools.Color32ArrayPool.PopExact(4);

        {
            verts[0] = vertices[0];
            verts[1] = vertices[1];
            verts[2] = vertices[2];
            verts[3] = vertices[3];

            cols[0] = palette[face.block.Type];
            cols[1] = palette[face.block.Type];
            cols[2] = palette[face.block.Type];
            cols[3] = palette[face.block.Type];

            BlockUtils.AdjustColors(chunk, cols, face.light);

            RenderGeometryBatcher batcher = chunk.RenderGeometryHandler.Batcher;
            batcher.AddFace(face.materialID, verts, cols, backFace);
        }

        pools.Color32ArrayPool.Push(cols);
        pools.Vector3ArrayPool.Push(verts);
    }
Ejemplo n.º 2
0
        public void Build(RenderGeometryBatcher batcher, ref BlockData block, BlockFace face, bool backFace, ref Vector3[] vecs, LocalPools pools)
        {
            int iface = (int)face;

            // Randomize the color a bit
            Color32 color = BlockDatabase.GetBlockInfo(block.BlockType).Color;

            if (block.BlockType != BlockType.None)
            {
                float value = m_noise.GetValue(vecs[0] + vecs[1] + vecs[2] + vecs[3]); // -1.0f..1.0f
                float noise = (255.0f * value) * 0.02f;                                // Deviation of 0.02f points from the original
                int   n     = (int)noise;
                byte  r     = (byte)Math.Max(0, Math.Min(color.r + n, 255));
                byte  g     = (byte)Math.Max(0, Math.Min(color.g + n, 255));
                byte  b     = (byte)Math.Max(0, Math.Min(color.b + n, 255));
                color = new Color32(r, g, b, color.a);
            }

            VertexData[]      vertexData      = pools.PopVertexDataArray(4);
            VertexDataFixed[] vertexDataFixed = pools.PopVertexDataFixedArray(4);
            {
                for (int i = 0; i < 4; i++)
                {
                    vertexData[i] = pools.PopVertexData();
                }

                for (int i = 0; i < 4; i++)
                {
                    vertexData[i].Vertex = vecs[i];
                    vertexData[i].Color  = color;
                    vertexData[i].UV     = AddFaceUV(i, GetTexture(iface), backFace);
                    vertexData[i].Normal = SNormals[iface][i];
                }

                for (int i = 0; i < 4; i++)
                {
                    vertexDataFixed[i] = VertexDataUtils.ClassToStruct(vertexData[i]);
                }
                batcher.AddFace(vertexDataFixed, backFace);

                for (int i = 0; i < 4; i++)
                {
                    pools.PushVertexData(vertexData[i]);
                }
            }
            pools.PushVertexDataFixedArray(vertexDataFixed);
            pools.PushVertexDataArray(vertexData);
        }
Ejemplo n.º 3
0
    public override void BuildFace(Chunk chunk, Vector3[] vertices, Color32[] palette, ref BlockFace face, bool rotated)
    {
        bool backFace = DirectionUtils.IsBackface(face.side);
        int  d        = DirectionUtils.Get(face.side);

        LocalPools pools = chunk.pools;
        var        verts = pools.Vector3ArrayPool.PopExact(4);
        var        cols  = pools.Color32ArrayPool.PopExact(4);

        {
            if (vertices == null)
            {
                Vector3 pos = face.pos;

                verts[0] = pos + BlockUtils.PaddingOffsets[d][0];
                verts[1] = pos + BlockUtils.PaddingOffsets[d][1];
                verts[2] = pos + BlockUtils.PaddingOffsets[d][2];
                verts[3] = pos + BlockUtils.PaddingOffsets[d][3];

                cols[0] = colors[d];
                cols[1] = colors[d];
                cols[2] = colors[d];
                cols[3] = colors[d];
            }
            else
            {
                verts[0] = vertices[0];
                verts[1] = vertices[1];
                verts[2] = vertices[2];
                verts[3] = vertices[3];

                cols[0] = colors[d];
                cols[1] = colors[d];
                cols[2] = colors[d];
                cols[3] = colors[d];
            }

            BlockUtils.AdjustColors(chunk, cols, face.light);

            RenderGeometryBatcher batcher = chunk.GeometryHandler.Batcher;
            batcher.AddFace(face.materialID, verts, cols, backFace);
        }

        pools.Color32ArrayPool.Push(cols);
        pools.Vector3ArrayPool.Push(verts);
    }
Ejemplo n.º 4
0
    public override void BuildFace(Chunk chunk, Vector3[] vertices, Color32[] palette, ref BlockFace face, bool rotated)
    {
        bool backface = DirectionUtils.IsBackface(face.side);
        int  d        = DirectionUtils.Get(face.side);

        var pools = Globals.WorkPool.GetPool(chunk.ThreadID);
        var verts = pools.Vector3ArrayPool.PopExact(4);
        var uvs   = pools.Vector2ArrayPool.PopExact(4);
        var cols  = pools.Color32ArrayPool.PopExact(4);

        {
            if (vertices == null)
            {
                Vector3 pos = face.pos;

                verts[0] = pos + BlockUtils.PaddingOffsets[d][0];
                verts[1] = pos + BlockUtils.PaddingOffsets[d][1];
                verts[2] = pos + BlockUtils.PaddingOffsets[d][2];
                verts[3] = pos + BlockUtils.PaddingOffsets[d][3];
            }
            else
            {
                verts[0] = vertices[0];
                verts[1] = vertices[1];
                verts[2] = vertices[2];
                verts[3] = vertices[3];
            }

            BlockUtils.PrepareTexture(chunk, ref face.pos, uvs, face.side, textures, rotated);
            BlockUtils.PrepareColors(chunk, cols, face.light);

            RenderGeometryBatcher batcher = chunk.RenderGeometryHandler.Batcher;
            batcher.AddFace(face.materialID, verts, cols, uvs, backface);
        }

        pools.Color32ArrayPool.Push(cols);
        pools.Vector2ArrayPool.Push(uvs);
        pools.Vector3ArrayPool.Push(verts);
    }
Ejemplo n.º 5
0
    public override void BuildBlock(Chunk chunk, ref Vector3Int localPos, int materialID)
    {
        var pools = Globals.WorkPool.GetPool(chunk.ThreadID);
        RenderGeometryBatcher batcher = chunk.RenderGeometryHandler.Batcher;

        // Using the block positions hash is much better for random numbers than saving the offset and height in the block data
        int hash = localPos.GetHashCode();

        float blockHeight = (hash & 63) * coef * Env.BlockSize;

        hash *= 39;
        float offsetX = (hash & 63) * coef * Env.BlockSizeHalf - Env.BlockSizeHalf * 0.5f;

        hash *= 39;
        float offsetZ = (hash & 63) * coef * Env.BlockSizeHalf - Env.BlockSizeHalf * 0.5f;

        // Converting the position to a vector adjusts it based on block size and gives us real world coordinates for x, y and z
        Vector3 vPos = localPos;

        vPos += new Vector3(offsetX, 0, offsetZ);

        float x1 = vPos.x - BlockUtils.blockPadding;
        float x2 = vPos.x + BlockUtils.blockPadding + Env.BlockSize;
        float y1 = vPos.y - BlockUtils.blockPadding;
        float y2 = vPos.y + BlockUtils.blockPadding + blockHeight;
        float z1 = vPos.z - BlockUtils.blockPadding;
        float z2 = vPos.z + BlockUtils.blockPadding + Env.BlockSize;

        var verts  = pools.Vector3ArrayPool.PopExact(4);
        var uvs    = pools.Vector2ArrayPool.PopExact(4);
        var colors = pools.Color32ArrayPool.PopExact(4);

        BlockUtils.PrepareTexture(chunk, ref localPos, uvs, Direction.north, texture, false);

        // TODO: How do I make sure that if I supply no color value, white is used?
        // TODO: These colors could be removed and memory would be saved
        {
            colors[0] = new Color32(255, 255, 255, 255);
            colors[1] = new Color32(255, 255, 255, 255);
            colors[2] = new Color32(255, 255, 255, 255);
            colors[3] = new Color32(255, 255, 255, 255);
        }

        {
            verts[0] = new Vector3(x1, y1, z2);
            verts[1] = new Vector3(x1, y2, z2);
            verts[2] = new Vector3(x2, y2, z1);
            verts[3] = new Vector3(x2, y1, z1);
            batcher.AddFace(materialID, verts, colors, uvs, false);
        }
        {
            verts[0] = new Vector3(x2, y1, z1);
            verts[1] = new Vector3(x2, y2, z1);
            verts[2] = new Vector3(x1, y2, z2);
            verts[3] = new Vector3(x1, y1, z2);
            batcher.AddFace(materialID, verts, colors, uvs, false);
        }
        {
            verts[0] = new Vector3(x2, y1, z2);
            verts[1] = new Vector3(x2, y2, z2);
            verts[2] = new Vector3(x1, y2, z1);
            verts[3] = new Vector3(x1, y1, z1);
            batcher.AddFace(materialID, verts, colors, uvs, false);
        }
        {
            verts[0] = new Vector3(x1, y1, z1);
            verts[1] = new Vector3(x1, y2, z1);
            verts[2] = new Vector3(x2, y2, z2);
            verts[3] = new Vector3(x2, y1, z2);
            batcher.AddFace(materialID, verts, colors, uvs, false);
        }

        pools.Color32ArrayPool.Push(colors);
        pools.Vector2ArrayPool.Push(uvs);
        pools.Vector3ArrayPool.Push(verts);
    }