Skip to content

Commit

Permalink
EnvironmentProbeControl, for simpler env baking
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Sep 11, 2019
1 parent c91b2dd commit ec01833
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<Geometry,Boolean> 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();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static LightProbe makeProbe(RenderManager rm,
am, Format.RGB16F, Format.Depth,
size, size);

baker.bakeEnvironment(scene,pos, frustumNear,frustumFar);
baker.bakeEnvironment(scene,pos, frustumNear,frustumFar,null);
baker.bakeSpecularIBL();
baker.bakeSphericalHarmonicsCoefficients();

Expand All @@ -72,7 +72,6 @@ public static LightProbe makeProbe(RenderManager rm,




/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,7 +13,7 @@
* @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<Geometry,Boolean> filter);
public TextureCubeMap getEnvMap();
public void clean();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -14,6 +15,7 @@
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;
Expand Down Expand Up @@ -117,7 +119,7 @@ public void clean(){


@Override
public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar) {
public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar,Function<Geometry,Boolean> filter) {
FrameBuffer envbaker=new FrameBuffer(env.getImage().getWidth(),env.getImage().getHeight(),1);
envbaker.setDepthTarget(FrameBuffer.newTarget(depthFormat));
envbaker.setSrgb(false);
Expand All @@ -138,8 +140,12 @@ public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNea
scene.updateLogicalState(0);
scene.updateModelBound();
scene.updateGeometricState();

Function<Geometry,Boolean> ofilter= renderManager.getRenderFilter();

renderManager.setRenderFilter(filter);
renderManager.renderViewPort(viewPort,0.16f);
renderManager.setRenderFilter(ofilter);

if(copyToRam){
ByteBuffer face=BufferUtils.createByteBuffer(
Expand Down
40 changes: 40 additions & 0 deletions jme3-examples/src/main/java/jme3test/light/pbr/TestPBRSimple.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit ec01833

Please sign in to comment.