small fixes & cleanup

This commit is contained in:
Crispy 2023-02-14 20:04:51 +01:00
parent 882f70be0b
commit 694dab564d
5 changed files with 60 additions and 52 deletions

View file

@ -6,12 +6,16 @@ public class Screenshot : MonoBehaviour
public bool takeScreenshot; public bool takeScreenshot;
public string filename = "unity_screenshot.png"; public string filename = "unity_screenshot.png";
public RenderTexture textureReference;
void Start() void Start()
{ {
if (takeScreenshot) if (takeScreenshot)
{ {
Camera cam = GetComponent<Camera>(); Camera cam = GetComponent<Camera>();
cam.targetTexture = textureReference;
RenderTexture currentRT = RenderTexture.active; RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = cam.targetTexture; RenderTexture.active = cam.targetTexture;

View file

@ -91,7 +91,7 @@ Material:
- _SunRadius: 0.037 - _SunRadius: 0.037
- _Temp: 0.5 - _Temp: 0.5
- _UVSec: 0 - _UVSec: 0
- _WaveStrength: 1 - _WaveStrength: 0.6
- _ZWrite: 1 - _ZWrite: 1
m_Colors: m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1} - _Color: {r: 1, g: 1, b: 1, a: 1}

View file

@ -13,8 +13,8 @@ RenderTexture:
m_ForcedFallbackFormat: 4 m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0 m_DownscaleFallback: 0
serializedVersion: 3 serializedVersion: 3
m_Width: 3840 m_Width: 1920
m_Height: 2160 m_Height: 1080
m_AntiAliasing: 1 m_AntiAliasing: 1
m_MipCount: -1 m_MipCount: -1
m_DepthFormat: 2 m_DepthFormat: 2

View file

@ -571,6 +571,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
takeScreenshot: 0 takeScreenshot: 0
filename: unity_screenshot.png filename: unity_screenshot.png
textureReference: {fileID: 8400000, guid: 3df80b8b4132800b79325d3f3d7bf8bd, type: 2}
--- !u!1001 &2104997433 --- !u!1001 &2104997433
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -44,7 +44,7 @@
#include "UnityCG.cginc" #include "UnityCG.cginc"
#define PI 3.1416f #define PI 3.1416f
#define WHITE fixed4(1, 1, 1, 1) #define WHITE 1
#define UP float3(0, 1, 0) #define UP float3(0, 1, 0)
struct appdata struct appdata
@ -60,7 +60,7 @@
}; };
sampler2D _NoiseTex; sampler2D _NoiseTex;
fixed4 _SkyCol; float3 _SkyCol;
float _HorizonTint; float _HorizonTint;
float _StarsMissing; float _StarsMissing;
@ -71,13 +71,13 @@
float _StarSizeRandom; float _StarSizeRandom;
float _StarTint; float _StarTint;
fixed4 _SunCol; float3 _SunCol;
float _SunAngle; float _SunAngle;
float _SunRadius; float _SunRadius;
float _SunCutoff; float _SunCutoff;
sampler2D _WaterSurface; sampler2D _WaterSurface;
fixed4 _WaterCol; float3 _WaterCol;
float _WaveStrength; float _WaveStrength;
float _Grid; float _Grid;
@ -91,8 +91,8 @@
return o; return o;
} }
fixed3 get_water_normal(float2 pos) { float3 get_water_normal(float2 pos) {
fixed3 normal = 0.0; float3 normal = 0;
float t1 = _Time.x * 0.18; float t1 = _Time.x * 0.18;
normal += (tex2D(_WaterSurface, pos * 1.04 + float2(t1, t1 * 0.5)) - 0.5); normal += (tex2D(_WaterSurface, pos * 1.04 + float2(t1, t1 * 0.5)) - 0.5);
float t2 = _Time.x * 0.37; float t2 = _Time.x * 0.37;
@ -101,66 +101,40 @@
normal += (tex2D(_WaterSurface, pos * 0.07 + float2(t3 * 0.8, -t3)) - 0.5); normal += (tex2D(_WaterSurface, pos * 0.07 + float2(t3 * 0.8, -t3)) - 0.5);
// return UP; // return UP;
return normalize(normal.zxy); return lerp(UP, normalize(normal.zxy), _WaveStrength);
} }
inline 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; float h = max(k - abs(a - b), 0) / k;
return min(a, b) - h*h*h*k * 1/6.0; return min(a, b) - h * h * h * k * 1/6;
} }
inline 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; float h = max(k - abs(a - b), 0) / k;
return max(a, b) + h*h*h*k * 1/6.0; return max(a, b) + h * h * h * k * 1/6;
} }
fixed4 frag(v2f i) : SV_Target float3 sky(float3 dir, float theta, float phi, float3 sun_dir) {
{ /// background
float water_mod = 0.0; float factor = smoothstep(0, 0.5, dir.y + 0.2);
float water_reflection = 0.0; float3 horizon_col = lerp(_SkyCol, _SunCol, _HorizonTint);
float3 col = lerp(horizon_col, _SkyCol, factor);
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 / (dir.y / dir.x),
0,
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;
}
/// sky
float factor = smoothstep(0.0, 0.5, dir.y + 0.2);
factor = min(factor, 1.0);
fixed4 horizon_col = lerp(_SkyCol, _SunCol, _HorizonTint);
fixed4 col = lerp(horizon_col, _SkyCol, factor);
/// stars /// stars
float2 cells = float2(-1, floor(_StarDensity)); float2 cells = float2(-1, floor(_StarDensity));
float cell_x_base = floor(cells.y * PI); float cell_x_base = floor(cells.y * PI);
float celly = phi * cells.y; float celly = phi * cells.y;
// cells per ring depend on y pos, to reduce warping around the poles: // 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); cells.x = floor(cos(floor(celly) / _StarDensity) * cell_x_base);
float cellx = (theta / PI * cells.x); float cellx = (theta / PI * cells.x);
float2 pos = float2(cellx, celly);// cell-space pos of this fragment float2 pos = float2(cellx, celly); // cell-space pos of this pixel
float2 cell_pos = float2(floor(cellx), floor(celly)); // position of this cell float2 cell_pos = float2(floor(cellx), floor(celly)); // position of this cell
float2 cell_center = cell_pos + float2(0.5, 0.5); float2 cell_center = cell_pos + 0.5;
float2 star_pos = cell_center + (tex2D(_NoiseTex, 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 /// star color
float3 r = tex2D(_NoiseTex, cell_pos / cells); float3 r = tex2D(_NoiseTex, cell_pos / cells);
@ -176,20 +150,49 @@
star_strength *= length(r) / 2; // fade stars star_strength *= length(r) / 2; // fade stars
star_strength *= rnum2 > _StarsMissing; // remove stars star_strength *= rnum2 > _StarsMissing; // remove stars
fixed3 star_col = lerp(1.0, r, _StarTint); float3 star_col = lerp(WHITE, r, _StarTint);
col = lerp(col, fixed4(star_col, 1.0), star_strength);
col = lerp(col, star_col, star_strength);
/// debug grid /// debug grid
col = lerp(col, WHITE, _Grid * (frac(cellx) < 0.04 || frac(celly) < 0.04)); col = lerp(col, WHITE, _Grid * (frac(cellx) < 0.04 || frac(celly) < 0.04));
/// sun /// sun
float3 sun_dir = float3(sin(_SunAngle), 0.0, cos(_SunAngle));
float alignment = min(acos(dot(dir, sun_dir)), 1); float alignment = min(acos(dot(dir, sun_dir)), 1);
float sun_amount = smax(min(_SunRadius / alignment, 5) - _SunCutoff, 0, 0.15); float sun_amount = smax(min(_SunRadius / alignment, 5) - _SunCutoff, 0, 0.15);
col = lerp(col, _SunCol, sun_amount); col = lerp(col, _SunCol, sun_amount);
return col;
}
float3 frag(v2f i) : SV_Target
{
float3 sun_dir = float3(sin(_SunAngle), 0, cos(_SunAngle));
float water_mod = 0;
// float water_reflection = 0;
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) {
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 / (dir.y / dir.x),
0,
camera_local_pos.z - camera_local_pos.y / (dir.y / dir.z)
);
float3 water_normal = get_water_normal(surface_pos.xz);
dir = reflect(dir, water_normal);
phi = asin(dir.y);
water_mod = dot(dir, water_normal);
// water_reflection = 1;
}
float3 col = sky(dir, theta, phi, sun_dir);
col = lerp(col, _WaterCol, water_mod); col = lerp(col, _WaterCol, water_mod);
return col; return col;
} }