Skip to content

Commit ed6be6e

Browse files
committed
fix accelerated pbr baker: apply band factors and avoid fireflies artifacts on overbright areas
1 parent b920f8c commit ed6be6e

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ public void setAssetManager(AssetManager assetManager) {
287287
}
288288

289289
void rebakeNow(RenderManager renderManager) {
290-
IBLHybridEnvBakerLight baker = new IBLGLEnvBakerLight(renderManager, assetManager, Format.RGB16F, Format.Depth,
291-
envMapSize, envMapSize);
290+
IBLHybridEnvBakerLight baker = new IBLGLEnvBakerLight(renderManager, assetManager, Format.RGB16F,
291+
Format.Depth,
292+
envMapSize, envMapSize);
292293

293294
baker.setTexturePulling(isRequiredSavableResults());
294295
baker.bakeEnvironment(spatial, getPosition(), frustumNear, frustumFar, filter);

jme3-core/src/main/java/com/jme3/environment/FastLightProbeFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import com.jme3.asset.AssetManager;
3535
import com.jme3.environment.baker.IBLGLEnvBakerLight;
36+
import com.jme3.environment.baker.IBLHybridEnvBakerLight;
3637
import com.jme3.environment.util.EnvMapUtils;
3738
import com.jme3.light.LightProbe;
3839
import com.jme3.math.Vector3f;
@@ -74,7 +75,8 @@ public class FastLightProbeFactory {
7475
* @return The baked LightProbe
7576
*/
7677
public static LightProbe makeProbe(RenderManager rm, AssetManager am, int size, Vector3f pos, float frustumNear, float frustumFar, Spatial scene) {
77-
IBLGLEnvBakerLight baker = new IBLGLEnvBakerLight(rm, am, Format.RGB16F, Format.Depth, size, size);
78+
IBLHybridEnvBakerLight baker = new IBLGLEnvBakerLight(rm, am, Format.RGB16F, Format.Depth, size,
79+
size);
7880

7981
baker.setTexturePulling(true);
8082
baker.bakeEnvironment(scene, pos, frustumNear, frustumFar, null);

jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBakerLight.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.ByteBuffer;
3535
import java.util.logging.Logger;
3636
import com.jme3.asset.AssetManager;
37+
import com.jme3.environment.util.EnvMapUtils;
3738
import com.jme3.material.Material;
3839
import com.jme3.math.ColorRGBA;
3940
import com.jme3.math.FastMath;
@@ -130,11 +131,12 @@ public void bakeSphericalHarmonicsCoefficients() {
130131
int s = renderOnT;
131132
renderOnT = renderOnT == 0 ? 1 : 0;
132133
mat.setTexture("ShCoef", shCoefTx[s]);
133-
mat.setInt("FaceId", faceId);
134134
} else {
135135
renderOnT = 0;
136136
}
137137

138+
mat.setInt("FaceId", faceId);
139+
138140
screen.updateLogicalState(0);
139141
screen.updateGeometricState();
140142

@@ -169,7 +171,7 @@ else if (weightAccum != c.a) {
169171
if (remapMaxValue > 0) shCoef[i].divideLocal(remapMaxValue);
170172
shCoef[i].multLocal(4.0f * FastMath.PI / weightAccum);
171173
}
172-
174+
EnvMapUtils.prepareShCoefs(shCoef);
173175
img.dispose();
174176

175177
}

jme3-core/src/main/java/com/jme3/environment/baker/IBLHybridEnvBakerLight.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public TextureCubeMap getSpecularIBL() {
200200
@Override
201201
public void bakeSphericalHarmonicsCoefficients() {
202202
shCoef = EnvMapUtils.getSphericalHarmonicsCoefficents(getEnvMap());
203+
EnvMapUtils.prepareShCoefs(shCoef);
203204
}
204205

205206
@Override

jme3-core/src/main/resources/Common/IBL/IBLKernels.frag

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void brdfKernel(){
3434
float NdotH = max(H.z, 0.0);
3535
float VdotH = max(dot(V, H), 0.0);
3636
if(NdotL > 0.0){
37-
float G = GeometrySmith(N, V, L, m_Roughness);
37+
float G = GeometrySmith(N, V, L, m_Roughness*m_Roughness);
3838
float G_Vis = (G * VdotH) / (NdotH * NdotV);
3939
float Fc = pow(1.0 - VdotH, 5.0);
4040
A += (1.0 - Fc) * G_Vis;
@@ -75,26 +75,33 @@ void prefilteredEnvKernel(){
7575
vec3 R = N;
7676
vec3 V = R;
7777

78-
// float a2 = m_Roughness;
79-
float a2 = m_Roughness * m_Roughness; // jme impl, why?
80-
a2 *= a2;
78+
float a2 = m_Roughness * m_Roughness;
8179

8280
const uint SAMPLE_COUNT = 1024u;
8381
float totalWeight = 0.0;
8482
vec3 prefilteredColor = vec3(0.0);
8583
for(uint i = 0u; i < SAMPLE_COUNT; ++i) {
8684
vec4 Xi = Hammersley(i, SAMPLE_COUNT);
8785
vec3 H = ImportanceSampleGGX(Xi, a2, N);
88-
float VoH = dot(V,H);
86+
float VoH = max(dot(V, H), 0.0);
8987
vec3 L = normalize(2.0 * VoH * H - V);
9088
float NdotL = max(dot(N, L), 0.0);
9189
if(NdotL > 0.0) {
90+
vec3 sampleColor = texture(m_EnvMap, L).rgb;
91+
92+
float luminance = dot(sampleColor, vec3(0.2126, 0.7152, 0.0722));
93+
if (luminance > 64.0) { // TODO use average?
94+
sampleColor *= 64.0/luminance;
95+
}
96+
9297
// TODO: use mipmap
93-
prefilteredColor += texture(m_EnvMap, L).rgb * NdotL;
98+
prefilteredColor += sampleColor * NdotL;
9499
totalWeight += NdotL;
95100
}
101+
96102
}
97-
prefilteredColor = prefilteredColor / totalWeight;
103+
104+
if(totalWeight > 0.001) prefilteredColor /= totalWeight;
98105
outFragColor = vec4(prefilteredColor, 1.0);
99106
}
100107

0 commit comments

Comments
 (0)