Search in sources :

Example 1 with CubeMapWrapper

use of com.jme3.environment.util.CubeMapWrapper in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method createPrefilteredEnvMap.

/**
     * initialize the pem map
     * @param size the size of the map
     * @param imageFormat the format of the image
     * @return the initialized prefiltered env map
     */
public static TextureCubeMap createPrefilteredEnvMap(int size, Image.Format imageFormat) {
    TextureCubeMap pem = new TextureCubeMap(size, size, imageFormat);
    pem.setMagFilter(Texture.MagFilter.Bilinear);
    pem.setMinFilter(Texture.MinFilter.Trilinear);
    pem.getImage().setColorSpace(ColorSpace.Linear);
    int nbMipMap = (int) (Math.log(size) / Math.log(2) - 1);
    CubeMapWrapper targetWrapper = new CubeMapWrapper(pem);
    targetWrapper.initMipMaps(nbMipMap);
    return pem;
}
Also used : TextureCubeMap(com.jme3.texture.TextureCubeMap)

Example 2 with CubeMapWrapper

use of com.jme3.environment.util.CubeMapWrapper in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method generateIrradianceMap.

/**
     * Generates the Irradiance map (used for image based difuse lighting) from
     * Spherical Harmonics coefficients previously computed with
     * {@link EnvMapUtils#getSphericalHarmonicsCoefficents(com.jme3.texture.TextureCubeMap)}
     * Note that the output cube map is in RGBA8 format.
     *
     * @param shCoeffs the SH coeffs
     * @param targetMapSize the size of the irradiance map to generate
     * @param fixSeamsMethod the method to fix seams
     * @param store
     * @return The irradiance cube map for the given coefficients
     */
public static TextureCubeMap generateIrradianceMap(Vector3f[] shCoeffs, int targetMapSize, FixSeamsMethod fixSeamsMethod, TextureCubeMap store) {
    TextureCubeMap irrCubeMap = store;
    if (irrCubeMap == null) {
        irrCubeMap = new TextureCubeMap(targetMapSize, targetMapSize, Image.Format.RGB16F);
        irrCubeMap.setMagFilter(Texture.MagFilter.Bilinear);
        irrCubeMap.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
        irrCubeMap.getImage().setColorSpace(ColorSpace.Linear);
    }
    for (int i = 0; i < 6; i++) {
        ByteBuffer buf = BufferUtils.createByteBuffer(targetMapSize * targetMapSize * irrCubeMap.getImage().getFormat().getBitsPerPixel() / 8);
        irrCubeMap.getImage().setData(i, buf);
    }
    Vector3f texelVect = new Vector3f();
    ColorRGBA color = new ColorRGBA(ColorRGBA.Black);
    float[] shDir = new float[9];
    CubeMapWrapper envMapWriter = new CubeMapWrapper(irrCubeMap);
    for (int face = 0; face < 6; face++) {
        for (int y = 0; y < targetMapSize; y++) {
            for (int x = 0; x < targetMapSize; x++) {
                getVectorFromCubemapFaceTexCoord(x, y, targetMapSize, face, texelVect, fixSeamsMethod);
                evalShBasis(texelVect, shDir);
                color.set(0, 0, 0, 0);
                for (int i = 0; i < NUM_SH_COEFFICIENT; i++) {
                    color.set(color.r + shCoeffs[i].x * shDir[i] * shBandFactor[i], color.g + shCoeffs[i].y * shDir[i] * shBandFactor[i], color.b + shCoeffs[i].z * shDir[i] * shBandFactor[i], 1.0f);
                }
                //clamping the color because very low value close to zero produce artifacts
                color.r = Math.max(0.0001f, color.r);
                color.g = Math.max(0.0001f, color.g);
                color.b = Math.max(0.0001f, color.b);
                envMapWriter.setPixel(x, y, face, color);
            }
        }
    }
    return irrCubeMap;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) Vector3f(com.jme3.math.Vector3f) ByteBuffer(java.nio.ByteBuffer)

Example 3 with CubeMapWrapper

use of com.jme3.environment.util.CubeMapWrapper in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method prefilterEnvMapTexel.

private static Vector3f prefilterEnvMapTexel(CubeMapWrapper envMapReader, float roughness, Vector3f N, int numSamples, Vector3f store) {
    Vector3f prefilteredColor = store;
    float totalWeight = 0.0f;
    TempVars vars = TempVars.get();
    Vector4f Xi = vars.vect4f1;
    Vector3f H = vars.vect1;
    Vector3f tmp = vars.vect2;
    ColorRGBA c = vars.color;
    // a = roughness² and a2 = a²
    float a2 = roughness * roughness;
    a2 *= a2;
    a2 *= 10;
    for (int i = 0; i < numSamples; i++) {
        Xi = getHammersleyPoint(i, numSamples, Xi);
        H = importanceSampleGGX(Xi, a2, N, H, vars);
        H.normalizeLocal();
        tmp.set(H);
        float NoH = N.dot(tmp);
        Vector3f L = tmp.multLocal(NoH * 2).subtractLocal(N);
        float NoL = clamp(N.dot(L), 0.0f, 1.0f);
        if (NoL > 0) {
            envMapReader.getPixel(L, c);
            prefilteredColor.setX(prefilteredColor.x + c.r * NoL);
            prefilteredColor.setY(prefilteredColor.y + c.g * NoL);
            prefilteredColor.setZ(prefilteredColor.z + c.b * NoL);
            totalWeight += NoL;
        }
    }
    vars.release();
    return prefilteredColor.divideLocal(totalWeight);
}
Also used : Vector4f(com.jme3.math.Vector4f) ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 4 with CubeMapWrapper

use of com.jme3.environment.util.CubeMapWrapper in project jmonkeyengine by jMonkeyEngine.

the class PrefilteredEnvMapFaceGenerator method prefilterEnvMapTexel.

private Vector3f prefilterEnvMapTexel(CubeMapWrapper envMapReader, float roughness, Vector3f N, int numSamples, Vector3f store) {
    Vector3f prefilteredColor = store;
    float totalWeight = 0.0f;
    // a = roughness² and a2 = a²
    float a2 = roughness * roughness;
    a2 *= a2;
    a2 *= 10;
    for (int i = 0; i < numSamples; i++) {
        Xi = getHammersleyPoint(i, numSamples, Xi);
        H = importanceSampleGGX(Xi, a2, N, H);
        H.normalizeLocal();
        tmp.set(H);
        float NoH = N.dot(tmp);
        Vector3f L = tmp.multLocal(NoH * 2).subtractLocal(N);
        float NoL = clamp(N.dot(L), 0.0f, 1.0f);
        if (NoL > 0) {
            envMapReader.getPixel(L, c);
            prefilteredColor.setX(prefilteredColor.x + c.r * NoL);
            prefilteredColor.setY(prefilteredColor.y + c.g * NoL);
            prefilteredColor.setZ(prefilteredColor.z + c.b * NoL);
            totalWeight += NoL;
        }
    }
    return prefilteredColor.divideLocal(totalWeight);
}
Also used : Vector3f(com.jme3.math.Vector3f) EnvMapUtils.getHammersleyPoint(com.jme3.environment.util.EnvMapUtils.getHammersleyPoint)

Example 5 with CubeMapWrapper

use of com.jme3.environment.util.CubeMapWrapper in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method generatePrefilteredEnvMap.

/**
     * Generates the prefiltered env map (used for image based specular
     * lighting) With the GGX/Shlick brdf
     * {@link EnvMapUtils#getSphericalHarmonicsCoefficents(com.jme3.texture.TextureCubeMap)}
     * Note that the output cube map is in RGBA8 format.
     *
     * @param sourceEnvMap
     * @param targetMapSize the size of the irradiance map to generate
     * @param store
     * @param fixSeamsMethod the method to fix seams
     * @return The irradiance cube map for the given coefficients
     */
public static TextureCubeMap generatePrefilteredEnvMap(TextureCubeMap sourceEnvMap, int targetMapSize, FixSeamsMethod fixSeamsMethod, TextureCubeMap store) {
    TextureCubeMap pem = store;
    if (pem == null) {
        pem = new TextureCubeMap(targetMapSize, targetMapSize, Image.Format.RGB16F);
        pem.setMagFilter(Texture.MagFilter.Bilinear);
        pem.setMinFilter(Texture.MinFilter.Trilinear);
        pem.getImage().setColorSpace(ColorSpace.Linear);
    }
    int nbMipMap = (int) (Math.log(targetMapSize) / Math.log(2) - 1);
    CubeMapWrapper sourceWrapper = new CubeMapWrapper(sourceEnvMap);
    CubeMapWrapper targetWrapper = new CubeMapWrapper(pem);
    targetWrapper.initMipMaps(nbMipMap);
    Vector3f texelVect = new Vector3f();
    Vector3f color = new Vector3f();
    ColorRGBA outColor = new ColorRGBA();
    for (int mipLevel = 0; mipLevel < nbMipMap; mipLevel++) {
        System.err.println("mip level " + mipLevel);
        float roughness = getRoughnessFromMip(mipLevel, nbMipMap);
        int nbSamples = getSampleFromMip(mipLevel, nbMipMap);
        int targetMipMapSize = (int) pow(2, nbMipMap + 1 - mipLevel);
        for (int face = 0; face < 6; face++) {
            System.err.println("face " + face);
            for (int y = 0; y < targetMipMapSize; y++) {
                for (int x = 0; x < targetMipMapSize; x++) {
                    color.set(0, 0, 0);
                    getVectorFromCubemapFaceTexCoord(x, y, targetMipMapSize, face, texelVect, FixSeamsMethod.Wrap);
                    prefilterEnvMapTexel(sourceWrapper, roughness, texelVect, nbSamples, color);
                    outColor.set(color.x, color.y, color.z, 1.0f);
                    // System.err.println("coords " + x + "," + y);
                    targetWrapper.setPixel(x, y, face, mipLevel, outColor);
                }
            }
        }
    }
    return pem;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) Vector3f(com.jme3.math.Vector3f)

Aggregations

Vector3f (com.jme3.math.Vector3f)7 ColorRGBA (com.jme3.math.ColorRGBA)6 TextureCubeMap (com.jme3.texture.TextureCubeMap)5 CubeMapWrapper (com.jme3.environment.util.CubeMapWrapper)2 EnvMapUtils.getHammersleyPoint (com.jme3.environment.util.EnvMapUtils.getHammersleyPoint)2 ByteBuffer (java.nio.ByteBuffer)2 Vector4f (com.jme3.math.Vector4f)1 TempVars (com.jme3.util.TempVars)1