Search in sources :

Example 1 with Material

use of com.jme3.material.Material 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 Material

use of com.jme3.material.Material in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readTechnique.

private void readTechnique(Statement techStat) throws IOException {
    isUseNodes = false;
    String[] split = techStat.getLine().split(whitespacePattern);
    Cloner cloner = new Cloner();
    String name;
    if (split.length == 1) {
        name = TechniqueDef.DEFAULT_TECHNIQUE_NAME;
    } else if (split.length == 2) {
        name = split[1];
    } else {
        throw new IOException("Technique statement syntax incorrect");
    }
    String techniqueUniqueName = materialDef.getAssetName() + "@" + name;
    technique = new TechniqueDef(name, techniqueUniqueName.hashCode());
    for (Statement statement : techStat.getContents()) {
        readTechniqueStatement(statement);
    }
    technique.setShaderPrologue(createShaderPrologue(presetDefines));
    switch(technique.getLightMode()) {
        case Disable:
            technique.setLogic(new DefaultTechniqueDefLogic(technique));
            break;
        case MultiPass:
            technique.setLogic(new MultiPassLightingLogic(technique));
            break;
        case SinglePass:
            technique.setLogic(new SinglePassLightingLogic(technique));
            break;
        case StaticPass:
            technique.setLogic(new StaticPassLightingLogic(technique));
            break;
        case SinglePassAndImageBased:
            technique.setLogic(new SinglePassAndImageBasedLightingLogic(technique));
            break;
        default:
            throw new UnsupportedOperationException();
    }
    List<TechniqueDef> techniqueDefs = new ArrayList<>();
    if (isUseNodes) {
        nodesLoaderDelegate.computeConditions();
        //used for caching later, the shader here is not a file.
        // KIRILL 9/19/2015
        // Not sure if this is needed anymore, since shader caching
        // is now done by TechniqueDef.
        technique.setShaderFile(technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100");
        techniqueDefs.add(technique);
    } else if (shaderNames.containsKey(Shader.ShaderType.Vertex) && shaderNames.containsKey(Shader.ShaderType.Fragment)) {
        if (shaderLanguages.size() > 1) {
            for (int i = 1; i < shaderLanguages.size(); i++) {
                cloner.clearIndex();
                TechniqueDef td = cloner.clone(technique);
                td.setShaderFile(shaderNames, shaderLanguages.get(i));
                techniqueDefs.add(td);
            }
        }
        technique.setShaderFile(shaderNames, shaderLanguages.get(0));
        techniqueDefs.add(technique);
    } else {
        technique = null;
        shaderLanguages.clear();
        shaderNames.clear();
        presetDefines.clear();
        langSize = 0;
        logger.log(Level.WARNING, "Fixed function technique was ignored");
        logger.log(Level.WARNING, "Fixed function technique ''{0}'' was ignored for material {1}", new Object[] { name, key });
        return;
    }
    for (TechniqueDef techniqueDef : techniqueDefs) {
        materialDef.addTechniqueDef(techniqueDef);
    }
    technique = null;
    langSize = 0;
    shaderLanguages.clear();
    shaderNames.clear();
    presetDefines.clear();
}
Also used : Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException) StaticPassLightingLogic(com.jme3.material.logic.StaticPassLightingLogic) Cloner(com.jme3.util.clone.Cloner)

Example 3 with Material

use of com.jme3.material.Material in project jmonkeyengine by jMonkeyEngine.

the class MaterialDebugAppState method reloadMaterial.

public Material reloadMaterial(Material mat) {
    //clear the entire cache, there might be more clever things to do, like clearing only the matdef, and the associated shaders.
    assetManager.clearCache();
    //creating a dummy mat with the mat def of the mat to reload
    Material dummy = new Material(mat.getMaterialDef());
    for (MatParam matParam : mat.getParams()) {
        dummy.setParam(matParam.getName(), matParam.getVarType(), matParam.getValue());
    }
    dummy.getAdditionalRenderState().set(mat.getAdditionalRenderState());
    //creating a dummy geom and assigning the dummy material to it
    Geometry dummyGeom = new Geometry("dummyGeom", new Box(1f, 1f, 1f));
    dummyGeom.setMaterial(dummy);
    try {
        //preloading the dummyGeom, this call will compile the shader again
        renderManager.preloadScene(dummyGeom);
    } catch (RendererException e) {
        //compilation error, the shader code will be output to the console
        //the following code will output the error
        //System.err.println(e.getMessage());
        Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.SEVERE, e.getMessage());
        return null;
    }
    Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.INFO, "Material succesfully reloaded");
    //System.out.println("Material succesfully reloaded");
    return dummy;
}
Also used : Geometry(com.jme3.scene.Geometry) RendererException(com.jme3.renderer.RendererException) MatParam(com.jme3.material.MatParam) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box)

Example 4 with Material

use of com.jme3.material.Material in project jmonkeyengine by jMonkeyEngine.

the class PlaceholderAssets method getPlaceholderModel.

public static Spatial getPlaceholderModel(AssetManager assetManager) {
    // What should be the size? Nobody knows
    // the user's expected scale...
    Box box = new Box(1, 1, 1);
    Geometry geom = new Geometry("placeholder", box);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture tex = assetManager.loadTexture("Common/Textures/MissingModel.png");
    tex.setWrap(Texture.WrapMode.Repeat);
    mat.setTexture("ColorMap", tex);
    geom.setMaterial(mat);
    return geom;
}
Also used : Geometry(com.jme3.scene.Geometry) Box(com.jme3.scene.shape.Box) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture)

Example 5 with Material

use of com.jme3.material.Material in project jmonkeyengine by jMonkeyEngine.

the class MTLLoader method loadTexture.

protected Texture loadTexture(String path) {
    String[] split = path.trim().split("\\p{javaWhitespace}+");
    // will crash if path is an empty string
    path = split[split.length - 1];
    String name = new File(path).getName();
    TextureKey texKey = new TextureKey(folderName + name);
    texKey.setGenerateMips(true);
    Texture texture;
    try {
        texture = assetManager.loadTexture(texKey);
        texture.setWrap(WrapMode.Repeat);
    } catch (AssetNotFoundException ex) {
        logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[] { texKey, key });
        texture = new Texture2D(PlaceholderAssets.getPlaceholderImage(assetManager));
        texture.setWrap(WrapMode.Repeat);
        texture.setKey(key);
    }
    return texture;
}
Also used : Texture2D(com.jme3.texture.Texture2D) File(java.io.File) Texture(com.jme3.texture.Texture)

Aggregations

Material (com.jme3.material.Material)310 Geometry (com.jme3.scene.Geometry)191 Vector3f (com.jme3.math.Vector3f)120 Box (com.jme3.scene.shape.Box)81 Texture (com.jme3.texture.Texture)70 Spatial (com.jme3.scene.Spatial)53 DirectionalLight (com.jme3.light.DirectionalLight)49 ColorRGBA (com.jme3.math.ColorRGBA)47 Node (com.jme3.scene.Node)47 Sphere (com.jme3.scene.shape.Sphere)44 Quaternion (com.jme3.math.Quaternion)31 Quad (com.jme3.scene.shape.Quad)26 ArrayList (java.util.ArrayList)25 Texture2D (com.jme3.texture.Texture2D)21 KeyTrigger (com.jme3.input.controls.KeyTrigger)20 Mesh (com.jme3.scene.Mesh)20 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)19 TextureKey (com.jme3.asset.TextureKey)18 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)18 ParticleEmitter (com.jme3.effect.ParticleEmitter)17