Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2024 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 org.jmonkeyengine.screenshottests.light.pbr;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.BaseAppState;
import com.jme3.asset.AssetManager;
import com.jme3.environment.EnvironmentCamera;
import com.jme3.environment.FastLightProbeFactory;
import com.jme3.light.DirectionalLight;
import com.jme3.light.LightProbe;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.ToneMapFilter;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.texture.plugins.ktx.KTXLoader;
import com.jme3.util.SkyFactory;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
import org.jmonkeyengine.screenshottests.testframework.ScreenshotTestBase;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

/**
* Screenshot tests for PBR lighting.
*
* @author nehon - original test
* @author Richard Tingle (aka richtea) - screenshot test adaptation
*
*/
public class TestPBRLighting extends ScreenshotTestBase {

private static Stream<Arguments> testParameters() {
return Stream.of(
Arguments.of("LowRoughness", 0.1f, false),
Arguments.of("HighRoughness", 1.0f, false),
Arguments.of("DefaultDirectionalLight", 0.5f, false),
Arguments.of("UpdatedDirectionalLight", 0.5f, true)
);
}

/**
* Test PBR lighting with different parameters
*
* @param testName The name of the test (used for screenshot filename)
* @param roughness The roughness value to use
* @param updateLight Whether to update the directional light to match camera direction
*/
@ParameterizedTest(name = "{0}")
@MethodSource("testParameters")
public void testPBRLighting(String testName, float roughness, boolean updateLight, TestInfo testInfo) {

if(!testInfo.getTestClass().isPresent() || !testInfo.getTestMethod().isPresent()) {
throw new RuntimeException("Test preconditions not met");
}

String imageName = testInfo.getTestClass().get().getName() + "." + testInfo.getTestMethod().get().getName() + "_" + testName;

screenshotTest(new BaseAppState() {
private static final int RESOLUTION = 256;

private Node modelNode;
private int frame = 0;

@Override
protected void initialize(Application app) {
Camera cam = app.getCamera();
cam.setLocation(new Vector3f(18, 10, 0));
cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);

AssetManager assetManager = app.getAssetManager();
assetManager.registerLoader(KTXLoader.class, "ktx");

app.getViewPort().setBackgroundColor(ColorRGBA.White);

modelNode = new Node("modelNode");
Geometry model = (Geometry) assetManager.loadModel("Models/Tank/tank.j3o");
MikktspaceTangentGenerator.generate(model);
modelNode.attachChild(model);

DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
SimpleApplication simpleApp = (SimpleApplication) app;
simpleApp.getRootNode().addLight(dl);
dl.setColor(ColorRGBA.White);

// If we need to update the light direction to match camera
if (updateLight) {
dl.setDirection(app.getCamera().getDirection().normalize());
}

simpleApp.getRootNode().attachChild(modelNode);

FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
int numSamples = app.getContext().getSettings().getSamples();
if (numSamples > 0) {
fpp.setNumSamples(numSamples);
}

fpp.addFilter(new ToneMapFilter(Vector3f.UNIT_XYZ.mult(4.0f)));
app.getViewPort().addProcessor(fpp);

Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Path.hdr", SkyFactory.EnvMapType.EquirectMap);
simpleApp.getRootNode().attachChild(sky);

Material pbrMat = assetManager.loadMaterial("Models/Tank/tank.j3m");
pbrMat.setFloat("Roughness", roughness);
model.setMaterial(pbrMat);

// Set up environment camera
EnvironmentCamera envCam = new EnvironmentCamera(RESOLUTION, new Vector3f(0, 3f, 0));
app.getStateManager().attach(envCam);
}

@Override
protected void cleanup(Application app) {}

@Override
protected void onEnable() {}

@Override
protected void onDisable() {}

@Override
public void update(float tpf) {
frame++;

if (frame == 2) {
modelNode.removeFromParent();
LightProbe probe;

SimpleApplication simpleApp = (SimpleApplication) getApplication();
probe = FastLightProbeFactory.makeProbe(simpleApp.getRenderManager(),
simpleApp.getAssetManager(),
RESOLUTION,
Vector3f.ZERO,
1f,
1000f,
simpleApp.getRootNode());

probe.getArea().setRadius(100);
simpleApp.getRootNode().addLight(probe);
}

if (frame > 10 && modelNode.getParent() == null) {
SimpleApplication simpleApp = (SimpleApplication) getApplication();
simpleApp.getRootNode().attachChild(modelNode);
}
}
}).setBaseImageFileName(imageName)
.setFramesToTakeScreenshotsOn(12)
.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2024 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 org.jmonkeyengine.screenshottests.light.pbr;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.BaseAppState;
import com.jme3.asset.AssetManager;
import com.jme3.environment.EnvironmentProbeControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.util.SkyFactory;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
import org.jmonkeyengine.screenshottests.testframework.ScreenshotTestBase;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

/**
* A simpler PBR example that uses EnvironmentProbeControl to bake the environment
*
* @author Richard Tingle (aka richtea) - screenshot test adaptation
*/
public class TestPBRSimple extends ScreenshotTestBase {

private static Stream<Arguments> testParameters() {
return Stream.of(
Arguments.of("WithRealtimeBaking", true),
Arguments.of("WithoutRealtimeBaking", false)
);
}

/**
* Test PBR simple with different parameters
*
* @param testName The name of the test (used for screenshot filename)
* @param realtimeBaking Whether to use realtime baking
*/
@ParameterizedTest(name = "{0}")
@MethodSource("testParameters")
public void testPBRSimple(String testName, boolean realtimeBaking, TestInfo testInfo) {
if(!testInfo.getTestClass().isPresent() || !testInfo.getTestMethod().isPresent()) {
throw new RuntimeException("Test preconditions not met");
}

String imageName = testInfo.getTestClass().get().getName() + "." + testInfo.getTestMethod().get().getName() + "_" + testName;

screenshotTest(new BaseAppState() {
private int frame = 0;

@Override
protected void initialize(Application app) {
Camera cam = app.getCamera();
cam.setLocation(new Vector3f(18, 10, 0));
cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);

AssetManager assetManager = app.getAssetManager();
SimpleApplication simpleApp = (SimpleApplication) app;

// Create the tank model
Geometry model = (Geometry) assetManager.loadModel("Models/Tank/tank.j3o");
MikktspaceTangentGenerator.generate(model);

Material pbrMat = assetManager.loadMaterial("Models/Tank/tank.j3m");
model.setMaterial(pbrMat);
simpleApp.getRootNode().attachChild(model);

// Create sky
Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Path.hdr", SkyFactory.EnvMapType.EquirectMap);
simpleApp.getRootNode().attachChild(sky);

// Create baker control
EnvironmentProbeControl envProbe = new EnvironmentProbeControl(assetManager, 256);
simpleApp.getRootNode().addControl(envProbe);

// Tag the sky, only the tagged spatials will be rendered in the env map
envProbe.tag(sky);
}

@Override
protected void cleanup(Application app) {}

@Override
protected void onEnable() {}

@Override
protected void onDisable() {}

@Override
public void update(float tpf) {
if (realtimeBaking) {
frame++;
if (frame == 2) {
SimpleApplication simpleApp = (SimpleApplication) getApplication();
simpleApp.getRootNode().getControl(EnvironmentProbeControl.class).rebake();
}
}
}
}).setBaseImageFileName(imageName)
.setFramesToTakeScreenshotsOn(10)
.run();
}
}
Loading