Analysis functions to achieve GlobalIllumination

代码在:
E:\OGL\scriptable-render-pipeline-08-global-illumination\Assets\My Pipeline\ShaderLibrary


float3 GlobalIllumination (VertexOutput input, LitSurface surface) 
{
	#if defined(LIGHTMAP_ON)
		float3 gi = SampleLightmap(input.lightmapUV);
		#if defined(DYNAMICLIGHTMAP_ON)
			gi += SampleDynamicLightmap(input.dynamicLightmapUV);
		#endif
		return gi;
	#elif defined(DYNAMICLIGHTMAP_ON)
		return SampleDynamicLightmap(input.dynamicLightmapUV);
	#else
		return SampleLightProbes(surface);
	#endif
}

Three cases:
1: If you open a static lighting, the sampling static lighting. If you open and dynamic lighting, dynamic lighting then continue sampling.
2: only open dynamic lighting, dynamic lighting.
3: The movement did not open, the default sampling probe light.

Sampling static lighting:

float3 SampleLightmap (float2 uv) 
{
	return SampleSingleLightmap(
		TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap), uv,
		float4(1, 1, 0, 0),
		#if defined(UNITY_LIGHTMAP_FULL_HDR)
			false,
		#else
			true,
		#endif
		float4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0, 0.0)
	);
}

Here the calling function SampleSingleLightmap function, which in:
D: \ Program Files \ Unity2018.3.0f2 \ Unity \ Editor \ the Data \ Resources \ PackageManager \ Editor \ Package Penalty for \ ShaderLibrary \ EntityLighting.hlsl

real3 SampleSingleLightmap(
TEXTURE2D_ARGS(lightmapTex, lightmapSampler), 
float2 uv, float4 transform, bool encodedLightmap, real4 decodeInstructions)
{
    // transform is scale and bias
    uv = uv * transform.xy + transform.zw;
    real3 illuminance = real3(0.0, 0.0, 0.0);
    // Remark: baked lightmap is RGBM for now, dynamic lightmap is RGB9E5
    if (encodedLightmap)
    {
        real4 encodedIlluminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgba;
        illuminance = DecodeLightmap(encodedIlluminance, decodeInstructions);
    }
    else
    {
        illuminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgb;
    }
    return illuminance;
}

TEXTURE2D_PARAM and TEXTURE2D_ARGS macro definitions:

#define TEXTURE2D_PARAM(textureName, samplerName)                textureName, samplerName
#define TEXTURE2D_ARGS(textureName, samplerName)                 TEXTURE2D(textureName),         SAMPLER(samplerName)

Here the fourth parameter: float4 transform uv is scaled and offset.

// transform is scale and bias
uv = uv * transform.xy + transform.zw;

As we try to pass uv that has been scaled and offset (in the vertex shader processing), it is passed into the transform is (1,1,0,0), the first two are scaling uv, after uv offset is two.

#if defined(LIGHTMAP_ON)
	output.lightmapUV = input.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif

The first five parameters: encodedLightmap
if true:

if (encodedLightmap)
{
    real4 encodedIlluminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgba;
    illuminance = DecodeLightmap(encodedIlluminance, decodeInstructions);
}

SAMPLE_TEXTURE2D defined as follows:

#define SAMPLE_TEXTURE2D(textureName, samplerName, coord2)                               textureName.Sample(samplerName, coord2)

Sampling returns #define real4 half4 (mobile platform) or REAL4 float4 #define
DecodeLightmap function as follows:
// Remark, the: Baked LightMap IS RGBM for now, Dynamic IS RGB9E5 LightMap

real3 DecodeLightmap(real4 encodedIlluminance, real4 decodeInstructions)
{
	#if defined(UNITY_LIGHTMAP_RGBM_ENCODING)
	    return UnpackLightmapRGBM(encodedIlluminance, decodeInstructions);
	#elif defined(UNITY_LIGHTMAP_DLDR_ENCODING) //双倍低动态double low dynamic range(色值乘二...)
	    return UnpackLightmapDoubleLDR(encodedIlluminance, decodeInstructions);
	#else // (UNITY_LIGHTMAP_FULL_HDR)
	    return encodedIlluminance.rgb;
	#endif
}

First: UnpackLightmapRGBM

real3 UnpackLightmapRGBM(real4 rgbmInput, real4 decodeInstructions)
{
	#ifdef UNITY_COLORSPACE_GAMMA
	    return rgbmInput.rgb * (rgbmInput.a * decodeInstructions.x);
	#else
	    return rgbmInput.rgb * (PositivePow(rgbmInput.a, decodeInstructions.y) * decodeInstructions.x);
	#endif
}

Second: UnpackLightmapDoubleLDR

real3 UnpackLightmapDoubleLDR(real4 encodedColor, real4 decodeInstructions)
{
    return encodedColor.rgb * decodeInstructions.x;
}

See two macros: LIGHTMAP_HDR_MULTIPLIER and LIGHTMAP_HDR_EXPONENT

#define LIGHTMAP_RGBM_MAX_GAMMA     real(5.0)       // NB: Must match value in RGBMRanges.h
#define LIGHTMAP_RGBM_MAX_LINEAR    real(34.493242) // LIGHTMAP_RGBM_MAX_GAMMA ^ 2.2

#ifdef UNITY_LIGHTMAP_RGBM_ENCODING
    #ifdef UNITY_COLORSPACE_GAMMA
        #define LIGHTMAP_HDR_MULTIPLIER LIGHTMAP_RGBM_MAX_GAMMA
        #define LIGHTMAP_HDR_EXPONENT   real(1.0)   // Not used in gamma color space
    #else
        #define LIGHTMAP_HDR_MULTIPLIER LIGHTMAP_RGBM_MAX_LINEAR
        #define LIGHTMAP_HDR_EXPONENT   real(2.2)
    #endif
#elif defined(UNITY_LIGHTMAP_DLDR_ENCODING)
    #ifdef UNITY_COLORSPACE_GAMMA
        #define LIGHTMAP_HDR_MULTIPLIER real(2.0)
    #else
        #define LIGHTMAP_HDR_MULTIPLIER real(4.59) // 2.0 ^ 2.2
    #endif
    #define LIGHTMAP_HDR_EXPONENT real(0.0)
#else // (UNITY_LIGHTMAP_FULL_HDR)
    #define LIGHTMAP_HDR_MULTIPLIER real(1.0)
    #define LIGHTMAP_HDR_EXPONENT real(1.0)
#endif

SampleDynamicLightmap

float3 SampleDynamicLightmap (float2 uv) 
{
	return SampleSingleLightmap(
		TEXTURE2D_PARAM(unity_DynamicLightmap, samplerunity_DynamicLightmap), uv,
		float4(1, 1, 0, 0), false,
		float4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0, 0.0)
	);
}

SampleLightProbes

float3 SampleLightProbes (LitSurface s) 
{
	if (unity_ProbeVolumeParams.x) 
	{
		return SampleProbeVolumeSH4(
			TEXTURE3D_PARAM(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH),
			s.position, s.normal, unity_ProbeVolumeWorldToObject,
			unity_ProbeVolumeParams.y, unity_ProbeVolumeParams.z,
			unity_ProbeVolumeMin, unity_ProbeVolumeSizeInv
		);
	}
	else {
		float4 coefficients[7];
		coefficients[0] = unity_SHAr;
		coefficients[1] = unity_SHAg;
		coefficients[2] = unity_SHAb;
		coefficients[3] = unity_SHBr;
		coefficients[4] = unity_SHBg;
		coefficients[5] = unity_SHBb;
		coefficients[6] = unity_SHC;
		return max(0.0, SampleSH9(coefficients, s.normal));
	}

Here rejected tracked, goodbye! ! !

Published 646 original articles · won praise 107 · views 360 000 +

Guess you like

Origin blog.csdn.net/wodownload2/article/details/105114964