unity shader学习:动态雾,供大家参考,具体内容如下
先将相机近裁面四个角向量传给shader,再通过观察空间下的深度值和相机位置算出像素在世界坐标系的位置,通过世界空间高度值来设定雾的范围和浓度,然后通过噪声和uv偏移实现扰动效果。得到了类似寂静岭或恶灵附身1的效果。
c#部分:
using system.collections; using system.collections.generic; using unityengine; [executeineditmode] public class raymarchingcamera : monobehaviour { private matrix4x4 frustumcorners = matrix4x4.identity; public material material; public camera mycamera; public transform cameratransform; // use this for initialization private void start() { mycamera.depthtexturemode = depthtexturemode.depth; } private void onrenderimage(rendertexture source, rendertexture destination) { //field of view float fov = mycamera.fieldofview; //近裁面距离 float near = mycamera.nearclipplane; //横纵比 float aspect = mycamera.aspect; //近裁面一半的高度 float halfheight = near * mathf.tan(fov * 0.5f * mathf.deg2rad); //向上和向右的向量 vector3 toright = mycamera.transform.right * halfheight * aspect; vector3 totop = mycamera.transform.up * halfheight; //分别得到相机到近裁面四个角的向量 //depth/dist=near/|topleft| //dist=depth*(|tl|/near) //scale=|tl|/near vector3 topleft = cameratransform.forward * near + totop - toright; float scale = topleft.magnitude / near; topleft.normalize(); topleft *= scale; vector3 topright = cameratransform.forward * near + totop + toright; topright.normalize(); topright *= scale; vector3 bottomleft = cameratransform.forward * near - totop - toright; bottomleft.normalize(); bottomleft *= scale; vector3 bottomright = cameratransform.forward * near - totop + toright; bottomright.normalize(); bottomright *= scale; //给矩阵赋值 frustumcorners.setrow(0, bottomleft); frustumcorners.setrow(1, bottomright); frustumcorners.setrow(2, topright); frustumcorners.setrow(3, topleft); //将向量传给定点着色器,将屏幕画面传个shader material.setmatrix("_frustumcornorsray", frustumcorners); material.settexture("_maintex", source); graphics.blit(source, destination,material,0); } }
shader部分:
shader "unlit/fog" { properties { _maintex ("texture", 2d) = "white" {} _noisetex("noisetex",2d)="white"{} //雾起始高度 _fogstartheight("fogstartheight",float)=0.0 //雾终点高度 _fogendheight("fogendheight",float)=1.0 //雾x位移速度 _fogxspeed("fogxspeed",float)=0.1 //雾y位移速度 _fogyspeed("fogyspeed",float)=0.1 } subshader { tags { "rendertype"="opaque" } lod 100 pass { cgprogram #pragma vertex vert #pragma fragment frag #include "unitycg.cginc" sampler2d _maintex; sampler2d _noisetex; sampler2d _cameradepthtexture; float _fogstartheight; float _fogendheight; float _fogxspeed; float _fogyspeed; //获得相机近裁面四个角向量 float4x4 _frustumcornorsray; struct a2v{ float4 vertex:position; float2 uv:texcoord0; }; struct v2f{ float4 pos:sv_position; float2 uv:texcoord0; float4 interpolatedray:texcoord1; }; v2f vert(a2v v){ v2f o; o.pos=unityobjecttoclippos(v.vertex); o.uv=v.uv; int index=0; if (v.uv.x<0.5&&v.uv.y<0.5) { index = 0; } else if (v.uv.x>0.5&&v.uv.y<0.5) { index = 1; } else if (v.uv.x>0.5&&v.uv.y>0.5) { index = 2; } else { index = 3; } //安排uv4个角对应四个角向量 o.interpolatedray = _frustumcornorsray[index]; return o; } float4 frag(v2f i):sv_target{ //观察空间下的线性深度值 float lineardepth=lineareyedepth(sample_depth_texture(_cameradepthtexture,i.uv)); //像素世界坐标=世界空间相机位置+像素相对相机距离 float3 worldpos=_worldspacecamerapos+lineardepth*i.interpolatedray.xyz; float speedx=_time.y*_fogxspeed; float speedy=_time.y*_fogyspeed; float noise=tex2d(_noisetex,i.uv+float2(speedx,speedy)); //让噪声图向黑色靠拢,黑白差距不要太大 noise=pow(noise,0.5); //雾浓度=世界高度/指定范围 float fogdensity=(_fogendheight-worldpos.y)/(_fogendheight-_fogstartheight); fogdensity=smoothstep(0.0,1.0,fogdensity*noise); float4 color=tex2d(_maintex,i.uv); //根据雾浓度混合场景和雾颜色 color.rgb=lerp(color.rgb,float3(0.5,0.5,0.5),fogdensity); return color; } endcg } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论