diff options
| author | yum <yum.food.vr@gmail.com> | 2025-06-20 23:26:52 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2025-06-20 23:26:52 -0700 |
| commit | 715bf39c13300dc262b7030a308502000aa6cd9e (patch) | |
| tree | 7ac40c7afa19c7a7e1f84452e4e58381910cb592 | |
| parent | 506d56be782d24f56d786b67d0f96f0c8646db6c (diff) | |
work on fog & custom30 stuff
| -rw-r--r-- | 2ner.cginc | 53 | ||||
| -rw-r--r-- | 2ner.shader | 73 | ||||
| -rw-r--r-- | custom30.cginc | 6 | ||||
| -rw-r--r-- | features.cginc | 12 | ||||
| -rw-r--r-- | fog.cginc | 80 | ||||
| -rw-r--r-- | globals.cginc | 11 | ||||
| -rw-r--r-- | tessellation.cginc | 6 |
7 files changed, 151 insertions, 90 deletions
@@ -191,12 +191,11 @@ v2f vert(appdata v) { UNITY_TRANSFER_LIGHTING(o, v.uv1);
UNITY_TRANSFER_FOG_COMBINED_WITH_EYE_VEC(o, o.pos);
+ TRANSFER_SHADOW(o);
#if defined(SHADOW_CASTER_PASS)
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
#endif
- TRANSFER_SHADOW(o);
-
// Vertex color
o.color = v.color;
return o;
@@ -207,14 +206,15 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace , out float depth : SV_DepthLessEqual
#endif
) : SV_Target {
-#if defined(_CUSTOM30) && defined(_DEPTH_PREPASS) && !defined(FORWARD_BASE_PASS)
- return 0;
-#endif
UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);
UNITY_SETUP_INSTANCE_ID(i);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
+#if defined(_CUSTOM30) && defined(_DEPTH_PREPASS) && !defined(FORWARD_BASE_PASS)
+ return 0;
+#endif
+
// Not necessarily normalized after interpolation
i.normal = normalize(i.normal);
i.tangent = normalize(i.tangent);
@@ -231,6 +231,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace // shader locker will inline those fields (incorrectly) as float4. Unity's
// shader compiler doesn't like that, demanding exact type correspondence.
// Overspecifying gets around the issue.
+
FogParams fog_params = {
_Raymarched_Fog_Color.rgb,
_Raymarched_Fog_Direct_Light_Intensity,
@@ -259,6 +260,17 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace normalize(cross(_Raymarched_Fog_Emitter_Texture_World_Normal, _Raymarched_Fog_Emitter_Texture_World_Tangent)).xyz,
_Raymarched_Fog_Emitter_Texture_World_Scale.xy,
1.0f / _Raymarched_Fog_Emitter_Texture_World_Scale.xy,
+ _Raymarched_Fog_Emitter_Texture_Luminance,
+ _Raymarched_Fog_Emitter_Texture_Intensity,
+ #endif
+ #if defined(_RAYMARCHED_FOG_EMITTER_TEXTURE_WARPING)
+ _Raymarched_Fog_Emitter_Texture_Warping_Octaves,
+ _Raymarched_Fog_Emitter_Texture_Warping_Strength,
+ _Raymarched_Fog_Emitter_Texture_Warping_Scale,
+ _Raymarched_Fog_Emitter_Texture_Warping_Speed,
+ #endif
+ #if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT)
+ _Raymarched_Fog_Density_Exponent,
#endif
};
FogResult fog_result = raymarched_fog(i, fog_params);
@@ -301,23 +313,16 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace i.uv01.xy = eye_effect_00.uv;
#endif
- float4x4 tangentToWorld = float4x4(
- float4(i.tangent.x, i.binormal.x, i.normal.x, 0),
- float4(i.tangent.y, i.binormal.y, i.normal.y, 0),
- float4(i.tangent.z, i.binormal.z, i.normal.z, 0),
- float4(0, 0, 0, 1)
- );
-
#if defined(_CUSTOM30)
#if defined(FORWARD_BASE_PASS) || (!defined(_DEPTH_PREPASS) && defined(SHADOW_CASTER_PASS))
#if defined(_CUSTOM30_BASICCUBE)
- Custom30Output c30_out = BasicCube(i, tangentToWorld);
+ Custom30Output c30_out = BasicCube(i);
#elif defined(_CUSTOM30_BASICWEDGE)
- Custom30Output c30_out = BasicWedge(i, tangentToWorld);
+ Custom30Output c30_out = BasicWedge(i);
#elif defined(_CUSTOM30_BASICPLATFORM)
- Custom30Output c30_out = BasicPlatform(i, tangentToWorld);
+ Custom30Output c30_out = BasicPlatform(i);
#elif defined(_CUSTOM30_RAINBOW)
- Custom30Output c30_out = Rainbow(i, tangentToWorld);
+ Custom30Output c30_out = Rainbow(i);
#else
Custom30Output c30_out = (Custom30Output) 0;
#endif
@@ -332,18 +337,18 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace #endif
#endif
- float ssao = 1;
-#if defined(_SSAO)
- float2 debug;
- ssao = get_ssao(i, tangentToWorld, debug);
-#endif
-
- tangentToWorld = float4x4(
+ float4x4 tangentToWorld = float4x4(
float4(i.tangent, 0),
float4(i.binormal, 0),
float4(i.normal, 0),
float4(0, 0, 0, 1)
);
+
+ float ssao = 1;
+#if defined(_SSAO)
+ float2 debug;
+ ssao = get_ssao(i, tangentToWorld, debug);
+#endif
YumPbr pbr = GetYumPbr(i, tangentToWorld);
pbr.ao *= ssao;
@@ -469,7 +474,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace // Output proper shadow data
SHADOW_CASTER_FRAGMENT(i)
-defined(MASKED_STENCIL1_PASS) || defined(MASKED_STENCIL2_PASS) || defined(MASKED_STENCIL3_PASS) || defined(MASKED_STENCIL4_PASS) || defined(DEPTH_PREPASS)
+#elif defined(MASKED_STENCIL1_PASS) || defined(MASKED_STENCIL2_PASS) || defined(MASKED_STENCIL3_PASS) || defined(MASKED_STENCIL4_PASS) || defined(DEPTH_PREPASS)
return 0;
#endif
}
diff --git a/2ner.shader b/2ner.shader index 8a8485c..7cce36d 100644 --- a/2ner.shader +++ b/2ner.shader @@ -2,7 +2,7 @@ Shader "yum_food/2ner" { // Certain parts of the Properties below are derived from Poiyomi's toon // shader. The license is available in this repository at the top of - // poi.cginc. B. + // poi.cginc. C. Properties { [HideInInspector] shader_master_label("<color=#de719bff>2ner</color>", Float) = 0 @@ -845,6 +845,12 @@ Shader "yum_food/2ner" _Raymarched_Fog_Turbulence("Turbulence", Float) = 1 _Raymarched_Fog_Step_Size("Step size", Float) = 0.8 _Raymarched_Fog_Step_Growth("Step growth", Float) = 1.25 + //ifex _Raymarched_Fog_Density_Exponent_Enabled==0 + [HideInInspector] m_start_Raymarched_Fog_Density_Exponent("Density exponent", Float) = 0 + [ThryToggle(_RAYMARCHED_FOG_DENSITY_EXPONENT)] _Raymarched_Fog_Density_Exponent_Enabled("Enable", Float) = 0 + _Raymarched_Fog_Density_Exponent("Exponent", Float) = 1 + [HideInInspector] m_end_Raymarched_Fog_Density_Exponent("Density exponent", Float) = 0 + //endex //ifex _Raymarched_Fog_Emitter_Texture_Enabled==0 [HideInInspector] m_start_Raymarched_Fog_Emitter_Texture("Emitter texture", Float) = 0 [ThryToggle(_RAYMARCHED_FOG_EMITTER_TEXTURE)] _Raymarched_Fog_Emitter_Texture_Enabled("Enable", Float) = 0 @@ -854,6 +860,19 @@ Shader "yum_food/2ner" _Raymarched_Fog_Emitter_Texture_World_Tangent("World tangent", Vector) = (0, 0, 0, 0) _Raymarched_Fog_Emitter_Texture_World_Scale("World scale", Vector) = (1, 1, 0, 0) _Raymarched_Fog_Emitter_Texture_World_Scale_Rcp("World scale reciprocal", Vector) = (1, 1, 0, 0) + _Raymarched_Fog_Emitter_Texture_Luminance("Luminance (W·sr⁻¹·m⁻²)", Float) = 100 + _Raymarched_Fog_Emitter_Texture_Intensity("Intensity", Float) = 2000 + + //ifex _Raymarched_Fog_Emitter_Texture_Warping_Enabled==0 + [HideInInspector] m_start_Raymarched_Fog_Emitter_Texture_Warping("Emitter texture warping", Float) = 0 + [ThryToggle(_RAYMARCHED_FOG_EMITTER_TEXTURE_WARPING)] _Raymarched_Fog_Emitter_Texture_Warping_Enabled("Enable", Float) = 0 + _Raymarched_Fog_Emitter_Texture_Warping_Octaves("Octaves", Float) = 1 + _Raymarched_Fog_Emitter_Texture_Warping_Strength("Strength", Float) = 0.1 + _Raymarched_Fog_Emitter_Texture_Warping_Scale("Scale", Float) = 0.1 + _Raymarched_Fog_Emitter_Texture_Warping_Speed("Speed", Float) = 1.0 + [HideInInspector] m_end_Raymarched_Fog_Emitter_Texture_Warping("Emitter texture warping", Float) = 0 + //endex + [HideInInspector] m_end_Raymarched_Fog_Emitter_Texture("Emitter texture", Float) = 0 //endex @@ -1836,7 +1855,7 @@ Shader "yum_food/2ner" //endex //ifex _Cast_Shadows_Enabled==0 [HideInInspector] m_start_Shadow_Casting("Cast shadows", Float) = 0 - [ThryToggle(_)] _Cast_Shadows_Enabled("Enable", Float) = 1 + [ThryToggle(_CAST_SHADOWS)] _Cast_Shadows_Enabled("Enable", Float) = 1 [HideInInspector] m_end_Shadow_Casting("Cast shadows", Float) = 0 //endex //ifex _Wrapped_Lighting_Enabled==0 @@ -2390,59 +2409,21 @@ Shader "yum_food/2ner" ENDCG } //endex + //ifex _Cast_Shadows_Enabled==0 Pass { - Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" } - ZWrite [_ZWrite] - ZTest [_ZTest] - - //ifex _Stencil_Enabled==0 - Stencil - { - Ref [_StencilRef] - ReadMask [_StencilReadMask] - WriteMask [_StencilWriteMask] - //ifex _StencilType==1 - Comp [_StencilCompareFunction] - Pass [_StencilPassOp] - Fail [_StencilFailOp] - ZFail [_StencilZFailOp] - //endex - - //ifex _StencilType==0 - CompBack [_StencilBackCompareFunction] - PassBack [_StencilBackPassOp] - FailBack [_StencilBackFailOp] - ZFailBack [_StencilBackZFailOp] - - CompFront [_StencilFrontCompareFunction] - PassFront [_StencilFrontPassOp] - FailFront [_StencilFrontFailOp] - ZFailFront [_StencilFrontZFailOp] - //endex - } - //endex - CGPROGRAM - #pragma target 5.0 - #pragma multi_compile_shadowcaster #pragma multi_compile_instancing + #pragma multi_compile_shadowcaster #pragma multi_compile_fog - #pragma vertex vert - #pragma fragment frag - - //ifex _Tessellation_Enabled==0 - #pragma hull hull - #pragma domain domain - //endex - - #define SHADOW_CASTER_PASS - - #include "2ner.cginc" + #pragma vertex vert + #pragma fragment frag + #include "mochie_shadow_caster.cginc" ENDCG } + //endex Pass { Name "META" Tags { "LightMode" = "Meta" } diff --git a/custom30.cginc b/custom30.cginc index be6b40b..938bd43 100644 --- a/custom30.cginc +++ b/custom30.cginc @@ -166,7 +166,7 @@ float3 BasicCube_normal(float3 p) { return normalize(n); } -Custom30Output BasicCube(v2f i, float4x4 tangentToWorld) { +Custom30Output BasicCube(v2f i) { float3 obj_space_camera_pos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)); float3 frag_to_origin = GetFragToOrigin(i); @@ -246,7 +246,7 @@ float3 BasicWedge_normal(float3 p) { return normalize(n); } -Custom30Output BasicWedge(v2f i, float4x4 tangentToWorld) { +Custom30Output BasicWedge(v2f i) { float3 obj_space_camera_pos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)); float3 frag_to_origin = GetFragToOrigin(i); @@ -347,7 +347,7 @@ float3 BasicPlatform_normal(float3 p) { return normalize(n); } -Custom30Output BasicPlatform(v2f i, float4x4 tangentToWorld) { +Custom30Output BasicPlatform(v2f i) { float3 obj_space_camera_pos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)); float3 frag_to_origin = GetFragToOrigin(i); diff --git a/features.cginc b/features.cginc index 734472c..324582d 100644 --- a/features.cginc +++ b/features.cginc @@ -10,6 +10,10 @@ #pragma shader_feature_local _RECEIVE_SHADOWS //endex +//ifex _Cast_Shadows_Enabled==0 +#pragma shader_feature_local _CAST_SHADOWS +//endex + //ifex _Alpha_Multiplier_Enabled==0 #pragma shader_feature_local _ALPHA_MULTIPLIER //endex @@ -325,6 +329,14 @@ #pragma shader_feature_local _RAYMARCHED_FOG_EMITTER_TEXTURE //endex +//ifex _Raymarched_Fog_Emitter_Texture_Warping_Enabled==0 +#pragma shader_feature_local _RAYMARCHED_FOG_EMITTER_TEXTURE_WARPING +//endex + +//ifex _Raymarched_Fog_Density_Exponent_Enabled==0 +#pragma shader_feature_local _RAYMARCHED_FOG_DENSITY_EXPONENT +//endex + //ifex _Unlit_Enabled==0 #pragma shader_feature_local _UNLIT //endex @@ -38,6 +38,17 @@ struct FogParams { float3 emitter_normal_x_tangent; float2 emitter_scale; // [tangent scale in meters, bitangent scale in meters] float2 emitter_scale_rcp; + float emitter_luminance; + float emitter_intensity; +#endif +#if defined(_RAYMARCHED_FOG_EMITTER_TEXTURE_WARPING) + float emitter_texture_warping_octaves; + float emitter_texture_warping_strength; + float emitter_texture_warping_scale; + float emitter_texture_warping_speed; +#endif +#if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT) + float density_exponent; #endif }; @@ -56,14 +67,22 @@ float3 getEmitterData(FogParams p, float3 pp) uv *= 0.5; uv += 0.5; + #if defined(_RAYMARCHED_FOG_EMITTER_TEXTURE_WARPING) + for (uint ii = 0; ii < p.emitter_texture_warping_octaves; ++ii) { + uv += p.dithering_noise.SampleLevel(bilinear_repeat_s, + uv * p.emitter_texture_warping_scale + _Time[0] * p.emitter_texture_warping_speed, 0).rgb + * p.emitter_texture_warping_strength; + } + #endif + bool in_range = uv.x < 1 && uv.y < 1 && uv.x > 0 && uv.y > 0; [branch] if (!in_range) { - return p.color; + return 0; } float4 c = p.emitter_texture.SampleLevel(linear_clamp_s, uv, 0); - return lerp(p.color, c.rgb, c.a); + return lerp(0, c.rgb, c.a); } #endif @@ -82,6 +101,15 @@ struct FogResult { float depth; }; +float3 aces_filmic(float3 x) { + float a = 2.51f; + float b = 0.03f; + float c = 2.43f; + float d = 0.59f; + float e = 0.14f; + return saturate((x*(a*x+b))/(x*(c*x+d)+e)); +} + FogResult raymarched_fog(v2f i, FogParams p) { float3 ro = _WorldSpaceCameraPos; @@ -156,12 +184,15 @@ FogResult raymarched_fog(v2f i, FogParams p) float fbm_f = 2.0f; float fbm_a = 0.5f; noise_sample += p.density_noise.SampleLevel(bilinear_repeat_s, noise_coord * fbm_f, 0).r * fbm_a; - fbm_f *= 2.0f; - fbm_a *= 0.5f; - noise_sample += p.density_noise.SampleLevel(bilinear_repeat_s, noise_coord * fbm_f, 0).r * fbm_a; - noise_sample *= 0.55f; + noise_sample *= 0.66666666f; - noise_sample *= noise_sample; + #if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT) + // The expected value (EV) of `noise_sample` is 0.5. If we set it to 1.0f + // then exponentiate, the EV will remain closer to 0.5f. + noise_sample += 0.5f; + noise_sample = pow(noise_sample, p.density_exponent); + noise_sample -= 0.5f; + #endif float noise_factor = lerp(turb_lo, turb_hi, noise_sample); @@ -175,18 +206,13 @@ FogResult raymarched_fog(v2f i, FogParams p) // --- Incoming radiance ------------------------------------------------ float3 L_in; -#if defined(_RAYMARCHED_FOG_EMITTER_TEXTURE) - L_in = getEmitterData(p, pp); -#else -#if 1 + + // No need for directional SH coefficients. Skipping them saves 2 3D texture reads. float3 l00 = LightVolumeSH_L0(pp); float3 l01r = 0; float3 l01g = 0; float3 l01b = 0; -#else - float3 l00, l01r, l01g, l01b; - LightVolumeSH(pp, l00, l01r, l01g, l01b); -#endif + float3 indirect = LightVolumeEvaluate(float3(0, 1, 0), l00, l01r, l01g, l01b); // Direct from the dominant realtime light @@ -197,6 +223,29 @@ FogResult raymarched_fog(v2f i, FogParams p) L_in = (direct * p.direct_light_intensity + indirect * p.indirect_light_intensity) * p.color; + +#if defined(_RAYMARCHED_FOG_EMITTER_TEXTURE) + // 1. emitted radiance of the pixel ------------------------------------ + float3 Le = getEmitterData(p, pp) * p.emitter_luminance; // [W·sr⁻¹·m⁻²] + + // 2. direction and phase term ----------------------------------------- + float3 w_e = normalize(p.emitter_world_pos - pp); // to pixel centre + float phase_e = PhaseHG(dot(w_e, rd), p.g); // same HG phase + + // 3. pixel's apparent solid angle (flat-quadrilateral approx) --------- + float dist2 = dot(p.emitter_world_pos - pp, + p.emitter_world_pos - pp); + float pixel_area = + 4.0f * p.emitter_scale.x * p.emitter_scale.y * + p.emitter_texture_texelsize.x * p.emitter_texture_texelsize.y; + float solid_ang = pixel_area / dist2; // Δω ≈ A / r² + + // 4. additive in-scattered radiance from the display ------------------ + float3 L_em = Le * solid_ang * phase_e * p.emitter_intensity; + + // Use baked luminance as a cheap proxy for shadowing from terrain. + float indirect_brightness = luminance(indirect); + L_in += L_em * indirect_brightness * indirect_brightness; #endif // --- Accumulate radiance --------------------------------------------- @@ -224,6 +273,7 @@ FogResult raymarched_fog(v2f i, FogParams p) FogResult r; r.color = color; + //r.color.rgb = saturate(log(linearZ) / 5.0); //r.color.rgb = float3(screen_uv, 0); //r.color.a = d; diff --git a/globals.cginc b/globals.cginc index 5ff5286..96fd1e0 100644 --- a/globals.cginc +++ b/globals.cginc @@ -580,6 +580,17 @@ float3 _Raymarched_Fog_Emitter_Texture_World_Pos; float3 _Raymarched_Fog_Emitter_Texture_World_Normal;
float3 _Raymarched_Fog_Emitter_Texture_World_Tangent;
float2 _Raymarched_Fog_Emitter_Texture_World_Scale;
+float _Raymarched_Fog_Emitter_Texture_Luminance;
+float _Raymarched_Fog_Emitter_Texture_Intensity;
+#endif
+#if defined(_RAYMARCHED_FOG_EMITTER_TEXTURE_WARPING)
+float _Raymarched_Fog_Emitter_Texture_Warping_Octaves;
+float _Raymarched_Fog_Emitter_Texture_Warping_Strength;
+float _Raymarched_Fog_Emitter_Texture_Warping_Scale;
+float _Raymarched_Fog_Emitter_Texture_Warping_Speed;
+#endif
+#if defined(_RAYMARCHED_FOG_DENSITY_EXPONENT)
+float _Raymarched_Fog_Density_Exponent;
#endif
#endif // _RAYMARCHED_FOG
diff --git a/tessellation.cginc b/tessellation.cginc index e5302d3..b088334 100644 --- a/tessellation.cginc +++ b/tessellation.cginc @@ -152,10 +152,12 @@ v2f domain( o.eyeVec.xyz = o.worldPos - _WorldSpaceCameraPos; o.eyeVec.w = 1; - // TODO what about UNITY_LIGHTING_COORDS(7,8) and instance id and shit? - UNITY_TRANSFER_LIGHTING(o, DOMAIN_INTERP(_unity_lightcoords)); + UNITY_TRANSFER_LIGHTING(o, o.uv01.zw); UNITY_TRANSFER_INSTANCE_ID(patch[0], o); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); +#if defined(SHADOW_CASTER_PASS) + TRANSFER_SHADOW_CASTER_NORMALOFFSET(o); +#endif return o; } |
