foreword
In the previous section, the depth judgment of the water surface and the bottom of the water was achieved, and the result is RawDepth
represented by . This section combs the use of depth relations to generate foam on the shore.
design function_getFoam(RawDepth)
float sinWave = _getFoam(RawDepth) ;
_getFoam function
The function of this function is to generate foam on the shore, use the sin function to RawDepth
generate stripes on the shore, and at the same time RawDepth
change to undertake the task of mask, forcing the stripes to only be generated on the shore
variable design
[Space][Header(_______BlendCtrl_______)][Space]
_FoamBlend("Water-Shore mixture", Range(0,1)) = 0 //水面和岸边的衔接柔和程度,在这里用作mask过度控制
[Space][Header(_______FoamCtrl_______)][Space]
_FoamRange("Foam Range", Range(0,1)) = 0
_FoamFreq("Foam Freq", Float) = 10
_FoamSpeed("Foam Speed", Float) = 1
The sin function generates a wave
- _FoamRange overall fine-tuning wavelength
- _FoamFreq frequency adjustment, foam generation interval
- _FoamSpeed speed
float _getFoam(float depth)
{
depth = clamp(depth/_FoamRange,0,1);
float a = depth * _FoamFreq + _Time.y * _FoamSpeed;
float sinWave = sin(a)
return sinWave;
}
fig1 clamp(depth/_FoamRange,0,1)
fig2 extra peaks
remove redundant peaks
float FoamDepth = smoothstep(_FoamBlend, 1, depth);
fig3 performs simple calculations based on depth to generate a mask with higher contrast.
After the mask is added to the calculation, _getFoam
the function is as follows
float _getFoam(float depth)
{
depth = clamp(depth/_FoamRange,0,1);
float a = depth * _FoamFreq + _Time.y * _FoamSpeed;
float FoamDepth = smoothstep(_FoamBlend, 1, depth);
float sinWave = sin(a) * (1-FoamDepth);
return sinWave;
}
Shaped stripes
Option to add sampling noise
float _getFoamWave(float depth, float wave_noise)
{
depth = clamp(depth/_FoamRange,0,1);
float a = depth * _FoamFreq + _Time.y * _FoamSpeed;
float FoamDepth = smoothstep(_FoamBlend, 1, depth);
float sinWave = sin(a) + wave_noise;
sinWave = sinWave * (1-FoamDepth);
return sinWave;
}
The new variable wave_noise of the function _getFoamWave
is the noise data we sampled
Foam Dissolution and Enhancement
The processing method is not unique
Now you have uneven edges and different light and dark stripes. What you do now is add _FoamDissolve
this control variable
depth- _FoamDissolve
to the equation: the closer to the shore, the higher the degree of ablation
float _getFoamWave(float depth, float wave_noise)
{
depth = clamp(depth/_FoamRange,0,1);
float a = depth * _FoamFreq + _Time.y * _FoamSpeed;
float FoamDepth = smoothstep(_FoamBlend, 1, depth);
float FoamDepth_oneminus = 1-FoamDepth;
float sinWave = sin(a) + wave_noise + (depth- _FoamDissolve);
sinWave = sinWave * FoamDepth_oneminus;
return sinWave;
}
Extremely _FoamDissolve
return to 0, and the stripes are too rigid
. In order to solve this problem, the interpolation of 0-1 is filled in the dichotomy
float _getFoamWave(float depth, float wave_noise)
{
depth = clamp(depth/_FoamRange,0,1);
float a = depth * _FoamFreq + _Time.y * _FoamSpeed;
float FoamDepth = smoothstep(_FoamBlend, 1, depth);
float FoamDepth_oneminus = 1-FoamDepth;
float sinWave = sin(a) + wave_noise + depth - _FoamDissolve;
sinWave = smoothstep(FoamDepth_oneminus-_FoamFade,1,sinWave);
sinWave = sinWave * FoamDepth_oneminus;
return sinWave;
}
The final result is shown in the figure above