com.jme3.texture.TextureCubeMap Java Examples

The following examples show how to use com.jme3.texture.TextureCubeMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LightProbeFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Updates a LightProbe with the given EnvironmentCamera in the given scene.
 * <p>
 * Note that this is an asynchronous process that will run on multiple threads.
 * The process is thread safe.
 * The created lightProbe will only be marked as ready when the rendering process is done.
 * <p>
 * The JobProgressListener will be notified of the progress of the generation.
 * Note that you can also use a {@link JobProgressAdapter}.
 *
 * @param probe    the Light probe to update
 * @param envCam   the EnvironmentCamera
 * @param scene    the Scene
 * @param genType  Fast or HighQuality. Fast may be ok for many types of environment, but you may need high quality when an environment map has very high lighting values.
 * @param listener the listener of the generation progress.
 * @return the created LightProbe
 * @see LightProbe
 * @see EnvironmentCamera
 * @see JobProgressListener
 */
public static LightProbe updateProbe(final LightProbe probe, final EnvironmentCamera envCam, Spatial scene, final EnvMapUtils.GenerationType genType, final JobProgressListener<LightProbe> listener) {
    
    envCam.setPosition(probe.getPosition());
    
    probe.setReady(false);

    if (probe.getPrefilteredEnvMap() != null) {
        probe.getPrefilteredEnvMap().getImage().dispose();
    }

    probe.setPrefilteredMap(EnvMapUtils.createPrefilteredEnvMap(envCam.getSize(), envCam.getImageFormat()));

    envCam.snapshot(scene, new JobProgressAdapter<TextureCubeMap>() {

        @Override
        public void done(TextureCubeMap map) {
            generatePbrMaps(map, probe, envCam.getApplication(), genType, listener);
        }
    });
    return probe;
}
 
Example #2
Source File: LightProbeFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Internally called to generate the maps.
 * This method will spawn 7 thread (one for the Irradiance spherical harmonics generator, and one for each face of the prefiltered env map).
 * Those threads will be executed in a ScheduledThreadPoolExecutor that will be shutdown when the generation is done.
 *
 * @param envMap the raw env map rendered by the env camera
 * @param probe the LightProbe to generate maps for
 * @param app the Application
 * @param listener a progress listener. (can be null if no progress reporting is needed)
 */
private static void generatePbrMaps(TextureCubeMap envMap, final LightProbe probe, Application app, EnvMapUtils.GenerationType genType, final JobProgressListener<LightProbe> listener) {
    IrradianceSphericalHarmonicsGenerator irrShGenerator;
    PrefilteredEnvMapFaceGenerator[] pemGenerators = new PrefilteredEnvMapFaceGenerator[6];

    final JobState jobState = new JobState(new ScheduledThreadPoolExecutor(7));

    irrShGenerator = new IrradianceSphericalHarmonicsGenerator(app, new JobListener(listener, jobState, probe, 6));
    int size = envMap.getImage().getWidth();
    irrShGenerator.setGenerationParam(EnvMapUtils.duplicateCubeMap(envMap), probe);

    jobState.executor.execute(irrShGenerator);

    for (int i = 0; i < pemGenerators.length; i++) {
        pemGenerators[i] = new PrefilteredEnvMapFaceGenerator(app, i, new JobListener(listener, jobState, probe, i));
        pemGenerators[i].setGenerationParam(EnvMapUtils.duplicateCubeMap(envMap), size, EnvMapUtils.FixSeamsMethod.None, genType, probe.getPrefilteredEnvMap());
        jobState.executor.execute(pemGenerators[i]);
    }
}
 
Example #3
Source File: CubeMapWrapper.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a CubeMapWrapper for the given cube map
 * Note that the cube map must be initialized, and the mipmaps sizes should 
 * be set if relevant for them to be readable/writable
 * @param cubeMap the cubemap to wrap.
 */
public CubeMapWrapper(TextureCubeMap cubeMap) {
    image = cubeMap.getImage();
    if (image.hasMipmaps()) {
        int nbMipMaps = image.getMipMapSizes().length;
        sizes = new int[nbMipMaps];
        mipMapRaster = new MipMapImageRaster(image, 0);

        for (int i = 0; i < nbMipMaps; i++) {
            sizes[i] = Math.max(1, image.getWidth() >> i);
        }
    } else {
        sizes = new int[1];
        sizes[0] = image.getWidth();
    }
    raster = new DefaultImageRaster(image, 0,0 , false);
}
 
Example #4
Source File: SkyFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a cube-mapped sky using six textures.
 *
 * @param assetManager from which to load materials
 * @param west texture for the western face of the cube
 * @param east texture for the eastern face of the cube
 * @param north texture for the northern face of the cube
 * @param south texture for the southern face of the cube
 * @param up texture for the top face of the cube
 * @param down texture for the bottom face of the cube
 * @param normalScale The normal scale is multiplied by the 3D normal to get
 * a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and
 * transformation to the normal.
 * @param sphereRadius the sky sphere's radius: for the sky to be visible,
 * its radius must fall between the near and far planes of the camera's
 * frustum
 * @return a new spatial representing the sky, ready to be attached to the
 * scene graph
 */
public static Spatial createSky(AssetManager assetManager, Texture west,
        Texture east, Texture north, Texture south, Texture up,
        Texture down, Vector3f normalScale, float sphereRadius) {

    Image westImg = west.getImage();
    Image eastImg = east.getImage();
    Image northImg = north.getImage();
    Image southImg = south.getImage();
    Image upImg = up.getImage();
    Image downImg = down.getImage();

    checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg);

    Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null, westImg.getColorSpace());

    cubeImage.addData(westImg.getData(0));
    cubeImage.addData(eastImg.getData(0));
    cubeImage.addData(downImg.getData(0));
    cubeImage.addData(upImg.getData(0));
    cubeImage.addData(southImg.getData(0));
    cubeImage.addData(northImg.getData(0));
    
    TextureCubeMap cubeMap = new TextureCubeMap(cubeImage);
    return createSky(assetManager, cubeMap, normalScale, EnvMapType.CubeMap, sphereRadius);
}
 
Example #5
Source File: WelcomeScreen.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void setupSkyBox() {
    Mesh sphere = new Sphere(32, 32, 10f);
    sphere.setStatic();
    skyBox = new Geometry("SkyBox", sphere);
    skyBox.setQueueBucket(Bucket.Sky);
    skyBox.setShadowMode(ShadowMode.Off);

    Image cube = SceneApplication.getApplication().getAssetManager().loadTexture("Textures/blue-glow-1024.dds").getImage();
    TextureCubeMap cubemap = new TextureCubeMap(cube);

    Material mat = new Material(SceneApplication.getApplication().getAssetManager(), "Common/MatDefs/Misc/Sky.j3md");
    mat.setBoolean("SphereMap", false);
    mat.setTexture("Texture", cubemap);
    mat.setVector3("NormalScale", new Vector3f(1, 1, 1));
    skyBox.setMaterial(mat);

    ((Node) SceneApplication.getApplication().getViewPort().getScenes().get(0)).attachChild(skyBox);
}
 
Example #6
Source File: LightProbe.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);

    prefilteredEnvMap = (TextureCubeMap) ic.readSavable("prefilteredEnvMap", null);
    position = (Vector3f) ic.readSavable("position", null);
    area = (ProbeArea)ic.readSavable("area", null);
    if(area == null) {
        // retro compat
        BoundingSphere bounds = (BoundingSphere) ic.readSavable("bounds", new BoundingSphere(1.0f, Vector3f.ZERO));
        area = new SphereProbeArea(bounds.getCenter(), bounds.getRadius());
    }
    area.setCenter(position);
    nbMipMaps = ic.readInt("nbMipMaps", 0);
    ready = ic.readBoolean("ready", false);

    Savable[] coeffs = ic.readSavableArray("shCoeffs", null);
    if (coeffs == null) {
        ready = false;
        logger.log(Level.WARNING, "LightProbe is missing parameters, it should be recomputed. Please use lightProbeFactory.updateProbe()");
    } else {
        shCoeffs = new Vector3f[coeffs.length];
        for (int i = 0; i < coeffs.length; i++) {
            shCoeffs[i] = (Vector3f) coeffs[i];
        }
    }
}
 
Example #7
Source File: TestRenderToCubemap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Texture setupOffscreenView(){
    Camera offCamera = new Camera(512, 512);
 
    offView = renderManager.createPreView("Offscreen View", offCamera);
    offView.setClearFlags(true, true, true);
    offView.setBackgroundColor(ColorRGBA.DarkGray);
 
    // create offscreen framebuffer
    FrameBuffer offBuffer = new FrameBuffer(512, 512, 1);
 
    //setup framebuffer's cam
    offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
    offCamera.setLocation(new Vector3f(0f, 0f, -5f));
    offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
 
    //setup framebuffer's texture
    TextureCubeMap offTex = new TextureCubeMap(512, 512, Format.RGBA8);
    offTex.setMinFilter(Texture.MinFilter.Trilinear);
    offTex.setMagFilter(Texture.MagFilter.Bilinear);
 
    //setup framebuffer to use texture
    offBuffer.setDepthBuffer(Format.Depth);
    offBuffer.setMultiTarget(true);
    offBuffer.addColorTexture(offTex, TextureCubeMap.Face.NegativeX);
    offBuffer.addColorTexture(offTex, TextureCubeMap.Face.PositiveX);
    offBuffer.addColorTexture(offTex, TextureCubeMap.Face.NegativeY);
    offBuffer.addColorTexture(offTex, TextureCubeMap.Face.PositiveY);
    offBuffer.addColorTexture(offTex, TextureCubeMap.Face.NegativeZ);
    offBuffer.addColorTexture(offTex, TextureCubeMap.Face.PositiveZ);
    
    //set viewport to render to offscreen framebuffer
    offView.setOutputFrameBuffer(offBuffer);
 
    // setup framebuffer's scene
    Box boxMesh = new Box( 1,1,1);
    Material material = assetManager.loadMaterial("Interface/Logo/Logo.j3m");
    offBox = new Geometry("box", boxMesh);
    offBox.setMaterial(material);
 
    // attach the scene to the viewport to be rendered
    offView.attachScene(offBox);
 
    return offTex;
}
 
Example #8
Source File: PrefilteredEnvMapFaceGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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
 */
private TextureCubeMap generatePrefilteredEnvMap(TextureCubeMap sourceEnvMap, int targetMapSize, EnvMapUtils.FixSeamsMethod fixSeamsMethod, TextureCubeMap store) {
    try {
        TextureCubeMap pem = store;

        int nbMipMap = store.getImage().getMipMapSizes().length;

        setEnd(nbMipMap);

        if (!sourceEnvMap.getImage().hasMipmaps() || sourceEnvMap.getImage().getMipMapSizes().length < nbMipMap) {
            throw new IllegalArgumentException("The input cube map must have at least " + nbMipMap + "mip maps");
        }

        CubeMapWrapper sourceWrapper = new CubeMapWrapper(sourceEnvMap);
        CubeMapWrapper targetWrapper = new CubeMapWrapper(pem);

        Vector3f texelVect = new Vector3f();
        Vector3f color = new Vector3f();
        ColorRGBA outColor = new ColorRGBA();
        int targetMipMapSize = targetMapSize;
        for (int mipLevel = 0; mipLevel < nbMipMap; mipLevel++) {
            float roughness = getRoughnessFromMip(mipLevel, nbMipMap);
            int nbSamples = getSampleFromMip(mipLevel, nbMipMap);

            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);
                    prefilterEnvMapTexel(sourceWrapper, roughness, texelVect, nbSamples, mipLevel, color);

                    outColor.set(Math.max(color.x, 0.0001f), Math.max(color.y, 0.0001f), Math.max(color.z, 0.0001f), 1);
                    targetWrapper.setPixel(x, y, face, mipLevel, outColor);

                }
            }
            targetMipMapSize /= 2;
            progress();
        }

        return pem;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
 
Example #9
Source File: SkyFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down, Vector3f normalScale, int sphereRadius) {
    final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true);
    Geometry sky = new Geometry("Sky", sphereMesh);
    sky.setQueueBucket(Bucket.Sky);
    sky.setCullHint(Spatial.CullHint.Never);
    sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO));

    Image westImg = west.getImage();
    Image eastImg = east.getImage();
    Image northImg = north.getImage();
    Image southImg = south.getImage();
    Image upImg = up.getImage();
    Image downImg = down.getImage();

    checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg);

    Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null);

    cubeImage.addData(westImg.getData(0));
    cubeImage.addData(eastImg.getData(0));

    cubeImage.addData(downImg.getData(0));
    cubeImage.addData(upImg.getData(0));

    cubeImage.addData(southImg.getData(0));
    cubeImage.addData(northImg.getData(0));
    
    if (westImg.getEfficentData() != null){
        // also consilidate efficient data
        ArrayList<Object> efficientData = new ArrayList<Object>(6);
        efficientData.add(westImg.getEfficentData());
        efficientData.add(eastImg.getEfficentData());
        efficientData.add(downImg.getEfficentData());
        efficientData.add(upImg.getEfficentData());
        efficientData.add(southImg.getEfficentData());
        efficientData.add(northImg.getEfficentData());
        cubeImage.setEfficentData(efficientData);
    }

    TextureCubeMap cubeMap = new TextureCubeMap(cubeImage);
    cubeMap.setAnisotropicFilter(0);
    cubeMap.setMagFilter(Texture.MagFilter.Bilinear);
    cubeMap.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
    cubeMap.setWrap(Texture.WrapMode.EdgeClamp);

    Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md");
    skyMat.setTexture("Texture", cubeMap);
    skyMat.setVector3("NormalScale", normalScale);
    sky.setMaterial(skyMat);

    return sky;
}
 
Example #10
Source File: PrefilteredEnvMapFaceGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Fills all the generation parameters
 *
 * @param sourceMap      the source cube map
 * @param targetMapSize  the size of the generated map (width or height in
 *                       pixel)
 * @param fixSeamsMethod the method used to fix seams as described in
 *                       {@link com.jme3.environment.util.EnvMapUtils.FixSeamsMethod}
 * @param genType
 * @param store          The cube map to store the result in.
 */
public void setGenerationParam(TextureCubeMap sourceMap, int targetMapSize, EnvMapUtils.FixSeamsMethod fixSeamsMethod, EnvMapUtils.GenerationType genType, TextureCubeMap store) {
    this.sourceMap = sourceMap;
    this.targetMapSize = targetMapSize;
    this.fixSeamsMethod = fixSeamsMethod;
    this.store = store;
    this.genType = genType;
    init();
}
 
Example #11
Source File: IrradianceSphericalHarmonicsGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Fills all the generation parameters
 *
 * @param sourceMap the source cube map
 *                  {@link com.jme3.environment.util.EnvMapUtils.FixSeamsMethod}
 * @param store     The cube map to store the result in.
 */
public void setGenerationParam(TextureCubeMap sourceMap, LightProbe store) {
    this.sourceMap = sourceMap;

    this.store = store;
    reset();
}
 
Example #12
Source File: LightProbeFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Creates a LightProbe with the giver EnvironmentCamera in the given scene.
 * 
 * Note that this is an asynchronous process that will run on multiple threads.
 * The process is thread safe.
 * The created lightProbe will only be marked as ready when the rendering process is done.
 *      
 * The JobProgressListener will be notified of the progress of the generation. 
 * Note that you can also use a {@link JobProgressAdapter}. 
 * 
 * @see LightProbe
 * @see EnvironmentCamera
 * @see JobProgressListener
 
 * @param envCam the EnvironmentCamera
 * @param scene the Scene
 * @param genType Fast or HighQuality. Fast may be ok for many types of environment, but you may need high quality when an environment map has very high lighting values.
 * @param listener the listener of the generation progress.
 * @return the created LightProbe
 */
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.setPrefilteredMap(EnvMapUtils.createPrefilteredEnvMap(envCam.getSize(), envCam.getImageFormat()));
    envCam.snapshot(scene, new JobProgressAdapter<TextureCubeMap>() {

        @Override
        public void done(TextureCubeMap map) {
            generatePbrMaps(map, probe, envCam.getApplication(), genType, listener);
        }
    });
    return probe;
}
 
Example #13
Source File: LightProbe.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * returns the prefiltered environment map texture of this light probe
 * Note that this Texture may not have image data yet if the LightProbe is not ready
 * @return the prefiltered environment map
 */
public TextureCubeMap getPrefilteredEnvMap() {
    return prefilteredEnvMap;
}
 
Example #14
Source File: LightProbe.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Sets the prefiltered environment map
 * @param prefileteredEnvMap the prefiltered environment map
 */
public void setPrefilteredMap(TextureCubeMap prefileteredEnvMap) {
    this.prefilteredEnvMap = prefileteredEnvMap;
}