浏览代码

[PlanarReflection] Refactored custom getters and setters

/main
Frédéric Vauchelles 7 年前
当前提交
d9fd9f38
共有 4 个文件被更改,包括 66 次插入99 次删除
  1. 80
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightDefinition.cs.custom.hlsl
  2. 43
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightUtilities.hlsl
  3. 36
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/VolumeProjection.hlsl
  4. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl

80
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightDefinition.cs.custom.hlsl


// Packing accessors
//-----------------------------------------------------------------------------
#define GETTER_FLOAT3(Type, field)\
float3 Type##_##Get##_##field(Type data)\
#define GETTER_FLOAT3(Type, Name, field)\
float3 Name(Type data)\
#define SETTER_FLOAT3(data, field, value)\
data.##field##X = value.x;\
data.##field##Y = value.y;\
data.##field##Z = value.z
GETTER_FLOAT3(EnvLightData, capturePositionWS);
#define EnvLightData_Set_capturePositionWS(data, value) SETTER_FLOAT3(data, capturePositionWS, value)
GETTER_FLOAT3(EnvLightData, proxyPositionWS);
#define EnvLightData_Set_proxyPositionWS(data, value) SETTER_FLOAT3(data, proxyPositionWS, value)
GETTER_FLOAT3(EnvLightData, proxyForward);
#define EnvLightData_Set_proxyForward(data, value) SETTER_FLOAT3(data, proxyForward, value)
GETTER_FLOAT3(EnvLightData, proxyUp);
#define EnvLightData_Set_proxyUp(data, value) SETTER_FLOAT3(data, proxyUp, value)
GETTER_FLOAT3(EnvLightData, proxyRight);
#define EnvLightData_Set_proxyRight(data, value) SETTER_FLOAT3(data, proxyRight, value)
GETTER_FLOAT3(EnvLightData, proxyExtents);
#define EnvLightData_Set_proxyExtents(data, value) SETTER_FLOAT3(data, proxyExtents, value)
GETTER_FLOAT3(EnvLightData, influencePositionWS);
#define EnvLightData_Set_influencePositionWS(data, value) SETTER_FLOAT3(data, influencePositionWS, value)
GETTER_FLOAT3(EnvLightData, influenceForward);
#define EnvLightData_Set_influenceForward(data, value) SETTER_FLOAT3(data, influenceForward, value)
GETTER_FLOAT3(EnvLightData, influenceUp);
#define EnvLightData_Set_influenceUp(data, value) SETTER_FLOAT3(data, influenceUp, value)
GETTER_FLOAT3(EnvLightData, influenceRight);
#define EnvLightData_Set_influenceRight(data, value) SETTER_FLOAT3(data, influenceRight, value)
GETTER_FLOAT3(EnvLightData, influenceExtents);
#define EnvLightData_Set_influenceExtents(data, value) SETTER_FLOAT3(data, influenceExtents, value)
GETTER_FLOAT3(EnvLightData, blendDistancePositive);
#define EnvLightData_Set_blendDistancePositive(data, value) SETTER_FLOAT3(data, blendDistancePositive, value)
GETTER_FLOAT3(EnvLightData, blendDistanceNegative);
#define EnvLightData_Set_blendDistanceNegative(data, value) SETTER_FLOAT3(data, blendDistanceNegative, value)
GETTER_FLOAT3(EnvLightData, blendNormalDistancePositive);
#define EnvLightData_Set_blendNormalDistancePositive(data, value) SETTER_FLOAT3(data, blendNormalDistancePositive, value)
GETTER_FLOAT3(EnvLightData, blendNormalDistanceNegative);
#define EnvLightData_Set_blendNormalDistanceNegative(data, value) SETTER_FLOAT3(data, blendNormalDistanceNegative, value)
GETTER_FLOAT3(EnvLightData, boxSideFadePositive);
#define EnvLightData_Set_boxSideFadePositive(data, value) SETTER_FLOAT3(data, boxSideFadePositive, value)
GETTER_FLOAT3(EnvLightData, boxSideFadeNegative);
#define EnvLightData_Set_boxSideFadeNegative(data, value) SETTER_FLOAT3(data, boxSideFadeNegative, value)
GETTER_FLOAT3(EnvLightData, sampleDirectionDiscardWS);
#define EnvLightData_Set_sampleDirectionDiscardWS(data, value) SETTER_FLOAT3(data, sampleDirectionDiscardWS, value)
GETTER_FLOAT3(EnvLightData, GetCapturePositionWS, capturePositionWS)
GETTER_FLOAT3(EnvLightData, GetProxyPositionWS, proxyPositionWS)
GETTER_FLOAT3(EnvLightData, GetProxyForward, proxyForward)
GETTER_FLOAT3(EnvLightData, GetProxyUp, proxyUp)
GETTER_FLOAT3(EnvLightData, GetProxyRight, proxyRight)
GETTER_FLOAT3(EnvLightData, GetProxyExtents, proxyExtents)
GETTER_FLOAT3(EnvLightData, GetInfluencePositionWS, influencePositionWS)
GETTER_FLOAT3(EnvLightData, GetInfluenceForward, influenceForward)
GETTER_FLOAT3(EnvLightData, GetInfluenceUp, influenceUp)
GETTER_FLOAT3(EnvLightData, GetInfluenceRight, influenceRight)
GETTER_FLOAT3(EnvLightData, GetInfluenceExtents, influenceExtents)
GETTER_FLOAT3(EnvLightData, GetBlendDistancePositive, blendDistancePositive)
GETTER_FLOAT3(EnvLightData, GetBlendDistanceNegative, blendDistanceNegative)
GETTER_FLOAT3(EnvLightData, GetBlendNormalDistancePositive, blendNormalDistancePositive)
GETTER_FLOAT3(EnvLightData, GetBlendNormalDistanceNegative, blendNormalDistanceNegative)
GETTER_FLOAT3(EnvLightData, GetBoxSideFadePositive, boxSideFadePositive)
GETTER_FLOAT3(EnvLightData, GetBoxSideFadeNegative, boxSideFadeNegative)
GETTER_FLOAT3(EnvLightData, GetSampleDirectionDiscardWS, sampleDirectionDiscardWS)
#undef GETTER_FLOAT3

43
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightUtilities.hlsl


#include "LightDefinition.cs.hlsl"
#define SETTER_FLOAT3(data, field, value)\
data.##field##X = value.x;\
data.##field##Y = value.y;\
data.##field##Z = value.z
// The EnvLightData of the sky light contains a bunch of compile-time constants.
// This function sets them directly to allow the compiler to propagate them and optimize the code.
EnvLightData InitSkyEnvLightData(int envIndex)

output.envIndex = envIndex;
EnvLightData_Set_capturePositionWS(output, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, capturePositionWS, float3(0.0, 0.0, 0.0));
EnvLightData_Set_influenceForward(output, float3(0.0, 0.0, 1.0));
EnvLightData_Set_influenceUp(output, float3(0.0, 1.0, 0.0));
EnvLightData_Set_influenceRight(output, float3(1.0, 0.0, 0.0));
EnvLightData_Set_influencePositionWS(output, float3(0.0, 0.0, 0.0));
EnvLightData_Set_influenceExtents(output, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, influenceForward, float3(0.0, 0.0, 1.0));
SETTER_FLOAT3(output, influenceUp, float3(0.0, 1.0, 0.0));
SETTER_FLOAT3(output, influenceRight, float3(1.0, 0.0, 0.0));
SETTER_FLOAT3(output, influencePositionWS, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, influenceExtents, float3(0.0, 0.0, 0.0));
EnvLightData_Set_blendDistancePositive(output, float3(0.0, 0.0, 0.0));
EnvLightData_Set_blendDistanceNegative(output, float3(0.0, 0.0, 0.0));
EnvLightData_Set_blendNormalDistancePositive(output, float3(0.0, 0.0, 0.0));
EnvLightData_Set_blendNormalDistanceNegative(output, float3(0.0, 0.0, 0.0));
EnvLightData_Set_boxSideFadePositive(output, float3(0.0, 0.0, 0.0));
EnvLightData_Set_boxSideFadeNegative(output, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, blendDistancePositive, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, blendDistanceNegative, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, blendNormalDistancePositive, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, blendNormalDistanceNegative, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, boxSideFadePositive, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, boxSideFadeNegative, float3(0.0, 0.0, 0.0));
EnvLightData_Set_sampleDirectionDiscardWS(output, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, sampleDirectionDiscardWS, float3(0.0, 0.0, 0.0));
EnvLightData_Set_proxyForward(output, float3(0.0, 0.0, 1.0));
EnvLightData_Set_proxyUp(output, float3(0.0, 1.0, 0.0));
EnvLightData_Set_proxyRight(output, float3(1.0, 0.0, 0.0));
EnvLightData_Set_proxyPositionWS(output, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, proxyForward, float3(0.0, 0.0, 1.0));
SETTER_FLOAT3(output, proxyUp, float3(0.0, 1.0, 0.0));
SETTER_FLOAT3(output, proxyRight, float3(1.0, 0.0, 0.0));
SETTER_FLOAT3(output, proxyPositionWS, float3(0.0, 0.0, 0.0));
EnvLightData_Set_proxyExtents(output, float3(0.0, 0.0, 0.0));
SETTER_FLOAT3(output, proxyExtents, float3(0.0, 0.0, 0.0));
#undef SETTER_FLOAT3
#endif // UNITY_LIGHT_UTILITIES_INCLUDED

36
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/VolumeProjection.hlsl


{
return transpose(
float3x3(
EnvLightData_Get_proxyRight(lightData),
EnvLightData_Get_proxyUp(lightData),
EnvLightData_Get_proxyForward(lightData)
GetProxyRight(lightData),
GetProxyUp(lightData),
GetProxyForward(lightData)
)
); // worldToLocal assume no scaling
}

float3 positionPS = positionWS - EnvLightData_Get_proxyPositionWS(lightData);
float3 positionPS = positionWS - GetProxyPositionWS(lightData);
positionPS = mul(positionPS, worldToPS).xyz;
return positionPS;
}

float IntersectBoxProxy(EnvLightData lightData, float3 dirPS, float3 positionPS)
{
float3 boxOuterDistance = EnvLightData_Get_proxyExtents(lightData);
float3 boxOuterDistance = GetProxyExtents(lightData);
float projectionDistance = IntersectRayAABBSimple(positionPS, dirPS, -boxOuterDistance, boxOuterDistance);
projectionDistance = max(projectionDistance, lightData.minProjectionDistance); // Setup projection to infinite if requested (mean no projection shape)

#if defined(ENVMAP_FEATURE_INFLUENCENORMAL)
float insideInfluenceNormalVolume = lengthPositionLS <= (lightData.influenceExtentsX - lightData.blendNormalDistancePositiveX) ? 1.0 : 0.0;
float insideWeight = InfluenceFadeNormalWeight(bsdfData.normalWS, normalize(positionWS - EnvLightData_Get_capturePositionWS(lightData)));
float insideWeight = InfluenceFadeNormalWeight(bsdfData.normalWS, normalize(positionWS - GetCapturePositionWS(lightData)));
alpha *= insideInfluenceNormalVolume ? 1.0 : insideWeight;
#endif

float InfluenceBoxWeight(EnvLightData lightData, BSDFData bsdfData, float3 positionWS, float3 positionIS, float3 dirIS)
{
float3 influenceExtents = EnvLightData_Get_influenceExtents(lightData);
float3 influenceExtents = GetInfluenceExtents(lightData);
// 2. Process the position influence
// Calculate falloff value, so reflections on the edges of the volume would gradually blend to previous reflection.
#if defined(ENVMAP_FEATURE_PERFACEINFLUENCE) || defined(ENVMAP_FEATURE_INFLUENCENORMAL) || defined(ENVMAP_FEATURE_PERFACEFADE)

#if defined(ENVMAP_FEATURE_PERFACEINFLUENCE)
// Influence falloff for each face
float3 negativeFalloff = negativeDistance / max(0.0001, EnvLightData_Get_blendDistanceNegative(lightData));
float3 positiveFalloff = positiveDistance / max(0.0001, EnvLightData_Get_blendDistancePositive(lightData));
float3 negativeFalloff = negativeDistance / max(0.0001, GetBlendDistanceNegative(lightData));
float3 positiveFalloff = positiveDistance / max(0.0001, GetBlendDistancePositive(lightData));
// Fallof is the min for all faces
float influenceFalloff = min(

#if defined(ENVMAP_FEATURE_INFLUENCENORMAL)
// 3. Process the normal influence
// Calculate a falloff value to discard normals pointing outward the center of the environment light
float3 belowPositiveInfluenceNormalVolume = positiveDistance / max(0.0001, EnvLightData_Get_blendNormalDistancePositive(lightData));
float3 aboveNegativeInfluenceNormalVolume = negativeDistance / max(0.0001, EnvLightData_Get_blendNormalDistanceNegative(lightData));
float3 belowPositiveInfluenceNormalVolume = positiveDistance / max(0.0001, GetBlendNormalDistancePositive(lightData));
float3 aboveNegativeInfluenceNormalVolume = negativeDistance / max(0.0001, GetBlendNormalDistanceNegative(lightData));
float insideWeight = InfluenceFadeNormalWeight(bsdfData.normalWS, normalize(positionWS - EnvLightData_Get_capturePositionWS(lightData)));
float insideWeight = InfluenceFadeNormalWeight(bsdfData.normalWS, normalize(positionWS - GetCapturePositionWS(lightData)));
alpha *= insideInfluenceNormalVolume ? 1.0 : insideWeight;
#endif

// We consider R.x as cos(X) and then fade as angle from 60°(=acos(1/2)) to 75°(=acos(1/4))
// For positive axes: axisFade = (R - 1/4) / (1/2 - 1/4)
// <=> axisFace = 4 * R - 1;
float3 faceFade = saturate((4 * dirIS - 1) * EnvLightData_Get_boxSideFadePositive(lightData))
+ saturate((-4 * dirIS - 1) * EnvLightData_Get_boxSideFadeNegative(lightData));
float3 faceFade = saturate((4 * dirIS - 1) * GetBoxSideFadePositive(lightData))
+ saturate((-4 * dirIS - 1) * GetBoxSideFadeNegative(lightData));
alpha *= saturate(faceFade.x + faceFade.y + faceFade.z);
#endif

{
return transpose(
float3x3(
EnvLightData_Get_influenceRight(lightData),
EnvLightData_Get_influenceUp(lightData),
EnvLightData_Get_influenceForward(lightData)
GetInfluenceRight(lightData),
GetInfluenceUp(lightData),
GetInfluenceForward(lightData)
)
); // worldToLocal assume no scaling
}

float3 positionIS = positionWS - EnvLightData_Get_influencePositionWS(lightData);
float3 positionIS = positionWS - GetInfluencePositionWS(lightData);
positionIS = mul(positionIS, worldToIS).xyz;
return positionIS;
}

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


{
projectionDistance = IntersectSphereProxy(lightData, dirPS, positionPS);
// We can reuse dist calculate in LS directly in WS as there is no scaling. Also the offset is already include in lightData.capturePositionWS
float3 capturePositionWS = EnvLightData_Get_capturePositionWS(lightData);
float3 capturePositionWS = GetCapturePositionWS(lightData);
R = (positionWS + projectionDistance * R) - capturePositionWS;
// Test again for clear coat

projectionDistance = IntersectBoxProxy(lightData, dirPS, positionPS);
// No need to normalize for fetching cubemap
// We can reuse dist calculate in LS directly in WS as there is no scaling. Also the offset is already include in lightData.capturePositionWS
float3 capturePositionWS = EnvLightData_Get_capturePositionWS(lightData);
float3 capturePositionWS = GetCapturePositionWS(lightData);
R = (positionWS + projectionDistance * R) - capturePositionWS;
// TODO: add distance based roughness

float roughness = PerceptualRoughnessToRoughness(preLightData.iblPerceptualRoughness);
R = lerp(R, preLightData.iblR, saturate(smoothstep(0, 1, roughness * roughness)));
float3 sampleDirectionDiscardWS = EnvLightData_Get_sampleDirectionDiscardWS(lightData);
float3 sampleDirectionDiscardWS = GetSampleDirectionDiscardWS(lightData);
if (dot(sampleDirectionDiscardWS, R) < 0)
return lighting;

正在加载...
取消
保存