Search in sources :

Example 6 with AssetKey

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

the class Material method read.

public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    name = ic.readString("name", null);
    additionalState = (RenderState) ic.readSavable("render_state", null);
    transparent = ic.readBoolean("is_transparent", false);
    // Load the material def
    String defName = ic.readString("material_def", null);
    HashMap<String, MatParam> params = (HashMap<String, MatParam>) ic.readStringSavableMap("parameters", null);
    boolean enableVcolor = false;
    boolean separateTexCoord = false;
    boolean applyDefaultValues = false;
    boolean guessRenderStateApply = false;
    int ver = ic.getSavableVersion(Material.class);
    if (ver < 1) {
        applyDefaultValues = true;
    }
    if (ver < 2) {
        guessRenderStateApply = true;
    }
    if (im.getFormatVersion() == 0) {
        // Enable compatibility with old models
        if (defName.equalsIgnoreCase("Common/MatDefs/Misc/VertexColor.j3md")) {
            // Using VertexColor, switch to Unshaded and set VertexColor=true
            enableVcolor = true;
            defName = "Common/MatDefs/Misc/Unshaded.j3md";
        } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/SimpleTextured.j3md") || defName.equalsIgnoreCase("Common/MatDefs/Misc/SolidColor.j3md")) {
            // Using SimpleTextured/SolidColor, just switch to Unshaded
            defName = "Common/MatDefs/Misc/Unshaded.j3md";
        } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/WireColor.j3md")) {
            // Using WireColor, set wireframe renderstate = true and use Unshaded
            getAdditionalRenderState().setWireframe(true);
            defName = "Common/MatDefs/Misc/Unshaded.j3md";
        } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/Unshaded.j3md")) {
            // Uses unshaded, ensure that the proper param is set
            MatParam value = params.get("SeperateTexCoord");
            if (value != null && ((Boolean) value.getValue()) == true) {
                params.remove("SeperateTexCoord");
                separateTexCoord = true;
            }
        }
        assert applyDefaultValues && guessRenderStateApply;
    }
    def = (MaterialDef) im.getAssetManager().loadAsset(new AssetKey(defName));
    paramValues = new ListMap<String, MatParam>();
    // load the textures and update nextTexUnit
    for (Map.Entry<String, MatParam> entry : params.entrySet()) {
        MatParam param = entry.getValue();
        if (param instanceof MatParamTexture) {
            MatParamTexture texVal = (MatParamTexture) param;
            // do not add to param values
            if (texVal.getTextureValue() == null || texVal.getTextureValue().getImage() == null) {
                continue;
            }
        }
        if (im.getFormatVersion() == 0 && param.getName().startsWith("m_")) {
            // Ancient version of jME3 ...
            param.setName(param.getName().substring(2));
        }
        if (def.getMaterialParam(param.getName()) == null) {
            logger.log(Level.WARNING, "The material parameter is not defined: {0}. Ignoring..", param.getName());
        } else {
            checkSetParam(param.getVarType(), param.getName());
            paramValues.put(param.getName(), param);
        }
    }
    if (applyDefaultValues) {
        // not available
        for (MatParam param : def.getMaterialParams()) {
            if (param.getValue() != null && paramValues.get(param.getName()) == null) {
                setParam(param.getName(), param.getVarType(), param.getValue());
            }
        }
    }
    if (guessRenderStateApply && additionalState != null) {
        // Try to guess values of "apply" render state based on defaults
        // if value != default then set apply to true
        additionalState.applyPolyOffset = additionalState.offsetEnabled;
        additionalState.applyBlendMode = additionalState.blendMode != BlendMode.Off;
        additionalState.applyColorWrite = !additionalState.colorWrite;
        additionalState.applyCullMode = additionalState.cullMode != FaceCullMode.Back;
        additionalState.applyDepthTest = !additionalState.depthTest;
        additionalState.applyDepthWrite = !additionalState.depthWrite;
        additionalState.applyStencilTest = additionalState.stencilTest;
        additionalState.applyWireFrame = additionalState.wireframe;
    }
    if (enableVcolor) {
        setBoolean("VertexColor", true);
    }
    if (separateTexCoord) {
        setBoolean("SeparateTexCoord", true);
    }
}
Also used : AssetKey(com.jme3.asset.AssetKey) ListMap(com.jme3.util.ListMap)

Example 7 with AssetKey

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

the class SceneMeshLoader method load.

@Override
public Object load(AssetInfo info) throws IOException {
    AssetKey key = info.getKey();
    Spatial output = cache.get(key);
    if (output == null) {
        output = (Spatial) super.load(info);
        cache.put(key, output);
    }
    return output.clone(false);
}
Also used : AssetKey(com.jme3.asset.AssetKey) Spatial(com.jme3.scene.Spatial)

Example 8 with AssetKey

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

the class MaterialExtensionLoader method load.

public MaterialList load(AssetManager assetManager, AssetKey key, MaterialExtensionSet matExts, List<Statement> statements) throws IOException {
    this.assetManager = assetManager;
    this.matExts = matExts;
    this.key = key;
    list = new MaterialList();
    for (Statement statement : statements) {
        if (statement.getLine().startsWith("import")) {
            // ignore
            continue;
        } else if (statement.getLine().startsWith("material")) {
            Material material = readExtendingMaterial(statement);
            list.put(matName, material);
            List<String> matAliases = matExts.getNameMappings(matName);
            if (matAliases != null) {
                for (String string : matAliases) {
                    list.put(string, material);
                }
            }
        }
    }
    return list;
}
Also used : Statement(com.jme3.util.blockparser.Statement) MaterialList(com.jme3.material.MaterialList) Material(com.jme3.material.Material) List(java.util.List) MaterialList(com.jme3.material.MaterialList)

Example 9 with AssetKey

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

the class TestMaterialWrite method testWriteMat.

@Test
public void testWriteMat() throws Exception {
    Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    mat.setBoolean("UseMaterialColors", true);
    mat.setColor("Diffuse", ColorRGBA.White);
    mat.setColor("Ambient", ColorRGBA.DarkGray);
    mat.setFloat("AlphaDiscardThreshold", 0.5f);
    mat.setFloat("Shininess", 2.5f);
    Texture tex = assetManager.loadTexture("Common/Textures/MissingTexture.png");
    tex.setMagFilter(Texture.MagFilter.Nearest);
    tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
    tex.setWrap(Texture.WrapAxis.S, Texture.WrapMode.Repeat);
    tex.setWrap(Texture.WrapAxis.T, Texture.WrapMode.MirroredRepeat);
    mat.setTexture("DiffuseMap", tex);
    mat.getAdditionalRenderState().setDepthWrite(false);
    mat.getAdditionalRenderState().setDepthTest(false);
    mat.getAdditionalRenderState().setLineWidth(5);
    mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    J3MExporter exporter = new J3MExporter();
    try {
        exporter.save(mat, stream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.err.println(stream.toString());
    J3MLoader loader = new J3MLoader();
    AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {

        @Override
        public InputStream openStream() {
            return new ByteArrayInputStream(stream.toByteArray());
        }
    };
    Material mat2 = (Material) loader.load(info);
    assertTrue(mat.contentEquals(mat2));
}
Also used : AssetKey(com.jme3.asset.AssetKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Material(com.jme3.material.Material) J3MExporter(com.jme3.material.plugin.export.material.J3MExporter) J3MLoader(com.jme3.material.plugins.J3MLoader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AssetInfo(com.jme3.asset.AssetInfo) Texture(com.jme3.texture.Texture) Test(org.junit.Test)

Example 10 with AssetKey

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

the class ShaderNodeDefinitionLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    AssetKey k = assetInfo.getKey();
    if (!(k instanceof ShaderNodeDefinitionKey)) {
        throw new IOException("ShaderNodeDefinition file must be loaded via ShaderNodeDefinitionKey");
    }
    ShaderNodeDefinitionKey key = (ShaderNodeDefinitionKey) k;
    loaderDelegate = new ShaderNodeLoaderDelegate();
    InputStream in = assetInfo.openStream();
    List<Statement> roots = BlockLanguageParser.parse(in);
    if (roots.size() == 2) {
        Statement exception = roots.get(0);
        String line = exception.getLine();
        if (line.startsWith("Exception")) {
            throw new AssetLoadException(line.substring("Exception ".length()));
        } else {
            throw new MatParseException("In multiroot shader node definition, expected first statement to be 'Exception'", exception);
        }
    } else if (roots.size() != 1) {
        throw new MatParseException("Too many roots in J3SN file", roots.get(0));
    }
    return loaderDelegate.readNodesDefinitions(roots.get(0).getContents(), key);
}
Also used : AssetKey(com.jme3.asset.AssetKey) ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) InputStream(java.io.InputStream) Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException) AssetLoadException(com.jme3.asset.AssetLoadException)

Aggregations

AssetKey (com.jme3.asset.AssetKey)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)5 AssetInfo (com.jme3.asset.AssetInfo)4 AssetLoadException (com.jme3.asset.AssetLoadException)4 ModelKey (com.jme3.asset.ModelKey)4 Material (com.jme3.material.Material)4 Statement (com.jme3.util.blockparser.Statement)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 AssetCache (com.jme3.asset.cache.AssetCache)3 Texture (com.jme3.texture.Texture)3 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 TextureKey (com.jme3.asset.TextureKey)2 MaterialList (com.jme3.material.MaterialList)2 J3MLoader (com.jme3.material.plugins.J3MLoader)2 Node (com.jme3.scene.Node)2 Spatial (com.jme3.scene.Spatial)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2