- Home /
Water with depth using Unity 3~4
Recently I've found a shader that allows to make water with depth in free version of Unity. But it was written for Unity 2.x, so it's a per-pixel lit shader and it needs to be rewritten as a surface shader. Cause I don't know much about rewriting shaders in such way, I'm asking for help. The code is below.
// Upgrade NOTE: replaced 'PositionFog()' with multiply of UNITY_MATRIX_MVP by position
// Upgrade NOTE: replaced 'V2F_POS_FOG' with 'float4 pos : SV_POSITION'
Shader "Depthmapped/Depthmapped Water Simple a" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_AlphaMultiplier ("Alpha Multiplier", Float) = 1.0
_DepthMap ("Depthmap (RGBA) ", 2D) = "" { }
_Fractal ("Fractal (A)", 2D) = "" {}
_WaveScale ("Wave scale", Range (0.001,0.15)) = 0.063
_WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
}
Category {
Tags {Queue=Transparent}
Alphatest Greater 0
Fog { Color [_AddFog] }
ZWrite Off
ColorMask RGB
// ------------------------------------------------------------------
// ARB fragment program
#warning Upgrade NOTE: SubShader commented out; uses Unity 2.x per-pixel lighting. You should rewrite shader into a Surface Shader.
/*SubShader {
//UsePass "Transparent/Specular/BASE"
// Pixel lights
Pass {
Blend SrcAlpha OneMinusSrcAlpha
Name "PPL"
Tags { "LightMode" = "Pixel" }
CGPROGRAM
// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members uvK,uv2,viewDirT,lightDirT,uvD,viewDirW,bumpuv)
#pragma exclude_renderers d3d11 xbox360
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin_noshadows
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct v2f {
float4 pos : SV_POSITION;
LIGHTING_COORDS
float3 uvK; // xy = UV, z = specular K
float2 uv2;
float3 viewDirT;
float3 lightDirT;
float2 uvD;
float3 viewDirW;
float2 bumpuv[2];
};
uniform float4 _MainTex_ST, _BumpMap_ST;
uniform float _Shininess;
uniform float _AlphaMultiplier;
uniform float4 _WaveSpeed;
uniform float _WaveScale;
v2f vert (appdata_tan v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uvK.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uvK.z = _Shininess * 128;
o.uv2 = TRANSFORM_TEX(v.texcoord,_BumpMap);
o.uvD = v.texcoord;
o.viewDirW = normalize(ObjSpaceViewDir( v.vertex ));
float4 temp;
temp.xyzw = (v.vertex.xzxz + _Time.x * _WaveSpeed.xyzw) * _WaveScale;
o.bumpuv[0] = temp.xy * float2(.4, .45);
o.bumpuv[1] = temp.wz;
TANGENT_SPACE_ROTATION;
o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );
o.viewDirT = mul( rotation, ObjSpaceViewDir( v.vertex ) );
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
uniform sampler2D _BumpMap;
uniform sampler2D _MainTex;
uniform float4 _Color;
uniform sampler2D _DepthMap;
uniform sampler2D _Fractal;
half depthToAlpha(half3 viewDir, half4 depth)
{
half4 dir;
dir.x = max(dot(viewDir,normalize(half3(0,0,-1))),0);
dir.y = max(dot(viewDir,normalize(half3(-1,0,1))),0);
dir.z = max(dot(viewDir,normalize(half3(1,0,1))),0);
dir.w = max(dot(viewDir,normalize(half3(0,1,0))),0);
dir = normalize(dir);
half alpha = dot(depth,dir);
return alpha;
}
float4 frag (v2f i) : COLOR
{
float4 texcol = tex2D( _MainTex, i.uvK.xy );
texcol = _Color + texcol*0.1;
float3 normal = tex2D(_BumpMap, i.uv2).xyz * 2 - 1;
half3 bump1 = tex2D( _BumpMap, i.bumpuv[0] ).rgb;
half3 bump2 = tex2D( _BumpMap, i.bumpuv[1] ).rgb;
normal = (bump1 + bump2 - 1);
half noise1 = tex2D( _Fractal, i.bumpuv[0] ).rgb;
half noise2 = tex2D( _Fractal, i.bumpuv[1] ).rgb;
half noise = saturate(noise1 + noise2 - 1);
half4 c = SpecularLight( i.lightDirT, i.viewDirT, normal, texcol, i.uvK.z, LIGHT_ATTENUATION(i) );
half4 depth = tex2D(_DepthMap, i.uvD);
c.a = (depthToAlpha(i.viewDirW,depth)*_AlphaMultiplier + 0.5)/1.5;
c.rgba += (clamp(noise,0.3,1.0)-0.3)*1;
return c;
}
ENDCG
}
}*/
}
}
Comment
Your answer
Follow this Question
Related Questions
Free Ocean Shaders 2 Answers
Need to compare 2 depth images (16 bit)... 0 Answers
Disabled ZWrite - Managing ZDepth Shader Unity 0 Answers
Shader based camera compositing. 2 Answers
Shader Depth Mask - is it possible to limit its distance? 1 Answer