Skip to content
Draft
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
Expand Up @@ -37,6 +37,7 @@
import com.jme3.environment.util.EnvMapUtils;
import com.jme3.light.LightProbe;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.*;
import com.jme3.scene.Spatial;
Expand Down Expand Up @@ -104,6 +105,7 @@ public class EnvironmentCamera extends BaseAppState {
protected ByteBuffer[] buffers;

protected Vector3f position = new Vector3f();
protected Quaternion rotation = new Quaternion();
protected ColorRGBA backGroundColor;

/**
Expand Down Expand Up @@ -270,6 +272,37 @@ public void setPosition(final Vector3f position) {
}
}

/**
* Returns the orientation of the captured cubemap in world space.
*
* @return the rotation (not a copy, do not modify)
*/
public Quaternion getRotation() {
return rotation;
}

/**
* Sets the orientation of the captured cubemap in world space, so the snapshot suits a {@link LightProbe} with
* that rotation.
*
* @param rotation the world-space rotation (not null, unaffected)
*/
public void setRotation(final Quaternion rotation) {
this.rotation.set(rotation);

if (viewports == null) {
return;
}

for (int i = 0; i < viewports.length; i++) {
viewports[i].getCamera().setRotation(faceRotation(axisX[i], axisY[i], axisZ[i]));
}
}

private Quaternion faceRotation(Vector3f axisX, Vector3f axisY, Vector3f axisZ) {
return rotation.mult(new Quaternion().fromAxes(axisX, axisY, axisZ));
}

/**
* Returns an array of the 6 ViewPorts used to capture the snapshot.
* Note that this will be null until after initialize() is called.
Expand Down Expand Up @@ -373,7 +406,7 @@ protected Camera createOffCamera(final int mapSize, final Vector3f worldPos,
final Vector3f axisX, final Vector3f axisY, final Vector3f axisZ) {
final Camera offCamera = new Camera(mapSize, mapSize);
offCamera.setLocation(worldPos);
offCamera.setAxes(axisX, axisY, axisZ);
offCamera.setRotation(faceRotation(axisX, axisY, axisZ));
offCamera.setFrustumPerspective(90f, 1f, 0.1f, 1000);
offCamera.setLocation(position);
return offCamera;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void rebakeNow(RenderManager renderManager) {
baker.setSphericalHarmonicsMode(sphericalHarmonicsMode);

baker.setTexturePulling(isRequiredSavableResults());
baker.bakeEnvironment(spatial, getPosition(), frustumNear, frustumFar, filter);
baker.bakeEnvironment(spatial, getPosition(), getRotation(), frustumNear, frustumFar, filter);
baker.bakeSpecularIBL();
baker.bakeSphericalHarmonicsCoefficients();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static LightProbe makeProbe(final EnvironmentCamera envCam, Spatial scene
public static LightProbe makeProbe(final EnvironmentCamera envCam, Spatial scene, final EnvMapUtils.GenerationType genType, final JobProgressListener<LightProbe> listener) {
final LightProbe probe = new LightProbe();
probe.setPosition(envCam.getPosition());
probe.setRotation(envCam.getRotation());
Renderer renderer = envCam.getApplication().getRenderManager().getRenderer();
probe.setPrefilteredMap(EnvMapUtils.createPrefilteredEnvMap(envCam.getSize(), envCam.getImageFormat(renderer)));
envCam.snapshot(scene, new JobProgressAdapter<TextureCubeMap>() {
Expand Down Expand Up @@ -163,7 +164,8 @@ public static LightProbe makeProbe(final EnvironmentCamera envCam, Spatial scene
public static LightProbe updateProbe(final LightProbe probe, final EnvironmentCamera envCam, Spatial scene, final EnvMapUtils.GenerationType genType, final JobProgressListener<LightProbe> listener) {

envCam.setPosition(probe.getPosition());

envCam.setRotation(probe.getRotation());

probe.setReady(false);

if (probe.getPrefilteredEnvMap() != null) {
Expand Down
15 changes: 15 additions & 0 deletions jme3-core/src/main/java/com/jme3/environment/baker/EnvBaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package com.jme3.environment.baker;

import java.util.function.Predicate;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
Expand Down Expand Up @@ -60,6 +61,20 @@ public interface EnvBaker {
*/
public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar, Predicate<Geometry> filter);

/**
* Bakes the environment as seen by a camera with the given orientation, so the cubemap is in that rotated space
* (as a rotated {@link com.jme3.light.LightProbe} expects). Bakers that do not support this only accept the
* identity rotation.
*
* @param rotation the orientation of the captured cubemap in world space
*/
public default void bakeEnvironment(Spatial scene, Vector3f position, Quaternion rotation, float frustumNear, float frustumFar, Predicate<Geometry> filter) {
if (!rotation.isIdentity()) {
throw new UnsupportedOperationException(getClass().getName() + " does not support baking with a rotation");
}
bakeEnvironment(scene, position, frustumNear, frustumFar, filter);
}

/**
* Gets the environment map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ private FrameBuffer[] getEnvBakers() {

@Override
public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear, float frustumFar, Predicate<Geometry> filter) {
bakeEnvironment(scene, position, Quaternion.IDENTITY, frustumNear, frustumFar, filter);
}

@Override
public void bakeEnvironment(Spatial scene, Vector3f position, Quaternion rotation, float frustumNear, float frustumFar, Predicate<Geometry> filter) {
FrameBuffer envbakers[] = getEnvBakers();

if (isTexturePulling()) {
Expand All @@ -216,7 +221,9 @@ public void bakeEnvironment(Spatial scene, Vector3f position, float frustumNear,
for (int i = 0; i < 6; i++) {
FrameBuffer envbaker = envbakers[i];

ViewPort viewPort = new ViewPort("EnvBaker", updateAndGetInternalCamera(i, envbaker.getWidth(), envbaker.getHeight(), position, frustumNear, frustumFar));
Camera faceCam = updateAndGetInternalCamera(i, envbaker.getWidth(), envbaker.getHeight(), position, frustumNear, frustumFar);
faceCam.setRotation(rotation.mult(faceCam.getRotation()));
ViewPort viewPort = new ViewPort("EnvBaker", faceCam);
viewPort.setClearFlags(true, true, true);
viewPort.setBackgroundColor(ColorRGBA.Pink);

Expand Down
82 changes: 82 additions & 0 deletions jme3-core/src/main/java/com/jme3/light/LightProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
* <p>
* The light probe has an area of effect centered on its position. It can have a Spherical area or an Oriented Box area
* <p>
* A light probe also has a rotation in world space: the orientation of its captured environment. Rotating a probe
* turns the lighting and reflections it gives, which is useful when the environment the probe was captured from is
* itself rotated (for example a probe captured from a sky that is later turned). Like the position, it can be kept in
* step with a spatial using a {@link com.jme3.scene.control.LightControl}.
* <p>
* A LightProbe will only be taken into account when it's marked as ready and enabled.
* A light probe is ready when it has valid environment map data set.
* Note that you should never call setReady yourself.
Expand All @@ -79,6 +84,8 @@ public class LightProbe extends Light implements Savable {
private ProbeArea area = new SphereProbeArea(Vector3f.ZERO, 1.0f);
private boolean ready = false;
private Vector3f position = new Vector3f();
private Quaternion rotation = new Quaternion();
private Matrix3f rotationMatrix = new Matrix3f();
private int nbMipMaps;

public enum AreaType{
Expand Down Expand Up @@ -163,6 +170,7 @@ public void write(JmeExporter ex) throws IOException {
oc.write(shCoefficients, "shCoeffs", null);
oc.write(prefilteredEnvMap, "prefilteredEnvMap", null);
oc.write(position, "position", null);
oc.write(rotation, "rotation", Quaternion.IDENTITY);
oc.write(area, "area", new SphereProbeArea(Vector3f.ZERO, 1.0f));
oc.write(ready, "ready", false);
oc.write(nbMipMaps, "nbMipMaps", 0);
Expand All @@ -182,6 +190,8 @@ public void read(JmeImporter im) throws IOException {
area = new SphereProbeArea(bounds.getCenter(), bounds.getRadius());
}
area.setCenter(position);
rotation = (Quaternion) ic.readSavable("rotation", new Quaternion());
rotation.toRotationMatrix(rotationMatrix);
nbMipMaps = ic.readInt("nbMipMaps", 0);
ready = ic.readBoolean("ready", false);

Expand Down Expand Up @@ -218,6 +228,7 @@ public void setAreaType(AreaType type){
break;
case OrientedBox:
area = new OrientedBoxProbeArea(new Transform());
((OrientedBoxProbeArea) area).setRotation(rotation);
break;
}
area.setCenter(position);
Expand Down Expand Up @@ -274,6 +285,48 @@ public void setPosition(Vector3f position) {
area.setCenter(position);
}

/**
* Returns the rotation of the LightProbe in world space: the orientation of its captured environment.
*
* @return the world-space rotation (not a copy, do not modify)
*/
public Quaternion getRotation() {
return rotation;
}

/**
* Sets the rotation of the LightProbe in world space. This turns the environment the probe lights with (both its
* irradiance and its prefiltered reflections). The probe is treated as a rigid whole, so an oriented box area is
* turned by the same change in rotation, just as {@link #setPosition(Vector3f)} moves the area. The box keeps any
* rotation it already had (for example a box aligned with the walls of a room that was not built on the world
* axes); the box's own rotation remains world space.
* <p>
* The bakers capture the environment in the probe's current rotation, so re-baking a rotated probe gives the same
* lighting as baking it unrotated and then rotating it.
*
* @param rotation the world-space rotation (not null, unaffected)
*/
public void setRotation(Quaternion rotation) {
if (area instanceof OrientedBoxProbeArea) {
OrientedBoxProbeArea box = (OrientedBoxProbeArea) area;
// carry the box through the same rigid turn (new * inverse(old)), keeping any rotation it already had
Quaternion boxRotation = rotation.mult(this.rotation.inverse()).multLocal(box.getRotation()).normalizeLocal();
box.setRotation(boxRotation);
}
this.rotation.set(rotation);
this.rotation.toRotationMatrix(rotationMatrix);
}

/**
* Returns the rotation to send to the shader, as a matrix taking directions from the probe's captured
* environment into world space.
*
* @return the pre-existing matrix
*/
public Matrix3f getUniformRotationMatrix() {
return rotationMatrix;
}

public int getNbMipMaps() {
return nbMipMaps;
}
Expand Down Expand Up @@ -312,11 +365,40 @@ public Type getType() {
return Type.Probe;
}

@Override
public LightProbe clone() {
LightProbe probe = (LightProbe) super.clone();
probe.position = position.clone();
probe.rotation = rotation.clone();
probe.rotationMatrix = rotationMatrix.clone();
probe.area = cloneArea(area);
return probe;
}

/**
* Copies the built-in area types; a custom {@link ProbeArea} cannot be copied through the interface, so it stays
* shared with the original.
*/
private static ProbeArea cloneArea(ProbeArea area) {
try {
if (area instanceof SphereProbeArea) {
return ((SphereProbeArea) area).clone();
}
if (area instanceof OrientedBoxProbeArea) {
return ((OrientedBoxProbeArea) area).clone();
}
} catch (CloneNotSupportedException ex) {
throw new AssertionError(ex);
}
return area;
}

@Override
public String toString() {
return getClass().getSimpleName()
+ "[name=" + name
+ ", position=" + position
+ ", rotation=" + rotation
+ ", area=" + area
+ ", enabled=" + enabled
+ "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ protected int updateLightListUniforms(Shader shader, Geometry g, LightList light
Uniform lightProbeData2 = shader.getUniform("g_LightProbeData2");
Uniform lightProbeData3 = shader.getUniform("g_LightProbeData3");

// Matrix3f
Uniform lightProbeRotation = shader.getUniform("g_LightProbeRotation");
Uniform lightProbeRotation2 = shader.getUniform("g_LightProbeRotation2");
Uniform lightProbeRotation3 = shader.getUniform("g_LightProbeRotation3");

Uniform shCoeffs = shader.getUniform("g_ShCoeffs");
Uniform lightProbePemMap = shader.getUniform("g_PrefEnvMap");
Uniform shCoeffs2 = shader.getUniform("g_ShCoeffs2");
Expand All @@ -153,14 +158,14 @@ protected int updateLightListUniforms(Shader shader, Geometry g, LightList light
//If there is a lightProbe in the list we force its render on the first pass
if (!lightProbes.isEmpty()) {
LightProbe lightProbe = lightProbes.get(0);
lastTexUnit = setProbeData(rm, lastTexUnit, lightProbeData, shCoeffs, lightProbePemMap, lightProbe);
lastTexUnit = setProbeData(rm, lastTexUnit, lightProbeData, lightProbeRotation, shCoeffs, lightProbePemMap, lightProbe);
if (lightProbes.size() > 1) {
lightProbe = lightProbes.get(1);
lastTexUnit = setProbeData(rm, lastTexUnit, lightProbeData2, shCoeffs2, lightProbePemMap2, lightProbe);
lastTexUnit = setProbeData(rm, lastTexUnit, lightProbeData2, lightProbeRotation2, shCoeffs2, lightProbePemMap2, lightProbe);
}
if (lightProbes.size() > 2) {
lightProbe = lightProbes.get(2);
setProbeData(rm, lastTexUnit, lightProbeData3, shCoeffs3, lightProbePemMap3, lightProbe);
setProbeData(rm, lastTexUnit, lightProbeData3, lightProbeRotation3, shCoeffs3, lightProbePemMap3, lightProbe);
}
} else {
//Disable IBL for this pass
Expand Down Expand Up @@ -244,9 +249,10 @@ protected int updateLightListUniforms(Shader shader, Geometry g, LightList light
return curIndex;
}

private int setProbeData(RenderManager rm, int lastTexUnit, Uniform lightProbeData, Uniform shCoeffs,
Uniform lightProbePemMap, LightProbe lightProbe) {
private int setProbeData(RenderManager rm, int lastTexUnit, Uniform lightProbeData, Uniform lightProbeRotation,
Uniform shCoeffs, Uniform lightProbePemMap, LightProbe lightProbe) {
lightProbeData.setValue(VarType.Matrix4, lightProbe.getUniformMatrix());
lightProbeRotation.setValue(VarType.Matrix3, lightProbe.getUniformRotationMatrix());
//setVector4InArray(lightProbe.getPosition().x, lightProbe.getPosition().y, lightProbe.getPosition().z, 1f / area.getRadius() + lightProbe.getNbMipMaps(), 0);
shCoeffs.setValue(VarType.Vector3Array, lightProbe.getShCoeffs());
/*
Expand Down
Loading
Loading