Example #1
0
        public Mesh?Generate(ChunkPos chunkPos)
        {
            var storages = new ChunkPaletteStorage <Block> [3, 3, 3];

            foreach (var(x, y, z) in Neighbors.ALL.Prepend(Neighbor.None))
            {
                if (_chunkStore.TryGetEntityID(chunkPos.Add(x, y, z), out var neighborID))
                {
                    if (_storageStore.TryGet(neighborID, out var storage))
                    {
                        storages[x + 1, y + 1, z + 1] = storage;
                    }
                }
            }
            var centerStorage = storages[1, 1, 1];

            var indexCount  = 0;
            var vertexCount = 0;

            for (var x = 0; x < 16; x++)
            {
                for (var y = 0; y < 16; y++)
                {
                    for (var z = 0; z < 16; z++)
                    {
                        var block = centerStorage[x, y, z];
                        if (block.Prototype.IsNone)
                        {
                            continue;
                        }

                        var blockVertex = new Vector3(x, y, z);
                        var textureCell = _textureCellStore.Get(block.Prototype.ID);

                        foreach (var facing in BlockFacings.ALL)
                        {
                            if (!IsNeighborEmpty(storages, x, y, z, facing))
                            {
                                continue;
                            }

                            if (_indices.Length <= indexCount + 6)
                            {
                                Array.Resize(ref _indices, _indices.Length << 1);
                            }
                            if (_vertices.Length <= vertexCount + 4)
                            {
                                Array.Resize(ref _vertices, _vertices.Length << 1);
                                Array.Resize(ref _normals, _vertices.Length << 1);
                                Array.Resize(ref _uvs, _vertices.Length << 1);
                            }

                            for (var i = 0; i < TRIANGLE_INDICES.Length; i++)
                            {
                                _indices[indexCount++] = (ushort)(vertexCount + TRIANGLE_INDICES[i]);
                            }

                            var normal = facing.ToVector3();
                            for (var i = 0; i < 4; i++)
                            {
                                var offset = OFFSET_PER_FACING[(int)facing][i];
                                _vertices[vertexCount] = blockVertex + offset;
                                _normals[vertexCount]  = normal;
                                _uvs[vertexCount]      = i switch {
                                    0 => textureCell.TopLeft,
                                    1 => textureCell.BottomLeft,
                                    2 => textureCell.BottomRight,
                                    3 => textureCell.TopRight,
                                    _ => throw new InvalidOperationException()
                                };
                                vertexCount++;
                            }
                        }
                    }
                }
            }

            return((indexCount > 0)
                                ? _meshManager.Create(
                       _indices.AsSpan(0, indexCount), _vertices.AsSpan(0, vertexCount),
                       _normals.AsSpan(0, vertexCount), _uvs.AsSpan(0, vertexCount))
                                : (Mesh?)null);
        }