normal based water reflection

This commit is contained in:
Crispy 2023-02-12 16:34:03 +01:00
parent cc23401dd8
commit e70b55ff58
2 changed files with 18 additions and 15 deletions

View file

@ -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}

View file

@ -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) {