Unity开发实战|Shader——水面效果
Shader "Custom/Water"
{
Properties
{
_Tint("Tint", Color) = (1, 1, 1, .5)
_MainTex("Main Texture", 2D) = "white" {}
_MainDistortionFactor("Main Distortion Factor", Range(0,10)) = 1
_Amount("Wave Amount", Range(0,1)) = 0.5
_Height("Wave Height", Range(0,1)) = 0.5
_Speed("Wave Speed", Range(0,1)) = 0.5
_FoamThickness("Foam Thickness", Range(0,10)) = 0.5
_DistortionMap("Distortion Tex", 2D) = "grey"{}
_DistortionFactor("Distortion Factor", Range(0.001,10)) = 1
_EdgeColor("Edge Color", Color) = (1, 1, 1, .5)
}SubShader
{
Tags { "RenderType" = "Opaque""Queue" = "Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlphaGrabPass{}Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"float4 _Tint, _EdgeColor;
float _Speed, _Amount, _Height, _FoamThickness, _DistortionFactor, _MainDistortionFactor;
sampler2D _DistortionMap, _MainTex;
sampler2D _CameraDepthTexture, _GrabTexture;
//unity内置变量,无需在Properties中声明
float4 _DistortionMap_ST, _MainTex_ST;
float4 _GrabTexture_TexelSize;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
float2 depthtex : TEXCOORD2;
float2 dismap : TEXCOORD3;
float4 grabtex : TEXCOORD4;
};
v2f vert(appdata v)
{
v2f o;
v.vertex.y += sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount)) * _Height;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
o.dismap = TRANSFORM_TEX(v.vertex, _DistortionMap);
o.grabtex = ComputeGrabScreenPos(o.vertex);
return o;
}fixed4 frag(v2f i) : SV_Target
{
float2 dism = UnpackNormal(tex2D(_DistortionMap, i.dismap + (_Time.x * 0.2)));
float2 offset = dism * (_DistortionFactor * 10) * _GrabTexture_TexelSize.xy * 10;
i.grabtex.xy = offset + i.grabtex.xy;
float4 dis = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.grabtex)) * _EdgeColor;
float4 depthSample = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, (i.screenPos));
float depth = LinearEyeDepth(depthSample);
float foamLine = 1 - saturate(_FoamThickness * (depth - i.screenPos.w));
half4 col = tex2D(_MainTex, i.uv + offset * _MainDistortionFactor / _DistortionFactor) * _Tint;
col += foamLine * _Tint;
col = (col + dis) * col.a;
return col;
}
ENDCG
}
}}
【Unity开发实战|Shader——水面效果】
推荐阅读
- 深入理解Go之generate
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 《机器学习实战》高清中文版PDF英文版PDF+源代码下载
- Unity和Android通信系列文章2——扩展UnityPlayerActivity
- --木木--|--木木-- 第二课作业#翼丰会(每日一淘6+1实战裂变被动引流# 6+1模式)
- 我的软件测试开发工程师书单
- echart|echart 双轴图开发
- NPDP拆书(三)(新产品开发战略(经营与创新战略))
- 芯灵思SinlinxA33开发板Linux内核定时器编程
- 2020-07-29《吴军·阅读与写作50讲》24实战才能转化效能