Skip to content

Commit

Permalink
Hekky PBR 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
hyblocker committed Aug 30, 2022
1 parent 7a80f92 commit 3a2d57d
Show file tree
Hide file tree
Showing 18 changed files with 793 additions and 289 deletions.
2 changes: 1 addition & 1 deletion Hekky.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 85 additions & 9 deletions Hekky/Examples/Material/PBR.mat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Material:
m_PrefabAsset: {fileID: 0}
m_Name: PBR
m_Shader: {fileID: 4800000, guid: de0b734b74e3ba84f9b7566dbe56016a, type: 3}
m_ShaderKeywords:
m_ShaderKeywords: _BAKERY_NONE _REFLECTIONSFORCEDMODE_DEFAULT
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
Expand All @@ -35,6 +35,10 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionPathingTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
Expand Down Expand Up @@ -67,69 +71,109 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SSSMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ShadowColor1Tex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ShadowColor2Tex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SubpixelMask:
m_Texture: {fileID: 2800000, guid: 3c62ea8b5b6e6014f890230924d7c9ec, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _AlphaClip: 0.5
- _AnisoAngleOffset: 0
- _AnisoStrength: 0
- _Aspect: 1.777777
- _AudioLink: 0
- _AudioLinkDebug: 0
- _AudioLinkDebugBass: 0
- _AudioLinkDebugHighMid: 0
- _AudioLinkDebugLowMid: 0
- _AudioLinkDebugTreble: 0
- _Backlight: 0.003
- _Bakery: 0
- _BlendModes: 0
- _BumpScale: 1
- _CullMode: 2
- _DetailLighting: 0
- _DoMatcap: 0
- _DoOutline: 0
- _DoOutline: 1
- _DoSSS: 0
- _DoSubsurf: 0
- _DstBlend: 0
- _EmissionAudioLinkAddChannel: 0
- _EmissionAudioLinkChannel: 0
- _EmissionAudioLinkMultiplyChannel: 0
- _EmissionAudioLinkPathingBandA: 3
- _EmissionAudioLinkPathingBandB: 2
- _EmissionAudioLinkPathingBandG: 1
- _EmissionAudioLinkPathingBandR: 0
- _EmissionAudioLinkRange: 0
- _EmissionIntensity: 1
- _EnableNormalReproj: 0
- _ExposureOcclusion: 0.2
- _FlipBackside: 0
- _FlipInMirrors: 0
- _FoldoutAdvancedBegin: 0
- _FoldoutAdvancedEnd: 0
- _FoldoutAnisoBegin: 0
- _FoldoutAnisoEnd: 0
- _FoldoutAudioLinkDebugBegin: 0
- _FoldoutAudioLinkDebugEnd: 0
- _FoldoutDisplayBegin: 0
- _FoldoutDisplayEnd: 0
- _FoldoutEmissionAudioLinkBegin: 1
- _FoldoutEmissionAudioLinkEnd: 0
- _FoldoutEmissionBegin: 1
- _FoldoutEmissionBegin: 0
- _FoldoutEmissionEnd: 0
- _FoldoutExternalModulesBegin: 0
- _FoldoutExternalModulesEnd: 0
- _FoldoutLTCGIConfigBegin: 0
- _FoldoutLTCGIConfigEnd: 0
- _FoldoutLightingBegin: 0
- _FoldoutLightingBegin: 1
- _FoldoutLightingEnd: 0
- _FoldoutMainBegin: 1
- _FoldoutMainEnd: 0
- _FoldoutMatcapBegin: 1
- _FoldoutMatcapBegin: 0
- _FoldoutMatcapEnd: 0
- _FoldoutNormalReprojectionBegin: 0
- _FoldoutNormalReprojectionEnd: 0
- _FoldoutOcclusionBegin: 0
- _FoldoutOcclusionBegin: 1
- _FoldoutOcclusionEnd: 0
- _FoldoutOutlineBegin: 0
- _FoldoutOutlineEnd: 0
- _FoldoutReflectionsBegin: 1
- _FoldoutReflectionsBegin: 0
- _FoldoutReflectionsEnd: 0
- _FoldoutSpecularBegin: 1
- _FoldoutShadowBegin: 0
- _FoldoutShadowEnd: 0
- _FoldoutSpecularBegin: 0
- _FoldoutSpecularEnd: 0
- _FoldoutStrobingBegin: 0
- _FoldoutStrobingEnd: 0
- _FoldoutSubsurfaceBegin: 1
- _FoldoutSubsurfaceEnd: 0
- _FoldoutSubsurfaceScatteringBegin: 0
- _FoldoutSubsurfaceScatteringEnd: 0
- _FoldoutToonBegin: 0
- _FoldoutToonEnd: 0
- _Footer: 0
- _Glossiness: 1
- _Grayscale: 0
- _Header: 0
- _IndirectBlend: 1
- _IndirectIntensity: 1
- _InvertGlossiness: 0
- _LTCGI: 0
- _LightingMode: 0
Expand All @@ -146,16 +190,35 @@ Material:
- _MatcapReplaceBlend: 1
- _MathGradientEnd: 1
- _MathGradientStart: 0
- _MaxLightIntensity: 2
- _Metallic: 0
- _MiscView: 0
- _Mode: 0
- _NormalReprojBlend: 1
- _OcclusionStrength: 1
- _OutlineWidth: 0
- _OutlineWidth: 15.67
- _ReflectionsForcedMode: 0
- _SSSIntensity: 0.15
- _SSSMaxLightIntensity: 1
- _SSSPower: 0.3
- _SSSVisibility: 0
- _ShadowBlur1: 0
- _ShadowBlur2: 0
- _ShadowBorder1: 0
- _ShadowBorder2: 0
- _SnapToSubpixels: 0
- _Spacing0: 0
- _Spacing2: 0
- _Spacing3: 0
- _Specular: 0
- _SpecularMode: 0
- _SrcBlend: 1
- _StrobeInCamera: 0
- _StrobeMinLight: 0.198
- _StrobePeriod: 1.76
- _StrobeSize: 0.84
- _StrobeSpeed: 0.27
- _Subsurface: 0
- _ToonMathGradientMaxBrightness: 1
- _ToonMathGradientMinBrightness: 0
- _Version: 1
Expand All @@ -166,10 +229,23 @@ Material:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionAudioLinkAddRange: {r: 0, g: 0, b: 0, a: 0}
- _EmissionAudioLinkMultiplyRange: {r: 0, g: 1, b: 0, a: 0}
- _EmissionAudioLinkPathingRColor: {r: 1, g: 1, b: 1, a: 1}
- _EmissionAudioLinkRange: {r: 0, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _GeneralScaleOffset: {r: 1, g: 1, b: 0, a: 0}
- _IndirectColor: {r: 0.735849, g: 0.735849, b: 0.735849, a: 1}
- _LTCGI_Intensity: {r: 1, g: 1, b: 1, a: 0}
- _LTCGI_Scale: {r: 1, g: 1, b: 1, a: 0}
- _MatcapColor: {r: 1, g: 1, b: 1, a: 1}
- _OutlineColor: {r: 0, g: 0, b: 0, a: 1}
- _Overscan: {r: 0, g: 0, b: 0, a: 0}
- _SSSColor: {r: 2, g: 0, b: 0, a: 1}
- _ShadowColor1Tint: {r: 0, g: 0, b: 0, a: 1}
- _ShadowColor2Tint: {r: 0, g: 0, b: 0, a: 1}
- _SpecularTint: {r: 1, g: 1, b: 1, a: 1}
- _StrobeSharpness: {r: 0.014, g: 0.122, b: 0, a: 0}
- _SubpixelMaskScale: {r: 1, g: 1, b: 0, a: 0}
- _SubpixelResolution: {r: 1280, g: 720, b: 0, a: 0}
- _ToonMathGradientBrightness: {r: 0, g: 0.5, b: 0, a: 0}
- _ToonMathGradientDiffuse: {r: 0, g: 1, b: 0, a: 0}
- _ToonMathGradientSpecular: {r: 0, g: 1, b: 0, a: 0}
162 changes: 161 additions & 1 deletion Hekky/Shaders/Common/Lighting.cginc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef HEKKY_LIGHTING
#define HEKKY_LIGHTING

#include "../Common/Sampling.cginc"

// Some functions in this file are taken from Standard Shader CGIncludes. Those are licensed under the MIT license

inline fixed4 SampleShadowMaskBicubic(float2 uv)
Expand Down Expand Up @@ -125,10 +127,168 @@ float SSDirectionalShadowAA(float4 _ShadowCoord, float atten)
}
}

a = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ShadowMapTexture, screenUV + uvOffsets[lowest]).r;
a = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ShadowMapTexture, screenUV + uvOffsets[lowest]).r * shadow;
}
return a;
}
#endif

// ====================================================================
// LIGHTING DATA
// ====================================================================

// A singular Unity Light
struct Light {
// rgb, pre-exposed intensity
float4 colorIntensity;
float3 l; // L
float NdotL;
float attenuation;
float3 worldPosition;
float distance; // Pre-computed distance between light and pixel
};

// =====================================================================================================
// SPHERICAL HARMONICS
// =====================================================================================================

// https://web.archive.org/web/20160313132301/http://www.geomerics.com/wp-content/uploads/2015/08/CEDEC_Geomerics_ReconstructingDiffuseLighting1.pdf
inline float shEvaluateDiffuseL1Geomerics_local(const float L0, const float3 L1, const float3 n)
{
// average energy
// clamp negative values
const float R0 = max(L0, 0.0);

// average light direction
const float3 R1 = 0.5f * L1;
const float magR1 = length(R1);
const float lightDirectionalRatio = magR1 / R0;

// solve for dynamic range constant a
const float a = (1.0f - lightDirectionalRatio) / (1.0f + lightDirectionalRatio);

// angle between normal and directional L1
float q = 0.5f + 0.5f * dot(normalize(R1), n);
q = saturate(q);

// power of q
const float p = 1.0f + 2.0f * lightDirectionalRatio;

return R0 * (a + (1.0f - a) * (p + 1.0f) * pow(q, p));
}

// ==============================================================================================================
// LIGHT PROBES
// ==============================================================================================================

inline float3 Irradiance_SphericalHarmonics(const float3 normal, const bool useL2, out Light outLight)
{
float3 finalSH = 0;

const float3 L0 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w)
+ float3(unity_SHBr.z, unity_SHBg.z, unity_SHBb.z) / 3.0;
finalSH.r = shEvaluateDiffuseL1Geomerics_local(L0.r, unity_SHAr.xyz, normal);
finalSH.g = shEvaluateDiffuseL1Geomerics_local(L0.g, unity_SHAg.xyz, normal);
finalSH.b = shEvaluateDiffuseL1Geomerics_local(L0.b, unity_SHAb.xyz, normal);
// Quadratic polynomials
if (useL2) finalSH += SHEvalLinearL2(float4(normal, 1));

// Light based on Light probes
outLight.l = normalize(unity_SHAr.xyz + unity_SHAg.xyz + unity_SHAb.xyz);
outLight.worldPosition = 0;
outLight.colorIntensity = 0;
outLight.NdotL = saturate(dot(normal, outLight.l));
outLight.distance = 100;
outLight.attenuation = 1.;

return finalSH;
}

#if UNITY_LIGHT_PROBE_PROXY_VOLUME

inline half3 Irradiance_SampleProbeVolume(half4 normal, float3 worldPos, out Light outLight)
{
const float transformToLocal = unity_ProbeVolumeParams.y;
const float texelSizeX = unity_ProbeVolumeParams.z;

//The SH coefficients textures and probe occlusion are packed into 1 atlas.
//-------------------------
//| ShR | ShG | ShB | Occ |
//-------------------------

float3 position = (transformToLocal == 1.0f) ? mul(unity_ProbeVolumeWorldToObject, float4(worldPos, 1.0)).xyz : worldPos;
float3 texCoord = (position - unity_ProbeVolumeMin.xyz) * unity_ProbeVolumeSizeInv.xyz;
texCoord.x = texCoord.x * 0.25f;

// We need to compute proper X coordinate to sample.
// Clamp the coordinate otherwize we'll have leaking between RGB coefficients
float texCoordX = clamp(texCoord.x, 0.5f * texelSizeX, 0.25f - 0.5f * texelSizeX);

// sampler state comes from SHr (all SH textures share the same sampler)
texCoord.x = texCoordX;
half4 SHAr = UNITY_SAMPLE_TEX3D_SAMPLER(unity_ProbeVolumeSH, unity_ProbeVolumeSH, texCoord);

texCoord.x = texCoordX + 0.25f;
half4 SHAg = UNITY_SAMPLE_TEX3D_SAMPLER(unity_ProbeVolumeSH, unity_ProbeVolumeSH, texCoord);

texCoord.x = texCoordX + 0.5f;
half4 SHAb = UNITY_SAMPLE_TEX3D_SAMPLER(unity_ProbeVolumeSH, unity_ProbeVolumeSH, texCoord);

// Linear + constant polynomial terms
half3 x1;

x1.r = shEvaluateDiffuseL1Geomerics_local(SHAr.w, SHAr.rgb, normal);
x1.g = shEvaluateDiffuseL1Geomerics_local(SHAg.w, SHAg.rgb, normal);
x1.b = shEvaluateDiffuseL1Geomerics_local(SHAb.w, SHAb.rgb, normal);

// Light based on Light probes
outLight.l = normalize(SHAr.rgb + SHAg.rgb + SHAb.rgb);
outLight.worldPosition = 0;
outLight.colorIntensity = 0;
outLight.NdotL = saturate(dot(normal, outLight.l));
outLight.distance = 100;
outLight.attenuation = 1.;

return x1;
}

#endif

inline float3 Irradiance_SphericalHarmonicsUnity(float3 normal, float3 ambient, float3 position, out Light outLight)
{
half3 ambient_contrib = 0.0;

#if UNITY_SAMPLE_FULL_SH_PER_PIXEL
#if UNITY_LIGHT_PROBE_PROXY_VOLUME
if (unity_ProbeVolumeParams.x == 1.0)
ambient_contrib = Irradiance_SampleProbeVolume(half4(normal, 1.0), position, outLight);
else
ambient_contrib = Irradiance_SphericalHarmonics(normal, true, outLight);
#else
ambient_contrib = Irradiance_SphericalHarmonics(normal, true, outLight);
#endif

ambient += max(half3(0, 0, 0), ambient_contrib);
#else
#if UNITY_LIGHT_PROBE_PROXY_VOLUME
if (unity_ProbeVolumeParams.x == 1.0)
ambient_contrib = Irradiance_SampleProbeVolume(half4(normal, 1.0), position, outLight);
else
ambient_contrib = Irradiance_SphericalHarmonics(normal, false, outLight);
#else
ambient_contrib = Irradiance_SphericalHarmonics(normal, false, outLight);
#endif

ambient = max(half3(0, 0, 0), ambient + ambient_contrib); // include L2 contribution in vertex shader before clamp.
#endif // UNITY_SAMPLE_FULL_SH_PER_PIXEL

#ifdef UNITY_COLORSPACE_GAMMA
ambient = LinearToGammaSpace(ambient);
#endif

outLight.colorIntensity = float4(ambient, 0);

return ambient;
}

#endif // HEKKY_LIGHTING
Loading

0 comments on commit 3a2d57d

Please sign in to comment.