[unity shader/stylized water surface rendering/basic notes] urp code version 02- generation of shore foam

foreword


In the previous section, the depth judgment of the water surface and the bottom of the water was achieved, and the result is RawDepthrepresented 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 RawDepthgenerate stripes on the shore, and at the same time RawDepthchange 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;
        }

insert image description here
fig1 clamp(depth/_FoamRange,0,1)

insert image description here
fig2 extra peaks

remove redundant peaks

float FoamDepth = smoothstep(_FoamBlend, 1, depth);

insert image description here
fig3 performs simple calculations based on depth to generate a mask with higher contrast.
After the mask is added to the calculation, _getFoamthe 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;
        }

insert image description here

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 _getFoamWaveis the noise data we sampled
insert image description here

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 _FoamDissolvethis control variable
depth- _FoamDissolveto 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 _FoamDissolvereturn to 0, and the stripes are too rigid
insert image description here
. 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;
        }

insert image description here
The final result is shown in the figure above

Guess you like

Origin blog.csdn.net/qq_43544518/article/details/128756436
Recommended