Skip to content

Commit

Permalink
fixed functional array issues
Browse files Browse the repository at this point in the history
  • Loading branch information
codex128 committed Jun 23, 2024
1 parent d2c5639 commit eb67e8e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public FrameGraphData() {
}
public FrameGraphData(FrameGraph fg, Collection<RenderPass> passes, Map<String, Object> settings) {
this.name = fg.getName();
this.passes = passes.toArray(RenderPass[]::new);
this.passes = passes.toArray(new RenderPass[0]);
this.settings = new HashMap<>();
for (String key : settings.keySet()) {
this.settings.put(key, new SavableObject(settings.get(key)));
Expand Down Expand Up @@ -103,7 +103,7 @@ public void write(JmeExporter ex) throws IOException {
OutputCapsule out = ex.getCapsule(this);
out.write(name, "name", DEF_NAME);
out.write(passes, "passes", DEF_PASSES);
out.write(list.toArray(SavablePassConnection[]::new), "connections", DEF_CONNECTIONS);
out.write(list.toArray(new SavablePassConnection[0]), "connections", DEF_CONNECTIONS);
out.writeStringSavableMap(settings, "settings", DEF_SETTINGS);
// reset export ids
for (RenderPass p : passes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,21 @@ public <T extends Texture> T acquireColorTarget(FrameBuffer fbo, ResourceTicket<
return replaceColorTarget(fbo, ticket, 0);
}
private <T extends Texture> T replaceColorTarget(FrameBuffer fbo, ResourceTicket<T> ticket, int i) {
Texture existing = fbo.getColorTarget(i).getTexture();
T acquired = acquire(ticket);
if (acquired != existing) {
fbo.setColorTarget(i, FrameBuffer.target(acquired));
fbo.setUpdateNeeded();
if (cap != null) cap.bindTexture(ticket.getWorldIndex(), ticket.getName());
textureBinds++;
if (i < fbo.getNumColorTargets()) {
Texture existing = fbo.getColorTarget(i).getTexture();
T acquired = acquire(ticket);
if (acquired != existing) {
fbo.setColorTarget(i, FrameBuffer.target(acquired));
fbo.setUpdateNeeded();
if (cap != null) cap.bindTexture(ticket.getWorldIndex(), ticket.getName());
textureBinds++;
}
return acquired;
} else {
T acquired = acquire(ticket);
fbo.addColorTarget(FrameBuffer.target(acquired));
return acquired;
}
return acquired;
}
/**
* Acquires and assigns a texture as the depth target to the framebuffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public class TiledRenderGrid implements Savable {

private int divisions = 4;
private int forcedTileSize = 128; //64
private int forcedTileSize = 64; //64
private int tileSize = 0;
private int gridWidth = 0;
private int gridHeight = 0;
Expand All @@ -55,6 +55,10 @@ public TiledRenderGrid() {}
public TiledRenderGrid(int divisions) {
this.divisions = divisions;
}
public TiledRenderGrid(int divisions, int forcedTileSize) {
this.divisions = divisions;
this.forcedTileSize = forcedTileSize;
}

public void update(Camera cam) {
tileSize = (forcedTileSize > 0 ? forcedTileSize : cam.getWidth()/divisions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.framegraph.DepthRange;
import com.jme3.renderer.framegraph.FGRenderContext;
import com.jme3.renderer.framegraph.FrameGraph;
import com.jme3.renderer.framegraph.ResourceTicket;
import com.jme3.renderer.framegraph.definitions.TextureDef;
import com.jme3.renderer.queue.GeometryList;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image;
Expand All @@ -55,32 +55,28 @@
*/
public class BucketPass extends RenderPass {

private Bucket bucket;
private final DepthRange depth = new DepthRange();
private int samples = 1;
private boolean flush = true;
private ResourceTicket<Texture2D> inColor, inDepth, outColor, outDepth;
private ResourceTicket<GeometryList> geometry;
private TextureDef<Texture2D> colorDef, depthDef;
private Texture2D depthTex;
private boolean perspective;

public BucketPass() {
this(Bucket.Opaque, DepthRange.IDENTITY);
this(DepthRange.IDENTITY, true);
}
public BucketPass(Bucket bucket) {
this(bucket, DepthRange.IDENTITY);
public BucketPass(DepthRange depth) {
this(depth, true);
}
public BucketPass(Bucket bucket, DepthRange depth) {
this.bucket = bucket;
public BucketPass(DepthRange depth, boolean perspective) {
this.depth.set(depth);
if (this.bucket == Bucket.Inherit) {
throw new IllegalArgumentException("Rendered bucket cannot be Inherit.");
}
this.perspective = perspective;
}

@Override
protected void initialize(FrameGraph frameGraph) {
inColor = addInput("Color");
inDepth = addInput("Depth");
geometry = addInput("Geometry");
outColor = addOutput("Color");
outDepth = addOutput("Depth");
colorDef = new TextureDef<>(Texture2D.class, img -> new Texture2D(img));
Expand All @@ -97,24 +93,24 @@ protected void prepare(FGRenderContext context) {
declare(colorDef, outColor);
declare(depthDef, outDepth);
reserve(outColor, outDepth);
reference(geometry);
referenceOptional(inColor, inDepth);
}
@Override
protected void execute(FGRenderContext context) {
FrameBuffer fb = getFrameBuffer(context, 1);
resources.acquireColorTargets(fb, outColor);
depthTex = resources.acquireDepthTarget(fb, outDepth);
System.out.println(fb.getDepthTarget());
resources.acquireColorTarget(fb, outColor);
resources.acquireDepthTarget(fb, outDepth);
context.getRenderer().setFrameBuffer(fb);
context.getRenderer().clearBuffers(true, true, true);
context.getRenderer().setBackgroundColor(ColorRGBA.BlackNoAlpha);
context.renderTextures(resources.acquireOrElse(inColor, null), resources.acquireOrElse(inDepth, null));
context.getRenderer().setDepthRange(depth);
if (bucket == Bucket.Gui) {
if (!perspective) {
context.getRenderManager().setCamera(context.getViewPort().getCamera(), true);
}
context.renderViewPortQueue(bucket, flush);
if (bucket == Bucket.Gui) {
context.renderGeometryList(resources.acquire(geometry), null, null);
if (!perspective) {
context.getRenderManager().setCamera(context.getViewPort().getCamera(), false);
}
}
Expand All @@ -123,29 +119,18 @@ protected void reset(FGRenderContext context) {}
@Override
protected void cleanup(FrameGraph frameGraph) {}
@Override
public String getProfilerName() {
return super.getProfilerName()+"["+bucket+"]";
}
@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
OutputCapsule out = ex.getCapsule(this);
out.write(bucket, "bucket", Bucket.Opaque);
out.write(depth, "depth", DepthRange.IDENTITY);
out.write(samples, "samples", 1);
out.write(flush, "flush", true);
out.write(perspective, "perspective", true);
}
@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule in = im.getCapsule(this);
bucket = in.readEnum("bucket", Bucket.class, Bucket.Opaque);
depth.set(in.readSavable("depth", DepthRange.class, DepthRange.IDENTITY));
samples = in.readInt("samples", 1);
flush = in.readBoolean("flush", true);
if (samples <= 0) {
throw new IllegalArgumentException("Samples must be greater than zero.");
}
perspective = in.readBoolean("perspective", true);
}

/**
Expand All @@ -166,9 +151,20 @@ public DepthRange getDepthRange() {
return depth;
}

@Override
public String toString() {
return "BucketPass["+bucket+"]";
/**
*
* @param perspective
*/
public void setPerspective(boolean perspective) {
this.perspective = perspective;
}

/**
*
* @return
*/
public boolean isPerspective() {
return perspective;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ void main(){
gl_FragColor.rgb += emissive;
gl_FragColor.a = alpha;

} /*else if (shadingModelId == SUBSURFACE_SCATTERING) {
} else if (shadingModelId == SUBSURFACE_SCATTERING) {
// TODO: implement subsurface scattering
}*/ else if (shadingModelId == UNLIT) {
} else if (shadingModelId == UNLIT) {

// unlit shading
gl_FragColor.rgb = shadingInfo.rgb;
Expand Down
121 changes: 62 additions & 59 deletions jme3-examples/src/main/java/jme3test/renderpath/sandbox/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
*/
package jme3test.renderpath.sandbox;

import com.jme3.app.DetailedProfilerState;
import com.jme3.app.SimpleApplication;
import com.jme3.environment.EnvironmentProbeControl;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.post.framegraph.CartoonEdgePass;
import com.jme3.renderer.framegraph.DepthRange;
import com.jme3.renderer.framegraph.FrameGraph;
import com.jme3.renderer.framegraph.FrameGraphFactory;
import com.jme3.renderer.framegraph.passes.BucketPass;
import com.jme3.renderer.framegraph.passes.OutputPass;
import com.jme3.renderer.framegraph.passes.SceneEnqueuePass;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.RectangleMesh;
import com.jme3.system.AppSettings;

/**
*
* @author codex
*/
public class Main extends SimpleApplication {

private final boolean pbr = true;


public static void main(String[] args) {
Main app = new Main();
AppSettings settings = new AppSettings(true);
Expand All @@ -40,66 +38,71 @@ public static void main(String[] args) {
@Override
public void simpleInitApp() {

//stateManager.attach(new DetailedProfilerState());
FrameGraph frameGraph = new FrameGraph(assetManager);

SceneEnqueuePass enqueue = frameGraph.add(new SceneEnqueuePass());
BucketPass opaque = frameGraph.add(new BucketPass());
BucketPass sky = frameGraph.add(new BucketPass(DepthRange.REAR));
BucketPass transparent = frameGraph.add(new BucketPass());
BucketPass gui = frameGraph.add(new BucketPass(DepthRange.FRONT, false));
BucketPass translucent = frameGraph.add(new BucketPass());
OutputPass output = frameGraph.add(new OutputPass());

opaque.makeInput(enqueue, "Opaque", "Geometry");

sky.makeInput(opaque, "Color", "Color");
sky.makeInput(opaque, "Depth", "Depth");
sky.makeInput(enqueue, "Sky", "Geometry");

FrameGraph deferred = FrameGraphFactory.deferred(assetManager, false);
deferred.setJunctionSetting("LightPackMethod", true);
//viewPort.setFrameGraph(deferred);
transparent.makeInput(sky, "Color", "Color");
transparent.makeInput(sky, "Depth", "Depth");
transparent.makeInput(enqueue, "Transparent", "Geometry");

// set camera move speed
flyCam.setMoveSpeed(30);
gui.makeInput(transparent, "Color", "Color");
gui.makeInput(transparent, "Depth", "Depth");
gui.makeInput(enqueue, "Gui", "Geometry");

translucent.makeInput(gui, "Color", "Color");
translucent.makeInput(gui, "Depth", "Depth");
translucent.makeInput(enqueue, "Translucent", "Geometry");

output.makeInput(translucent, "Color", "Color");
output.makeInput(translucent, "Depth", "Depth");

viewPort.setFrameGraph(frameGraph);

// setup camera
flyCam.setDragToRotate(true);
viewPort.setBackgroundColor(ColorRGBA.White.mult(0.05f));

// add a large floor to the scene
RectangleMesh floorMesh = new RectangleMesh(
new Vector3f(-100, 0, -100), new Vector3f(100, 0, -100), new Vector3f(-100, 0, 100));
floorMesh.flip();
Geometry floor = new Geometry("Floor", floorMesh);
floor.setLocalTranslation(0, -5, 0);
Material mat = createMaterial(ColorRGBA.White);
floor.setMaterial(mat);
rootNode.attachChild(floor);
flyCam.setMoveSpeed(20);

DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(1, -1, 1));
//rootNode.addLight(dl);
// setup background
viewPort.setBackgroundColor(ColorRGBA.White.mult(0.02f));

// add a lot of boxes to the scene
for (int i = 0; i < 500; i++) {
Geometry box = new Geometry("Box", new Box(1, 1, 1));
box.setLocalTranslation(FastMath.rand.nextFloat(-100, 100), 0, FastMath.rand.nextFloat(-100, 100));
box.setMaterial(createMaterial(ColorRGBA.White));
rootNode.attachChild(box);
EnvironmentProbeControl.tagGlobal(box);
}
// add a cube to the scene
Geometry cube = new Geometry("cube", new Box(1, 1, 1));
Material mat = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
mat.setColor("BaseColor", ColorRGBA.White);
mat.setFloat("Metallic", 0.5f);
cube.setMaterial(mat);
rootNode.attachChild(cube);

// add some lights to the scene
for (int i = 0; i < 100; i++) {
PointLight pl = new PointLight();
pl.setPosition(new Vector3f(FastMath.rand.nextFloat(-100, 100), 5, FastMath.rand.nextFloat(-100, 100)));
pl.setRadius(100);
pl.setColor(ColorRGBA.randomColor());
rootNode.addLight(pl);
}
// add a light to the scene
PointLight pl = new PointLight();
pl.setColor(ColorRGBA.White);
pl.setPosition(new Vector3f(2, 3, 3));
pl.setRadius(20);
rootNode.addLight(pl);

//rootNode.addControl(new EnvironmentProbeControl(assetManager, 256));
//rootNode.addLight(new AmbientLight(ColorRGBA.White.mult(0f)));
// profiler
DetailedProfilerState profiler = new DetailedProfilerState();
profiler.setEnabled(false);
stateManager.attach(profiler);

}

private Material createMaterial(ColorRGBA color) {
if (pbr) {
Material mat = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
mat.setColor("BaseColor", color);
mat.setFloat("Metallic", 1.0f);
return mat;
} else {
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat.setBoolean("UseMaterialColors", true);
mat.setColor("Diffuse", color);
return mat;
}
@Override
public void simpleUpdate(float tpf) {
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
}

}

0 comments on commit eb67e8e

Please sign in to comment.