From 32d7e50e40fe6bd8321209dfd94ae6f67306d911 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 18 Feb 2023 22:45:57 +0100 Subject: [PATCH] sunset box: refraction --- Assets/sunset_box/Sunset Env.mat | 12 +++-- Assets/sunset_box/sunset_env_dynamic.shader | 49 +++++++++++++++------ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/Assets/sunset_box/Sunset Env.mat b/Assets/sunset_box/Sunset Env.mat index cc79cb4..1164674 100644 --- a/Assets/sunset_box/Sunset Env.mat +++ b/Assets/sunset_box/Sunset Env.mat @@ -59,6 +59,10 @@ Material: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} + - _WaterNormal: + m_Texture: {fileID: 2800000, guid: cafeae09bbcef5a6cabeaac76bab5462, type: 3} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} - _WaterSurface: m_Texture: {fileID: 2800000, guid: cafeae09bbcef5a6cabeaac76bab5462, type: 3} m_Scale: {x: 1.6, y: 1} @@ -72,7 +76,7 @@ Material: - _Glossiness: 0.5 - _GlossyReflections: 1 - _Grid: 0 - - _HeightOffset: -0.7 + - _HeightOffset: -1 - _HorizonTint: 0.1 - _Metallic: 0 - _Mode: 0 @@ -92,12 +96,12 @@ Material: - _SunRadius: 0.037 - _Temp: 0.703 - _UVSec: 0 - - _WaveStrength: 0.5 + - _WaveStrength: 1 - _ZWrite: 1 m_Colors: - _Color: {r: 1, g: 1, b: 1, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} - _HorizonCol: {r: 0.43, g: 0.27, b: 0.49, a: 1} - - _SkyCol: {r: 0.13841508, g: 0.08388217, b: 0.2735849, a: 1} + - _SkyCol: {r: 0.08235293, g: 0.09343877, b: 0.27450982, a: 1} - _SunCol: {r: 1, g: 0.5988961, b: 0.05098039, a: 1} - - _WaterCol: {r: 0.041651834, g: 0.0754717, b: 0.060873438, a: 1} + - _WaterCol: {r: 0.6556604, g: 1, b: 0.8435335, a: 1} diff --git a/Assets/sunset_box/sunset_env_dynamic.shader b/Assets/sunset_box/sunset_env_dynamic.shader index e24ae53..10c9a9b 100644 --- a/Assets/sunset_box/sunset_env_dynamic.shader +++ b/Assets/sunset_box/sunset_env_dynamic.shader @@ -24,7 +24,7 @@ _WaterCol ("Water color", Color) = (0.03, 0.08, 0.12, 1.0) _WaveStrength ("Wave scale", Range(0, 1)) = 1 [NoScaleOffset] - _WaterSurface ("Surface Normal", 2D) = "white" {} + _WaterNormal ("Surface Normal", 2D) = "white" {} [Header(Debug)] _Grid ("Grid visibility", Range(0, 1)) = 0 @@ -76,7 +76,7 @@ float _SunRadius; float _SunCutoff; - sampler2D _WaterSurface; + sampler2D _WaterNormal; float3 _WaterCol; float _HeightOffset; float _WaveStrength; @@ -93,15 +93,20 @@ } float3 get_water_normal(float2 pos) { - pos *= 0.03; + pos *= 0.3; float3 normal = 0; float t1 = _Time.x * 0.18; - normal += (tex2D(_WaterSurface, pos * 1.04 + float2(t1, t1 * 0.5)) - 0.5); + normal += (tex2D(_WaterNormal, float2(1, -1) * pos + float2(t1, t1 * 0.5)) - 0.5).zxy; float t2 = _Time.x * 0.37; - normal += (tex2D(_WaterSurface, pos * 0.276 + float2(t2 * 0.8, t2)) - 0.5); - // float t3 = _Time.x * 0.08; - // normal += (tex2D(_WaterSurface, pos * 0.07 + float2(t3 * 0.8, -t3)) - 0.5); - normal = normalize(normal.zxy); + normal += (tex2D(_WaterNormal, pos * 0.276 + float2(t2 * 0.8, t2)) - 0.5).zxy; + float t3 = _Time.x * 0.08; + normal += (tex2D(_WaterNormal, pos * 0.07 + float2(t3 * 0.8, -t3)) - 0.5).zxy; + // normal = normalize(normal.zxy); + + // normal = UP; + // normal += (tex2D(_NoiseTex, pos).zxy - 0.5)*0.4; + normal = normalize(normal); + // return UP; return lerp(UP, normal, _WaveStrength); @@ -157,14 +162,21 @@ col = lerp(col, star_col, star_strength); - /// debug grid - col = lerp(col, WHITE, _Grid * (frac(cellx) < 0.04 || frac(celly) < 0.04)); - /// sun float alignment = min(acos(dot(dir, sun_dir)), 1); float sun_amount = smax(min(_SunRadius / alignment, 5) - _SunCutoff, 0, 0.15); col = lerp(col, _SunCol, sun_amount); + + /// debug grid + // col = lerp(col, WHITE, _Grid * (frac(cellx) < 0.04 || frac(celly) < 0.04)); + col = lerp(col, WHITE, _Grid * ( + pow(frac( cellx), 20) + + pow(frac( celly), 20) + + pow(frac(-cellx), 20) + + pow(frac(-celly), 20) + )); + return col; } @@ -172,7 +184,9 @@ { // float3 horizon_col = lerp(_SkyCol, _SunCol, _HorizonTint); float3 origin = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)); - float3 sun_dir = normalize(mul(unity_ObjectToWorld, float4(0, 0, 1, 1)) * float3(1, 0, 1) - origin); + float3 sun_dir = mul(unity_ObjectToWorld, float4(0, 0, 1, 1)) * float3(1, 0, 1) - origin; + // sun_dir.y = sin(_Time.x * 10) * 0.05; + sun_dir = normalize(sun_dir); float3 dir = normalize(i.hit_pos - i.cam_pos); float theta = atan2(dir.x, dir.z); // latitude @@ -191,12 +205,19 @@ ); float3 water_normal = get_water_normal(surface_pos.xz); + float3 refracted_dir = normalize(refract(dir, water_normal, 1/1.333)); + float subsurf = (refracted_dir.y + 1); + dir = reflect(dir, water_normal); phi = asin(dir.y); float hit_angle = dot(dir, water_normal); - float3 sky_reflection = sky(dir, theta, phi, sun_dir); - col = lerp(sky_reflection, _WaterCol, hit_angle); + float3 sky_reflection = sky(dir, theta, phi, sun_dir) ; + + float3 water_col = lerp(_SkyCol, _SunCol, 0.01) * _WaterCol; + col = water_col * subsurf * 12; + col = lerp(sky_reflection, col, hit_angle); + // float distance = length(surface_pos - camera_local_pos); // float fog_factor = smoothstep(10, 70, distance) * 0.4; // col = lerp(col, horizon_col, fog_factor);