Unity Shader学习:2D图形绘制(蓝天白云大海)

news/2024/11/17 10:03:27/

Unity Shader学习:2D图形绘制(蓝天白云大海)

基本是一些数学上的算法
在这里插入图片描述
shader部分:

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 100Blend SrcAlpha  OneMinusSrcAlphaPass{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;//画单个圆(uv,位置,大小,抗锯齿)float4 Circle(float2 uv, float2 center, float size, float blur) {uv = uv - center;uv = uv / size;float len = length(uv);//长度大于一个半径单位透明度为0,小于透明度为1float val = smoothstep(1.0,1.0-blur, len);return float4(1, 1, 1, val);}//画单个云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);//将圆中不想要的部分剪切掉col = col * smoothstep(-0.1, -0.1 + 0.01, uv.y);//圆摆放不同位置组成云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;}//画复数云float4 DrawClouds(float2 uv) {uv.x += 0.03*_Time.y;//使得左右连续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;}//画太阳光环float4 DrawSunCircle(float2 uv,float2 center,float size) {uv = uv - center;uv = uv / size;//atan2返回点(x,y)与x轴的夹角,范围(-π,π]//获取极坐标的θ角度float degree = atan2(uv.y , uv.x ) + _Time.y * -0.1;//uv向量离中心点距离//获取极坐标的r=x2+y2开方float len = length(uv);//根据极坐标玫瑰线:r(θ)=a*sin(kθ)//求得r;a为扩散幅度,k为花瓣数*0.5float r = 0.3*abs(sin(degree*5.0));//画花瓣//保留r值小于到中心点距离的所有像素float sunRound= smoothstep(r + 0.1 + _SunRoundFactor, r + _SunRoundFactor, len);return float4(1,1,1,sunRound)*_SunRoundColor;}//画波浪(参数:uv,频率,振幅,整体高度,移动速度)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;//uv.y大于sin函数的部分透明度为0,小于的部分透明度为1float val = uv.y - sin(uv.x*waveFrequency)*waveAmplitude - waveHeight;val = smoothstep(0.001,0.0, val);return float4(1, 1, 1, val);}//画背景float4 DrawBackground(float2 uv) {float4 val = lerp(float4(1,1,1,1), _SkyColor, uv.y);return val;}//混合图层(b为前景,a为背景)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{ //太阳中心float4 sunCoreColor = Circle(i.uv, _SunPos.xy, _SunSize, 0.05)*_SunColor;//太阳光环float4 sunAroundColor = DrawSunCircle(i.uv, _SunPos.xy, _SunSize);//云float4 cloudsColor = DrawClouds(i.uv);//波浪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;//背景float4 backgroundColor = DrawBackground(i.uv);//图层叠加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}}
}

http://www.ppmy.cn/news/568264.html

相关文章

向往大海

一天的时间并不长,朝九晚六,午休一个小时,晚上加班到九点,再除去晚饭一个小时,一共工作十个小时。有时候这十个小时感觉实在太漫长,而又有时候感觉又太短。不同的时间不同的心情,不同的喜悦不同…

蓝天白云何在?国外的天空分外蓝,外国的月亮更加圆?真实与谎言

蓝天白云何在?国外的天空分外蓝,外国的月亮更加圆?真实与谎言 最近怎么老是在真实与谎言之间纠结呢? 事情有三 一、从南海搬家广州 话说三四年前,我从南海桂城回广州,记得当时在桂城过得很爽,周…

蓝天和白云

蓝天和白云的心一样,希望白鸽自由翱翔。 老师和父母的心一样,希望我们健康成长。 花开的日子我们走进大学校园这个快乐的地方,在平安校园愉快歌唱; 花开的日子我们遨游在大学校园这个知识的海洋,和老师一起编织梦想…

关于大海的儿歌

1、大鱼吃小鱼    2、海浪花       3、浪花         4、水晶宫里真美丽 大海里,真稀奇,  风儿风儿轻轻吹,   浪花家在哪儿?       红珊瑚,搭舞台, 大鱼吃小鱼,    大海…

城市消防应急通信三级作战网构建

项目背景 随着我国《消防信息化“十三五”总体规划》对消防信息化的发展规划做了统一部署,以城市为代表的消防通信成为专网通信行业重点关注的领域之一。目前,我国城市化发展面临高层建筑林立、地铁、人防工程分布密集,大型综合体不断涌现&a…

【计算机视觉】MaskFormer:将语义分割和实例分割作为同一任务进行训练

文章目录 一、导读二、逐像素分类和掩码分类的区别2.1 逐像素分类2.2 掩码分类2.3 区别 三、DETR四、MaskFormer五、MaskFormer用于语义和实例分割六、总结 一、导读 目标检测和实例分割是计算机视觉的基本任务,在从自动驾驶到医学成像的无数应用中发挥着关键作用。…

HackTheBox - 学院【CPTS】复习3 - XSS、文件包含、文件上传、命令注入

XSS 登录表单 document.write(<h3>Please login to continue</h3><form actionhttp://OUR_IP><input type"username" name"username" placeholder"Username"><input type"password" name"password&…

Studio3T 无限破解 (2019.3.0.0)

一.将资源下载到本机,并解压,即可看到Studio3T.exe 二.找到你电脑上的Studio 3T 安装目录,双击打开 三.将解压的Studio3T.exe, 复制到你电脑上的Studio 3T 安装目录,即可 四.双击运行exe文件(原来的和新拷贝的exe文件都可以) 你会发现已经被破解好了~