From e70b55ff58fe187b3817824e54d7c7d3e632f8eb Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 12 Feb 2023 16:34:03 +0100 Subject: [PATCH] normal based water reflection --- Assets/sunset_box/Sunset Env.mat | 3 ++- Assets/sunset_box/sunset_env_dynamic.shader | 30 +++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Assets/sunset_box/Sunset Env.mat b/Assets/sunset_box/Sunset Env.mat index 8c8f500..541f423 100644 --- a/Assets/sunset_box/Sunset Env.mat +++ b/Assets/sunset_box/Sunset Env.mat @@ -84,7 +84,7 @@ Material: - _SunAngle: 0 - _SunCutoff: 0.06 - _SunRadius: 0.06 - - _Temp: 0.594 + - _Temp: 0.826 - _UVSec: 0 - _ZWrite: 1 m_Colors: @@ -93,3 +93,4 @@ Material: - _HorizonCol: {r: 0.43, g: 0.27, b: 0.49, a: 1} - _SkyCol: {r: 0.22, g: 0.23, b: 0.58, a: 1} - _SunCol: {r: 1, g: 0.65, b: 0.05, a: 1} + - _WaterCol: {r: 0, g: 0, b: 0, a: 1} diff --git a/Assets/sunset_box/sunset_env_dynamic.shader b/Assets/sunset_box/sunset_env_dynamic.shader index 831af73..fee4381 100644 --- a/Assets/sunset_box/sunset_env_dynamic.shader +++ b/Assets/sunset_box/sunset_env_dynamic.shader @@ -21,10 +21,11 @@ _StarSizeRandom ("Star size randomness", Range(0, 1)) = 0.5 _StarTint ("Star tint", Range(0, 1)) = 0.4 [Header(Water)] + _WaterCol ("Water color", Color) = (0.03, 0.08, 0.12, 1.0) _WaterSurface ("Surface Normal", 2D) = "white" {} + _Temp ("Wave scale", Range(0, 1)) = 0 [Header(Debug)] _Grid ("Grid visibility", Range(0, 1)) = 0 - _Temp ("Test", Range(0, 1)) = 0 } SubShader @@ -53,12 +54,11 @@ struct v2f { float4 vertex : SV_POSITION; - float3 camPos : TEXCOORD1; - float3 hitPos : TEXCOORD2; + float3 cam_pos : TEXCOORD0; + float3 hit_pos : TEXCOORD1; }; sampler2D _MainTex; - sampler2D _WaterSurface; fixed4 _SkyCol; fixed4 _HorizonCol; @@ -74,6 +74,9 @@ float _SunAngle; float _SunRadius; float _SunCutoff; + + sampler2D _WaterSurface; + fixed4 _WaterCol; float _Temp; float _Grid; @@ -82,30 +85,29 @@ { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); - o.camPos = _WorldSpaceCameraPos; - o.hitPos = mul(unity_ObjectToWorld, v.vertex); + o.cam_pos = _WorldSpaceCameraPos; + o.hit_pos = mul(unity_ObjectToWorld, v.vertex); return o; } fixed4 frag (v2f i) : SV_Target { - fixed4 water_mod = 1.0; + float water_mod = 0.0; - float3 real_dir = normalize(i.hitPos - i.camPos); + float3 real_dir = normalize(i.hit_pos - i.cam_pos); float3 dir = real_dir; float real_y = asin(dir.y); if (real_y < 0.0) { // distort to look like water reflection - float3 camera_local_pos = i.camPos + mul(unity_WorldToObject, float4(0, 0, 0, 1)); + float3 camera_local_pos = i.cam_pos + mul(unity_WorldToObject, float4(0, 0, 0, 1)); float3 surface_pos = float3 ( camera_local_pos.x - camera_local_pos.y / (real_dir.y / real_dir.x), 0.0, camera_local_pos.z - camera_local_pos.y / (real_dir.y / real_dir.z) ); - float3 water_normal = normalize ((tex2D(_WaterSurface, surface_pos.xz * 0.05) - 0.5).zxy ); - water_normal = lerp(UP, water_normal, _Temp); - dir = reflect(dir, water_normal); + float3 water_normal = normalize((tex2D(_WaterSurface, surface_pos.xz * _Temp) - 0.5).zxy); - // water_mod = fixed4(0.4, 0.7, 0.8, 1.0); + dir = reflect(dir, water_normal); + water_mod = dot(dir, water_normal); } float y = abs(real_y); dir.y = abs(dir.y); @@ -162,7 +164,7 @@ col = lerp(col, _SunCol, sun_amount); - col *= water_mod; + col = lerp(col, _WaterCol, water_mod); // debug, this should not be visible if (dir.y < 0) {