コード例 #1
0
    // Create one cell with a specific position
    private FlowfieldCell CreateCell(Vector2 position, Vector2 direction)
    {
        FlowfieldCell cell = new FlowfieldCell
        {
            position  = position,
            direction = direction,
            intensity = Random.value
        };

        return(cell);
    }
コード例 #2
0
    // Prepare compute buffer for compute shader
    private void CreateFlowfieldBuffer()
    {
        // Calculate cells count for rows & columns
        cellsCountX = (int)Mathf.Floor(simulationSpace.x / flowfieldCellSize);
        cellsCountY = (int)Mathf.Floor(simulationSpace.y / flowfieldCellSize);

        maxCellsCount = cellsCountX * cellsCountY;

        // Create buffer for flowfield
        flowfieldBuffer = new ComputeBuffer(maxCellsCount, Marshal.SizeOf(typeof(FlowfieldCell)));

        var cellsArray = new FlowfieldCell[maxCellsCount];

        int iterations = 0;

        // Set position for each cell in array
        for (int y = 0; y < cellsCountY; y++)
        {
            for (int x = 0; x < cellsCountX; x++)
            {
                var currentCell = (x * cellsCountY + y);

                Vector2 position = new Vector2(
                    simulationSpace.x / cellsCountX * x + flowfieldCellSize / 2,
                    simulationSpace.y / cellsCountY * y + flowfieldCellSize / 2
                    );

                position.x += this.transform.position.x - simulationSpace.x / 2;
                position.y += this.transform.position.y - simulationSpace.y / 2;

                cellsArray[currentCell] = CreateCell(position, cellDirection[iterations]);

                iterations++;
            }
        }

        // Define data for compute buffer
        flowfieldBuffer.SetData(cellsArray);
    }