예제 #1
0
        public void Execute()
        {
            var sandCellImplementation  = new SandCellImplementation();
            var waterCellImplementation = new WaterCellImplementation();

            var shouldGoReverse = frameCount % 2 == 0;
            // var shouldGoReverse2 = frameCount % 2 == 0;
            // var shouldGoReverse = random.NextBool();
            // var shouldGoReverse2 = random.NextBool();

            // var hasDirtyRect = false;
            // int? minCellPositionX = null;
            // int? minCellPositionY = null;
            // int? maxCellPositionX = null;
            // int? maxCellPositionY = null;
            //
            // void CheckForDirtyRect(Cell cell, int2 cellPosition)
            // {
            //     if (cell.isStale) return;
            //
            //     hasDirtyRect = true;
            //
            //     if (!minCellPositionX.HasValue || cellPosition.x < minCellPositionX.Value)
            //     {
            //         minCellPositionX = cellPosition.x;
            //     }
            //
            //     if (!minCellPositionY.HasValue || cellPosition.y < minCellPositionY.Value)
            //     {
            //         minCellPositionY = cellPosition.y;
            //     }
            //
            //     if (!maxCellPositionX.HasValue || cellPosition.x > maxCellPositionX.Value)
            //     {
            //         maxCellPositionX = cellPosition.x;
            //     }
            //
            //     if (!maxCellPositionY.HasValue || cellPosition.y < maxCellPositionY.Value)
            //     {
            //         maxCellPositionY = cellPosition.y;
            //     }
            // }
            //
            // for (var chunkX = -1; chunkX < 2; chunkX++)
            // {
            //     for (var chunkY = -1; chunkY < 2; chunkY++)
            //     {
            //         if (chunkX == 0 && chunkY == 0) continue;
            //
            //         var chunkPosition = int2(chunkX, chunkY);
            //         var chunk = chunkWithNeighbors.ChunkFromPosition(chunkPosition);
            //
            //         if (chunk.IsOutOfBounds) continue;
            //
            //         if (chunkX == 1 && chunkY == 1)
            //         {
            //             var cell = chunk.GetCell(int2(0, 0));
            //             CheckForDirtyRect(cell , int2(0, 0));
            //         }
            //         else if (chunkX == 1 && chunkY == -1)
            //         {
            //             var cell = chunk.GetCell(int2(0, ChunkSize - 1));
            //             CheckForDirtyRect(cell , int2(0, ChunkSize - 1));
            //         }
            //         else if (chunkX == -1 && chunkY == 1)
            //         {
            //             var cell = chunk.GetCell(int2(ChunkSize - 1, 0));
            //             CheckForDirtyRect(cell , int2(ChunkSize - 1, 0));
            //         }
            //         else if (chunkX == -1 && chunkY == -1)
            //         {
            //             var cell = chunk.GetCell(int2(ChunkSize - 1, ChunkSize - 1));
            //             CheckForDirtyRect(cell , int2(ChunkSize - 1, ChunkSize - 1));
            //         }
            //         else if (chunkY == -1)
            //         {
            //             for (var x = 0; x < ChunkSize; x++)
            //             {
            //                 var cell = chunk.GetCell(int2(x, ChunkSize - 1));
            //                 CheckForDirtyRect(cell , int2(x, ChunkSize - 1));
            //             }
            //         }
            //         else if (chunkY == 1)
            //         {
            //             for (var x = 0; x < ChunkSize; x++)
            //             {
            //                 var cell = chunk.GetCell(int2(x, 0));
            //                 CheckForDirtyRect(cell , int2(x, 0));
            //             }
            //         }
            //         else if (chunkX == -1)
            //         {
            //             for (var y = 0; y < ChunkSize; y++)
            //             {
            //                 var cell = chunk.GetCell(int2(ChunkSize - 1, y));
            //                 CheckForDirtyRect(cell , int2(ChunkSize - 1, y));
            //             }
            //         }
            //         else if (chunkX == 1)
            //         {
            //             for (var y = 0; y < ChunkSize; y++)
            //             {
            //                 var cell = chunk.GetCell(int2(0, y));
            //                 CheckForDirtyRect(cell , int2(0, y));
            //             }
            //         }
            //     }
            // }

            // for (var x = 0; x < ChunkSize; x++)
            // {
            //     for (var y = 0; y < ChunkSize; y++)
            //     {
            //         var cellPosition = new int2(x, y);
            //         var cell = chunkWithNeighbors.Value.GetCell(cellPosition);
            //
            //         if (cell.isStale) continue;
            //         hasDirtyRect = true;
            //
            //         if (!minCellPositionX.HasValue || cellPosition.x < minCellPositionX.Value)
            //         {
            //             minCellPositionX = cellPosition.x;
            //         }
            //
            //         if (!minCellPositionY.HasValue || cellPosition.y < minCellPositionY.Value)
            //         {
            //             minCellPositionY = cellPosition.y;
            //         }
            //
            //         if (!maxCellPositionX.HasValue || cellPosition.x > maxCellPositionX.Value)
            //         {
            //             maxCellPositionX = cellPosition.x;
            //         }
            //
            //         if (!maxCellPositionY.HasValue || cellPosition.y > maxCellPositionY.Value)
            //         {
            //             maxCellPositionY = cellPosition.y;
            //         }
            //     }
            // }
            //
            //
            // if (!hasDirtyRect) return;

            // var minCellPosition = max(int2(minCellPositionX.Value, minCellPositionY.Value) - int2(1), int2(0));
            // var maxCellPosition = min(int2(maxCellPositionX.Value, maxCellPositionY.Value) + int2(1), int2(ChunkSize));

            // var minCellPercentage = ((float2) minCellPosition) / (float)ChunkSize;
            // var maxCellPercentage = ((float2) maxCellPosition) / (float)ChunkSize;


            var minCellPosition = int2(0);
            var maxCellPosition = int2(ChunkSize);


            // var a = new Rect(
            //     chunkPosition.x - (chunkScale / 2),// + (minCellPercentage.x * chunkScale),
            //     chunkPosition.y - (chunkScale / 2) + chunkScale - (minCellPercentage.x * chunkScale),// - (maxCellPercentage.y * chunkScale),
            //     chunkScale,// * maxCellPercentage.x,
            //     chunkScale// * maxCellPercentage.y
            // );
            //
            // drawingCommands.PushLineWidth(0.5f);
            // drawingCommands.PushDuration(simulationStep * 50);
            // drawingCommands.WireRectangle(a, Color.red);
            // drawingCommands.PopLineWidth();
            // drawingCommands.PopDuration();

            // for (var x = shouldGoReverse ? ChunkSize - 1 : 0; shouldGoReverse ? x >= 0 : x < ChunkSize; x += (shouldGoReverse ? -1 : 1))
            for (var x = shouldGoReverse ? maxCellPosition.x - 1 : minCellPosition.x; shouldGoReverse?x >= minCellPosition.x : x < maxCellPosition.x; x += (shouldGoReverse ? -1 : 1))
            {
                // for (var y = 0; y < ChunkSize; y++)
                for (var y = maxCellPosition.y - 1; y >= minCellPosition.y; y--)
                // for (var y = ChunkSize - 1; y >= 0; y--)
                // for (var y = shouldGoReverse2 ? ChunkSize - 1 : 0; shouldGoReverse2 ? y >= 0 : y < ChunkSize; y += (shouldGoReverse2 ? -1 : 1))
                {
                    var cellPosition = new int2(x, y);

                    var cell = chunkWithNeighbors.Value.GetCell(cellPosition);
                    if (cell.type == CellType.None)
                    {
                        continue;
                    }
                    if (cell.clock == frameCount)
                    {
                        continue;
                    }

                    cell.clock = frameCount;
                    chunkWithNeighbors.Value.SetCell(cellPosition, cell);

                    if (cell.type == CellType.Sand)
                    {
                        // cell.isStale = false;
                        // chunkWithNeighbors.Value.SetCell(cellPosition, cell);

                        if (sandCellImplementation.Update(chunkWithNeighbors, cellPosition, random))
                        {
                        }
                        else
                        {
                            // cell.isStale = true;
                            // chunkWithNeighbors.Value.SetCell(cellPosition, cell);
                        }
                    }
                    else if (cell.type == CellType.Water)
                    {
                        // cell.isStale = false;

                        if (waterCellImplementation.Update(chunkWithNeighbors, cellPosition, random))
                        {
                        }
                        else
                        {
                            // cell.isStale = true;
                            // chunkWithNeighbors.Value.SetCell(cellPosition, cell);
                        }
                    }
                }
            }
        }
예제 #2
0
    public void LoadChunk(int2 chunkPosition)
    {
        if (allChunks.TryGetValue(chunkPosition, out var existingChunkContainer))
        {
            loadedChunks.Add(chunkPosition, existingChunkContainer);

            return;
        }

        var chunkRenderer = Instantiate(chunkPrefab, transform, true);

        chunkRenderer.transform.position   = ChunkScale * new Vector3(chunkPosition.x, chunkPosition.y);
        chunkRenderer.transform.localScale = new Vector3(ChunkScale, ChunkScale, 1);

        var outputRenderTexture = new RenderTexture(ChunkSize, ChunkSize, 32)
        {
            enableRandomWrite = true,
            useMipMap         = false,
            filterMode        = FilterMode.Point
        };

        outputRenderTexture.Create();

        var chunk = new Chunk(Allocator.Persistent);

        var cellsComputeBuffer = new ComputeBuffer(chunk.Cells.Length, SizeOfCell);

        chunkRenderer.SetTexture(outputRenderTexture);

        if (chunkPosition.y == 0)
        {
            for (var cellX = 0; cellX < ChunkSize; cellX++)
            {
                for (var cellY = 0; cellY < ChunkSize; cellY++)
                {
                    chunk.SetCell(int2(cellX, cellY), (Random.value > 0.5f) ? Cell.EmptyCell : SandCellImplementation.CreateSandCell(ref random));
                }
            }
        }
        else if (chunkPosition.y < 0)
        {
            for (var cellX = 0; cellX < ChunkSize; cellX++)
            {
                for (var cellY = 0; cellY < ChunkSize; cellY++)
                {
                    chunk.SetCell(int2(cellX, cellY), SandCellImplementation.CreateSandCell(ref random));
                }
            }
        }

        var chunkContainer = new ChunkContainer
        {
            Chunk               = chunk,
            ChunkBehaviour      = chunkRenderer,
            CellsComputeBuffer  = cellsComputeBuffer,
            OutputRenderTexture = outputRenderTexture
        };

        allChunks.Add(chunkPosition, chunkContainer);
        loadedChunks.Add(chunkPosition, chunkContainer);
    }