# Thread: Cubicfalloff soft edge Brush (Lees brush challenge)

Join Date
Sep 2004
Location
Århus, Denmark
Posts
159

## Cubicfalloff soft edge Brush (Lees brush challenge)

Someone had to step up to the challenge

The smooth function was totally and utterly ripped off from the interwebs (I hate reinventing the spoon again and again )

Like this one a lot more than the linear soft edge brush as it has a more natural look to it. Am definitely going to create more of this type of brushes, had one that lowered the outer edge of the brush and raised the inner edge, which gave some interesting results.

Next one will be a crater like brush, i.e raising by a lot at the edges and then having a cubic falloff lowering near the center

Anyways, here comes the code
SoftCubicEdgeBrush:
Code:
```using UnityEngine;

namespace _CodeBase.Demos.Brushes {
class SoftCubicEdgeBrush : Brush {
public SoftCubicEdgeBrush() {
}

public override float sample(UnityEngine.Vector2 samplePoint, float brushSize) {
var sample = 0f;

if (samplePoint.magnitude <= brushSize / 2f) {
var start = 0;
var end = brushSize / 2f;

// sample at current distance.
var val = 1f - samplePoint.magnitude / end;
// smooth the sample
sample = Smooth(start, end, val);

// get the sample back into proper range.
sample = sample / (brushSize / 2f);
}
return sample * Opacity;
}

public float Smooth(float start, float end, float amount) {
// Clamp to 0-1;
amount = (amount > 1f) ? 1f : amount;
amount = (amount < 0f) ? 0f : amount;

// Cubicly adjust the amount value.
amount = (amount * amount) * (3f - (2f * amount));

return (start + ((end - start) * amount));
}
}
}```
Edit:
Well the crater one was easier than I thought, just had to change
Code:
```
// sample at current distance.
var val = 1f - samplePoint.magnitude / end;```
to

Code:
```
// sample at current distance.
var val = samplePoint.magnitude / end;```
Last edited by mhylle; 04-20-2011 at 11:01 AM.

2. This looks fun... Can't wait till I catch up! Thanks for creating brushes and sharing.

-Dane

3. Registered User
Join Date
Jan 2011
Posts
72
This is awsome, I didn't expect this to happen with terrain brushes but with ingame spells. Thank you for posting !

4. I took a different approach & skipped up to a 5th order polynomial... our noisy friend 6t^5 - 15t^4 + 10t^3

I put an enum of FalloffTypes and matching property in TerrainSculptor.cs, along with the following method:
Code:
```        private static void SelectFalloff()
{
if (Input.GetKeyDown(KeyCode.Keypad1) && Falloff != FalloffType.Linear)
Falloff = FalloffType.Linear;
if (Input.GetKeyDown(KeyCode.Keypad2) && Falloff != FalloffType.Mound)
Falloff = FalloffType.Mound;
if (Input.GetKeyDown(KeyCode.Keypad3) && Falloff != FalloffType.FifthOrder)
Falloff = FalloffType.FifthOrder;
}```
which is called from Update() before SelectBrush() & SelectSculptTool().

Then I just did a switch in BrushSoftEdge.cs ...here's my new BrushSoftEdge.Sample() method:
Code:
```        public override float Sample(Vector2 samplePoint, float brushSize)
{
var sample = 0f;
if (samplePoint.magnitude <= brushSize / 2f)
{
var t = samplePoint.magnitude/(brushSize/2f);

switch (TerrainSculptor.Falloff)
{
case TerrainSculptor.FalloffType.Linear:
sample = 1f - t;
break;
case TerrainSculptor.FalloffType.Mound:
sample = 1f - t*t;
break;
case TerrainSculptor.FalloffType.FifthOrder:
sample = 1f - t*t*t*(t*(t*6 - 15) + 10);
break;
}
}

return sample*Opacity;
}```
EDIT: Here's the result of the hard-edged brush & the 3 different curves for the soft-edged brush:
Last edited by fatgav; 04-21-2011 at 03:02 PM. Reason: Added image

Join Date
Sep 2004
Location
Århus, Denmark
Posts
159
Nice one gav. looks cool
Great idea with the select falloff method, helps a lot when adding new falloffs. So far I have had my cubic falloff on the 5 key and the crater on the 6 key

Here's mine.

So much fun to play with, especially combining the crate with a noise brush, gets to look really natural
Last edited by mhylle; 04-23-2011 at 04:32 AM.

6. Can't wait to get my hands on this system

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•