From ab981b76fc3e6c8422d04ce46a6bfe9cb6d347d3 Mon Sep 17 00:00:00 2001 From: Nehon Date: Sun, 15 Nov 2015 18:43:50 +0100 Subject: [PATCH] Added a define to toggle indirect lighting on or off, depending if there is a light probe in the light list or not. However, this is not ideal, as this code will trigger a recompilation of the shader on each pass, in case there is more than 1 lighting pass. Another way could be to multiply the indirect lighting contribution by the Ambient light color in the shader, and always perform the indirect lighting code (even without light probe). As in the second pass ambient light is forced to 0, the indirect lighting contribution would be nullified. However we'd have to force ambient light to 0 if there are no light probe. This would also have the nice side effect of having a way to dim or boost indirect lighting with the ambient light color. --- .../src/main/java/com/jme3/material/Material.java | 10 ++++++++-- .../src/main/java/com/jme3/material/Technique.java | 9 +++++++++ .../resources/Common/MatDefs/Light/PBRLighting.frag | 4 ++-- .../src/main/java/jme3test/light/pbr/TestPbrEnv.java | 11 ++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/material/Material.java b/jme3-core/src/main/java/com/jme3/material/Material.java index a727f104e..6e3e36bfc 100644 --- a/jme3-core/src/main/java/com/jme3/material/Material.java +++ b/jme3-core/src/main/java/com/jme3/material/Material.java @@ -777,6 +777,7 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { Vector4f tmpVec = vars.vect4f1; int curIndex; int endIndex = numLights + startIndex; + boolean useIBL = false; for (curIndex = startIndex; curIndex < endIndex && curIndex < lightList.size(); curIndex++) { @@ -841,7 +842,8 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { lightDataIndex++; break; case Probe: - + useIBL = true; + technique.setUseIndirectLighting(true); endIndex++; LightProbe probe = (LightProbe)l; BoundingSphere s = (BoundingSphere)probe.getBounds(); @@ -862,6 +864,10 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { } } vars.release(); + + if(!useIBL ){ + technique.setUseIndirectLighting(false); + } //Padding of unsued buffer space while(lightDataIndex < numLights * 3) { lightData.setVector4InArray(0f, 0f, 0f, 0f, lightDataIndex); @@ -1238,7 +1244,7 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { nbRenderedLights = updateLightListUniforms(shader, geom, lights, rm.getSinglePassLightBatchSize(), rm, nbRenderedLights); r.setShader(shader); renderMeshFromGeometry(r, geom); - } + } } return; case FixedPipeline: diff --git a/jme3-core/src/main/java/com/jme3/material/Technique.java b/jme3-core/src/main/java/com/jme3/material/Technique.java index 8321991bf..66fe319e6 100644 --- a/jme3-core/src/main/java/com/jme3/material/Technique.java +++ b/jme3-core/src/main/java/com/jme3/material/Technique.java @@ -187,6 +187,15 @@ public class Technique /* implements Savable */ { loadShader(assetManager,rendererCaps); } } + + public void setUseIndirectLighting(boolean useIBL){ + if(useIBL){ + defines.set("INDIRECT_LIGHTING", VarType.Boolean, true); + }else{ + defines.remove("INDIRECT_LIGHTING"); + } + needReload = true; + } private void loadShader(AssetManager manager,EnumSet rendererCaps) { diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag index 240fed2ba..de9150b86 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag @@ -202,7 +202,7 @@ void main(){ gl_FragColor.rgb += directLighting * fallOff; } - // #ifdef INDIRECT_LIGHTING + #ifdef INDIRECT_LIGHTING vec3 rv = reflect(-viewDir.xyz, normal.xyz); //prallax fix for spherical bounds. rv = g_ProbeData.w * (wPosition - g_ProbeData.xyz) +rv; @@ -223,7 +223,7 @@ void main(){ vec3 indirectLighting = indirectDiffuse + indirectSpecular; gl_FragColor.rgb = gl_FragColor.rgb + indirectLighting ; - // #endif + #endif #if defined(EMISSIVE) || defined (EMISSIVEMAP) #ifdef EMISSIVEMAP diff --git a/jme3-examples/src/main/java/jme3test/light/pbr/TestPbrEnv.java b/jme3-examples/src/main/java/jme3test/light/pbr/TestPbrEnv.java index fa6fa8df2..e1754f6fa 100644 --- a/jme3-examples/src/main/java/jme3test/light/pbr/TestPbrEnv.java +++ b/jme3-examples/src/main/java/jme3test/light/pbr/TestPbrEnv.java @@ -265,13 +265,15 @@ public class TestPbrEnv extends SimpleApplication implements ActionListener { inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_DOWN)); inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_RIGHT)); inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_LEFT)); + inputManager.addMapping("delete", new KeyTrigger(KeyInput.KEY_DELETE)); - inputManager.addListener(this, "switchGroundMat", "snapshot", "debugTex", "debugProbe", "fc", "up", "down", "left", "right"); + inputManager.addListener(this, "delete","switchGroundMat", "snapshot", "debugTex", "debugProbe", "fc", "up", "down", "left", "right"); } private LightProbe lastProbe; private Node debugGui ; + @Override public void onAction(String name, boolean keyPressed, float tpf) { if (name.equals("switchGroundMat") && keyPressed) { @@ -290,6 +292,13 @@ public class TestPbrEnv extends SimpleApplication implements ActionListener { rootNode.addLight(lastProbe); } + + if (name.equals("delete") && keyPressed) { + System.err.println(rootNode.getWorldLightList().size()); + rootNode.removeLight(lastProbe); + System.err.println("deleted"); + System.err.println(rootNode.getWorldLightList().size()); + } if (name.equals("fc") && keyPressed) {