您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
80 行
2.6 KiB
80 行
2.6 KiB
#ifndef UNITY_ENTITY_LIGHTING_INCLUDED
|
|
#define UNITY_ENTITY_LIGHTING_INCLUDED
|
|
|
|
#include "common.hlsl"
|
|
|
|
// TODO: Check if PI is correctly handled!
|
|
|
|
// Ref: "Efficient Evaluation of Irradiance Environment Maps" from ShaderX 2
|
|
float3 SHEvalLinearL0L1(float3 N, float4 shAr, float4 shAg, float4 shAb)
|
|
{
|
|
float4 vA = float4(N, 1.0);
|
|
|
|
float3 x1;
|
|
// Linear (L1) + constant (L0) polynomial terms
|
|
x1.r = dot(shAr, vA);
|
|
x1.g = dot(shAg, vA);
|
|
x1.b = dot(shAb, vA);
|
|
|
|
return x1;
|
|
}
|
|
|
|
float3 SHEvalLinearL2(float3 N, float4 shBr, float4 shBg, float4 shBb, float4 shC)
|
|
{
|
|
float3 x2;
|
|
// 4 of the quadratic (L2) polynomials
|
|
float4 vB = N.xyzz * N.yzzx;
|
|
x2.r = dot(shBr, vB);
|
|
x2.g = dot(shBg, vB);
|
|
x2.b = dot(shBb, vB);
|
|
|
|
// Final (5th) quadratic (L2) polynomial
|
|
float vC = N.x * N.x - N.y * N.y;
|
|
float3 x3 = shC.rgb * vC;
|
|
|
|
return x2 + x3;
|
|
}
|
|
|
|
float3 SampleSH9(float4 SHCoefficients[7], float3 N)
|
|
{
|
|
float4 shAr = SHCoefficients[0];
|
|
float4 shAg = SHCoefficients[1];
|
|
float4 shAb = SHCoefficients[2];
|
|
float4 shBr = SHCoefficients[3];
|
|
float4 shBg = SHCoefficients[4];
|
|
float4 shBb = SHCoefficients[5];
|
|
float4 shCr = SHCoefficients[6];
|
|
|
|
// Linear + constant polynomial terms
|
|
float3 res = SHEvalLinearL0L1(N, shAr, shAg, shAb);
|
|
|
|
// Quadratic polynomials
|
|
res += SHEvalLinearL2(N, shBr, shBg, shBb, shCr);
|
|
|
|
return res;
|
|
}
|
|
|
|
// This sample a 3D volume storing SH
|
|
// Volume is store as 3D texture with 4 R, G, B, X set of 4 coefficient store atlas in same 3D texture. X unused.
|
|
// TODO: the packing here is innefficient as we will fetch values far away from each other and they may not fit into the cache
|
|
// Suggest we pack only RGB not X and continuous
|
|
float3 SampleProbeVolumeL0L1(TEXTURE3D_ARGS(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture, float texelSizeX)
|
|
{
|
|
float3 texCoord = mul(WorldToTexture, float4(positionWS, 1.0)).xyz;
|
|
// Each component is store in the same texture 3D. Each use one quater on the x axis
|
|
// Here we get R component then increase by step size (0.25) to get other component. This assume 4 component
|
|
// but last one is not used.
|
|
// Clamp to edge of the "internal" texture, as R is from half texel to size of R texture minus half texel.
|
|
// This avoid leaking
|
|
texCoord.x = clamp(texCoord.x * 0.25, 0.5 * texelSizeX, 0.25 - 0.5 * texelSizeX);
|
|
|
|
float4 shAr = SAMPLE_TEXTURE3D(SHVolumeTexture, SHVolumeSampler, texCoord);
|
|
texCoord.x += 0.25;
|
|
float4 shAg = SAMPLE_TEXTURE3D(SHVolumeTexture, SHVolumeSampler, texCoord);
|
|
texCoord.x += 0.25;
|
|
float4 shAb = SAMPLE_TEXTURE3D(SHVolumeTexture, SHVolumeSampler, texCoord);
|
|
|
|
return SHEvalLinearL0L1(normalWS, shAr, shAg, shAb);
|
|
}
|
|
|
|
#endif // UNITY_ENTITY_LIGHTING_INCLUDED
|