Unity Shader实现图形绘制(蓝天白云大海)
程序员文章站
2022-11-19 20:33:36
unity shader学习:2d图形绘制(蓝天白云大海),供大家参考,具体内容如下基本是一些数学上的算法shader部分:shader "unlit/2d-ocean"{ properties {...
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 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; //画单个圆(uv,位置,大小,抗锯齿) float4 circle(float2 uv, float2 center, float size, float blur) { uv = uv - center; uv = uv / size; float len = length(uv); //长度大于一个半径单位透明度为0,小于透明度为1 float 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.5 float 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,小于的部分透明度为1 float 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 } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 如何预防哮喘病发作 6个生活习惯远离哮喘
下一篇: 这时候老板看见了