Beispiel #1
0
        public override bool Paint(float dt, IPaintable canvas, IGridManager gridManager, Painter.InputState inputState, float minVal, float maxVal, Rect rect, Matrix4x4 TRS)
        {
            bool dirty      = false;
            var  pos        = inputState.GridPosition;
            var  baseCell   = gridManager.GetCell(pos);
            int  stealCells = 2;

            float stolenValue = 0;

            for (var i = -stealCells; i <= stealCells; i += 1)
            {
                for (var j = -stealCells; j <= stealCells; j += 1)
                {
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    var offset = gridManager.GetOffset(i, j);
                    var value  = canvas.GetValue(baseCell + offset);

                    if (value <= 0)
                    {
                        continue;
                    }

                    var stealAmount = Mathf.Min(value, Strength);

                    stolenValue += stealAmount;
                    canvas.SetValue(baseCell + offset, value - stealAmount);
                    dirty = true;
                }
            }

            canvas.SetValue(baseCell, canvas.GetValue(baseCell) + stolenValue);
            return(dirty);
        }
Beispiel #2
0
        public override bool Paint(float dt, IPaintable canvas, IGridManager gridManager, Painter.InputState inputState, float minVal, float maxVal, Rect rect, Matrix4x4 TRS)
        {
            var dirty     = false;
            var scaledRad = gridManager.GetGridSize() * Radius;

            for (var i = -scaledRad; i <= scaledRad; i += gridManager.GetGridSize())
            {
                for (var j = -scaledRad; j <= scaledRad; j += gridManager.GetGridSize())
                {
                    var pos  = inputState.GridPosition + new Vector3(i, 0, j);
                    var cell = gridManager.GetCell(pos);

                    if (rect.size.magnitude > 0 && !rect.Contains(pos.xz()))
                    {
                        canvas.RemoveCell(cell);
                        continue;
                    }

                    var normalisedDist = 1f;
                    if (BrushShape == EBrushShape.Circle)
                    {
                        var circleDist = Vector2.Distance(inputState.GridPosition.xz(), pos.xz());
                        if (circleDist > scaledRad)
                        {
                            continue;
                        }
                        normalisedDist = circleDist / scaledRad;
                    }
                    else
                    {
                        normalisedDist = Mathf.Abs(inputState.GridPosition.x - pos.x) +
                                         Mathf.Abs(inputState.GridPosition.y - pos.y);
                        normalisedDist /= scaledRad;
                    }

                    var falloff = 1f;
                    if (!float.IsNaN(normalisedDist))
                    {
                        falloff = Falloff.Evaluate(normalisedDist);
                    }
                    var val = Strength * falloff;

                    //var existingVal = canvas.GetValue(cell);

                    var sum = 0f;
                    for (var iter = 0; iter < Iterations; ++iter)
                    {
                        for (var u = -1; u <= 1; ++u)
                        {
                            for (var v = -1; v <= 1; ++v)
                            {
                                var neighbourPos  = inputState.GridPosition + new Vector3(i + u * gridManager.GetGridSize(), 0, j + v * gridManager.GetGridSize());
                                var neighbourCell = gridManager.GetCell(neighbourPos);
                                sum += canvas.GetValue(neighbourCell);
                            }
                        }
                    }

                    val = Mathf.Lerp(sum / 9, val, Strength * Flow * dt);
                    val = Mathf.Clamp(val, minVal, maxVal);

                    canvas.SetValue(cell, val);
                    dirty = true;
                }
            }
            return(dirty);
        }