public override void BuildFace(Chunk chunk, Vector3Int localPos, Direction direction) { VertexData[] vertexData = chunk.pools.PopVertexDataArray(4); VertexDataFixed[] vertexDataFixed = chunk.pools.PopVertexDataFixedArray(4); { for (int i = 0; i < 4; i++) { vertexData[i] = chunk.pools.PopVertexData(); } BlockUtils.PrepareVertices(localPos, vertexData, direction); BlockUtils.PrepareTexture(chunk, localPos, vertexData, direction, texture); BlockUtils.SetColors(vertexData, ref color); for (int i = 0; i < 4; i++) { vertexDataFixed[i] = VertexDataUtils.ClassToStruct(vertexData[i]); } chunk.GeometryHandler.Batcher.AddFace(vertexDataFixed); for (int i = 0; i < 4; i++) { chunk.pools.PushVertexData(vertexData[i]); } } chunk.pools.PushVertexDataFixedArray(vertexDataFixed); chunk.pools.PushVertexDataArray(vertexData); }
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 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, ref face.light); RenderGeometryBatcher batcher = chunk.GeometryHandler.Batcher; batcher.AddFace(face.materialID, verts, cols, uvs, backface); } pools.Color32ArrayPool.Push(cols); pools.Vector2ArrayPool.Push(uvs); pools.Vector3ArrayPool.Push(verts); }
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); }