Unity shader for graphics rendering (blue sky, white clouds, sea)

Posted on

Unity shader learning: 2D graphics rendering (blue sky, white clouds, sea), for your reference, the specific content is as follows

It’s basically some mathematical algorithms

Shader section:

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
}
SubShader
{
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, antialiasing)
float4
Circle(float2 uv, float2 center, float size, float blur) {
uv
= uv - center;
uv
= uv / size;
float len = length(uv);
//The length is greater than one radius unit, the 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 out the unwanted part of the circle
col
= col * smoothstep(-0.1, -0.1 + 0.01, uv.y);
//Circles are placed in different positions to form clouds
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;
}

//Painting complex clouds
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 halo of the sun
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, in the range (- π, π]
//Get theta angle of polar coordinates
float degree = atan2(uv.y , uv.x ) + _Time.y * -0.1;
//Distance between UV vector and center
//Get the r = x2 + Y2 root of polar coordinates
float len = length(uv);
//According to the polar rose line: R (θ) = a * sin (K θ)
//The results show that R; a is the diffusion range and K is the number of petals * 0.5
float r = 0.3*abs(sin(degree*5.0));
//Painting petals
//All pixels with R values less than the distance from the center point are retained
float sunRound= smoothstep(r +

Leave a Reply

Your email address will not be published.