Skip to content

Commit

Permalink
Box projection support for IBL probes
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Dec 23, 2024
1 parent 8aa1ecf commit dce2bd9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ uniform vec4 fogColor;
uniform float fogStart;
uniform float fogEnd;

uniform bool useBoxProjection;
uniform vec3 boxSize;
uniform vec3 boxPosition;

#include <unproject.glsl>
#include <gamma.glsl>
#include <fresnel.glsl>
Expand Down Expand Up @@ -64,6 +68,20 @@ subroutine uniform srtAmbient ambient;
uniform sampler2D ambientBRDF;
uniform bool haveAmbientBRDF;

vec3 bpcm(in vec3 pos, in vec3 v, vec3 boxMax, vec3 boxMin, vec3 boxPos)
{
vec3 nrdir = v;
vec3 rbmax = (boxMax - pos) / nrdir;
vec3 rbmin = (boxMin - pos) / nrdir;
vec3 rbminmax;
rbminmax.x = (nrdir.x > 0.0)? rbmax.x : rbmin.x;
rbminmax.y = (nrdir.y > 0.0)? rbmax.y : rbmin.y;
rbminmax.z = (nrdir.z > 0.0)? rbmax.z : rbmin.z;
float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
vec3 posOnBox = pos + nrdir * fa;
return posOnBox - boxPos;
}

layout(location = 0) out vec4 fragColor;

void main()
Expand Down Expand Up @@ -99,6 +117,14 @@ void main()
vec3 worldN = normalize((vec4(N, 0.0) * viewMatrix).xyz);
vec3 worldR = reflect(worldE, worldN);

if (useBoxProjection)
{
vec3 boxMin = boxPosition - boxSize;
vec3 boxMax = boxPosition + boxSize;
//worldN = normalize(bpcm(worldPos, worldN, boxMax, boxMin, boxPosition));
worldR = normalize(bpcm(worldPos, worldR, boxMax, boxMin, boxPosition));
}

vec4 pbr = texture(pbrBuffer, gbufTexCoord);
float roughness = pbr.r;
float metallic = pbr.g;
Expand Down
1 change: 1 addition & 0 deletions src/dagon/graphics/entity.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Entity: Owner, Updateable
bool dynamic = true;
bool decal = false;
bool probe = false;
bool probeUseBoxProjection = false;
bool transparent = false;
float opacity = 1.0f;
float gbufferMask = 1.0f;
Expand Down
1 change: 1 addition & 0 deletions src/dagon/render/deferred/passes/probe.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class PassEnvironmentProbe: RenderPass
{
if (entity.visible && entity.drawable)
{
environmentProbeShader.useBoxProjection = entity.probeUseBoxProjection;
renderEntity(entity, environmentProbeShader);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/dagon/render/deferred/shaders/probe.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import dagon.graphics.state;
class EnvironmentProbeShader: Shader
{
String vs, fs;

bool useBoxProjection = false;

this(Owner owner)
{
Expand Down Expand Up @@ -154,6 +156,12 @@ class EnvironmentProbeShader: Shader
}

setParameter("ambientEnergy", mat.emissionEnergy);

Vector3f boxSize = state.modelMatrix.scaling;
Vector3f boxPosition = state.modelMatrix.translation;
setParameter("boxSize", boxSize);
setParameter("boxPosition", boxPosition);
setParameter("useBoxProjection", useBoxProjection);

// Texture 6 - occlusion buffer
glActiveTexture(GL_TEXTURE6);
Expand Down

0 comments on commit dce2bd9

Please sign in to comment.