diff --git a/CustomShaders/Surface/TU-Include-Lighting.cginc b/CustomShaders/Surface/TU-Include-Lighting.cginc index 2ca6517..f490021 100644 --- a/CustomShaders/Surface/TU-Include-Lighting.cginc +++ b/CustomShaders/Surface/TU-Include-Lighting.cginc @@ -5,7 +5,7 @@ inline void LightingTU_GI (SurfaceOutputTU s, UnityGIInput data, inout UnityGI gi) { UNITY_GI(gi, s, data); - } + } //custom lighting function to enable SubSurf functionality inline half4 LightingTU(SurfaceOutputTU s, half3 viewDir, UnityGI gi) @@ -21,7 +21,7 @@ half4 c = UNITY_BRDF_PBS (s.Albedo, specSampleColor, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect); c.rgb += UNITY_BRDF_GI (s.Albedo, specSampleColor, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, s.Occlusion, gi); c.a = outputAlpha; - + //subsurface scattering contribution #if TU_SUBSURF c.rgb += subsurf(_SubSurfScale, _SubSurfPower, _SubSurfDistort, _SubSurfAtten, s.Backlight.a, s.Albedo, s.Backlight.rgb, s.Normal, viewDir, gi.light.color, gi.light.dir); @@ -32,6 +32,36 @@ return c; } + + + inline half4 LightingTU_Deferred (SurfaceOutputTU s, float3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2) + { + half oneMinusReflectivity; + half3 specSampleColor; + s.Albedo = DiffuseAndSpecularFromMetallic (s.Albedo, s.Metallic, /*out*/ specSampleColor, /*out*/ oneMinusReflectivity); + + UnityStandardData data; + data.diffuseColor = s.Albedo; + data.occlusion = s.Occlusion; + data.specularColor = specSampleColor; + data.smoothness = s.Smoothness; + data.normalWorld = s.Normal; + + UnityStandardDataToGbuffer(data, outGBuffer0, outGBuffer1, outGBuffer2); + + half4 emission = half4(s.Emission, 1); + + // _LightColor0 and _WorldSpaceLightPos0 are legacy properties of the main light available in forward, and correctly set in deferred rendering by the Deferred mod + // This means subsurface scattering will only work for the main light for now. In the future move subsurface scattering to a second forward-only material that gets added + // to the GameObject so that all lights will work with it + #if TU_SUBSURF + emission.rgb += subsurf(_SubSurfScale, _SubSurfPower, _SubSurfDistort, _SubSurfAtten, s.Backlight.a, s.Albedo, s.Backlight.rgb, s.Normal, viewDir, _LightColor0, _WorldSpaceLightPos0.rgb); + #endif + + return emission; + } + + #endif #if TU_LIGHT_SPEC @@ -46,23 +76,6 @@ { s.Normal = normalize(s.Normal); - #if TU_SUBSURF - //SSS implementation from: https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/ - - half fLTScale = _SubSurfScale;//main output scalar - half iLTPower = _SubSurfPower;//exponent used in power - half fLTDistortion = _SubSurfDistort;;//how much the surface normal distorts the outgoing light - half fLightAttenuation = _SubSurfAtten;//how much light attenuates while traveling through the surface (gets multiplied by distance) - - half fLTAmbient = s.Backlight.a;//ambient from texture/material - half3 fLTThickness = s.Backlight.rgb;//sampled from texture - - float3 H = normalize(gi.light.dir + s.Normal * fLTDistortion); - float vdh = pow(saturate(dot(viewDir, -H)), iLTPower) * fLTScale; - float3 I = fLightAttenuation * (vdh + fLTAmbient) * fLTThickness; - half3 backColor = I * gi.light.color; - #endif - //Unity 'Standard' lighting function, unabridged // energy conservation half oneMinusReflectivity; @@ -84,44 +97,62 @@ return c; } + + + inline half4 LightingTU_Deferred (SurfaceOutputTU s, float3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2) + { + // energy conservation + half oneMinusReflectivity; + s.Albedo = EnergyConservationBetweenDiffuseAndSpecular (s.Albedo, s.SpecularColor, /*out*/ oneMinusReflectivity); + + UnityStandardData data; + data.diffuseColor = s.Albedo; + data.occlusion = s.Occlusion; + data.specularColor = s.SpecularColor; + data.smoothness = s.Smoothness; + data.normalWorld = s.Normal; + + UnityStandardDataToGbuffer(data, outGBuffer0, outGBuffer1, outGBuffer2); + + half4 emission = half4(s.Emission, 1); + + // _LightColor0 and _WorldSpaceLightPos0 are legacy properties of the main light available in forward, and correctly set in deferred rendering by the Deferred mod + // This means subsurface scattering will only work for the main light for now. In the future move subsurface scattering to a second forward-only material that gets added + // to the GameObject so that all lights will work with it + #if TU_SUBSURF + emission.rgb += subsurf(_SubSurfScale, _SubSurfPower, _SubSurfDistort, _SubSurfAtten, s.Backlight.a, s.Albedo, s.Backlight.rgb, s.Normal, viewDir, _LightColor0, _WorldSpaceLightPos0.rgb); + #endif + + return emission; + } + + #endif #if TU_LIGHT_SPECLEGACY - inline half4 LightingTU(SurfaceOutputTU s, half3 lightDir, half3 viewDir, half atten) + //replacement for Unity bridge method to call GI with custom structs + inline void LightingTU_GI ( + SurfaceOutputTU s, + UnityGIInput data, + inout UnityGI gi) + { + gi = UnityGlobalIllumination (data, 1.0, s.Normal); + } + + inline half4 LightingTU(SurfaceOutputTU s, half3 viewDir, UnityGI gi) { + UnityLight light = gi.light; + #if TU_BUMPMAP s.Normal = normalize(s.Normal); #endif - - #if TU_SUBSURF - //SSS implementation from: https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/ - - half fLTScale = _SubSurfScale;//main output scalar - half iLTPower = _SubSurfPower;//exponent used in power - half fLTDistortion = _SubSurfDistort;;//how much the surface normal distorts the outgoing light - half fLightAttenuation = _SubSurfAtten;//how much light attenuates while traveling through the surface (gets multiplied by distance) - - //half fLTScale = s.SubSurfParams.r;//main output scalar - //half iLTPower = s.SubSurfParams.g;//exponent used in power - //half fLTDistortion = s.SubSurfParams.b;//how much the surface normal distorts the outgoing light - //half fLightAttenuation = s.SubSurfParams.a;//how much light attenuates while traveling through the surface (gets multiplied by distance) - - half fLTAmbient = s.Backlight.a;//ambient from texture/material - half3 fLTThickness = s.Backlight.rgb;//sampled from texture - - float3 H = normalize(lightDir + s.Normal * fLTDistortion); - float vdh = pow(saturate(dot(viewDir, -H)), iLTPower) * fLTScale; - float3 I = fLightAttenuation * (vdh + fLTAmbient) * fLTThickness; - half3 backColor = I * _LightColor0.rgb; - #endif - s.Smoothness = max(0.01, s.Smoothness); //standard blinn-phong lighting model //diffuse light intensity, from surface normal and light direction - half diff = max (0, dot (s.Normal, lightDir)); + half diff = max (0, dot (s.Normal, light.dir)); //specular light calculations - half3 h = normalize (lightDir + viewDir); + half3 h = normalize (light.dir + viewDir); float nh = max (0, dot (s.Normal, h)); float spec = pow (nh, s.Smoothness * 128); half3 specCol = spec * s.SpecularColor; @@ -131,11 +162,12 @@ #if TU_ICON //diff *= _Multiplier; #endif - c.rgb = ((s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol)) * atten; + c.rgb = ((s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol)); + c.rgb += s.Albedo * gi.indirect.diffuse; c.a = s.Alpha; #if TU_SUBSURF - c.rgb += subsurf(_SubSurfScale, _SubSurfPower, _SubSurfDistort, _SubSurfAtten, s.Backlight.a, s.Albedo, s.Backlight.rgb, s.Normal, viewDir, _LightColor0.rgb, lightDir); + c.rgb += subsurf(_SubSurfScale, _SubSurfPower, _SubSurfDistort, _SubSurfAtten, s.Backlight.a, s.Albedo, s.Backlight.rgb, s.Normal, viewDir, _LightColor0.rgb, light.dir); #endif #if TU_ICON //c.rgb *= _Multiplier.rrr; @@ -143,4 +175,36 @@ return c; } -#endif \ No newline at end of file + + inline half4 LightingTU_Deferred (SurfaceOutputTU s, half3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2) + { + #if TU_BUMPMAP + s.Normal = normalize(s.Normal); + #endif + + s.Smoothness = max(0.01, s.Smoothness); + + UnityStandardData data; + data.diffuseColor = s.Albedo; + data.occlusion = 1; + // PI factor come from StandardBDRF (UnityStandardBRDF.cginc:351 for explanation) + data.specularColor = _SpecColor.rgb * (1/UNITY_PI); + data.smoothness = s.SpecularColor; + data.normalWorld = s.Normal; + + UnityStandardDataToGbuffer(data, outGBuffer0, outGBuffer1, outGBuffer2); + + half4 emission = half4(s.Emission, 1); + + #ifdef UNITY_LIGHT_FUNCTION_APPLY_INDIRECT + emission.rgb += s.Albedo * gi.indirect.diffuse; + #endif + + #if TU_SUBSURF + emission.rgb += subsurf(_SubSurfScale, _SubSurfPower, _SubSurfDistort, _SubSurfAtten, s.Backlight.a, s.Albedo, s.Backlight.rgb, s.Normal, viewDir, gi.light.color, gi.light.dir); + #endif + + return emission; + } + +#endif diff --git a/CustomShaders/Surface/TU-Legacy.shader b/CustomShaders/Surface/TU-Legacy.shader index a3315a0..983cd14 100644 --- a/CustomShaders/Surface/TU-Legacy.shader +++ b/CustomShaders/Surface/TU-Legacy.shader @@ -57,9 +57,16 @@ Shader "TU/Legacy" SubShader { Tags {"RenderType"="Opaque"} + + Stencil + { + Ref 1 + Comp Always + Pass Replace + } + ZWrite On ZTest LEqual - Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM diff --git a/CustomShaders/Surface/TU-Metallic.shader b/CustomShaders/Surface/TU-Metallic.shader index 5d6a1ac..778978a 100644 --- a/CustomShaders/Surface/TU-Metallic.shader +++ b/CustomShaders/Surface/TU-Metallic.shader @@ -59,12 +59,19 @@ Shader "TU/Metallic" Tags {"RenderType"="Opaque"} ZWrite On ZTest LEqual - Blend SrcAlpha OneMinusSrcAlpha + //Blend SrcAlpha OneMinusSrcAlpha + + Stencil + { + Ref 1 + Comp Always + Pass Replace + } CGPROGRAM //directives for 'surface shader' 'surface name = 'TU'' and 'don't discard alpha values' - #pragma surface surf TU keepalpha + #pragma surface surf TU //keepalpha #pragma target 3.0 //#pragma skip_variants POINT POINT_COOKIE DIRECTIONAL_COOKIE //need to find out what variants are -actually- used... //#pragma multi_compile_fwdadd_fullshadows //stalls out Unity Editor while compiling shader.... diff --git a/CustomShaders/Surface/TU-Specular.shader b/CustomShaders/Surface/TU-Specular.shader index 10e5284..12fd3c7 100644 --- a/CustomShaders/Surface/TU-Specular.shader +++ b/CustomShaders/Surface/TU-Specular.shader @@ -57,12 +57,19 @@ Shader "TU/Specular" Tags {"RenderType"="Opaque"} ZWrite On ZTest LEqual - Blend SrcAlpha OneMinusSrcAlpha + //Blend SrcAlpha OneMinusSrcAlpha + + Stencil + { + Ref 1 + Comp Always + Pass Replace + } CGPROGRAM //directives for 'surface shader' 'surface name = 'TU'' and 'don't discard alpha values' - #pragma surface surf TU keepalpha + #pragma surface surf TU //keepalpha #pragma target 3.0 //#pragma skip_variants POINT POINT_COOKIE DIRECTIONAL_COOKIE //need to find out what variants are -actually- used... //#pragma multi_compile_fwdadd_fullshadows //stalls out Unity Editor while compiling shader.... diff --git a/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf b/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf index 1446c9b..f8e530f 100644 Binary files a/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf and b/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf differ diff --git a/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf.manifest b/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf.manifest index 16058ee..52e8ba6 100644 --- a/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf.manifest +++ b/GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf.manifest @@ -1,24 +1,18 @@ ManifestFileVersion: 0 -CRC: 578736539 +CRC: 3015458744 Hashes: AssetFileHash: serializedVersion: 2 - Hash: e6c7da60690d29fa7dfe5ce70393e33a + Hash: 549b0f11c429cc051dc54e9195344764 TypeTreeHash: serializedVersion: 2 - Hash: 6872b87c561f08cfccb851e91fe48bfa + Hash: 677a1d6a6e8c5d2da1d9d0fadde392d8 HashAppended: 0 ClassTypes: - Class: 48 Script: {instanceID: 0} -- Class: 109 - Script: {instanceID: 0} Assets: -- Assets/Shaders/Surface/TU-Include-Surfaces.cginc -- Assets/Shaders/Surface/TU-Metallic.shader -- Assets/Shaders/Surface/TU-Specular.shader -- Assets/Shaders/Surface/TU-Include-Functions.cginc -- Assets/Shaders/Surface/TU-Include-Lighting.cginc -- Assets/Shaders/Surface/TU-Include-Structs.cginc -- Assets/Shaders/Surface/TU-Legacy.shader +- Assets/CustomShaders/Surface/TU-Metallic.shader +- Assets/CustomShaders/Surface/TU-Specular.shader +- Assets/CustomShaders/Surface/TU-Legacy.shader Dependencies: []