From ec01833fa12240cc3cdf54044b43abc65bc180ba Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 11 Sep 2019 11:04:28 +0200 Subject: [PATCH] EnvironmentProbeControl, for simpler env baking --- .../environment/EnvironmentProbeControl.java | 104 ++++++++++++++++++ .../jme3/environment/LightProbeFactory2.java | 3 +- .../com/jme3/environment/baker/EnvBaker.java | 5 +- .../environment/baker/GenericEnvBaker.java | 8 +- .../jme3test/light/pbr/TestPBRSimple.java | 40 +++++++ 5 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java create mode 100644 jme3-examples/src/main/java/jme3test/light/pbr/TestPBRSimple.java diff --git a/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java b/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java new file mode 100644 index 000000000..ec43733dd --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java @@ -0,0 +1,104 @@ +package com.jme3.environment; + +import java.util.function.Function; +import java.util.function.Predicate; + +import com.jme3.asset.AssetManager; +import com.jme3.environment.baker.IBLGLEnvBakerLight; +import com.jme3.light.LightProbe; +import com.jme3.math.Vector3f; +import com.jme3.renderer.RenderManager; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Geometry; +import com.jme3.scene.Node; +import com.jme3.scene.Spatial; +import com.jme3.scene.control.Control; +import com.jme3.texture.Image.Format; + +/** + * SmartLightProbe + */ +public class EnvironmentProbeControl extends LightProbe implements Control { + + + + RenderManager renderManager; + AssetManager assetManager; + int envMapSize; + Spatial spatial; + boolean BAKE_NEEDED=true; + Function filter=(s)->{ + return s.getUserData("tags.env")!=null; + }; + + public static void tag(Spatial s){ + if(s instanceof Node){ + Node n=(Node)s; + for(Spatial sx:n.getChildren()){ + tag(sx); + } + }else if(s instanceof Geometry){ + s.setUserData("tags.env", true); + } + } + + public EnvironmentProbeControl(RenderManager rm,AssetManager am, int size){ + renderManager=rm; + assetManager=am; + envMapSize=size; + } + + @Override + public Control cloneForSpatial(Spatial spatial) { + return null; + } + + @Override + public void setSpatial(Spatial spatial) { + + spatial.addLight(this); + this.spatial=spatial; + + } + + @Override + public void update(float tpf) { + + } + + @Override + public void render(RenderManager rm, ViewPort vp) { + if(BAKE_NEEDED){ + BAKE_NEEDED=false; + rebakeNow(); + } + } + + public void rebake(){ + BAKE_NEEDED=true; + } + + void rebakeNow() { + System.out.println("BAKE"); + + IBLGLEnvBakerLight baker = new IBLGLEnvBakerLight(renderManager, assetManager, Format.RGB16F, Format.Depth, + envMapSize, envMapSize); + + + baker.bakeEnvironment(spatial, Vector3f.ZERO, 0.001f, 1000f,filter); + baker.bakeSpecularIBL(); + baker.bakeSphericalHarmonicsCoefficients(); + + + // probe.setPosition(Vector3f.ZERO); + setPrefilteredMap(baker.getSpecularIBL()); + setNbMipMaps(getPrefilteredEnvMap().getImage().getMipMapSizes().length); + setShCoeffs(baker.getSphericalHarmonicsCoefficients()); + setPosition(Vector3f.ZERO); + setReady(true); + + baker.clean(); + + } + +} \ No newline at end of file diff --git a/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory2.java b/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory2.java index 543682d6f..6e28d1cbe 100644 --- a/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory2.java +++ b/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory2.java @@ -51,7 +51,7 @@ public class LightProbeFactory2 { am, Format.RGB16F, Format.Depth, size, size); - baker.bakeEnvironment(scene,pos, frustumNear,frustumFar); + baker.bakeEnvironment(scene,pos, frustumNear,frustumFar,null); baker.bakeSpecularIBL(); baker.bakeSphericalHarmonicsCoefficients(); @@ -72,7 +72,6 @@ public class LightProbeFactory2 { - /** * For debuging porpose only * Will return a Node meant to be added to a GUI presenting the 2 cube maps in a cross pattern with all the mip maps. diff --git a/jme3-core/src/main/java/com/jme3/environment/baker/EnvBaker.java b/jme3-core/src/main/java/com/jme3/environment/baker/EnvBaker.java index 5b73b6824..7df8f68d2 100644 --- a/jme3-core/src/main/java/com/jme3/environment/baker/EnvBaker.java +++ b/jme3-core/src/main/java/com/jme3/environment/baker/EnvBaker.java @@ -1,6 +1,9 @@ package com.jme3.environment.baker; +import java.util.function.Function; + import com.jme3.math.Vector3f; +import com.jme3.scene.Geometry; import com.jme3.scene.Spatial; import com.jme3.texture.TextureCubeMap; @@ -10,7 +13,7 @@ import com.jme3.texture.TextureCubeMap; * @author Riccardo Balbo */ public interface EnvBaker { - public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar); + public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar,Function filter); public TextureCubeMap getEnvMap(); public void clean(); } \ No newline at end of file diff --git a/jme3-core/src/main/java/com/jme3/environment/baker/GenericEnvBaker.java b/jme3-core/src/main/java/com/jme3/environment/baker/GenericEnvBaker.java index 096a66d14..4046f7534 100644 --- a/jme3-core/src/main/java/com/jme3/environment/baker/GenericEnvBaker.java +++ b/jme3-core/src/main/java/com/jme3/environment/baker/GenericEnvBaker.java @@ -4,6 +4,7 @@ import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.HashMap; +import java.util.function.Function; import com.jme3.asset.AssetManager; import com.jme3.environment.baker.EnvBaker; @@ -14,6 +15,7 @@ import com.jme3.math.Vector3f; import com.jme3.renderer.Camera; import com.jme3.renderer.RenderManager; import com.jme3.renderer.ViewPort; +import com.jme3.scene.Geometry; import com.jme3.scene.Spatial; import com.jme3.texture.FrameBuffer; import com.jme3.texture.Image.Format; @@ -117,7 +119,7 @@ public abstract class GenericEnvBaker implements EnvBaker{ @Override - public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar) { + public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar,Function filter) { FrameBuffer envbaker=new FrameBuffer(env.getImage().getWidth(),env.getImage().getHeight(),1); envbaker.setDepthTarget(FrameBuffer.newTarget(depthFormat)); envbaker.setSrgb(false); @@ -138,8 +140,12 @@ public abstract class GenericEnvBaker implements EnvBaker{ scene.updateLogicalState(0); scene.updateModelBound(); scene.updateGeometricState(); + + Function ofilter= renderManager.getRenderFilter(); + renderManager.setRenderFilter(filter); renderManager.renderViewPort(viewPort,0.16f); + renderManager.setRenderFilter(ofilter); if(copyToRam){ ByteBuffer face=BufferUtils.createByteBuffer( diff --git a/jme3-examples/src/main/java/jme3test/light/pbr/TestPBRSimple.java b/jme3-examples/src/main/java/jme3test/light/pbr/TestPBRSimple.java new file mode 100644 index 000000000..611445566 --- /dev/null +++ b/jme3-examples/src/main/java/jme3test/light/pbr/TestPBRSimple.java @@ -0,0 +1,40 @@ +package jme3test.light.pbr; + +import com.jme3.app.SimpleApplication; +import com.jme3.environment.EnvironmentProbeControl; +import com.jme3.material.Material; +import com.jme3.scene.Geometry; +import com.jme3.scene.Spatial; +import com.jme3.util.SkyFactory; +import com.jme3.util.mikktspace.MikktspaceTangentGenerator; + +/** + * TestPBRSimple + */ +public class TestPBRSimple extends SimpleApplication{ + + public static void main(String[] args) { + new TestPBRSimple().start(); + } + + @Override + public void simpleInitApp() { + + Geometry model = (Geometry) assetManager.loadModel("Models/Tank/tank.j3o"); + MikktspaceTangentGenerator.generate(model); + + Material pbrMat = assetManager.loadMaterial("Models/Tank/tank.j3m"); + model.setMaterial(pbrMat); + + rootNode.attachChild(model); + + + EnvironmentProbeControl envProbe=new EnvironmentProbeControl(renderManager,assetManager,256); + rootNode.addControl(envProbe); + + Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Path.hdr", SkyFactory.EnvMapType.EquirectMap); + rootNode.attachChild(sky); + EnvironmentProbeControl.tag(sky); + } + +} \ No newline at end of file