This commit is contained in:
Azaezel 2019-03-03 14:22:28 -06:00
parent f15fd381aa
commit fd0c90c575

View file

@ -82,97 +82,32 @@ float3 iblSkylightSpecular(Surface surface, ProbeData probe)
float3 iblBoxDiffuse( Surface surface, ProbeData probe) float3 iblBoxDiffuse( Surface surface, ProbeData probe)
{ {
float3 wsPosition; float3 dir = boxProject(surface, probe);
float radius;
float3 boxMin;
float3 boxMax;
float attenuation;
float4x4 worldToLocal;
uint probeIdx;
uint type; //box = 0, sphere = 1
float contribution;
float3 refPosition;
float3 pad;
inline void defineSphereSpaceInfluence(Surface surface, float3 wsEyeRay) float lod = surface.roughness*cubeMips;
{ float3 color = TORQUE_TEXCUBEARRAYLOD(irradianceCubemapAR, dir, probe.probeIdx, lod).xyz;
float3 L = wsPosition.xyz - surface.P;
contribution = 1.0 - length(L) / radius;
}
inline void defineBoxSpaceInfluence(Surface surface, float3 wsEyeRay) return color;
{ }
float3 surfPosLS = mul(worldToLocal, float4(surface.P, 1.0)).xyz;
float3 boxMinLS = wsPosition.xyz - (float3(1, 1, 1)*radius); float3 iblBoxSpecular(Surface surface, ProbeData probe)
float3 boxMaxLS = wsPosition.xyz + (float3(1, 1, 1)*radius); {
// BRDF
float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV,0.0,0.0)).xy;
float boxOuterRange = length(boxMaxLS - boxMinLS); float3 dir = boxProject(surface, probe);
float boxInnerRange = boxOuterRange / attenuation;
float3 localDir = float3(abs(surfPosLS.x), abs(surfPosLS.y), abs(surfPosLS.z)); // Radiance (Specular)
localDir = (localDir - boxInnerRange) / (boxOuterRange - boxInnerRange); #if DEBUGVIZ_SPECCUBEMAP == 0
float lod = surface.roughness*cubeMips;
#elif DEBUGVIZ_SPECCUBEMAP == 1
float lod = 0;
#endif
contribution = max(localDir.x, max(localDir.y, localDir.z)) * -1; float3 color = TORQUE_TEXCUBEARRAYLOD(cubeMapAR, dir, probe.probeIdx, lod).xyz * (brdf.x + brdf.y);
}
// Box Projected IBL Lighting
// Based on: http://www.gamedev.net/topic/568829-box-projected-cubemap-environment-mapping/
// and https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
inline float3 boxProject(Surface surface) //float3 wsPosition, float3 wsEyeRay, float3 reflectDir, float3 boxWSPos, float3 boxMin, float3 boxMax
{
float3 nrdir = normalize(surface.R);
float3 offset = surface.P;
float3 plane1vec = (boxMax - offset) / nrdir;
float3 plane2vec = (boxMin - offset) / nrdir;
float3 furthestPlane = max(plane1vec, plane2vec);
float dist = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
float3 posonbox = offset + nrdir * dist;
return posonbox - refPosition;
}
inline float3 iblBoxDiffuse( Surface surface)
{
float3 dir = boxProject(surface);
float lod = surface.roughness*cubeMips;
float3 color = TORQUE_TEXCUBEARRAYLOD(irradianceCubemapAR, dir, probeIdx, lod).xyz;
if (contribution>0)
return color*contribution;
else
return float3(0,0,0);
}
inline float3 iblBoxSpecular(Surface surface)
{
// BRDF
float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV,0.0,0.0)).xy;
float3 dir = boxProject(surface);
// Radiance (Specular)
#if DEBUGVIZ_SPECCUBEMAP == 0
float lod = surface.roughness*cubeMips;
#elif DEBUGVIZ_SPECCUBEMAP == 1
float lod = 0;
#endif
float3 color = TORQUE_TEXCUBEARRAYLOD(cubeMapAR, dir, probeIdx, lod).xyz * (brdf.x + brdf.y);
if (contribution>0)
return color*contribution;
else
return float3(0,0,0);
}
inline void reweight(float bias)
{
contribution = bias;
}
};
return color;
}
float4 main( PFXVertToPix IN ) : SV_TARGET float4 main( PFXVertToPix IN ) : SV_TARGET
{ {
@ -190,6 +125,7 @@ float4 main( PFXVertToPix IN ) : SV_TARGET
} }
int i = 0; int i = 0;
float blendVal[MAX_PROBES];
float blendFactor[MAX_PROBES]; float blendFactor[MAX_PROBES];
float blendSum = 0; float blendSum = 0;
float blendFacSum = 0; float blendFacSum = 0;
@ -214,11 +150,11 @@ float4 main( PFXVertToPix IN ) : SV_TARGET
if (probes[i].type == 0) //box if (probes[i].type == 0) //box
{ {
probes[i].defineBoxSpaceInfluence(surface, IN.wsEyeRay); blendVal[i] = defineBoxSpaceInfluence(surface, probes[i], IN.wsEyeRay);
} }
else if (probes[i].type == 1) //sphere else if (probes[i].type == 1) //sphere
{ {
probes[i].defineSphereSpaceInfluence(surface,IN.wsEyeRay); blendVal[i] = defineSphereSpaceInfluence(surface, probes[i], IN.wsEyeRay);
} }
else //skylight else //skylight
{ {
@ -240,36 +176,33 @@ float4 main( PFXVertToPix IN ) : SV_TARGET
{ {
if (numProbes>1) if (numProbes>1)
{ {
blendFactor[i] = (( probes[i].contribution / blendSum)) / (numProbes - 1); blendFactor[i] = ((1.0f - blendVal[i] / blendSum)) / (numProbes - 1);
blendFactor[i] *= (( probes[i].contribution) / invBlendSum); blendFactor[i] *= ((1.0f - blendVal[i]) / invBlendSum);
blendFacSum += blendFactor[i]; blendFacSum += blendFactor[i];
} }
else else
{ {
blendFactor[i] = probes[i].contribution; blendFactor[i] = blendVal[i];
blendFacSum = probes[i].contribution; blendFacSum = blendVal[i];
} }
} }
// Normalize blendVal // Normalize blendVal
#if DEBUGVIZ_ATTENUATION == 0 //this can likely be removed when we fix the above normalization behavior
if (blendFacSum == 0.0f) // Possible with custom weight if (blendFacSum == 0.0f) // Possible with custom weight
{ {
blendFacSum = 1.0f; blendFacSum = 1.0f;
} }
#endif
float invBlendSumWeighted = 1.0f / blendFacSum; float invBlendSumWeighted = 1.0f / blendFacSum;
for (i = 0; i < numProbes; ++i) for (i = 0; i < numProbes; ++i)
{ {
blendFactor[i] *= invBlendSumWeighted; blendFactor[i] *= invBlendSumWeighted;
probes[i].reweight(blendFactor[i]);
} }
#if DEBUGVIZ_ATTENUATION == 1 #if DEBUGVIZ_ATTENUATION == 1
float attenVis = 0; return float4(blendFacSum, blendFacSum, blendFacSum, 1);
for (i = 0; i < numProbes; ++i)
{
attenVis += probes[i].contribution;
}
return float4(attenVis, attenVis, attenVis, 1);
#endif #endif
#if DEBUGVIZ_CONTRIB == 1 #if DEBUGVIZ_CONTRIB == 1
@ -277,10 +210,10 @@ float4 main( PFXVertToPix IN ) : SV_TARGET
float3 finalContribColor = float3(0, 0, 0); float3 finalContribColor = float3(0, 0, 0);
for (i = 0; i < numProbes; ++i) for (i = 0; i < numProbes; ++i)
{ {
if (probes[i].contribution == 0) if (blendFactor[i] == 0)
continue; continue;
finalContribColor += probes[i].contribution * probeContribColors[i].rgb; finalContribColor += blendFactor[i] * probeContribColors[i].rgb;
} }
return float4(finalContribColor, 1); return float4(finalContribColor, 1);
@ -297,7 +230,7 @@ float4 main( PFXVertToPix IN ) : SV_TARGET
kD *= 1.0 - surface.metalness; kD *= 1.0 - surface.metalness;
for (i = 0; i < numProbes; ++i) for (i = 0; i < numProbes; ++i)
{ {
if (probes[i].contribution == 0) if (blendVal[i] == 0)
continue; continue;
if (probes[i].type == 2) //skylight if (probes[i].type == 2) //skylight