Home>

### Unity Shader achieves graphics drawing (blue sky, white clouds and sea)

Basically some mathematical algorithms

``````shader "unlit/2d-ocean"
{
properties
{
_maintex ("texture", 2d)="white" {}
_suncolor ("suncolor", color)=(1,1,1,1)
_sunroundcolor ("sunroundcolor", color)=(1,0,0,1)
_wavecolor1 ("wavecolor1", color)=(1,1,1,1)
_wavecolor2 ("wavecolor2", color)=(1,1,1,1)
_wavecolor3 ("wavecolor3", color)=(1,1,1,1)
_wavecolor4 ("wavecolor4", color)=(1,1,1,1)
_wavecolor5 ("wavecolor5", color)=(1,1,1,1)
_skycolor ("skycolor", color)=(0,1,0,1)
_cloudpos1 ("cloudpos1", vector)=(0,0,0,0)
_cloudpos2 ("cloudpos2", vector)=(0,0,0,0)
_cloudpos3 ("cloudpos3", vector)=(0,0,0,0)
_cloudpos4 ("cloudpos4", vector)=(0,0,0,0)
_cloudpos5 ("cloudpos5", vector)=(0,0,0,0)
_wavefactor1 ("wavefactor1", vector)=(0,0,0,0)
_wavefactor2 ("wavefactor2", vector)=(0,0,0,0)
_wavefactor3 ("wavefactor3", vector)=(0,0,0,0)
_wavefactor4 ("wavefactor4", vector)=(0,0,0,0)
_wavefactor5 ("wavefactor5", vector)=(0,0,0,0)
_sunpos ("sunpos", vector)=(0,0,0,0)
_sunroundfactor ("sunroundfactor", range (0.0,2.0))=0.1
_sunsize ("sunsize", range (0.0,1.0))=1.0
}
{
tags {"rendertype"="transparent"}
lod 100
blend srcalpha oneminussrcalpha
pass
{
cgprogram
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
struct appdata
{
float4 vertex:position;
float2 uv:texcoord0;
};
struct v2f
{
float2 uv:texcoord0;
float4 vertex:sv_position;
};
sampler2d _maintex;
float4 _maintex_st;
float4 _cloudpos1;
float4 _cloudpos2;
float4 _cloudpos3;
float4 _cloudpos4;
float4 _cloudpos5;
float4 _wavecolor1;
float4 _wavecolor2;
float4 _wavecolor3;
float4 _wavecolor4;
float4 _wavecolor5;
float4 _wavefactor1;
float4 _wavefactor2;
float4 _wavefactor3;
float4 _wavefactor4;
float4 _wavefactor5;
float4 _suncolor;
float4 _sunroundcolor;
float4 _sunpos;
float _sunroundfactor;
float _sunsize;
float4 _skycolor;
//Draw a single circle (uv, position, size, anti-aliasing)
float4 circle (float2 uv, float2 center, float size, float blur) {
uv=uv-center;
uv=uv/size;
float len ​​= length (uv);
//Length greater than a radius unit transparency is 0, less than transparency is 1
float val=smoothstep (1.0,1.0-blur, len);
return float4 (1, 1, 1, val);
}
//Draw a single cloud
float4 drawcloud (float2 uv, float2 center, float size) {
uv=uv-center;
uv=uv/size;
float4 col=circle (uv, float2 (0.0, 0.0), 0.2, 1);
//Cut off the unwanted part of the circle
col=col * smoothstep (-0.1, -0.1 + 0.01, uv.y);
//The circle is placed in different positions to form a cloud
col +=circle (uv, float2 (0.15, -0.05), 0.1, 1);
col +=circle (uv, float2 (0.0, -0.1), 0.11, 1);
col +=circle (uv, float2 (-0.15, -0.1), 0.1, 1);
col +=circle (uv, float2 (-0.3, -0.08), 0.1, 1);
col +=circle (uv, float2 (-0.2, 0.0), 0.15, 1);
return col;
}
//Draw a plural cloud
float4 drawclouds (float2 uv) {
uv.x +=0.03 * _time.y;
//Make left and right continuous
uv.x=frac (uv.x + 0.5)-0.5;
float4 col=drawcloud (uv, _cloudpos1.xy, 0.1);
col +=drawcloud (uv, _cloudpos2.xy, 0.12);
col +=drawcloud (uv, _cloudpos3.xy, 0.14);
col +=drawcloud (uv, _cloudpos4.xy, 0.16);
col +=drawcloud (uv, _cloudpos5.xy, 0.18);
return col;
}
//Draw the sun halo
float4 drawsuncircle (float2 uv, float2 center, float size) {
uv=uv-center;
uv=uv/size;
//atan2 returns the angle between the point (x, y) and the x axis,Range (-π, π)
//Get the θ angle of polar coordinates
float degree=atan2 (uv.y, uv.x) + _time.y * -0.1;
//The distance of the uv vector from the center
//Get the square of r=x2 + y2 in polar coordinates
float len ​​= length (uv);
//According to the polar coordinate rose line:r (θ)=a * sin (kθ)
//Find r;a is the diffusion amplitude,k is the number of petals * 0.5
float r=0.3 * abs (sin (degree * 5.0));
//Draw petals
//Retain all pixels whose r value is less than the distance to the center
float sunround=smoothstep (r + 0.1 + _sunroundfactor, r + _sunroundfactor, len);
return float4 (1,1,1, sunround) * _ sunroundcolor;
}
//Draw waves (parameters:uv, frequency, amplitude, overall height,Moving speed)
float4 drawwave (float2 uv, float wavefrequency, float waveamplitude, float waveheight, float speed) {
uv.x +=speed * _time.y;
uv.y +=sin (_time.y + waveheight) * 0.02;
//The transparency of uv.y greater than the sin function is 0, and the transparency of the less than 1 is 1.
float val=uv.y-sin (uv.x * wavefrequency) * waveamplitude-waveheight;
val=smoothstep (0.001,0.0, val);
return float4 (1, 1, 1, val);
}
//Draw background
float4 drawbackground (float2 uv) {
float4 val=lerp (float4 (1,1,1,1), _skycolor, uv.y);
return val;
}
//Mixed layer (b is foreground, a is background)
float4 lerpimages (float4 a, float4 b) {
return float4 (lerp (a, b, b.a) .rgb, a.a + b.a);
}
v2f vert (appdata v)
{
v2f o;
o.vertex=unityobjecttoclippos (v.vertex);
o.uv=transform_tex (v.uv, _maintex);
return o;
}
float4 frag (v2f i):sv_target
{
//Sun Center
float4 suncorecolor=circle (i.uv, _sunpos.xy, _sunsize, 0.05) * _ suncolor;
//Sun Halo
float4 sunaroundcolor=drawsuncircle (i.uv, _sunpos.xy, _sunsize);
//cloud
float4 cloudscolor=drawclouds (i.uv);
//wave
float4 wave1=drawwave (i.uv, _wavefactor1.x, _wavefactor1.y, _wavefactor1.z, _wavefactor1.w) * _ wavecolor1;
float4 wave2=drawwave (i.uv, _wavefactor2.x, _wavefactor2.y, _wavefactor2.z, _wavefactor2.w) * _ wavecolor2;
float4 wave3=drawwave (i.uv, _wavefactor3.x, _wavefactor3.y, _wavefactor3.z, _wavefactor3.w) * _ wavecolor3;
float4 wave4=drawwave (i.uv, _wavefactor4.x, _wavefactor4.y, _wavefactor4.z, _wavefactor4.w) * _ wavecolor4;
float4 wave5=drawwave (i.uv, _wavefactor5.x, _wavefactor5.y, _wavefactor5.z, _wavefactor5.w) * _ wavecolor5;
//background
float4 backgroundcolor=drawbackground (i.uv);
//Layer overlay
float4 finalcolor=lerpimages (backgroundcolor, sunaroundcolor);
finalcolor=lerpimages (finalcolor, suncorecolor);
finalcolor=lerpimages (finalcolor, wave1);
finalcolor=lerpimages (finalcolor, wave2);
finalcolor=lerpimages (finalcolor, wave3);
finalcolor=lerpimages (finalcolor, wave4);
finalcolor=lerpimages (finalcolor, wave5);
finalcolor=lerpimages (finalcolor, cloudscolor);
return finalcolor;
}
endcg
}
}
}
``````