From 99d161288bfe2d10c331c97e6b7571f9c884e912 Mon Sep 17 00:00:00 2001 From: yum Date: Wed, 6 Aug 2025 16:42:42 -0700 Subject: initial commit --- UnityImageBasedLightingMinimal.cginc | 73 ++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 UnityImageBasedLightingMinimal.cginc (limited to 'UnityImageBasedLightingMinimal.cginc') diff --git a/UnityImageBasedLightingMinimal.cginc b/UnityImageBasedLightingMinimal.cginc new file mode 100644 index 0000000..fb8b8f3 --- /dev/null +++ b/UnityImageBasedLightingMinimal.cginc @@ -0,0 +1,73 @@ +// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) + +#ifndef UNITY_IMAGE_BASED_LIGHTING_INCLUDED +#define UNITY_IMAGE_BASED_LIGHTING_INCLUDED + +#include "pbr_utils.cginc" +#include "UnityStandardUtils.cginc" + +// ---------------------------------------------------------------------------- +// GlossyEnvironment - Function to integrate the specular lighting with default sky or reflection probes +// ---------------------------------------------------------------------------- +struct Unity_GlossyEnvironmentData +{ + // - Deferred case have one cubemap + // - Forward case can have two blended cubemap (unusual should be deprecated). + + // Surface properties use for cubemap integration + half roughness; // CAUTION: This is perceptualRoughness but because of compatibility this name can't be change :( + half3 reflUVW; +}; + +// ---------------------------------------------------------------------------- + +Unity_GlossyEnvironmentData UnityGlossyEnvironmentSetup(half Smoothness, half3 worldViewDir, half3 Normal, half3 fresnel0) +{ + Unity_GlossyEnvironmentData g; + + g.roughness /* perceptualRoughness */ = smoothnessToPerceptualRoughness(Smoothness); + g.reflUVW = reflect(-worldViewDir, Normal); + + return g; +} + +// ---------------------------------------------------------------------------- +half perceptualRoughnessToMipmapLevel(half perceptualRoughness) +{ + return perceptualRoughness * UNITY_SPECCUBE_LOD_STEPS; +} + +// ---------------------------------------------------------------------------- +half mipmapLevelToPerceptualRoughness(half mipmapLevel) +{ + return mipmapLevel / UNITY_SPECCUBE_LOD_STEPS; +} + +// ---------------------------------------------------------------------------- +half3 Unity_GlossyEnvironment (UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn) +{ + half perceptualRoughness = glossIn.roughness /* perceptualRoughness */ ; + +// TODO: CAUTION: remap from Morten may work only with offline convolution, see impact with runtime convolution! +// For now disabled +#if 0 + float m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the real roughness parameter + const float fEps = 1.192092896e-07F; // smallest such that 1.0+FLT_EPSILON != 1.0 (+1e-4h is NOT good here. is visibly very wrong) + float n = (2.0/max(fEps, m*m))-2.0; // remap to spec power. See eq. 21 in --> https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf + + n /= 4; // remap from n_dot_h formulatino to n_dot_r. See section "Pre-convolved Cube Maps vs Path Tracers" --> https://s3.amazonaws.com/docs.knaldtech.com/knald/1.0.0/lys_power_drops.html + + perceptualRoughness = pow( 2/(n+2), 0.25); // remap back to square root of real roughness (0.25 include both the sqrt root of the conversion and sqrt for going from roughness to perceptualRoughness) +#else + // MM: came up with a surprisingly close approximation to what the #if 0'ed out code above does. + perceptualRoughness = perceptualRoughness*(1.7 - 0.7*perceptualRoughness); +#endif + + + half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness); + half3 R = glossIn.reflUVW; + half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip); + + return DecodeHDR(rgbm, hdr); +} +#endif // UNITY_IMAGE_BASED_LIGHTING_INCLUDED \ No newline at end of file -- cgit v1.2.3