Search in sources :

Example 1 with Texture3D

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

the class J3MLoader method parseTextureType.

private Texture parseTextureType(final VarType type, final String value) {
    final List<String> textureValues = tokenizeTextureValue(value);
    final List<TextureOptionValue> textureOptionValues = parseTextureOptions(textureValues);
    TextureKey textureKey = null;
    // If there is only one token on the value, it must be the path to the texture.
    if (textureValues.size() == 1) {
        textureKey = new TextureKey(textureValues.get(0), false);
    } else {
        String texturePath = value.trim();
        // If there are no valid "new" texture options specified but the path is split into several parts, lets parse the old way.
        if (isTexturePathDeclaredTheTraditionalWay(textureOptionValues, texturePath)) {
            boolean flipY = false;
            if (texturePath.startsWith("Flip Repeat ") || texturePath.startsWith("Repeat Flip ")) {
                texturePath = texturePath.substring(12).trim();
                flipY = true;
            } else if (texturePath.startsWith("Flip ")) {
                texturePath = texturePath.substring(5).trim();
                flipY = true;
            } else if (texturePath.startsWith("Repeat ")) {
                texturePath = texturePath.substring(7).trim();
            }
            // Support path starting with quotes (double and single)
            if (texturePath.startsWith("\"") || texturePath.startsWith("'")) {
                texturePath = texturePath.substring(1);
            }
            // Support path ending with quotes (double and single)
            if (texturePath.endsWith("\"") || texturePath.endsWith("'")) {
                texturePath = texturePath.substring(0, texturePath.length() - 1);
            }
            textureKey = new TextureKey(texturePath, flipY);
        }
        if (textureKey == null) {
            textureKey = new TextureKey(textureValues.get(textureValues.size() - 1), false);
        }
        // Apply texture options to the texture key
        if (!textureOptionValues.isEmpty()) {
            for (final TextureOptionValue textureOptionValue : textureOptionValues) {
                textureOptionValue.applyToTextureKey(textureKey);
            }
        }
    }
    switch(type) {
        case Texture3D:
            textureKey.setTextureTypeHint(Texture.Type.ThreeDimensional);
            break;
        case TextureArray:
            textureKey.setTextureTypeHint(Texture.Type.TwoDimensionalArray);
            break;
        case TextureCubeMap:
            textureKey.setTextureTypeHint(Texture.Type.CubeMap);
            break;
    }
    textureKey.setGenerateMips(true);
    Texture texture;
    try {
        texture = assetManager.loadTexture(textureKey);
    } catch (AssetNotFoundException ex) {
        logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[] { textureKey, key });
        texture = null;
    }
    if (texture == null) {
        texture = new Texture2D(PlaceholderAssets.getPlaceholderImage(assetManager));
        texture.setKey(textureKey);
        texture.setName(textureKey.getName());
    }
    // Apply texture options to the texture
    if (!textureOptionValues.isEmpty()) {
        for (final TextureOptionValue textureOptionValue : textureOptionValues) {
            textureOptionValue.applyToTexture(texture);
        }
    }
    return texture;
}
Also used : Texture2D(com.jme3.texture.Texture2D) Texture(com.jme3.texture.Texture)

Example 2 with Texture3D

use of com.jme3.texture.Texture3D 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 3 with Texture3D

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

the class DDSLoader method load.

public Object load(AssetInfo info) throws IOException {
    if (!(info.getKey() instanceof TextureKey)) {
        throw new IllegalArgumentException("Texture assets must be loaded using a TextureKey");
    }
    InputStream stream = null;
    try {
        stream = info.openStream();
        in = new LittleEndien(stream);
        loadHeader();
        if (texture3D) {
            ((TextureKey) info.getKey()).setTextureTypeHint(Texture.Type.ThreeDimensional);
        } else if (depth > 1) {
            ((TextureKey) info.getKey()).setTextureTypeHint(Texture.Type.CubeMap);
        }
        ArrayList<ByteBuffer> data = readData(((TextureKey) info.getKey()).isFlipY());
        return new Image(pixelFormat, width, height, depth, data, sizes, ColorSpace.sRGB);
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) InputStream(java.io.InputStream) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) LittleEndien(com.jme3.util.LittleEndien)

Example 4 with Texture3D

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

the class TextureProcessor method postProcess.

@Override
public Object postProcess(AssetKey key, Object obj) {
    TextureKey texKey = (TextureKey) key;
    Image img = (Image) obj;
    if (img == null) {
        return null;
    }
    Texture tex;
    if (texKey.getTextureTypeHint() == Texture.Type.CubeMap) {
        if (texKey.isFlipY()) {
            // also flip -y and +y image in cubemap
            ByteBuffer pos_y = img.getData(2);
            img.setData(2, img.getData(3));
            img.setData(3, pos_y);
        }
        tex = new TextureCubeMap();
    } else if (texKey.getTextureTypeHint() == Texture.Type.ThreeDimensional) {
        tex = new Texture3D();
    } else {
        tex = new Texture2D();
    }
    // or generate them if requested by user
    if (img.hasMipmaps() || texKey.isGenerateMips()) {
        tex.setMinFilter(Texture.MinFilter.Trilinear);
    }
    tex.setAnisotropicFilter(texKey.getAnisotropy());
    tex.setName(texKey.getName());
    tex.setImage(img);
    return tex;
}
Also used : TextureKey(com.jme3.asset.TextureKey) ByteBuffer(java.nio.ByteBuffer)

Example 5 with Texture3D

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

the class TestTexture3D method getTexture.

/**
         * This method creates a RGB8 texture with the sizes of 10x10x10 pixels.
         */
private Texture getTexture() throws IOException {
    ArrayList<ByteBuffer> data = new ArrayList<ByteBuffer>(1);
    //all data must be inside one buffer
    ByteBuffer bb = BufferUtils.createByteBuffer(10 * 10 * 10 * 3);
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10 * 10; ++j) {
            bb.put((byte) (255f * i / 10f));
            bb.put((byte) (255f * i / 10f));
            bb.put((byte) (255f));
        }
    }
    bb.rewind();
    data.add(bb);
    return new Texture3D(new Image(Format.RGB8, 10, 10, 10, data, null, ColorSpace.Linear));
}
Also used : Texture3D(com.jme3.texture.Texture3D) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)4 TextureKey (com.jme3.asset.TextureKey)3 Image (com.jme3.texture.Image)2 Texture (com.jme3.texture.Texture)2 GLImageFormat (com.jme3.renderer.opengl.GLImageFormat)1 Texture2D (com.jme3.texture.Texture2D)1 Texture3D (com.jme3.texture.Texture3D)1 LittleEndien (com.jme3.util.LittleEndien)1 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1