Search in sources :

Example 6 with TextureCubeMap

use of com.jme3.texture.TextureCubeMap in project jmonkeyengine by jMonkeyEngine.

the class MatParam method getValueAsString.

/**
     * Returns the material parameter value as it would appear in a J3M
     * file. E.g.<br/>
     * <code>
     * MaterialParameters {<br/>
     *     ABC : 1 2 3 4<br/>
     * }<br/>
     * </code>
     * Assuming "ABC" is a Vector4 parameter, then the value
     * "1 2 3 4" would be returned by this method.
     * <br/><br/>
     * @return material parameter value as it would appear in a J3M file.
     */
public String getValueAsString() {
    switch(type) {
        case Boolean:
        case Float:
        case Int:
            return value.toString();
        case Vector2:
            Vector2f v2 = (Vector2f) value;
            return v2.getX() + " " + v2.getY();
        /* 
This may get used at a later point of time
When arrays can be inserted in J3M files

            case Vector2Array:
                Vector2f[] v2Arr = (Vector2f[]) value;
                String v2str = "";
                for (int i = 0; i < v2Arr.length ; i++) {
                    v2str += v2Arr[i].getX() + " " + v2Arr[i].getY() + "\n";
                }
                return v2str;
*/
        case Vector3:
            Vector3f v3 = (Vector3f) value;
            return v3.getX() + " " + v3.getY() + " " + v3.getZ();
        /*
            case Vector3Array:
                Vector3f[] v3Arr = (Vector3f[]) value;
                String v3str = "";
                for (int i = 0; i < v3Arr.length ; i++) {
                    v3str += v3Arr[i].getX() + " "
                            + v3Arr[i].getY() + " "
                            + v3Arr[i].getZ() + "\n";
                }
                return v3str;
            case Vector4Array:
                // can be either ColorRGBA, Vector4f or Quaternion
                if (value instanceof Vector4f) {
                    Vector4f[] v4arr = (Vector4f[]) value;
                    String v4str = "";
                    for (int i = 0; i < v4arr.length ; i++) {
                        v4str += v4arr[i].getX() + " "
                                + v4arr[i].getY() + " "
                                + v4arr[i].getZ() + " "
                                + v4arr[i].getW() + "\n";
                    }
                    return v4str;
                } else if (value instanceof ColorRGBA) {
                    ColorRGBA[] colorArr = (ColorRGBA[]) value;
                    String colStr = "";
                    for (int i = 0; i < colorArr.length ; i++) {
                        colStr += colorArr[i].getRed() + " "
                                + colorArr[i].getGreen() + " "
                                + colorArr[i].getBlue() + " "
                                + colorArr[i].getAlpha() + "\n";
                    }
                    return colStr;
                } else if (value instanceof Quaternion) {
                    Quaternion[] quatArr = (Quaternion[]) value;
                    String quatStr = "";
                    for (int i = 0; i < quatArr.length ; i++) {
                        quatStr += quatArr[i].getX() + " "
                                + quatArr[i].getY() + " "
                                + quatArr[i].getZ() + " "
                                + quatArr[i].getW() + "\n";
                    }
                    return quatStr;
                } else {
                    throw new UnsupportedOperationException("Unexpected Vector4Array type: " + value);
                }
*/
        case Vector4:
            // can be either ColorRGBA, Vector4f or Quaternion
            if (value instanceof Vector4f) {
                Vector4f v4 = (Vector4f) value;
                return v4.getX() + " " + v4.getY() + " " + v4.getZ() + " " + v4.getW();
            } else if (value instanceof ColorRGBA) {
                ColorRGBA color = (ColorRGBA) value;
                return color.getRed() + " " + color.getGreen() + " " + color.getBlue() + " " + color.getAlpha();
            } else if (value instanceof Quaternion) {
                Quaternion quat = (Quaternion) value;
                return quat.getX() + " " + quat.getY() + " " + quat.getZ() + " " + quat.getW();
            } else {
                throw new UnsupportedOperationException("Unexpected Vector4 type: " + value);
            }
        case Texture2D:
        case Texture3D:
        case TextureArray:
        case TextureBuffer:
        case TextureCubeMap:
            Texture texVal = (Texture) value;
            TextureKey texKey = (TextureKey) texVal.getKey();
            if (texKey == null) {
                // often does as well, even implicitly. 
                return texVal + ":returned null key";
            }
            String ret = "";
            if (texKey.isFlipY()) {
                ret += "Flip ";
            }
            //Wrap mode
            ret += getWrapMode(texVal, Texture.WrapAxis.S);
            ret += getWrapMode(texVal, Texture.WrapAxis.T);
            ret += getWrapMode(texVal, Texture.WrapAxis.R);
            //Min and Mag filter
            Texture.MinFilter def = Texture.MinFilter.BilinearNoMipMaps;
            if (texVal.getImage().hasMipmaps() || texKey.isGenerateMips()) {
                def = Texture.MinFilter.Trilinear;
            }
            if (texVal.getMinFilter() != def) {
                ret += "Min" + texVal.getMinFilter().name() + " ";
            }
            if (texVal.getMagFilter() != Texture.MagFilter.Bilinear) {
                ret += "Mag" + texVal.getMagFilter().name() + " ";
            }
            return ret + "\"" + texKey.getName() + "\"";
        default:
            // parameter type not supported in J3M
            return null;
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) Texture(com.jme3.texture.Texture)

Example 7 with TextureCubeMap

use of com.jme3.texture.TextureCubeMap in project jmonkeyengine by jMonkeyEngine.

the class LightProbe method read.

@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);
    irradianceMap = (TextureCubeMap) ic.readSavable("irradianceMap", null);
    prefilteredEnvMap = (TextureCubeMap) ic.readSavable("prefilteredEnvMap", null);
    position = (Vector3f) ic.readSavable("position", this);
    bounds = (BoundingVolume) ic.readSavable("bounds", new BoundingSphere(1.0f, Vector3f.ZERO));
    ready = ic.readBoolean("ready", false);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) InputCapsule(com.jme3.export.InputCapsule)

Example 8 with TextureCubeMap

use of com.jme3.texture.TextureCubeMap in project jmonkeyengine by jMonkeyEngine.

the class CombinedTexture method generateSkyTexture.

/**
     * Generates a texture that will be used by the sky spatial.
     * The result texture has 6 layers. Every image in each layer has equal size and its shape is a square.
     * The size of each image is the maximum size (width or height) of the textures given.
     * The default sky generated texture size is used (this value is set in the BlenderKey) if no picture textures
     * are present or their sizes is lower than the generated texture size.
     * The textures of lower sizes are properly scaled.
     * All the textures are mixed into one and put as layers in the result texture.
     * 
     * @param horizontalColor
     *            the horizon color
     * @param zenithColor
     *            the zenith color
     * @param blenderContext
     *            the blender context
     * @return texture for the sky
     */
public TextureCubeMap generateSkyTexture(ColorRGBA horizontalColor, ColorRGBA zenithColor, BlenderContext blenderContext) {
    LOGGER.log(Level.FINE, "Preparing sky texture from {0} applied textures.", textureDatas.size());
    LOGGER.fine("Computing the texture size.");
    int size = -1;
    for (TextureData textureData : textureDatas) {
        if (textureData.texture instanceof Texture2D) {
            size = Math.max(textureData.texture.getImage().getWidth(), size);
            size = Math.max(textureData.texture.getImage().getHeight(), size);
        }
    }
    if (size < 0) {
        size = blenderContext.getBlenderKey().getSkyGeneratedTextureSize();
    }
    LOGGER.log(Level.FINE, "The sky texture size will be: {0}x{0}.", size);
    TextureCubeMap result = null;
    for (TextureData textureData : textureDatas) {
        TextureCubeMap texture = null;
        if (textureData.texture instanceof GeneratedTexture) {
            texture = ((GeneratedTexture) textureData.texture).generateSkyTexture(size, horizontalColor, zenithColor, blenderContext);
        } else {
            // first create a grayscale version of the image
            Image image = textureData.texture.getImage();
            if (image.getWidth() != image.getHeight() || image.getWidth() != size) {
                image = ImageUtils.resizeTo(image, size, size);
            }
            Image grayscaleImage = ImageUtils.convertToGrayscaleTexture(image);
            // add the sky colors to the image
            PixelInputOutput sourcePixelIO = PixelIOFactory.getPixelIO(grayscaleImage.getFormat());
            PixelInputOutput targetPixelIO = PixelIOFactory.getPixelIO(image.getFormat());
            TexturePixel texturePixel = new TexturePixel();
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    sourcePixelIO.read(grayscaleImage, 0, texturePixel, x, y);
                    // no matter which factor we use here, in grayscale they are all equal
                    texturePixel.intensity = texturePixel.red;
                    ImageUtils.color(texturePixel, horizontalColor, zenithColor);
                    targetPixelIO.write(image, 0, texturePixel, x, y);
                }
            }
            // create the cubemap texture from the coloured image
            ByteBuffer sourceData = image.getData(0);
            ArrayList<ByteBuffer> data = new ArrayList<ByteBuffer>(6);
            for (int i = 0; i < 6; ++i) {
                data.add(BufferUtils.clone(sourceData));
            }
            texture = new TextureCubeMap(new Image(image.getFormat(), image.getWidth(), image.getHeight(), 6, data, ColorSpace.Linear));
        }
        if (result == null) {
            result = texture;
        } else {
            ImageUtils.mix(result.getImage(), texture.getImage());
        }
    }
    return result;
}
Also used : Texture2D(com.jme3.texture.Texture2D) PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) TextureCubeMap(com.jme3.texture.TextureCubeMap) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage) ByteBuffer(java.nio.ByteBuffer)

Example 9 with TextureCubeMap

use of com.jme3.texture.TextureCubeMap in project jmonkeyengine by jMonkeyEngine.

the class GeneratedTexture method generateSkyTexture.

/**
     * Creates a texture for the sky. The result texture has 6 layers.
     * @param size
     *            the size of the texture (width and height are equal)
     * @param horizontalColor
     *            the horizon color
     * @param zenithColor
     *            the zenith color
     * @param blenderContext
     *            the blender context
     * @return the sky texture
     */
public TextureCubeMap generateSkyTexture(int size, ColorRGBA horizontalColor, ColorRGBA zenithColor, BlenderContext blenderContext) {
    Image image = ImageUtils.createEmptyImage(Format.RGB8, size, size, 6);
    PixelInputOutput pixelIO = PixelIOFactory.getPixelIO(image.getFormat());
    TexturePixel pixel = new TexturePixel();
    float delta = 1 / (float) (size - 1);
    float sideV, sideS = 1, forwardU = 1, forwardV, upS;
    TempVars tempVars = TempVars.get();
    CastFunction castFunction = CAST_FUNCTIONS[blenderContext.getBlenderKey().getSkyGeneratedTextureShape().ordinal()];
    float castRadius = blenderContext.getBlenderKey().getSkyGeneratedTextureRadius();
    for (int x = 0; x < size; ++x) {
        sideV = 1;
        forwardV = 1;
        upS = 0;
        for (int y = 0; y < size; ++y) {
            castFunction.cast(tempVars.vect1.set(1, sideV, sideS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // right
            pixelIO.write(image, NEGATIVE_X, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(0, sideV, 1 - sideS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // left
            pixelIO.write(image, POSITIVE_X, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, forwardV, 0), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // front
            pixelIO.write(image, POSITIVE_Z, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(1 - forwardU, forwardV, 1), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // back
            pixelIO.write(image, NEGATIVE_Z, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, 0, upS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // top
            pixelIO.write(image, NEGATIVE_Y, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, 1, 1 - upS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // bottom
            pixelIO.write(image, POSITIVE_Y, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            sideV = FastMath.clamp(sideV - delta, 0, 1);
            forwardV = FastMath.clamp(forwardV - delta, 0, 1);
            upS = FastMath.clamp(upS + delta, 0, 1);
        }
        sideS = FastMath.clamp(sideS - delta, 0, 1);
        forwardU = FastMath.clamp(forwardU - delta, 0, 1);
    }
    tempVars.release();
    return new TextureCubeMap(image);
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) TextureCubeMap(com.jme3.texture.TextureCubeMap) TempVars(com.jme3.util.TempVars) Image(com.jme3.texture.Image)

Example 10 with TextureCubeMap

use of com.jme3.texture.TextureCubeMap in project jmonkeyengine by jMonkeyEngine.

the class LandscapeHelper method toSky.

/**
     * Loads scene's sky. Sky can be plain or textured.
     * If no sky type is selected in blender then no sky is loaded.
     * @param worldStructure
     *            the world's structure
     * @return the scene's sky
     * @throws BlenderFileException
     *             blender exception is thrown when problems with blender file occur
     */
public Spatial toSky(Structure worldStructure) throws BlenderFileException {
    int skytype = ((Number) worldStructure.getFieldValue("skytype")).intValue();
    if (skytype == 0) {
        return null;
    }
    LOGGER.fine("Loading sky.");
    ColorRGBA horizontalColor = this.toBackgroundColor(worldStructure);
    float zenr = ((Number) worldStructure.getFieldValue("zenr")).floatValue();
    float zeng = ((Number) worldStructure.getFieldValue("zeng")).floatValue();
    float zenb = ((Number) worldStructure.getFieldValue("zenb")).floatValue();
    ColorRGBA zenithColor = new ColorRGBA(zenr, zeng, zenb, 1);
    // jutr for this case load generated textures wheather user had set it or not because those might be needed to properly load the sky
    boolean loadGeneratedTextures = blenderContext.getBlenderKey().isLoadGeneratedTextures();
    blenderContext.getBlenderKey().setLoadGeneratedTextures(true);
    TextureHelper textureHelper = blenderContext.getHelper(TextureHelper.class);
    List<CombinedTexture> loadedTextures = null;
    try {
        loadedTextures = textureHelper.readTextureData(worldStructure, new float[] { horizontalColor.r, horizontalColor.g, horizontalColor.b, horizontalColor.a }, true);
    } finally {
        blenderContext.getBlenderKey().setLoadGeneratedTextures(loadGeneratedTextures);
    }
    TextureCubeMap texture = null;
    if (loadedTextures != null && loadedTextures.size() > 0) {
        if (loadedTextures.size() > 1) {
            throw new IllegalStateException("There should be only one combined texture for sky!");
        }
        CombinedTexture combinedTexture = loadedTextures.get(0);
        texture = combinedTexture.generateSkyTexture(horizontalColor, zenithColor, blenderContext);
    } else {
        LOGGER.fine("Preparing colors for colorband.");
        int colorbandType = ColorBand.IPO_CARDINAL;
        List<ColorRGBA> colorbandColors = new ArrayList<ColorRGBA>(3);
        colorbandColors.add(horizontalColor);
        if ((skytype & SKYTYPE_BLEND) != 0) {
            if ((skytype & SKYTYPE_PAPER) != 0) {
                colorbandType = ColorBand.IPO_LINEAR;
            }
            if ((skytype & SKYTYPE_REAL) != 0) {
                colorbandColors.add(0, zenithColor);
            }
            colorbandColors.add(zenithColor);
        }
        int size = blenderContext.getBlenderKey().getSkyGeneratedTextureSize();
        List<Integer> positions = new ArrayList<Integer>(colorbandColors.size());
        positions.add(0);
        if (colorbandColors.size() == 2) {
            positions.add(size - 1);
        } else if (colorbandColors.size() == 3) {
            positions.add(size / 2);
            positions.add(size - 1);
        }
        LOGGER.fine("Generating sky texture.");
        float[][] values = new ColorBand(colorbandType, colorbandColors, positions, size).computeValues();
        Image image = ImageUtils.createEmptyImage(Format.RGB8, size, size, 6);
        PixelInputOutput pixelIO = PixelIOFactory.getPixelIO(image.getFormat());
        TexturePixel pixel = new TexturePixel();
        LOGGER.fine("Creating side textures.");
        int[] sideImagesIndexes = new int[] { 0, 1, 4, 5 };
        for (int i : sideImagesIndexes) {
            for (int y = 0; y < size; ++y) {
                pixel.red = values[y][0];
                pixel.green = values[y][1];
                pixel.blue = values[y][2];
                for (int x = 0; x < size; ++x) {
                    pixelIO.write(image, i, pixel, x, y);
                }
            }
        }
        LOGGER.fine("Creating top texture.");
        pixelIO.read(image, 0, pixel, 0, image.getHeight() - 1);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                pixelIO.write(image, 3, pixel, x, y);
            }
        }
        LOGGER.fine("Creating bottom texture.");
        pixelIO.read(image, 0, pixel, 0, 0);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                pixelIO.write(image, 2, pixel, x, y);
            }
        }
        texture = new TextureCubeMap(image);
    }
    LOGGER.fine("Sky texture created. Creating sky.");
    return SkyFactory.createSky(blenderContext.getAssetManager(), texture, SkyFactory.EnvMapType.CubeMap);
}
Also used : ColorBand(com.jme3.scene.plugins.blender.textures.ColorBand) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel) CombinedTexture(com.jme3.scene.plugins.blender.textures.CombinedTexture)

Aggregations

TextureCubeMap (com.jme3.texture.TextureCubeMap)16 Image (com.jme3.texture.Image)10 ColorRGBA (com.jme3.math.ColorRGBA)6 Vector3f (com.jme3.math.Vector3f)6 ByteBuffer (java.nio.ByteBuffer)6 Material (com.jme3.material.Material)4 Geometry (com.jme3.scene.Geometry)4 Texture2D (com.jme3.texture.Texture2D)4 ArrayList (java.util.ArrayList)4 PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)3 TextureKey (com.jme3.asset.TextureKey)2 BoundingSphere (com.jme3.bounding.BoundingSphere)2 CubeMapWrapper (com.jme3.environment.util.CubeMapWrapper)2 Quaternion (com.jme3.math.Quaternion)2 Node (com.jme3.scene.Node)2 Quad (com.jme3.scene.shape.Quad)2 Texture (com.jme3.texture.Texture)2 Picture (com.jme3.ui.Picture)2 IrradianceMapGenerator (com.jme3.environment.generation.IrradianceMapGenerator)1 PrefilteredEnvMapFaceGenerator (com.jme3.environment.generation.PrefilteredEnvMapFaceGenerator)1