中国电信网站备案 锁定,长沙城市建筑工程有限公司,重庆seo快速优化,成都网站建设 雷文章目录 前言一、使用一张法线纹理#xff0c;作为水下扭曲的纹理1、在属性面板定义一个纹理#xff0c;用于传入法线贴图2、在Pass中#xff0c;定义对应的纹理和采样器3、在常量缓冲区#xff0c;申明修改 Tilling 和 Offset 的ST4、在顶点着色器#xff0c;计算得到 应… 文章目录 前言一、使用一张法线纹理作为水下扭曲的纹理1、在属性面板定义一个纹理用于传入法线贴图2、在Pass中定义对应的纹理和采样器3、在常量缓冲区申明修改 Tilling 和 Offset 的ST4、在顶点着色器计算得到 应用了 ST 和 随时间流动的UV用于纹理采样(_WaterSpeed是上篇文章中用到的)5、在片元着色器中对其进行法线纹理进行采样 二、实现水下扭曲的效果1、定义一个扰度值控制扭曲水下的扭曲程度2、在URP设置中开启抓屏3、在Pass中定义抓屏的 纹理 和 采样器4、使用线性插值后的结果进行抓屏的纹理采样5、最后与上篇文章计算得到的水的颜色混合请添加图片描述 三、最终代码 前言
在上篇文章中我们实现了水体中 和 物体接触时产生泡沫的效果。
Unity中URP实现水体效果泡沫
在这篇文章中我们在上一篇文章的基础上来实现水下扭曲的效果。 一、使用一张法线纹理作为水下扭曲的纹理
原理
Unity中Shader的UV扭曲效果的实现
1、在属性面板定义一个纹理用于传入法线贴图 _DistortTex(“DistortNormalTex”,2D) “white”{} 2、在Pass中定义对应的纹理和采样器 TEXTURE2D(_DistortTex);SAMPLER(sampler_DistortTex); 3、在常量缓冲区申明修改 Tilling 和 Offset 的ST half4 _DistortTex_ST; 4、在顶点着色器计算得到 应用了 ST 和 随时间流动的UV用于纹理采样(_WaterSpeed是上篇文章中用到的) o.uv.xy TRANSFORM_TEX(v.uv,_DistortTex)_Time.y * _WaterSpeed; 5、在片元着色器中对其进行法线纹理进行采样 half4 distortTex SAMPLE_TEXTURE2D(_DistortTex,sampler_DistortTex,i.uv.xy); 二、实现水下扭曲的效果
原理把抓取到的屏幕纹理使用进行流动扭曲
Unity中URP下抓屏的 开启 和 使用
1、定义一个扰度值控制扭曲水下的扭曲程度
屏幕UV 和 法线纹理扭曲之间线性插值 float2 distortUV lerp(screenUV,distortTex,_Distort); 2、在URP设置中开启抓屏 3、在Pass中定义抓屏的 纹理 和 采样器 TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture); 4、使用线性插值后的结果进行抓屏的纹理采样 half4 cameraOpaqueTex SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,distortUV); 5、最后与上篇文章计算得到的水的颜色混合 col * cameraOpaqueTex; 三、最终代码
//水的深度
Shader MyShader/URP/P4_8
{Properties {[Header(Base)]_WaterColor1(WaterColor1,Color) (1,1,1,1)_WaterColor2(WaterColor2,Color) (1,1,1,1)_WaterSpeed(WaterSpeed,Range(0,1)) 0.1[Header(Foam)]_FoamTex(FoamTex,2D) white{} _FoamColor(FoamColor,Color) (1,1,1,1)_FoamRange(FoamRange,Range(0,5)) 1_FoamNoise(FoamNoise,Range(0,3)) 1[Header(Distort)]_DistortTex(DistortNormalTex,2D) white{}[PowerSlider(3)]_Distort(Distort,Range(0,0.5)) 0}SubShader{Tags{//告诉引擎该Shader只用于 URP 渲染管线RenderPipelineUniversalPipeline//渲染类型RenderTypeTransparent//渲染队列QueueTransparent}//Blend SrcAlpha OneMinusSrcAlphaZWrite OffPass{Name UnlitHLSLPROGRAM#pragma vertex vert#pragma fragment frag// Pragmas#pragma target 2.0// Includes#include Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl#include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl#include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlslCBUFFER_START(UnityPerMaterial)half4 _WaterColor1;half4 _WaterColor2;half _FoamRange;half _WaterSpeed;half4 _FoamColor;half _FoamNoise;half4 _FoamTex_ST;half _Distort;half4 _DistortTex_ST;CBUFFER_ENDTEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);TEXTURE2D(_FoamTex);SAMPLER(sampler_FoamTex);TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);TEXTURE2D(_DistortTex);SAMPLER(sampler_DistortTex);//struct appdata//顶点着色器的输入struct Attributes{float3 positionOS : POSITION;float2 uv : TEXCOORD0;};//struct v2f//片元着色器的输入struct Varyings{float4 positionCS : SV_POSITION;float4 uv : TEXCOORD0;// xy distortUV,zw foamUVfloat4 screenPos : TEXCOORD1;float3 positionVS : TEXCOORD2;float3 positionWS : TEXCOORD3;};//v2f vert(Attributes v)//顶点着色器Varyings vert(Attributes v){Varyings o (Varyings)0;o.positionWS TransformObjectToWorld(v.positionOS);o.positionVS TransformWorldToView(o.positionWS);o.positionCS TransformWViewToHClip(o.positionVS);o.screenPos ComputeScreenPos(o.positionCS);//计算得到泡沫纹理采样需要的顶点世界空间下的坐标值的流动效果o.uv.zw o.positionWS.xz *_FoamTex_ST.xy _Time.y * _WaterSpeed;//计算得到水下扭曲纹理的流动UVo.uv.xy TRANSFORM_TEX(v.uv,_DistortTex)_Time.y * _WaterSpeed;return o;}//fixed4 frag(v2f i) : SV_TARGET//片元着色器half4 frag(Varyings i) : SV_TARGET{//1、水的深度//获取屏幕空间下的 UV 坐标float2 screenUV i.positionCS.xy / _ScreenParams.xy;half depthTex SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,screenUV).x;//深度图转化到观察空间下float depthScene LinearEyeDepth(depthTex,_ZBufferParams);//获取水面模型顶点在观察空间下的Z值可以在顶点着色器中对其直接进行转化得到顶点观察空间下的坐标float4 depthWater depthScene i.positionVS.z;//2、水的颜色线性插值得到水 和 接触物体的水的 颜色的过度half4 waterColor lerp(_WaterColor1,_WaterColor2,depthWater);//3、水面泡沫//对泡沫纹理进行采样(这里使用顶点世界空间下的坐标进行纹理采样防止水体缩放影响泡沫的平铺和重复方式)half4 foamTex SAMPLE_TEXTURE2D(_FoamTex,sampler_FoamTex,i.uv.zw);foamTex pow(foamTex,_FoamNoise);//这里增加一个调整深度图范围的功能half4 foamRange depthWater * _FoamRange;//使用泡沫纹理 和 泡沫范围 比较得到泡沫遮罩half4 foamMask step(foamRange,foamTex);//给泡沫加上颜色half4 foamColor foamMask * _FoamColor;half4 col foamColor waterColor;//4、水下的扭曲half4 distortTex SAMPLE_TEXTURE2D(_DistortTex,sampler_DistortTex,i.uv.xy);float2 distortUV lerp(screenUV,distortTex,_Distort);half4 cameraOpaqueTex SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,distortUV);col * cameraOpaqueTex;//水的高光//水的反射//水的焦散return col;}ENDHLSL}}FallBack Hidden/Shader Graph/FallbackError
}
文章转载自: http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.fksrg.cn.gov.cn.fksrg.cn http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.xq3nk42mvv.cn.gov.cn.xq3nk42mvv.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.lsjtq.cn.gov.cn.lsjtq.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.zwyuan.com.gov.cn.zwyuan.com http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn