proper water reflections

This commit is contained in:
Crispy 2023-02-12 16:13:50 +01:00
parent a2f5d5fe6c
commit cc23401dd8
3 changed files with 31 additions and 15 deletions

View file

@ -84,6 +84,7 @@ Material:
- _SunAngle: 0 - _SunAngle: 0
- _SunCutoff: 0.06 - _SunCutoff: 0.06
- _SunRadius: 0.06 - _SunRadius: 0.06
- _Temp: 0.594
- _UVSec: 0 - _UVSec: 0
- _ZWrite: 1 - _ZWrite: 1
m_Colors: m_Colors:

View file

@ -567,7 +567,7 @@ PrefabInstance:
- target: {fileID: -8679921383154817045, guid: 5009e49fafd33ed0e980b53b1f9e954d, - target: {fileID: -8679921383154817045, guid: 5009e49fafd33ed0e980b53b1f9e954d,
type: 3} type: 3}
propertyPath: m_LocalPosition.z propertyPath: m_LocalPosition.z
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 5009e49fafd33ed0e980b53b1f9e954d, - target: {fileID: -8679921383154817045, guid: 5009e49fafd33ed0e980b53b1f9e954d,
type: 3} type: 3}

View file

@ -24,6 +24,7 @@
_WaterSurface ("Surface Normal", 2D) = "white" {} _WaterSurface ("Surface Normal", 2D) = "white" {}
[Header(Debug)] [Header(Debug)]
_Grid ("Grid visibility", Range(0, 1)) = 0 _Grid ("Grid visibility", Range(0, 1)) = 0
_Temp ("Test", Range(0, 1)) = 0
} }
SubShader SubShader
@ -41,7 +42,8 @@
#include "UnityCG.cginc" #include "UnityCG.cginc"
#define PI 3.1416f #define PI 3.1416f
#define WHITE fixed4(1.0, 1.0, 1.0, 1.0) #define WHITE fixed4(1, 1, 1, 1)
#define UP float3(0, 1, 0)
struct appdata struct appdata
{ {
@ -72,6 +74,7 @@
float _SunAngle; float _SunAngle;
float _SunRadius; float _SunRadius;
float _SunCutoff; float _SunCutoff;
float _Temp;
float _Grid; float _Grid;
@ -86,21 +89,26 @@
fixed4 frag (v2f i) : SV_Target fixed4 frag (v2f i) : SV_Target
{ {
float3 dir = normalize(i.hitPos - i.camPos); fixed4 water_mod = 1.0;
float real_y = asin(dir.y);
float y = abs(real_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 surface_pos = float3(
camera_local_pos.x - camera_local_pos.y / (dir.y / dir.x),
0.0,
camera_local_pos.z - camera_local_pos.y / (dir.y / dir.z)
);
// return fixed4(surface_pos, 1.0);
// float3 surface_pos = float3 real_dir = normalize(i.hitPos - i.camPos);
dir += (tex2D(_WaterSurface, surface_pos.xz * 0.1) - 0.5) * 0.4; 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 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);
// water_mod = fixed4(0.4, 0.7, 0.8, 1.0);
} }
float y = abs(real_y);
dir.y = abs(dir.y);
float phi = atan2(dir.x, dir.z); float phi = atan2(dir.x, dir.z);
@ -153,6 +161,13 @@
float sun_amount = max(min(_SunRadius/alignment * sun_gradient, 5) - _SunCutoff, 0); float sun_amount = max(min(_SunRadius/alignment * sun_gradient, 5) - _SunCutoff, 0);
col = lerp(col, _SunCol, sun_amount); col = lerp(col, _SunCol, sun_amount);
col *= water_mod;
// debug, this should not be visible
if (dir.y < 0) {
return fixed4(1, 0, 0, 1);
}
return col; return col;
} }
ENDCG ENDCG