Created
September 28, 2021 17:58
-
-
Save unitycoder/ec38bb08141ea49ee1db5ffe90ac38cf to your computer and use it in GitHub Desktop.
URP Simple Water Custom Shader (SRB batcher compatible)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://forum.unity.com/threads/can-anyone-give-me-a-quick-rundown-of-the-basics-of-a-handwritten-urp-shader-vs-in-built.757823/#post-7530506 | |
| Shader "URP/Unlit/SimpleWater" | |
| { | |
| Properties | |
| { | |
| _Tint("Tint", Color) = (1, 1, 1, .5) | |
| _MainTex("Main Texture", 2D) = "white" {} | |
| _NoiseTex("Extra Wave Noise", 2D) = "white" {} | |
| _Speed("Wave Speed", Range(0,1)) = 0.5 | |
| _Amount("Wave Amount", Range(0,1)) = 0.5 | |
| _Height("Wave Height", Range(0,1)) = 0.5 | |
| _Foam("Foamline Thickness", Range(0,3)) = 0.5 | |
| } | |
| SubShader | |
| { | |
| Tags { "RenderType" = "Opaque" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline"} | |
| LOD 100 | |
| Blend SrcAlpha OneMinusSrcAlpha | |
| Pass | |
| { | |
| Name "Unlit" | |
| HLSLPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| // make fog work | |
| #pragma multi_compile_fog | |
| //#include "UnityCG.cginc" | |
| #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
| struct appdata | |
| { | |
| float4 vertex : POSITION; | |
| float2 uv : TEXCOORD0; | |
| }; | |
| struct v2f | |
| { | |
| float2 uv : TEXCOORD0; | |
| float fogCoords : TEXCOORD1; | |
| float4 vertex : SV_POSITION; | |
| float4 scrPos : TEXCOORD2;// | |
| }; | |
| CBUFFER_START(UnityPerMaterial) | |
| float4 _Tint; | |
| Texture2D _CameraDepthTexture; SamplerState sampler_CameraDepthTexture; //Depth Texture | |
| sampler2D _MainTex, _NoiseTex;// | |
| float4 _MainTex_ST; | |
| float _Speed, _Amount, _Height, _Foam;// | |
| CBUFFER_END | |
| v2f vert(appdata v) | |
| { | |
| v2f o; | |
| float4 tex = tex2Dlod(_NoiseTex, float4(v.uv.xy, 0, 0));//extra noise tex | |
| v.vertex.y += sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount * tex)) * _Height;//movement | |
| //o.vertex = UnityObjectToClipPos(v.vertex); | |
| VertexPositionInputs vertInputs = GetVertexPositionInputs(v.vertex.xyz); //This function calculates all the relative spaces of the objects vertices | |
| o.vertex = vertInputs.positionCS; | |
| o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
| o.scrPos = ComputeScreenPos(o.vertex); // grab position on screen | |
| //UNITY_TRANSFER_FOG(o,o.vertex); | |
| o.fogCoords = ComputeFogFactor(o.vertex.z); | |
| return o; | |
| } | |
| half4 frag(v2f i) : SV_Target | |
| { | |
| // sample the texture | |
| half4 col = tex2D(_MainTex, i.uv) * _Tint;// texture times tint; | |
| half depth = LinearEyeDepth(_CameraDepthTexture.Sample(sampler_CameraDepthTexture, i.scrPos.xy / i.scrPos.w).r, _ZBufferParams); | |
| half4 foamLine = 1 - saturate(_Foam * (depth - i.scrPos.w));// foam line by comparing depth and screenposition | |
| col += foamLine * _Tint; // add the foam line and tint to the texture | |
| return half4(MixFog(col, i.fogCoords), col.a); | |
| } | |
| ENDHLSL | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment