Assets/Examples/Scripts/TerrainDeformer.cs
/// <summary>
/// Deforms the terrain in a spherical region around the point
/// </summary>
/// <param name="point">The point to modify the terrain around</param>
/// <param name="addTerrain">Should terrain be added or removed</param>
/// <param name="deformSpeed">How fast the terrain should be deformed</param>
/// <param name="range">How far the deformation can reach</param>
Deforms the terrain in a spherical region around the point
Deforms the terrain in a spherical region around the point
private void EditTerrain(Vector3 point, bool addTerrain, float deformSpeed, float range)
        {
            int buildModifier = addTerrain ? 1 : -1;
            int hitX = Mathf.RoundToInt(point.x);
            int hitY = Mathf.RoundToInt(point.y);
            int hitZ = Mathf.RoundToInt(point.z);
            int intRange = Mathf.CeilToInt(range);
            for (int x = -intRange; x <= intRange; x++)
            {
                for (int y = -intRange; y <= intRange; y++)
                {
                    for (int z = -intRange; z <= intRange; z++)
                    {
                        int offsetX = hitX - x;
                        int offsetY = hitY - y;
                        int offsetZ = hitZ - z;
                        var offsetPoint = new int3(offsetX, offsetY, offsetZ);
                        float distance = math.distance(offsetPoint, point);
                        if (distance > range)
                        {
                            continue;
                        }
                        float modificationAmount = deformSpeed / distance * buildModifier;
                        float oldDensity = world.GetDensity(offsetPoint);
                        float newDensity = Mathf.Clamp(oldDensity - modificationAmount, -1, 1);
                        world.SetDensity(newDensity, offsetPoint);
                    }
                }
            }
        }
private void EditTerrain ( Vector3 point , bool addTerrain , float deformSpeed , float range ) { int buildModifier = addTerrain ? 1 : - 1 ; int hitX = Mathf . RoundToInt ( point . x ) ; int hitY = Mathf . RoundToInt ( point . y ) ; int hitZ = Mathf . RoundToInt ( point . z ) ; int intRange = Mathf . CeilToInt ( range ) ; for ( int x = - intRange ; x <= intRange ; x ++ ) { for ( int y = - intRange ; y <= intRange ; y ++ ) { for ( int z = - intRange ; z <= intRange ; z ++ ) { int offsetX = hitX - x ; int offsetY = hitY - y ; int offsetZ = hitZ - z ; var offsetPoint = new int3 ( offsetX , offsetY , offsetZ ) ; float distance = math . distance ( offsetPoint , point ) ; if ( distance > range ) { continue ; } float modificationAmount = deformSpeed / distance * buildModifier ; float oldDensity = world . GetDensity ( offsetPoint ) ; float newDensity = Mathf . Clamp ( oldDensity - modificationAmount , - 1 , 1 ) ; world . SetDensity ( newDensity , offsetPoint ) ; } } } }
Deforms the terrain in a spherical region around the point
Deforms the terrain in a spherical region around the point
point
Vector3
The point to modify the terrain around
The point to modify the terrain around
addTerrain
bool
Should terrain be added or removed
Should terrain be added or removed
deformSpeed
float
None
range
float
None