diff options
| author | yum <yum.food.vr@gmail.com> | 2024-11-06 01:44:35 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2024-11-06 01:44:35 -0800 |
| commit | 4f7592f7b4ba6195db1b968adf06d9e2c2008018 (patch) | |
| tree | 22c0d4a7d31d7b94e7b37a89d4e42fcc982f70db | |
| parent | 23ed2a68de0d1d9c4538e71b05b87b791d30459f (diff) | |
Add baked normals & ltcgi to fog
| -rw-r--r-- | Editor/tooner.cs | 5 | ||||
| -rw-r--r-- | feature_macros.cginc | 1 | ||||
| -rw-r--r-- | fog.cginc | 57 | ||||
| -rw-r--r-- | fog_ltcgi.cginc | 30 | ||||
| -rw-r--r-- | globals.cginc | 4 | ||||
| -rw-r--r-- | ltcgi.cginc | 28 | ||||
| -rw-r--r-- | pbr.cginc | 27 | ||||
| -rw-r--r-- | tooner.shader | 2 |
8 files changed, 108 insertions, 46 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs index 6747bcb..6f37937 100644 --- a/Editor/tooner.cs +++ b/Editor/tooner.cs @@ -2091,6 +2091,11 @@ public class ToonerGUI : ShaderGUI { TexturePropertySingleLine( MakeLabel(bc, "3D Noise"), bc); + bc = FindProperty("_Gimmick_Fog_00_Noise_Normals"); + TexturePropertySingleLine( + MakeLabel(bc, "3D Noise (RGB normals)"), + bc); + SetKeyword("_GIMMICK_FOG_00_NOISE_3D_NORMALS", bc.textureValue); bc = FindProperty("_Gimmick_Fog_00_Noise_2D"); TexturePropertySingleLine( MakeLabel(bc, "2D Noise (optional)"), diff --git a/feature_macros.cginc b/feature_macros.cginc index 3367e7a..0a5b083 100644 --- a/feature_macros.cginc +++ b/feature_macros.cginc @@ -215,6 +215,7 @@ #pragma shader_feature_local _ SSR_MASK #pragma shader_feature_local _ _GIMMICK_FOG_00 #pragma shader_feature_local _ _GIMMICK_FOG_00_NOISE_2D +#pragma shader_feature_local _ _GIMMICK_FOG_00_NOISE_3D_NORMALS #pragma shader_feature_local _ _GIMMICK_FOG_00_EMITTER_TEXTURE #pragma shader_feature_local _ _GIMMICK_FOG_00_EMITTER_VARIABLE_DENSITY #pragma shader_feature_local _ _GIMMICK_FOG_00_EMITTER_1 @@ -2,6 +2,7 @@ #include "atrix256.cginc" #include "cnlohr.cginc" +#include "fog_ltcgi.cginc" #include "globals.cginc" #include "interpolators.cginc" #include "math.cginc" @@ -17,7 +18,6 @@ struct Fog00PBR { float4 albedo; - float3 diffuse; float depth; }; @@ -34,8 +34,13 @@ struct Fog00PBR { float perlin_noise_3d_tex(float3 p) { // 1/256 = 0.00390625 - float r_lo = _Gimmick_Fog_00_Noise.SampleLevel(trilinear_repeat_s, p.xyz * 0.00390625, 0); - return r_lo; + return _Gimmick_Fog_00_Noise.SampleLevel(trilinear_repeat_s, p.xyz * 0.00390625, 0); +} + +float3 perlin_noise_3d_tex_normal(float3 p) +{ + // 1/256 = 0.00390625 + return _Gimmick_Fog_00_Noise_Normals.SampleLevel(trilinear_repeat_s, p.xyz * 0.00390625, 0) * 2 - 1; } float3 light_fog00( @@ -52,7 +57,7 @@ float3 light_fog00( return diffCol; } -float map(float3 p) { +float map(float3 p, out float3 normal) { float3 t = _Time[1] * FOG_PERLIN_NOISE_SCALE * .2; #define RADIUS_TRANS_WIDTH 800 #define RADIUS_TRANS_WIDTH_RCP (1.0 / RADIUS_TRANS_WIDTH) @@ -62,16 +67,15 @@ float map(float3 p) { float3 pp = p * _Gimmick_Fog_00_Noise_Scale * FOG_PERLIN_NOISE_SCALE; float density = FOG_PERLIN_NOISE(pp+t) * radius2 * 0.7; + normal = perlin_noise_3d_tex_normal(pp+t) * density; // Add higher octave to create more visual interest #if 1 - density += FOG_PERLIN_NOISE(pp*3+t*1.5) * radius2 * 0.3; + float cur_density = FOG_PERLIN_NOISE(pp*3+t*1.5) * radius2 * 0.3; + density += cur_density; + normal += perlin_noise_3d_tex_normal(pp*3+t*1.5) * cur_density; #endif - // Exponentiate to increase contrast. - density *= density; - // Scale to make expected value remain constant. - density *= 2; - + normal = normalize(normal); return saturate(density); } @@ -110,9 +114,8 @@ float3 getEmitterData(float3 p, float emitter_lod = floor(abs(t) / (_Gimmick_Fog_00_Emitter_Lod_Half_Life * step_size)); float3 em_color = _Gimmick_Fog_00_Emitter_Texture.SampleLevel(linear_repeat_s, emitter_uv, emitter_lod); - em_color *= _Gimmick_Fog_00_Emitter_Brightness; float emitter_dist = in_range ? abs(t) : 1000; - float emitter_falloff = min(1, rcp(pow(emitter_dist, 1.4))); + float emitter_falloff = min(1, rcp(emitter_dist)); return in_range * emitter_falloff * em_color; } #endif // defined(_GIMMICK_FOG_00_EMITTER_TEXTURE) @@ -223,7 +226,9 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) { float density_ss_term = 1 / _Gimmick_Fog_00_Density; //density_ss_term = dclamp(density_ss_term, 0.33, 3.00, 5); - float step_size = _Gimmick_Fog_00_Step_Size_Factor * density_ss_term; + const float step_size = _Gimmick_Fog_00_Step_Size_Factor * density_ss_term; + const float step_size_sqrt = sqrt(step_size); + const float step_size_sqrt_max1 = max(1, step_size_sqrt); //step_size = clamp(step_size, 1E-2, 1E2); uint2 screen_uv_round = floor(screen_uv * _ScreenParams.xy); #if defined(_GIMMICK_FOG_00_NOISE_2D) @@ -274,7 +279,8 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) { float4 c; float3 c_lit = 0; #if 1 - const float map_p_raw = map(p); + float3 map_normal; + const float map_p_raw = map(p, map_normal); const float map_p = map_p_raw * _Gimmick_Fog_00_Density * step_size; c = float4(_Color.rgb, map_p); float3 diffuse_light = 0; @@ -282,7 +288,9 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) { // We put the emitter color into diffuse instead of doing a directional // calculation because it looks better and it's cheaper. Less accurate // though! - diffuse_light += getEmitterData(p, step_size, em_loc, em_normal, em_scale, em_scale_rcp); + if (_Gimmick_Fog_00_Enable_Area_Lighting) { + diffuse_light += getEmitterData(p, step_size, em_loc, em_normal, em_scale, em_scale_rcp); + } #endif #if defined(_GIMMICK_FOG_00_RAY_MARCH_0) { @@ -314,12 +322,21 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) { // Directional derivative epsilon. // TODO this should scale based on distance float dd_e = 1 * noise_scale_rcp; - float NoL = saturate((map(p + dd_e * direct_light.dir) - map_p_raw) / dd_e); + float NoL = dot(map_normal, direct_light.dir); +#if 1 + if (_Gimmick_Fog_00_Enable_Area_Lighting) { + ltcgi_acc acc = (ltcgi_acc) 0; + LTCGI_Contribution(acc, p, map_normal, rd, /*roughness=*/0.5, 0); + diffuse_light += acc.diffuse; + } +#endif + diffuse_light *= _Gimmick_Fog_00_Emitter_Brightness; + // Scaling brightness by sqrt(step_size) seems to look more consistent? c_lit += light_fog00( c.rgb, NoL, - direct_light.color * step_size, - (indirect_light.diffuse + diffuse_light) * step_size); + direct_light.color * step_size_sqrt_max1, + (indirect_light.diffuse + diffuse_light) * step_size_sqrt_max1); #else c_lit = .05 * step_size; c.a = 0.1; @@ -353,9 +370,7 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) { } Fog00PBR pbr; - pbr.albedo.rgb = acc.rgb; - pbr.albedo.a = saturate(acc.a); - pbr.diffuse = acc.rgb; + pbr.albedo = saturate(acc); // Add some dithering to lit color to break up banding pbr.albedo.rgb += ign(tdata.screen_uv_round) * .00390625; diff --git a/fog_ltcgi.cginc b/fog_ltcgi.cginc new file mode 100644 index 0000000..5e64442 --- /dev/null +++ b/fog_ltcgi.cginc @@ -0,0 +1,30 @@ +#ifndef __FOG_LTCGI_INC +#define __FOG_LTCGI_INC + +#if defined(_GIMMICK_FOG_00) + +#define LTCGI_SPECULAR_OFF +#define LTCGI_ALWAYS_LTC_DIFFUSE +#define LTCGI_FAST_SAMPLING + +#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI_structs.cginc" + +#undef LTCGI_AVATAR_MODE + +struct ltcgi_acc { + float3 diffuse; +}; + +void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output); +void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output); + +#define LTCGI_V2_CUSTOM_INPUT ltcgi_acc +#define LTCGI_V2_DIFFUSE_CALLBACK ltcgi_cb_diffuse + +#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI.cginc" +void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output) { + acc.diffuse += output.intensity * output.color; +} +#endif // _GIMMICK_FOG_00 + +#endif // __FOG_LTCGI_INC diff --git a/globals.cginc b/globals.cginc index b3dd7f8..1666d4d 100644 --- a/globals.cginc +++ b/globals.cginc @@ -744,6 +744,7 @@ float _Gimmick_AL_Chroma_00_Hue_Shift_Theta; #if defined(_GIMMICK_FOG_00) float _Gimmick_Fog_00_Max_Ray; +float _Gimmick_Fog_00_Enable_Area_Lighting; float _Gimmick_Fog_00_Radius; float _Gimmick_Fog_00_Step_Size_Factor; float _Gimmick_Fog_00_Noise_Scale; @@ -754,6 +755,9 @@ float _Gimmick_Fog_00_Alpha_Cutoff; float _Gimmick_Fog_00_Ray_Origin_Randomization; float _Gimmick_Fog_00_Lod_Half_Life; texture3D _Gimmick_Fog_00_Noise; +#if defined(_GIMMICK_FOG_00_NOISE_3D_NORMALS) +texture3D _Gimmick_Fog_00_Noise_Normals; +#endif #if defined(_GIMMICK_FOG_00_NOISE_2D) texture2D _Gimmick_Fog_00_Noise_2D; float4 _Gimmick_Fog_00_Noise_2D_TexelSize; diff --git a/ltcgi.cginc b/ltcgi.cginc new file mode 100644 index 0000000..0d4a0b1 --- /dev/null +++ b/ltcgi.cginc @@ -0,0 +1,28 @@ +#ifndef __LTCGI_INC +#define __LTCGI_INC + +#if defined(_LTCGI) +#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI_structs.cginc" + +struct ltcgi_acc { + float3 diffuse; + float3 specular; +}; + +void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output); +void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output); + +#define LTCGI_V2_CUSTOM_INPUT ltcgi_acc +#define LTCGI_V2_DIFFUSE_CALLBACK ltcgi_cb_diffuse +#define LTCGI_V2_SPECULAR_CALLBACK ltcgi_cb_specular + +#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI.cginc" +void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output) { + acc.diffuse += output.intensity * output.color * _LTCGI_DiffuseColor; +} +void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output) { + acc.specular += output.intensity * output.color * _LTCGI_SpecularColor; +} +#endif // __LTCGI + +#endif // __LTCGI_INC @@ -1,5 +1,6 @@ #include "atrix256.cginc" #include "globals.cginc" +#include "ltcgi.cginc" #include "filament_math.cginc" #include "globals.cginc" #include "interpolators.cginc" @@ -9,30 +10,6 @@ #ifndef __PBR_INC #define __PBR_INC -#if defined(_LTCGI) -#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI_structs.cginc" - -struct ltcgi_acc { - float3 diffuse; - float3 specular; -}; - -void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output); -void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output); - -#define LTCGI_V2_CUSTOM_INPUT ltcgi_acc -#define LTCGI_V2_DIFFUSE_CALLBACK ltcgi_cb_diffuse -#define LTCGI_V2_SPECULAR_CALLBACK ltcgi_cb_specular - -#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI.cginc" -void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output) { - acc.diffuse += output.intensity * output.color * _LTCGI_DiffuseColor; -} -void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output) { - acc.specular += output.intensity * output.color * _LTCGI_SpecularColor; -} -#endif // __LTCGI - UNITY_DECLARE_TEXCUBE(_Cubemap); half4 _Cubemap_HDR; float _Cubemap_Limit_To_Metallic; @@ -214,7 +191,7 @@ float4 getLitColor( albedo.rgb = DiffuseAndSpecularFromMetallic( albedo, metallic, specular_tint, one_minus_reflectivity); - const float3 view_dir = normalize(i.centerCamPos - worldPos); + const float3 view_dir = normalize(_WorldSpaceCameraPos - worldPos); uint normals_mode = round(_Mesh_Normals_Mode); switch (normals_mode) { case 0: diff --git a/tooner.shader b/tooner.shader index ee4ab47..437c16a 100644 --- a/tooner.shader +++ b/tooner.shader @@ -645,7 +645,9 @@ Shader "yum_food/tooner" _Mochie_UI_Show("UI show", Float) = 0 _Gimmick_Fog_00_Enable_Static("Enable fog 00", Float) = 0 + _Gimmick_Fog_00_Enable_Area_Lighting("Enable fog 00 area lighting", Float) = 1 _Gimmick_Fog_00_Noise("3D noise", 3D) = "black" {} + _Gimmick_Fog_00_Noise_Normals("3D noise (normals)", 3D) = "white" {} _Gimmick_Fog_00_Noise_2D("2D noise", 2D) = "black" {} _Gimmick_Fog_00_Max_Ray("Max ray", Float) = 25 _Gimmick_Fog_00_Radius("Radius", Float) = 25 |
