|
@@ -29,107 +29,141 @@
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
*/
|
|
-
|
|
|
|
package com.jme3.environment.baker;
|
|
package com.jme3.environment.baker;
|
|
|
|
|
|
|
|
+import java.nio.ByteBuffer;
|
|
|
|
+import java.util.logging.Logger;
|
|
|
|
+
|
|
import com.jme3.asset.AssetManager;
|
|
import com.jme3.asset.AssetManager;
|
|
-import com.jme3.environment.util.EnvMapUtils;
|
|
|
|
import com.jme3.material.Material;
|
|
import com.jme3.material.Material;
|
|
|
|
+import com.jme3.math.ColorRGBA;
|
|
import com.jme3.math.FastMath;
|
|
import com.jme3.math.FastMath;
|
|
|
|
+import com.jme3.math.Vector2f;
|
|
import com.jme3.math.Vector3f;
|
|
import com.jme3.math.Vector3f;
|
|
|
|
+import com.jme3.renderer.Caps;
|
|
import com.jme3.renderer.RenderManager;
|
|
import com.jme3.renderer.RenderManager;
|
|
import com.jme3.scene.Geometry;
|
|
import com.jme3.scene.Geometry;
|
|
import com.jme3.scene.shape.Box;
|
|
import com.jme3.scene.shape.Box;
|
|
import com.jme3.texture.FrameBuffer;
|
|
import com.jme3.texture.FrameBuffer;
|
|
-import com.jme3.texture.TextureCubeMap;
|
|
|
|
|
|
+import com.jme3.texture.Image;
|
|
|
|
+import com.jme3.texture.Texture2D;
|
|
import com.jme3.texture.FrameBuffer.FrameBufferTarget;
|
|
import com.jme3.texture.FrameBuffer.FrameBufferTarget;
|
|
import com.jme3.texture.Image.Format;
|
|
import com.jme3.texture.Image.Format;
|
|
-import com.jme3.texture.Texture.MagFilter;
|
|
|
|
-import com.jme3.texture.Texture.MinFilter;
|
|
|
|
-import com.jme3.texture.Texture.WrapMode;
|
|
|
|
import com.jme3.texture.image.ColorSpace;
|
|
import com.jme3.texture.image.ColorSpace;
|
|
|
|
+import com.jme3.texture.image.ImageRaster;
|
|
|
|
+import com.jme3.util.BufferUtils;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * An env baker for IBL that bakes the specular map on the GPU and uses
|
|
|
|
- * spherical harmonics for the irradiance map.
|
|
|
|
|
|
+ * Fully accelerated env baker for IBL that bakes the specular map and spherical harmonics
|
|
|
|
+ * on the GPU.
|
|
*
|
|
*
|
|
- * This is lighter on VRAM but uses the CPU to compute the irradiance map.
|
|
|
|
|
|
+ * This is lighter on VRAM but it is not as parallelized as IBLGLEnvBaker
|
|
*
|
|
*
|
|
* @author Riccardo Balbo
|
|
* @author Riccardo Balbo
|
|
*/
|
|
*/
|
|
-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);
|
|
|
|
- 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 < nbMipMaps; i++) {
|
|
|
|
- int size = (int) FastMath.pow(2, nbMipMaps - 1 - i);
|
|
|
|
- sizes[i] = size * size * (specular.getImage().getFormat().getBitsPerPixel() / 8);
|
|
|
|
- }
|
|
|
|
- specular.getImage().setMipMapSizes(sizes);
|
|
|
|
|
|
+public class IBLGLEnvBakerLight extends IBLHybridEnvBakerLight {
|
|
|
|
+ public final static int NUM_SH_COEFFICIENT = 9;
|
|
|
|
+ private static final Logger LOG = Logger.getLogger(IBLGLEnvBakerLight.class.getName());
|
|
|
|
+
|
|
|
|
+ public IBLGLEnvBakerLight(RenderManager rm, AssetManager am, Format format, Format depthFormat, int env_size, int specular_size) {
|
|
|
|
+ super(rm, am, format, depthFormat, env_size, specular_size);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isTexturePulling() {
|
|
|
|
+ return this.texturePulling;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void bakeSpecularIBL() {
|
|
|
|
|
|
+ public void bakeSphericalHarmonicsCoefficients() {
|
|
Box boxm = new Box(1, 1, 1);
|
|
Box boxm = new Box(1, 1, 1);
|
|
Geometry screen = new Geometry("BakeBox", boxm);
|
|
Geometry screen = new Geometry("BakeBox", boxm);
|
|
|
|
|
|
- Material mat = new Material(assetManager, "Common/IBL/IBLKernels.j3md");
|
|
|
|
- mat.setBoolean("UseSpecularIBL", true);
|
|
|
|
- mat.setTexture("EnvMap", env);
|
|
|
|
|
|
+ Material mat = new Material(assetManager, "Common/IBLSphH/IBLSphH.j3md");
|
|
|
|
+ mat.setTexture("Texture", env);
|
|
|
|
+ mat.setVector2("Resolution", new Vector2f(env.getImage().getWidth(), env.getImage().getHeight()));
|
|
screen.setMaterial(mat);
|
|
screen.setMaterial(mat);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ float remapMaxValue = 0;
|
|
|
|
+ Format format = Format.RGBA32F;
|
|
|
|
+ if (!renderManager.getRenderer().getCaps().contains(Caps.FloatTexture)) {
|
|
|
|
+ LOG.warning("Float textures not supported, using RGB8 instead. This may cause accuracy issues.");
|
|
|
|
+ format = Format.RGBA8;
|
|
|
|
+ remapMaxValue = 0.05f;
|
|
|
|
+ }
|
|
|
|
|
|
- for (int mip = 0; mip < specular.getImage().getMipMapSizes().length; mip++) {
|
|
|
|
- int mipWidth = (int) (specular.getImage().getWidth() * FastMath.pow(0.5f, mip));
|
|
|
|
- int mipHeight = (int) (specular.getImage().getHeight() * FastMath.pow(0.5f, mip));
|
|
|
|
|
|
+
|
|
|
|
+ if (remapMaxValue > 0) {
|
|
|
|
+ mat.setFloat("RemapMaxValue", remapMaxValue);
|
|
|
|
+ } else {
|
|
|
|
+ mat.clearParam("RemapMaxValue");
|
|
|
|
+ }
|
|
|
|
|
|
- FrameBuffer specularbaker = new FrameBuffer(mipWidth, mipHeight, 1);
|
|
|
|
- specularbaker.setSrgb(false);
|
|
|
|
- for (int i = 0; i < 6; i++) specularbaker.addColorTarget(FrameBufferTarget.newTarget(specular).level(mip).face(i));
|
|
|
|
|
|
+ Texture2D shCoefTx[] = {
|
|
|
|
+ new Texture2D(NUM_SH_COEFFICIENT, 1, 1, format),
|
|
|
|
+ new Texture2D(NUM_SH_COEFFICIENT, 1, 1, format)
|
|
|
|
+ };
|
|
|
|
|
|
- float roughness = (float) mip / (float) (specular.getImage().getMipMapSizes().length - 1);
|
|
|
|
- mat.setFloat("Roughness", roughness);
|
|
|
|
|
|
|
|
- for (int i = 0; i < 6; i++) {
|
|
|
|
- specularbaker.setTargetIndex(i);
|
|
|
|
- mat.setInt("FaceId", i);
|
|
|
|
|
|
+ FrameBuffer shbaker = new FrameBuffer(NUM_SH_COEFFICIENT, 1, 1);
|
|
|
|
+ shbaker.setSrgb(false);
|
|
|
|
+ shbaker.addColorTarget(FrameBufferTarget.newTarget(shCoefTx[0]));
|
|
|
|
+ shbaker.addColorTarget(FrameBufferTarget.newTarget(shCoefTx[1]));
|
|
|
|
|
|
- screen.updateLogicalState(0);
|
|
|
|
- screen.updateGeometricState();
|
|
|
|
|
|
+ int renderOnT = -1;
|
|
|
|
|
|
- renderManager.setCamera(getCam(i, specularbaker.getWidth(), specularbaker.getHeight(), Vector3f.ZERO, 1, 1000), false);
|
|
|
|
- renderManager.getRenderer().setFrameBuffer(specularbaker);
|
|
|
|
- renderManager.renderGeometry(screen);
|
|
|
|
|
|
+ for (int faceId = 0; faceId < 6; faceId++) {
|
|
|
|
+ if (renderOnT != -1) {
|
|
|
|
+ int s = renderOnT;
|
|
|
|
+ renderOnT = renderOnT == 0 ? 1 : 0;
|
|
|
|
+ mat.setTexture("ShCoef", shCoefTx[s]);
|
|
|
|
+ mat.setInt("FaceId", faceId);
|
|
|
|
+ } else {
|
|
|
|
+ renderOnT = 0;
|
|
}
|
|
}
|
|
- specularbaker.dispose();
|
|
|
|
|
|
+
|
|
|
|
+ screen.updateLogicalState(0);
|
|
|
|
+ screen.updateGeometricState();
|
|
|
|
+
|
|
|
|
+ shbaker.setTargetIndex(renderOnT);
|
|
|
|
+
|
|
|
|
+ renderManager.setCamera(getCam(0, shbaker.getWidth(), shbaker.getHeight(), Vector3f.ZERO, 1, 1000), false);
|
|
|
|
+ renderManager.getRenderer().setFrameBuffer(shbaker);
|
|
|
|
+ renderManager.renderGeometry(screen);
|
|
}
|
|
}
|
|
- specular.setMinFilter(MinFilter.Trilinear);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
- public TextureCubeMap getSpecularIBL() {
|
|
|
|
- return specular;
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ ByteBuffer shCoefRaw = BufferUtils.createByteBuffer(
|
|
|
|
+ NUM_SH_COEFFICIENT * 1 * ( shbaker.getColorTarget().getFormat().getBitsPerPixel() / 8)
|
|
|
|
+ );
|
|
|
|
+ renderManager.getRenderer().readFrameBufferWithFormat(shbaker, shCoefRaw, shbaker.getColorTarget().getFormat());
|
|
|
|
+ shCoefRaw.rewind();
|
|
|
|
+
|
|
|
|
+ Image img = new Image(format, NUM_SH_COEFFICIENT, 1, shCoefRaw, ColorSpace.Linear);
|
|
|
|
+ ImageRaster imgr=ImageRaster.create(img);
|
|
|
|
+
|
|
|
|
+ shCoef = new Vector3f[NUM_SH_COEFFICIENT];
|
|
|
|
+ float weightAccum = 0.0f;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < shCoef.length; i++) {
|
|
|
|
+ ColorRGBA c = imgr.getPixel(i, 0);
|
|
|
|
+ shCoef[i] = new Vector3f(c.r, c.g, c.b);
|
|
|
|
+ if (weightAccum == 0) weightAccum = c.a;
|
|
|
|
+ else if (weightAccum != c.a) {
|
|
|
|
+ LOG.warning("SH weight is not uniform, this may cause issues.");
|
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
|
- public void bakeSphericalHarmonicsCoefficients() {
|
|
|
|
- shCoef = EnvMapUtils.getSphericalHarmonicsCoefficents(getEnvMap());
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (remapMaxValue > 0) weightAccum /= remapMaxValue;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < NUM_SH_COEFFICIENT; ++i) {
|
|
|
|
+ if (remapMaxValue > 0) shCoef[i].divideLocal(remapMaxValue);
|
|
|
|
+ shCoef[i].multLocal(4.0f * FastMath.PI / weightAccum);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ img.dispose();
|
|
|
|
|
|
- @Override
|
|
|
|
- public Vector3f[] getSphericalHarmonicsCoefficients() {
|
|
|
|
- return shCoef;
|
|
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|