Search in sources :

Example 1 with GeneratedTextureKey

use of com.jme3.asset.GeneratedTextureKey in project jmonkeyengine by jMonkeyEngine.

the class TextureHelper method getTexture.

/**
     * This class returns a texture read from the file or from packed blender
     * data. The returned texture has the name set to the value of its blender
     * type.
     * 
     * @param textureStructure
     *            texture structure filled with data
     * @param blenderContext
     *            the blender context
     * @return the texture that can be used by JME engine
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is
     *             somehow invalid or corrupted
     */
public Texture getTexture(Structure textureStructure, Structure mTex, BlenderContext blenderContext) throws BlenderFileException {
    Texture result = (Texture) blenderContext.getLoadedFeature(textureStructure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (result != null) {
        return result;
    }
    if ("ID".equals(textureStructure.getType())) {
        LOGGER.fine("Loading texture from external blend file.");
        return (Texture) this.loadLibrary(textureStructure);
    }
    int type = ((Number) textureStructure.getFieldValue("type")).intValue();
    int imaflag = ((Number) textureStructure.getFieldValue("imaflag")).intValue();
    switch(type) {
        case // (it is first because probably this will be most commonly used)
        TEX_IMAGE:
            Pointer pImage = (Pointer) textureStructure.getFieldValue("ima");
            if (pImage.isNotNull()) {
                Structure image = pImage.fetchData().get(0);
                Texture loadedTexture = this.loadImageAsTexture(image, imaflag, blenderContext);
                if (loadedTexture != null) {
                    result = loadedTexture;
                    this.applyColorbandAndColorFactors(textureStructure, result.getImage(), blenderContext);
                }
            }
            break;
        case TEX_CLOUDS:
        case TEX_WOOD:
        case TEX_MARBLE:
        case TEX_MAGIC:
        case TEX_BLEND:
        case TEX_STUCCI:
        case TEX_NOISE:
        case TEX_MUSGRAVE:
        case TEX_VORONOI:
        case TEX_DISTNOISE:
            result = new GeneratedTexture(textureStructure, mTex, textureGeneratorFactory.createTextureGenerator(type), blenderContext);
            break;
        case // No texture, do nothing
        TEX_NONE:
            break;
        case TEX_POINTDENSITY:
        case TEX_VOXELDATA:
        case TEX_PLUGIN:
        case TEX_ENVMAP:
        case TEX_OCEAN:
            LOGGER.log(Level.WARNING, "Unsupported texture type: {0} for texture: {1}", new Object[] { type, textureStructure.getName() });
            break;
        default:
            throw new BlenderFileException("Unknown texture type: " + type + " for texture: " + textureStructure.getName());
    }
    if (result != null) {
        result.setName(textureStructure.getName());
        result.setWrap(WrapMode.Repeat);
        // decide if the mipmaps will be generated
        switch(blenderContext.getBlenderKey().getMipmapGenerationMethod()) {
            case ALWAYS_GENERATE:
                result.setMinFilter(MinFilter.Trilinear);
                break;
            case NEVER_GENERATE:
                break;
            case GENERATE_WHEN_NEEDED:
                if ((imaflag & 0x04) != 0) {
                    result.setMinFilter(MinFilter.Trilinear);
                }
                break;
            default:
                throw new IllegalStateException("Unknown mipmap generation method: " + blenderContext.getBlenderKey().getMipmapGenerationMethod());
        }
        if (type != TEX_IMAGE) {
            // only generated textures should have this key
            result.setKey(new GeneratedTextureKey(textureStructure.getName()));
        }
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, "Adding texture {0} to the loaded features with OMA = {1}", new Object[] { result.getName(), textureStructure.getOldMemoryAddress() });
        }
        blenderContext.addLoadedFeatures(textureStructure.getOldMemoryAddress(), LoadedDataType.STRUCTURE, textureStructure);
        blenderContext.addLoadedFeatures(textureStructure.getOldMemoryAddress(), LoadedDataType.FEATURE, result);
    }
    return result;
}
Also used : BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) Pointer(com.jme3.scene.plugins.blender.file.Pointer) GeneratedTextureKey(com.jme3.asset.GeneratedTextureKey) Structure(com.jme3.scene.plugins.blender.file.Structure) Texture(com.jme3.texture.Texture)

Aggregations

GeneratedTextureKey (com.jme3.asset.GeneratedTextureKey)1 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)1 Pointer (com.jme3.scene.plugins.blender.file.Pointer)1 Structure (com.jme3.scene.plugins.blender.file.Structure)1 Texture (com.jme3.texture.Texture)1