Unity shader实现移动端模拟深度水效果

所属分类: 软件编程 / C#教程 阅读数: 159
收藏 0 赞 0 分享

本文实例为大家分享了Unity shader实现移动端模拟深度水的具体代码,供大家参考,具体内容如下

描述:

在网上看到很多效果很好的水,比如根据水的深度,颜色有深浅变化,能让水变得更真实,但是又会涉及到比较复杂的计算,在移动端上面还是有些吃力的。

最近研究了一下,想在移动端上面模拟这样的效果 :

1 水的深浅透明度变化

2 水的深浅颜色变化

3 水上的阴影模拟(大面积的水通过烘焙比较浪费烘焙图)

根据上面的3点,可以通过一张黑白图的rg通道来实现深浅以及阴影的模拟 效果如下

如图,浅色的偏绿,深色的偏蓝 ,颜色可以手动调节,左边为阴影位置

代码如下:

Shader "Game_XXX/whater"
{
 Properties 
 {
 _WaterTex ("Normal Map (RGB), Foam (A)", 2D) = "white" {}
 _AlphaTex("AlphaTex", 2D) = "black" {}
 _shadowLight ("shadowLight",range(0,1)) = 0
 _Tiling ("Wave Scale", Range(0.00025, 0.007)) = 0.25
 _WaveSpeed("Wave Speed", Float) = 0.4
 _SpecularRatio ("Specular Ratio", Range(10,500)) = 200
 _outSideColor("outSideColor",Color) = (0,0,0,0)
 _outSideLight("outSideLight",Range(0,10))=1
 _inSideColor("inSideColor",Color) = (0,0,0,0)
 _inSideLight("intSideLight",Range(0,10))=1
 _Alpha("Alpha",Range(0,1)) = 1
 //模拟灯光颜色
 _LightColorSelf ("LightColorSelf",Color) = (1,1,1,1)
 //模拟灯光方向
 _LightDir ("LightDir",vector) = (0,1,0,0)
 //高光强度
 _specularLight("specularLight",range(0.1,2)) =1 
 }
 
 SubShader { 
 Tags {
 "Queue"="Transparent-200"
 "RenderType"="Transparent" 
 "IgnoreProjector" = "True"
 "LightMode" = "ForwardBase"
 }
 LOD 250
 Pass
 {
 
 ZWrite Off
 Blend SrcAlpha OneMinusSrcAlpha
 CGPROGRAM
 
 #pragma vertex Vert
 #pragma fragment Frag
 #include "UnityCG.cginc"
 
 float _Tiling;
 float _WaveSpeed;
 float _SpecularRatio;
 sampler2D _WaterTex;
 sampler2D _AlphaTex;
 float4 _LightColorSelf;
 float4 _LightDir;
 float4 _outSideColor;
 float _outSideLight;
 float4 _inSideColor;
 float _inSideLight;
 float _shadowLight;
 float _specularLight;
 float _Alpha;
 
 struct v2f
 {
 float4 position : POSITION;
 float3 worldView : TEXCOORD0;
 float3 tilingAndOffset:TEXCOORD2;
 float3x3 tangentTransform:TEXCOORD4; 
 float2 alphaUV :TEXCOORD7;
 
 };
 
 
 
 v2f Vert(appdata_full v)
 {
 v2f o;
 float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
 //视向量(世界空间)
 o.worldView = -normalize(worldPos - _WorldSpaceCameraPos);
 o.position = UnityObjectToClipPos(v.vertex);
 //uv动画
 o.tilingAndOffset.z =frac( _Time.x * _WaveSpeed);//frac :返回标量或矢量的小数
 o.tilingAndOffset.xy = worldPos.xz*_Tiling;
 o.alphaUV = v.texcoord;
 //求世界法线三件套 
 float3 normal =normalize( UnityObjectToWorldNormal(v.normal)); 
    float3 tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );//切线空间转化为世界空间 
    float3 bitangentDir = normalize(cross(normal, tangentDir) * v.tangent.w);//切线 法线 计算副切线 
 
 o.tangentTransform = float3x3( tangentDir, bitangentDir, normal); 
 return o;
 }
 
 float4 Frag(v2f i):COLOR
 {
 
  
 
  //法线采样
  fixed3 BumpMap01 = UnpackNormal(tex2D(_WaterTex,i.tilingAndOffset.xy + i.tilingAndOffset.z ));
  fixed3 BumpMap02 = UnpackNormal(tex2D(_WaterTex,i.tilingAndOffset.xy*1.1 - i.tilingAndOffset.z));
  //两张法线相混合
  //fixed3 N1 =saturate( normalize(mul( BumpMap01.rgb, i.tangentTransform )));
  //fixed3 N2 =saturate( normalize(mul( BumpMap02.rgb, i.tangentTransform )));
  //fixed3 worldNormal = N1 - float3(N2.x,0,N2.z);
 
  fixed3 N1 = normalize(mul( BumpMap01.rgb, i.tangentTransform ));
  fixed3 N2 = normalize(mul( BumpMap02.rgb, i.tangentTransform ));
  fixed3 worldNormal = N1*0.5 +N2*0.5;
 
 
 
 
  float LdotN = dot(worldNormal, _LightDir.xyz); //_LightDir为模拟灯光
 
 
  //高光 
  float dotSpecular = dot(worldNormal, normalize( i.worldView+_LightDir.xyz));
  fixed3 specularReflection = pow(saturate(dotSpecular), _SpecularRatio)*_specularLight;
 
 
  //通道贴图采样
  fixed4 alphaTex = tex2D (_AlphaTex,i.alphaUV);
  //模拟灯光的颜色 * 漫反射系数= 基础水的颜色
  fixed4 col =_LightColorSelf*2 * saturate (LdotN) ;
  //用alpha贴图的r通道来模拟水的深浅的颜色,白色为深色,黑色为浅色 ,同时乘以想要的颜色
  col.rgb = col.rgb * alphaTex.r *_inSideColor * _inSideLight + col.rgb * (1-alphaTex.r) * _outSideColor *_outSideLight + specularReflection;
 
  //控制透明度,根据alpha的r通道 来控制深浅的透明度,深色的透明度小 浅色的透明度大
  col.a = _Alpha * alphaTex.r;
 
  //手动绘制阴影 用alpha贴图的g通道 跟col相乘 来模拟阴影 
  alphaTex.g = saturate(alphaTex.g + _shadowLight);
  col.rgb *= alphaTex.g;
  return col;
 }
  ENDCG 
  } 
 }
 
 FallBack "Diffuse"
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多