From edf7e051182b878e942f13631fa52a44075cf194 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Tue, 8 Aug 2023 10:11:34 +0200 Subject: [PATCH] Add copyright and javadoc, fix some merging issues --- .../environment/EnvironmentProbeControl.java | 77 ++++---- .../jme3/environment/LightProbeFactory.java | 31 +-- .../jme3/environment/LightProbeFactory2.java | 16 +- .../com/jme3/environment/baker/EnvBaker.java | 23 ++- .../environment/baker/GenericEnvBaker.java | 184 +++++++++--------- .../jme3/environment/baker/IBLEnvBaker.java | 55 +++++- .../environment/baker/IBLEnvBakerLight.java | 37 +++- .../jme3/environment/baker/IBLGLEnvBaker.java | 43 +++- .../environment/baker/IBLGLEnvBakerLight.java | 121 +++++++----- 9 files changed, 396 insertions(+), 191 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java b/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java index ec43733dd0..f5eb773a10 100644 --- a/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java +++ b/jme3-core/src/main/java/com/jme3/environment/EnvironmentProbeControl.java @@ -16,36 +16,45 @@ import com.jme3.texture.Image.Format; /** - * SmartLightProbe + * A control that automatically handles environment bake and rebake including + * only tagged spatials. + * + * @author Riccardo Balbo */ public class EnvironmentProbeControl extends LightProbe implements Control { + private RenderManager renderManager; + private AssetManager assetManager; + private int envMapSize; + private Spatial spatial; + private boolean BAKE_NEEDED = true; - - RenderManager renderManager; - AssetManager assetManager; - int envMapSize; - Spatial spatial; - boolean BAKE_NEEDED=true; - Function filter=(s)->{ - return s.getUserData("tags.env")!=null; + private 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 spatial as part of the environment. Only tagged spatials will be + * rendered in the environment map. + * + * @param s + * the spatial + */ + 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){ + } else if (s instanceof Geometry) { s.setUserData("tags.env", true); } } - public EnvironmentProbeControl(RenderManager rm,AssetManager am, int size){ - renderManager=rm; - assetManager=am; - envMapSize=size; + public EnvironmentProbeControl(RenderManager rm, AssetManager am, int size) { + renderManager = rm; + assetManager = am; + envMapSize = size; } @Override @@ -55,9 +64,9 @@ public Control cloneForSpatial(Spatial spatial) { @Override public void setSpatial(Spatial spatial) { - + spatial.addLight(this); - this.spatial=spatial; + this.spatial = spatial; } @@ -67,30 +76,28 @@ public void update(float tpf) { } @Override - public void render(RenderManager rm, ViewPort vp) { - if(BAKE_NEEDED){ - BAKE_NEEDED=false; + public void render(RenderManager rm, ViewPort vp) { + if (BAKE_NEEDED) { + BAKE_NEEDED = false; rebakeNow(); } } - public void rebake(){ - BAKE_NEEDED=true; + /** + * Schedule a rebake of the environment map. + */ + 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); + 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()); @@ -100,5 +107,5 @@ void rebakeNow() { baker.clean(); } - + } \ No newline at end of file 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 7becab82e8..eac66502d0 100644 --- a/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory.java +++ b/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory.java @@ -45,31 +45,36 @@ /** * Creates LightProbes within a scene, given an EnvironmentCamera. * - * Since this process can take a long time, you can provide a JobProgressListener that - * will be notified of the ongoing generation process when calling the makeProbe method. + * Since this process can take a long time, you can provide a + * JobProgressListener that will be notified of the ongoing generation process + * when calling the makeProbe method. * - * The process is as follows: - * 1. Create an EnvironmentCamera - * 2. give it a position in the scene - * 3. call {@link LightProbeFactory#makeProbe(com.jme3.environment.EnvironmentCamera, com.jme3.scene.Spatial)} - * 4. add the created LightProbe to a node with the {@link Node#addLight(com.jme3.light.Light) } method. + * The process is as follows: 1. Create an EnvironmentCamera 2. give it a + * position in the scene 3. call + * {@link LightProbeFactory#makeProbe(com.jme3.environment.EnvironmentCamera, com.jme3.scene.Spatial)} + * 4. add the created LightProbe to a node with the + * {@link Node#addLight(com.jme3.light.Light) } method. * * Optionally for step 3 call * {@link #makeProbe(com.jme3.environment.EnvironmentCamera, com.jme3.scene.Spatial, com.jme3.environment.generation.JobProgressListener)} - * with a {@link JobProgressListener} to be notified of the progress of the generation process. + * with a {@link JobProgressListener} to be notified of the progress of the + * generation process. * - * The generation will be split in several threads for faster generation. + * The generation will be split in several threads for faster generation. * - * This class is entirely thread safe and can be called from any thread. + * This class is entirely thread safe and can be called from any thread. * * Note that in case you are using a {@link JobProgressListener}, all its - * methods will be called inside an app.enqueue callable. - * This means that it's completely safe to modify the scenegraph within the - * Listener method, but also means that the event will be delayed until next update loop. + * methods will be called inside an app.enqueue callable. This means that it's + * completely safe to modify the scenegraph within the Listener method, but also + * means that the event will be delayed until next update loop. * + * @deprecated Use LightProbeFactory2 or EnvironmentProbeControl whenever possible. * @see EnvironmentCamera * @author bouquet */ + +@Deprecated public class LightProbeFactory { /** 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 6e28d1cbed..04fb73d875 100644 --- a/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory2.java +++ b/jme3-core/src/main/java/com/jme3/environment/LightProbeFactory2.java @@ -41,10 +41,24 @@ import com.jme3.scene.Spatial; import com.jme3.texture.Image.Format; - +/** + * A faster version of LightProbeFactory that uses accelerated Baking. + * @author Riccardo Balbo + */ public class LightProbeFactory2 { + /** + * Creates a LightProbe with the giver EnvironmentCamera in the given scene. + * @param rm The RenderManager + * @param am The AssetManager + * @param size The size of the probe + * @param pos The position of the probe + * @param frustumNear The near frustum of the probe + * @param frustumFar The far frustum of the probe + * @param scene The scene to bake + * @return The baked LightProbe + */ public static LightProbe makeProbe(RenderManager rm, AssetManager am, int size,Vector3f pos, float frustumNear,float frustumFar,Spatial scene) { IBLGLEnvBakerLight baker=new IBLGLEnvBakerLight(rm, 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 7df8f68d23..f41646536e 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 @@ -8,12 +8,31 @@ import com.jme3.texture.TextureCubeMap; /** - * And environment baker. It bakes the environment. ( ͡° ͜ʖ ͡°) + * And environment baker. It bakes the environment. * * @author Riccardo Balbo */ public interface EnvBaker { - public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar,Function filter); + /** + * Bake the environment + * @param scene The scene to bake + * @param position The position of the camera + * @param frustumNear The near frustum + * @param frustumFar The far frustum + * @param filter A filter to select which geometries to bake + */ + public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar, Function filter); + + /** + * Get the environment map + * @return The environment map + */ public TextureCubeMap getEnvMap(); + + /** + * Clean the environment baker + * This method should be called when the baker is no longer needed + * It will clean up all the resources + */ 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 4046f7534e..72e4e0301a 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 @@ -1,16 +1,43 @@ +/* + * Copyright (c) 2009-2023 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 com.jme3.environment.baker; -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; import com.jme3.math.ColorRGBA; import com.jme3.math.Quaternion; -import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; import com.jme3.renderer.Camera; import com.jme3.renderer.RenderManager; @@ -18,6 +45,7 @@ import com.jme3.scene.Geometry; import com.jme3.scene.Spatial; import com.jme3.texture.FrameBuffer; +import com.jme3.texture.FrameBuffer.FrameBufferTarget; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture.MagFilter; import com.jme3.texture.Texture.MinFilter; @@ -26,113 +54,102 @@ import com.jme3.texture.image.ColorSpace; import com.jme3.util.BufferUtils; - /** * Render the environment into a cubemap * * @author Riccardo Balbo */ -public abstract class GenericEnvBaker implements EnvBaker{ - - protected static Vector3f[] axisX=new Vector3f[6]; - protected static Vector3f[] axisY=new Vector3f[6]; - protected static Vector3f[] axisZ=new Vector3f[6]; - static{ - //PositiveX axis(left, up, direction) - axisX[0]=Vector3f.UNIT_Z.mult(1.0F); - axisY[0]=Vector3f.UNIT_Y.mult(-1.0F); - axisZ[0]=Vector3f.UNIT_X.mult(1.0F); - //NegativeX - axisX[1]=Vector3f.UNIT_Z.mult(-1.0F); - axisY[1]=Vector3f.UNIT_Y.mult(-1.0F); - axisZ[1]=Vector3f.UNIT_X.mult(-1.0F); - //PositiveY - axisX[2]=Vector3f.UNIT_X.mult(-1.0F); - axisY[2]=Vector3f.UNIT_Z.mult(1.0F); - axisZ[2]=Vector3f.UNIT_Y.mult(1.0F); - //NegativeY - axisX[3]=Vector3f.UNIT_X.mult(-1.0F); - axisY[3]=Vector3f.UNIT_Z.mult(-1.0F); - axisZ[3]=Vector3f.UNIT_Y.mult(-1.0F); - //PositiveZ - axisX[4]=Vector3f.UNIT_X.mult(-1.0F); - axisY[4]=Vector3f.UNIT_Y.mult(-1.0F); - axisZ[4]=Vector3f.UNIT_Z; - //NegativeZ - axisX[5]=Vector3f.UNIT_X.mult(1.0F); - axisY[5]=Vector3f.UNIT_Y.mult(-1.0F); - axisZ[5]=Vector3f.UNIT_Z.mult(-1.0F); +public abstract class GenericEnvBaker implements EnvBaker { + + protected static Vector3f[] axisX = new Vector3f[6]; + protected static Vector3f[] axisY = new Vector3f[6]; + protected static Vector3f[] axisZ = new Vector3f[6]; + static { + // PositiveX axis(left, up, direction) + axisX[0] = Vector3f.UNIT_Z.mult(1.0F); + axisY[0] = Vector3f.UNIT_Y.mult(-1.0F); + axisZ[0] = Vector3f.UNIT_X.mult(1.0F); + // NegativeX + axisX[1] = Vector3f.UNIT_Z.mult(-1.0F); + axisY[1] = Vector3f.UNIT_Y.mult(-1.0F); + axisZ[1] = Vector3f.UNIT_X.mult(-1.0F); + // PositiveY + axisX[2] = Vector3f.UNIT_X.mult(-1.0F); + axisY[2] = Vector3f.UNIT_Z.mult(1.0F); + axisZ[2] = Vector3f.UNIT_Y.mult(1.0F); + // NegativeY + axisX[3] = Vector3f.UNIT_X.mult(-1.0F); + axisY[3] = Vector3f.UNIT_Z.mult(-1.0F); + axisZ[3] = Vector3f.UNIT_Y.mult(-1.0F); + // PositiveZ + axisX[4] = Vector3f.UNIT_X.mult(-1.0F); + axisY[4] = Vector3f.UNIT_Y.mult(-1.0F); + axisZ[4] = Vector3f.UNIT_Z; + // NegativeZ + axisX[5] = Vector3f.UNIT_X.mult(1.0F); + axisY[5] = Vector3f.UNIT_Y.mult(-1.0F); + axisZ[5] = Vector3f.UNIT_Z.mult(-1.0F); } protected TextureCubeMap env; protected Format depthFormat; - protected final RenderManager renderManager; protected final AssetManager assetManager; protected final Camera cam; protected final boolean copyToRam; + public GenericEnvBaker(RenderManager rm, AssetManager am, Format colorFormat, Format depthFormat, int env_size, boolean copyToRam) { + this.copyToRam = copyToRam; + this.depthFormat = depthFormat; - public GenericEnvBaker( - RenderManager rm, - AssetManager am, - Format colorFormat, - Format depthFormat, - int env_size, - boolean copyToRam - ){ - this.copyToRam=copyToRam; - this.depthFormat=depthFormat; - - renderManager=rm; - assetManager=am; + renderManager = rm; + assetManager = am; + cam = new Camera(128, 128); - cam=new Camera(128,128); - - env=new TextureCubeMap(env_size,env_size,colorFormat); + env = new TextureCubeMap(env_size, env_size, colorFormat); env.setMagFilter(MagFilter.Bilinear); env.setMinFilter(MinFilter.BilinearNoMipMaps); env.setWrap(WrapMode.EdgeClamp); env.getImage().setColorSpace(ColorSpace.Linear); } - public TextureCubeMap getEnvMap(){ + public TextureCubeMap getEnvMap() { return env; - } + } - Camera getCam(int id,int w,int h,Vector3f position,float frustumNear,float frustumFar){ - cam.resize(w,h,false); + Camera getCam(int id, int w, int h, Vector3f position, float frustumNear, float frustumFar) { + cam.resize(w, h, false); cam.setLocation(position); - cam.setFrustumPerspective(90.0F,1F,frustumNear,frustumFar); - cam.setRotation(new Quaternion().fromAxes(axisX[id],axisY[id],axisZ[id])); + cam.setFrustumPerspective(90.0F, 1F, frustumNear, frustumFar); + cam.setRotation(new Quaternion().fromAxes(axisX[id], axisY[id], axisZ[id])); return cam; - } + } @Override - public void clean(){ + public void clean() { env.getImage().dispose(); System.gc(); - System.gc(); + System.gc(); } - @Override - 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)); + 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(FrameBufferTarget.newTarget(depthFormat)); envbaker.setSrgb(false); - for(int i=0;i<6;i++) envbaker.addColorTarget(FrameBuffer.newTarget(env).face(TextureCubeMap.Face.values()[i])); - - for(int i=0;i<6;i++){ + for (int i = 0; i < 6; i++) envbaker.addColorTarget(FrameBufferTarget.newTarget(env).face(TextureCubeMap.Face.values()[i])); + + for (int i = 0; i < 6; i++) { envbaker.setTargetIndex(i); - ViewPort viewPort=new ViewPort("EnvBaker",getCam(i,envbaker.getWidth(),envbaker.getHeight(),position,frustumNear,frustumFar)); - viewPort.setClearFlags(true,true,true); + ViewPort viewPort = new ViewPort("EnvBaker", getCam(i, envbaker.getWidth(), envbaker.getHeight(), position, frustumNear, frustumFar)); + viewPort.setClearFlags(true, true, true); viewPort.setBackgroundColor(ColorRGBA.Pink); - + viewPort.setOutputFrameBuffer(envbaker); viewPort.clearScenes(); viewPort.attachScene(scene); @@ -140,32 +157,25 @@ public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNea scene.updateLogicalState(0); scene.updateModelBound(); scene.updateGeometricState(); - - Function ofilter= renderManager.getRenderFilter(); + + Function ofilter = renderManager.getRenderFilter(); renderManager.setRenderFilter(filter); - renderManager.renderViewPort(viewPort,0.16f); + renderManager.renderViewPort(viewPort, 0.16f); renderManager.setRenderFilter(ofilter); - if(copyToRam){ - ByteBuffer face=BufferUtils.createByteBuffer( - ( - env.getImage().getWidth()*env.getImage().getHeight()*( - env.getImage().getFormat().getBitsPerPixel()/8 - ) - ) - ); - renderManager.getRenderer().readFrameBufferWithFormat(envbaker, face,env.getImage().getFormat()); + if (copyToRam) { + ByteBuffer face = BufferUtils.createByteBuffer((env.getImage().getWidth() * env.getImage().getHeight() * (env.getImage().getFormat().getBitsPerPixel() / 8))); + renderManager.getRenderer().readFrameBufferWithFormat(envbaker, face, env.getImage().getFormat()); face.rewind(); - env.getImage().setData(i,face); + env.getImage().setData(i, face); } } env.getImage().clearUpdateNeeded(); - + envbaker.dispose(); } - } \ No newline at end of file diff --git a/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBaker.java b/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBaker.java index cef731dcd7..36de5c35bc 100644 --- a/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBaker.java +++ b/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBaker.java @@ -1,3 +1,36 @@ +/* + * Copyright (c) 2009-2023 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 com.jme3.environment.baker; import com.jme3.texture.Texture2D; @@ -8,12 +41,32 @@ * * @author Riccardo Balbo */ -public interface IBLEnvBaker extends EnvBaker{ +public interface IBLEnvBaker extends EnvBaker { + /** + * Generate the BRDF texture + * @return The BRDF texture + */ public Texture2D genBRTF() ; + /** + * Bake the irradiance map + */ public void bakeIrradiance(); + + /** + * Bake the specular IBL map + */ public void bakeSpecularIBL() ; + /** + * Get the specular IBL map + * @return The specular IBL map + */ public TextureCubeMap getSpecularIBL(); + + /** + * Get the irradiance map + * @return The irradiance map + */ public TextureCubeMap getIrradiance(); } \ No newline at end of file diff --git a/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBakerLight.java b/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBakerLight.java index 7949714300..76b2fdeb2c 100644 --- a/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBakerLight.java +++ b/jme3-core/src/main/java/com/jme3/environment/baker/IBLEnvBakerLight.java @@ -1,3 +1,36 @@ +/* + * Copyright (c) 2009-2023 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 com.jme3.environment.baker; import com.jme3.math.Vector3f; @@ -5,10 +38,10 @@ /** * An environment baker for IBL, that uses spherical harmonics for irradiance. - * * @author Riccardo Balbo */ -public interface IBLEnvBakerLight extends EnvBaker{ +public interface IBLEnvBakerLight extends EnvBaker { + public void bakeSpecularIBL(); public void bakeSphericalHarmonicsCoefficients(); diff --git a/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBaker.java b/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBaker.java index 83de93808c..0cabed25ec 100644 --- a/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBaker.java +++ b/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBaker.java @@ -1,7 +1,39 @@ +/* + * Copyright (c) 2009-2023 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 com.jme3.environment.baker; import com.jme3.asset.AssetManager; -import com.jme3.environment.baker.IBLEnvBaker; import com.jme3.material.Material; import com.jme3.math.FastMath; import com.jme3.math.Vector3f; @@ -16,12 +48,13 @@ import com.jme3.texture.Texture.WrapMode; import com.jme3.texture.Texture2D; import com.jme3.texture.TextureCubeMap; +import com.jme3.texture.FrameBuffer.FrameBufferTarget; import com.jme3.texture.image.ColorSpace; import com.jme3.ui.Picture; /** - * An env baker for IBL that runs on the GPU + * An env baker for IBL that runs entirely on the GPU * * @author Riccardo Balbo */ @@ -92,7 +125,7 @@ public void bakeSpecularIBL() { FrameBuffer specularbaker=new FrameBuffer(mipWidth,mipHeight,1); specularbaker.setSrgb(false); - for(int i=0;i<6;i++)specularbaker.addColorTarget( FrameBuffer.newTarget(specular).level(mip).face(i) ); + for(int i=0;i<6;i++)specularbaker.addColorTarget( FrameBufferTarget.newTarget(specular).level(mip).face(i) ); float roughness=(float)mip/(float)(specular.getImage().getMipMapSizes().length-1); mat.setFloat("Roughness",roughness); @@ -122,7 +155,7 @@ public Texture2D genBRTF() { FrameBuffer brtfbaker=new FrameBuffer(brtf.getImage().getWidth(),brtf.getImage().getHeight(),1); brtfbaker.setSrgb(false); - brtfbaker.addColorTarget(FrameBuffer.newTarget(brtf)); + brtfbaker.addColorTarget(FrameBufferTarget.newTarget(brtf)); Camera envcam=getCam(0,brtf.getImage().getWidth(),brtf.getImage().getHeight(),Vector3f.ZERO,1,1000); @@ -152,7 +185,7 @@ public void bakeIrradiance() { FrameBuffer irradiancebaker=new FrameBuffer(irradiance.getImage().getWidth(),irradiance.getImage().getHeight(),1); irradiancebaker.setSrgb(false); - for(int i=0;i<6;i++) irradiancebaker.addColorTarget(FrameBuffer.newTarget(irradiance).face(TextureCubeMap.Face.values()[i])); + for(int i=0;i<6;i++) irradiancebaker.addColorTarget(FrameBufferTarget.newTarget(irradiance).face(TextureCubeMap.Face.values()[i])); Material mat=new Material(assetManager,"Common/IBL/IBLKernels.j3md"); mat.setBoolean("UseIrradiance",true); diff --git a/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBakerLight.java b/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBakerLight.java index 718a12f669..a436448ff4 100644 --- a/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBakerLight.java +++ b/jme3-core/src/main/java/com/jme3/environment/baker/IBLGLEnvBakerLight.java @@ -1,7 +1,38 @@ +/* + * Copyright (c) 2009-2023 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 com.jme3.environment.baker; import com.jme3.asset.AssetManager; -import com.jme3.environment.baker.IBLEnvBakerLight; import com.jme3.environment.util.EnvMapUtils; import com.jme3.material.Material; import com.jme3.math.FastMath; @@ -11,6 +42,7 @@ import com.jme3.scene.shape.Box; import com.jme3.texture.FrameBuffer; import com.jme3.texture.TextureCubeMap; +import com.jme3.texture.FrameBuffer.FrameBufferTarget; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture.MagFilter; import com.jme3.texture.Texture.MinFilter; @@ -18,87 +50,86 @@ import com.jme3.texture.image.ColorSpace; /** - * An env baker for IBL that runs on the GPU + * An env baker for IBL that bakes the specular map on the GPU and uses + * spherical harmonics for the irradiance map. + * + * This is lighter on VRAM but uses the CPU to compute the irradiance map. * * @author Riccardo Balbo */ -public class IBLGLEnvBakerLight extends GenericEnvBaker implements IBLEnvBakerLight{ +public class IBLGLEnvBakerLight extends GenericEnvBaker implements IBLEnvBakerLight { protected TextureCubeMap specular; protected Vector3f[] shCoef; - public IBLGLEnvBakerLight(RenderManager rm,AssetManager am, - Format format, - Format depthFormat, - int env_size, - int specular_size - - ){ - super(rm,am,format,depthFormat,env_size,true); - - specular=new TextureCubeMap(specular_size,specular_size,format); + + public IBLGLEnvBakerLight(RenderManager rm, AssetManager am, Format format, Format depthFormat, int env_size, int specular_size + + ) { + super(rm, am, format, depthFormat, env_size, true); + + specular = new TextureCubeMap(specular_size, specular_size, format); specular.setMagFilter(MagFilter.Bilinear); specular.setMinFilter(MinFilter.BilinearNoMipMaps); specular.setWrap(WrapMode.EdgeClamp); specular.getImage().setColorSpace(ColorSpace.Linear); - int nbMipMaps=(int)(Math.log(specular_size)/Math.log(2)+1); - if(nbMipMaps>6)nbMipMaps=6; - int[] sizes=new int[nbMipMaps]; - for(int i=0;i 6) nbMipMaps = 6; + int[] sizes = new int[nbMipMaps]; + for (int i = 0; i < nbMipMaps; i++) { + int size = (int) FastMath.pow(2, nbMipMaps - 1 - i); + sizes[i] = size * size * (specular.getImage().getFormat().getBitsPerPixel() / 8); } specular.getImage().setMipMapSizes(sizes); - } - + } @Override public void bakeSpecularIBL() { - Box boxm=new Box(1,1,1); - Geometry screen=new Geometry("BakeBox",boxm); - - Material mat=new Material(assetManager,"Common/IBL/IBLKernels.j3md"); - mat.setBoolean("UseSpecularIBL",true); - mat.setTexture("EnvMap",env); + Box boxm = new Box(1, 1, 1); + Geometry screen = new Geometry("BakeBox", boxm); + + Material mat = new Material(assetManager, "Common/IBL/IBLKernels.j3md"); + mat.setBoolean("UseSpecularIBL", true); + mat.setTexture("EnvMap", env); screen.setMaterial(mat); - - for(int mip=0;mip