示例#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
    private void Update()
    {
        if (loadedChunks.Count == 0)
        {
            return;
        }

        if (Input.GetMouseButton(0))
        {
            var worldCursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            var absoluteCellCursorPositionVector = Vector2Int.RoundToInt(transform.InverseTransformPoint(worldCursorPosition) * pixelsPerUnit);
            var absoluteCellCursorPosition       = int2(absoluteCellCursorPositionVector.x, absoluteCellCursorPositionVector.y) + (ChunkSize / 2);

            for (var x = -2; x < 3; x++)
            {
                for (var y = -2; y < 3; y++)
                {
                    var offsetedCursorPosition = absoluteCellCursorPosition + int2(x, y);

                    var chunkPosition = int2(floor(offsetedCursorPosition / (float2)ChunkSize));
                    var cellPosition  = offsetedCursorPosition - (chunkPosition * ChunkSize);

                    if (loadedChunks.TryGetValue(chunkPosition, out var chunkContainer))
                    {
                        chunkContainer.Chunk.SetCell(cellPosition, WaterCellImplementation.CreateWaterCell(ref random));
                    }
                }
            }
        }
        else if (Input.GetMouseButton(1))
        {
            var worldCursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            var absoluteCellCursorPositionVector = Vector2Int.RoundToInt(transform.InverseTransformPoint(worldCursorPosition) * pixelsPerUnit);
            var absoluteCellCursorPosition       = int2(absoluteCellCursorPositionVector.x, absoluteCellCursorPositionVector.y) + (ChunkSize / 2);

            for (var x = -2; x < 3; x++)
            {
                for (var y = -2; y < 3; y++)
                {
                    var offsetedCursorPosition = absoluteCellCursorPosition + int2(x, y);

                    var chunkPosition = int2(floor(offsetedCursorPosition / (float2)ChunkSize));
                    var cellPosition  = offsetedCursorPosition - (chunkPosition * ChunkSize);

                    if (loadedChunks.TryGetValue(chunkPosition, out var chunkContainer))
                    {
                        chunkContainer.Chunk.SetCell(cellPosition, Cell.EmptyCell);
                    }
                }
            }
        }

        var minX = loadedChunks.Min(a => a.Key.x);
        var minY = loadedChunks.Min(a => a.Key.y);
        var maxX = loadedChunks.Max(a => a.Key.x);
        var maxY = loadedChunks.Max(a => a.Key.y);


        for (var x = minX; x <= maxX; x++)
        {
            for (var y = minY; y <= maxY; y++)
            {
                var chunkPosition = int2(x, y);
                if (!loadedChunks.TryGetValue(chunkPosition, out var chunkContainer))
                {
                    continue;
                }

                chunkContainer.CellsComputeBuffer.SetData(chunkContainer.Chunk.Cells);

                computeShader.SetInt("chunk_size", ChunkSize);
                computeShader.SetBuffer(renderChunkKernelIndex, "cells", chunkContainer.CellsComputeBuffer);
                computeShader.SetTexture(renderChunkKernelIndex, "texture_out", chunkContainer.OutputRenderTexture);

                computeShader.Dispatch(renderChunkKernelIndex, ChunkSize / 8, ChunkSize / 8, 1);
            }
        }
    }