cleanup and fix reflection angles of stars

This commit is contained in:
Crispy 2023-02-14 18:48:42 +01:00
parent 8f9dd2d571
commit 882f70be0b
3 changed files with 24 additions and 27 deletions

View file

@ -47,6 +47,10 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _NoiseTex:
m_Texture: {fileID: 2800000, guid: 1f675a065618d234abec48e229fcf618, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}

View file

@ -533,7 +533,7 @@ Camera:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 8400000, guid: 3df80b8b4132800b79325d3f3d7bf8bd, type: 2}
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1

View file

@ -12,7 +12,7 @@
_SunCutoff ("Sun cutoff", Range(0, 0.5)) = 0.08
[Header(Star Layout)]
[NoScaleOffset]
_MainTex ("Noise source", 2D) = "white" {}
_NoiseTex ("Noise source", 2D) = "white" {}
_StarDensity ("Star density", Range(4, 50)) = 20
_StarRandom ("Star randomness", Range(0, 1)) = 0.85
[Header(Star)]
@ -59,7 +59,7 @@
float3 hit_pos : TEXCOORD1;
};
sampler2D _MainTex;
sampler2D _NoiseTex;
fixed4 _SkyCol;
float _HorizonTint;
@ -92,7 +92,7 @@
}
fixed3 get_water_normal(float2 pos) {
float3 normal = 0.0;
fixed3 normal = 0.0;
float t1 = _Time.x * 0.18;
normal += (tex2D(_WaterSurface, pos * 1.04 + float2(t1, t1 * 0.5)) - 0.5);
float t2 = _Time.x * 0.37;
@ -100,16 +100,17 @@
float t3 = _Time.x * 0.08;
normal += (tex2D(_WaterSurface, pos * 0.07 + float2(t3 * 0.8, -t3)) - 0.5);
// return UP;
return normalize(normal.zxy);
}
float smin(float a, float b, float k)
inline float smin(float a, float b, float k)
{
float h = max(k - abs(a-b), 0) / k;
return min(a, b) - h*h*h*k * 1/6.0;
}
float smax(float a, float b, float k)
inline float smax(float a, float b, float k)
{
float h = max(k - abs(a - b), 0) / k;
return max(a, b) + h*h*h*k * 1/6.0;
@ -120,28 +121,26 @@
float water_mod = 0.0;
float water_reflection = 0.0;
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 dir = normalize(i.hit_pos - i.cam_pos);
float theta = atan2(dir.x, dir.z); // latitude
float phi = asin(dir.y); // longitude
if (phi < 0.0) { // distort to look like water reflection
float3 object_pos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
float3 camera_local_pos = i.cam_pos - object_pos;
float3 surface_pos = float3 (
camera_local_pos.x - camera_local_pos.y / (real_dir.y / real_dir.x),
camera_local_pos.x - camera_local_pos.y / (dir.y / dir.x),
0,
camera_local_pos.z - camera_local_pos.y / (real_dir.y / real_dir.z)
camera_local_pos.z - camera_local_pos.y / (dir.y / dir.z)
);
float3 water_normal = get_water_normal(surface_pos.xz);
water_normal = lerp(UP, water_normal, _WaveStrength);
dir = reflect(dir, water_normal);
phi = asin(dir.y);
water_mod = dot(dir, water_normal);
water_reflection = 1.0;
}
float y = abs(real_y);
dir.y = abs(dir.y);
float phi = atan2(dir.x, dir.z);
/// sky
float factor = smoothstep(0.0, 0.5, dir.y + 0.2);
@ -152,19 +151,19 @@
/// stars
float2 cells = float2(-1, floor(_StarDensity));
float cell_x_base = floor(cells.y * PI);
float celly = y * cells.y;
float celly = phi * cells.y;
// cells per ring depend on y pos, to reduce warping around the poles:
cells.x = max(floor(cos(floor(celly)/_StarDensity) * cell_x_base), 3);
float cellx = (phi / PI * cells.x);
float cellx = (theta / PI * cells.x);
float2 pos = float2(cellx, celly);// cell-space pos of this fragment
float2 cell_pos = float2(floor(cellx), floor(celly)); // position of this cell
float2 cell_center = cell_pos + float2(0.5, 0.5);
float2 star_pos = cell_center + (tex2D(_MainTex, cell_pos / cells + float2(0., 0.1)) - 0.5) * _StarRandom;
float2 star_pos = cell_center + (tex2D(_NoiseTex, cell_pos / cells + float2(0., 0.1)) - 0.5) * _StarRandom;
/// star color
float3 r = tex2D(_MainTex, cell_pos / cells);
float3 r = tex2D(_NoiseTex, cell_pos / cells);
float rnum = frac((r.r + r.g - r.b) * 10);
float rnum2 = frac((r.r - r.g + r.b) * 10);
@ -173,7 +172,7 @@
float distance = length(pos - star_pos);
float star_strength = max(min(star_size / distance * 0.5, 1.25) - 0.25, 0); // star glow
star_strength *= clamp(sin(y * 2) - 0.1, 0, 1); // fade stars near/under horizon
star_strength *= clamp(sin(phi * 2) - 0.1, 0, 1); // fade stars near/under horizon
star_strength *= length(r) / 2; // fade stars
star_strength *= rnum2 > _StarsMissing; // remove stars
@ -192,12 +191,6 @@
col = lerp(col, _SunCol, sun_amount);
col = lerp(col, _WaterCol, water_mod);
/// shiny water
// float view_perpendicular = 1 - abs(dot(dir, water_normal));
// float view_perpendicular = 1 - abs(dot(dir, water_normal));
// water_reflection = _SunCol * pow(view_perpendicular, 40);
// col += water_reflection *;
return col;
}
ENDCG