From c00407da33a3d679a8c6fe1686379c14e16cf01d Mon Sep 17 00:00:00 2001 From: Nehon Date: Mon, 18 Sep 2017 23:00:57 +0200 Subject: [PATCH] PBR : Removed specularColor contribution from direct lighting specular. --- .../jme3/environment/LightProbeFactory.java | 3 +- .../Common/MatDefs/Light/PBRLighting.frag | 4 +- .../light/pbr/TestPBRDirectLighting.java | 136 ++++++++++++++++++ 3 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 jme3-examples/src/main/java/jme3test/light/pbr/TestPBRDirectLighting.java diff --git a/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory.java b/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory.java index 0238ca8fc..b41bbd479 100644 --- a/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory.java +++ b/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory.java @@ -273,8 +273,7 @@ public class LightProbeFactory { jobState.done[index] = true; if (jobState.isDone()) { - int nbMipMap = (int) (Math.log(probe.getPrefilteredEnvMap().getImage().getWidth()) / Math.log(2) - 1); - probe.setNbMipMaps(nbMipMap); + probe.setNbMipMaps(probe.getPrefilteredEnvMap().getImage().getMipMapSizes().length); probe.setReady(true); if (globalListener != null) { globalListener.done(probe); 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 66b0e2a68..80b9c4220 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag @@ -236,7 +236,7 @@ void main(){ lightColor.rgb,specular, Roughness, ndotv, directDiffuse, directSpecular); - vec3 directLighting = diffuseColor.rgb *directDiffuse + directSpecular * specularColor.rgb; + vec3 directLighting = diffuseColor.rgb *directDiffuse + directSpecular; gl_FragColor.rgb += directLighting * fallOff; } @@ -251,7 +251,7 @@ void main(){ //horizon fade from http://marmosetco.tumblr.com/post/81245981087 float horiz = dot(rv, wNormal.xyz); - float horizFadePower= 1.0 - Roughness; + float horizFadePower = 1.0 - Roughness; horiz = clamp( 1.0 + horizFadePower * horiz, 0.0, 1.0 ); horiz *= horiz; diff --git a/jme3-examples/src/main/java/jme3test/light/pbr/TestPBRDirectLighting.java b/jme3-examples/src/main/java/jme3test/light/pbr/TestPBRDirectLighting.java new file mode 100644 index 000000000..26cae098e --- /dev/null +++ b/jme3-examples/src/main/java/jme3test/light/pbr/TestPBRDirectLighting.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2009-2015 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package jme3test.light.pbr; + +import com.jme3.app.ChaseCameraAppState; +import com.jme3.app.SimpleApplication; +import com.jme3.bounding.BoundingSphere; +import com.jme3.environment.EnvironmentCamera; +import com.jme3.environment.LightProbeFactory; +import com.jme3.environment.generation.JobProgressAdapter; +import com.jme3.environment.util.EnvMapUtils; +import com.jme3.environment.util.LightsDebugState; +import com.jme3.input.ChaseCamera; +import com.jme3.input.KeyInput; +import com.jme3.input.controls.ActionListener; +import com.jme3.input.controls.KeyTrigger; +import com.jme3.light.DirectionalLight; +import com.jme3.light.LightProbe; +import com.jme3.material.Material; +import com.jme3.math.*; +import com.jme3.post.FilterPostProcessor; +import com.jme3.post.filters.ToneMapFilter; +import com.jme3.scene.*; +import com.jme3.scene.shape.Sphere; +import com.jme3.texture.plugins.ktx.KTXLoader; +import com.jme3.util.MaterialDebugAppState; +import com.jme3.util.SkyFactory; +import com.jme3.util.mikktspace.MikktspaceTangentGenerator; + +/** + * A test case for PBR lighting. + * Still experimental. + * + * @author nehon + */ +public class TestPBRDirectLighting extends SimpleApplication { + + public static void main(String[] args) { + TestPBRDirectLighting app = new TestPBRDirectLighting(); + app.start(); + } + + private DirectionalLight dl; + + private float roughness = 0.0f; + + @Override + public void simpleInitApp() { + + + viewPort.setBackgroundColor(ColorRGBA.DarkGray); + + dl = new DirectionalLight(); + dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal()); + rootNode.addLight(dl); + dl.setColor(ColorRGBA.White); + + ChaseCameraAppState chaser = new ChaseCameraAppState(); + chaser.setDragToRotate(true); + chaser.setMinVerticalRotation(-FastMath.HALF_PI); + chaser.setMaxDistance(1000); + chaser.setInvertVerticalAxis(true); + getStateManager().attach(chaser); + chaser.setTarget(rootNode); + flyCam.setEnabled(false); + + Geometry sphere = new Geometry("sphere", new Sphere(32, 32, 1)); + final Material m = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md"); + m.setColor("BaseColor", ColorRGBA.Black); + m.setFloat("Metallic", 0f); + m.setFloat("Roughness", roughness); + sphere.setMaterial(m); + rootNode.attachChild(sphere); + + inputManager.addListener(new ActionListener() { + @Override + public void onAction(String name, boolean isPressed, float tpf) { + + if (name.equals("rup") && isPressed) { + roughness = FastMath.clamp(roughness + 0.1f, 0.0f, 1.0f); + m.setFloat("Roughness", roughness); + } + if (name.equals("rdown") && isPressed) { + roughness = FastMath.clamp(roughness - 0.1f, 0.0f, 1.0f); + m.setFloat("Roughness", roughness); + } + + if (name.equals("light") && isPressed) { + dl.setDirection(cam.getDirection().normalize()); + } + } + }, "light", "rup", "rdown"); + + + inputManager.addMapping("light", new KeyTrigger(KeyInput.KEY_F)); + inputManager.addMapping("rup", new KeyTrigger(KeyInput.KEY_UP)); + inputManager.addMapping("rdown", new KeyTrigger(KeyInput.KEY_DOWN)); + + + } + + @Override + public void simpleUpdate(float tpf) { + } + +} +