Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Fayanzar · Aug 31, 2013 at 11:26 AM · shaderwaterdepthfree

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges