Search in sources :

Example 1 with VarType

use of com.jme3.shader.VarType 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 VarType

use of com.jme3.shader.VarType in project jmonkeyengine by jMonkeyEngine.

the class Material method setTexture.

/**
     * Pass a texture to the material shader.
     *
     * @param name the name of the texture defined in the material definition
     * (j3md) (for example Texture for Lighting.j3md)
     * @param value the Texture object previously loaded by the asset manager
     */
public void setTexture(String name, Texture value) {
    if (value == null) {
        // clear it
        clearParam(name);
        return;
    }
    VarType paramType = null;
    switch(value.getType()) {
        case TwoDimensional:
            paramType = VarType.Texture2D;
            break;
        case TwoDimensionalArray:
            paramType = VarType.TextureArray;
            break;
        case ThreeDimensional:
            paramType = VarType.Texture3D;
            break;
        case CubeMap:
            paramType = VarType.TextureCubeMap;
            break;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + value.getType());
    }
    setTextureParam(name, paramType, value);
}
Also used : VarType(com.jme3.shader.VarType)

Example 3 with VarType

use of com.jme3.shader.VarType in project jmonkeyengine by jMonkeyEngine.

the class Material method applyOverrides.

private int applyOverrides(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> overrides, int unit) {
    for (MatParamOverride override : overrides.getArray()) {
        VarType type = override.getVarType();
        MatParam paramDef = def.getMaterialParam(override.getName());
        if (paramDef == null || paramDef.getVarType() != type || !override.isEnabled()) {
            continue;
        }
        Uniform uniform = shader.getUniform(override.getPrefixedName());
        if (override.getValue() != null) {
            if (type.isTextureType()) {
                renderer.setTexture(unit, (Texture) override.getValue());
                uniform.setValue(VarType.Int, unit);
                unit++;
            } else {
                uniform.setValue(type, override.getValue());
            }
        } else {
            uniform.clearValue();
        }
    }
    return unit;
}
Also used : Uniform(com.jme3.shader.Uniform) VarType(com.jme3.shader.VarType)

Example 4 with VarType

use of com.jme3.shader.VarType in project jmonkeyengine by jMonkeyEngine.

the class Material method updateShaderMaterialParameters.

private int updateShaderMaterialParameters(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> worldOverrides, SafeArrayList<MatParamOverride> forcedOverrides) {
    int unit = 0;
    if (worldOverrides != null) {
        unit = applyOverrides(renderer, shader, worldOverrides, unit);
    }
    if (forcedOverrides != null) {
        unit = applyOverrides(renderer, shader, forcedOverrides, unit);
    }
    for (int i = 0; i < paramValues.size(); i++) {
        MatParam param = paramValues.getValue(i);
        VarType type = param.getVarType();
        Uniform uniform = shader.getUniform(param.getPrefixedName());
        if (uniform.isSetByCurrentMaterial()) {
            continue;
        }
        if (type.isTextureType()) {
            renderer.setTexture(unit, (Texture) param.getValue());
            uniform.setValue(VarType.Int, unit);
            unit++;
        } else {
            uniform.setValue(type, param.getValue());
        }
    }
    //TODO HACKY HACK remove this when texture unit is handled by the uniform.
    return unit;
}
Also used : Uniform(com.jme3.shader.Uniform) VarType(com.jme3.shader.VarType)

Example 5 with VarType

use of com.jme3.shader.VarType in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readParam.

// <TYPE> <NAME> [ "(" <FFBINDING> ")" ] [-LINEAR] [ ":" <DEFAULTVAL> ]
private void readParam(String statement) throws IOException {
    String name;
    String defaultVal = null;
    ColorSpace colorSpace = null;
    String[] split = statement.split(":");
    // Parse default val
    if (split.length == 1) {
    // Doesn't contain default value
    } else {
        if (split.length != 2) {
            throw new IOException("Parameter statement syntax incorrect");
        }
        statement = split[0].trim();
        defaultVal = split[1].trim();
    }
    if (statement.endsWith("-LINEAR")) {
        colorSpace = ColorSpace.Linear;
        statement = statement.substring(0, statement.length() - "-LINEAR".length());
    }
    // Parse ffbinding
    int startParen = statement.indexOf("(");
    if (startParen != -1) {
        // get content inside parentheses
        int endParen = statement.indexOf(")", startParen);
        String bindingStr = statement.substring(startParen + 1, endParen).trim();
        // don't care about bindingStr
        statement = statement.substring(0, startParen);
    }
    // Parse type + name
    split = statement.split(whitespacePattern);
    if (split.length != 2) {
        throw new IOException("Parameter statement syntax incorrect");
    }
    VarType type;
    if (split[0].equals("Color")) {
        type = VarType.Vector4;
    } else {
        type = VarType.valueOf(split[0]);
    }
    name = split[1];
    Object defaultValObj = null;
    if (defaultVal != null) {
        defaultValObj = readValue(type, defaultVal);
    }
    if (type.isTextureType()) {
        materialDef.addMaterialParamTexture(type, name, colorSpace);
    } else {
        materialDef.addMaterialParam(type, name, defaultValObj);
    }
}
Also used : ColorSpace(com.jme3.texture.image.ColorSpace) IOException(java.io.IOException)

Aggregations

VarType (com.jme3.shader.VarType)5 Uniform (com.jme3.shader.Uniform)3 DefineList (com.jme3.shader.DefineList)1 Texture (com.jme3.texture.Texture)1 Texture2D (com.jme3.texture.Texture2D)1 ColorSpace (com.jme3.texture.image.ColorSpace)1 IOException (java.io.IOException)1