Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deferred rendering on main shaders #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 112 additions & 48 deletions CustomShaders/Surface/TU-Include-Lighting.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -131,16 +162,49 @@
#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;
#endif

return c;
}
#endif

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
9 changes: 8 additions & 1 deletion CustomShaders/Surface/TU-Legacy.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions CustomShaders/Surface/TU-Metallic.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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....
Expand Down
11 changes: 9 additions & 2 deletions CustomShaders/Surface/TU-Specular.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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....
Expand Down
Binary file modified GameData/000_TexturesUnlimited/Shaders/tushaders-universal.ssf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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: []