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

所属分类: 软件编程 / C#教程 阅读数: 144
收藏 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#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多