Search in sources :

Example 6 with MaterialList

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

the class MeshLoader method load.

public Object load(AssetInfo info) throws IOException {
    try {
        key = info.getKey();
        meshName = key.getName();
        folderName = key.getFolder();
        String ext = key.getExtension();
        meshName = meshName.substring(0, meshName.length() - ext.length() - 1);
        if (folderName != null && folderName.length() > 0) {
            meshName = meshName.substring(folderName.length());
        }
        assetManager = info.getManager();
        if (key instanceof OgreMeshKey) {
            // OgreMeshKey is being used, try getting the material list
            // from it
            OgreMeshKey meshKey = (OgreMeshKey) key;
            materialList = meshKey.getMaterialList();
            String materialName = meshKey.getMaterialName();
            // Material list not set but material name is available
            if (materialList == null && materialName != null) {
                OgreMaterialKey materialKey = new OgreMaterialKey(folderName + materialName + ".material");
                try {
                    materialList = (MaterialList) assetManager.loadAsset(materialKey);
                } catch (AssetNotFoundException e) {
                    logger.log(Level.WARNING, "Cannot locate {0} for model {1}", new Object[] { materialKey, key });
                }
            }
        } else {
            // Make sure to reset it to null so that previous state
            // doesn't leak onto this one
            materialList = null;
        }
        // default method.
        if (materialList == null) {
            OgreMaterialKey materialKey = new OgreMaterialKey(folderName + meshName + ".material");
            try {
                materialList = (MaterialList) assetManager.loadAsset(materialKey);
            } catch (AssetNotFoundException e) {
                logger.log(Level.WARNING, "Cannot locate {0} for model {1}", new Object[] { materialKey, key });
            }
        }
        // Added by larynx 25.06.2011
        // Android needs the namespace aware flag set to true                 
        // Kirill 30.06.2011
        // Now, hack is applied for both desktop and android to avoid
        // checking with JmeSystem.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(info.openStream());
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return compileModel();
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 7 with MaterialList

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

the class SceneLoader method parseEntity.

private void parseEntity(Attributes attribs) throws SAXException {
    String name = attribs.getValue("name");
    if (name == null) {
        name = "OgreEntity-" + (++nodeIdx);
    } else {
        name += "-entity";
    }
    String meshFile = attribs.getValue("meshFile");
    if (meshFile == null) {
        throw new SAXException("Required attribute 'meshFile' missing for 'entity' node");
    }
    // TODO: Not currently used
    String materialName = attribs.getValue("materialName");
    if (folderName != null) {
        meshFile = folderName + meshFile;
    }
    // NOTE: append "xml" since its assumed mesh files are binary in dotScene
    meshFile += ".xml";
    entityNode = new com.jme3.scene.Node(name);
    OgreMeshKey meshKey = new OgreMeshKey(meshFile, materialList);
    try {
        try {
            Spatial ogreMesh = (Spatial) meshLoader.load(assetManager.locateAsset(meshKey));
            entityNode.attachChild(ogreMesh);
        } catch (IOException e) {
            throw new AssetNotFoundException(meshKey.toString());
        }
    } catch (AssetNotFoundException ex) {
        if (ex.getMessage().equals(meshFile)) {
            logger.log(Level.WARNING, "Cannot locate {0} for scene {1}", new Object[] { meshKey, key });
            // Attach placeholder asset.
            Spatial model = PlaceholderAssets.getPlaceholderModel(assetManager);
            model.setKey(key);
            entityNode.attachChild(model);
        } else {
            throw ex;
        }
    }
    node.attachChild(entityNode);
    node = null;
}
Also used : Spatial(com.jme3.scene.Spatial) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 8 with MaterialList

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

the class SceneLoader method load.

public Object load(AssetInfo info) throws IOException {
    try {
        key = info.getKey();
        assetManager = info.getManager();
        sceneName = key.getName();
        String ext = key.getExtension();
        folderName = key.getFolder();
        sceneName = sceneName.substring(0, sceneName.length() - ext.length() - 1);
        reset();
        // == Run 1st pass over XML file to determine material list ==
        materialList = materialLoader.load(assetManager, folderName, info.openStream());
        if (materialList == null || materialList.isEmpty()) {
            // NOTE: No materials were found by searching the externals section.
            // Try finding a similarly named material file in the same folder.
            // (Backward compatibility only!)
            OgreMaterialKey materialKey = new OgreMaterialKey(sceneName + ".material");
            try {
                materialList = (MaterialList) assetManager.loadAsset(materialKey);
            } catch (AssetNotFoundException ex) {
                logger.log(Level.WARNING, "Cannot locate {0} for scene {1}", new Object[] { materialKey, key });
                materialList = null;
            }
        }
        // == Run 2nd pass to load entities and other objects ==
        // Added by larynx 25.06.2011
        // Android needs the namespace aware flag set to true 
        // Kirill 30.06.2011
        // Now, hack is applied for both desktop and android to avoid
        // checking with JmeSystem.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(info.openStream());
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return root;
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 9 with MaterialList

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

the class MaterialLoader method load.

private MaterialList load(AssetManager assetManager, AssetKey key, InputStream in) throws IOException {
    folderName = key.getFolder();
    this.assetManager = assetManager;
    MaterialList list = null;
    List<Statement> statements = BlockLanguageParser.parse(in);
    for (Statement statement : statements) {
        if (statement.getLine().startsWith("import")) {
            MaterialExtensionSet matExts = null;
            if (key instanceof OgreMaterialKey) {
                matExts = ((OgreMaterialKey) key).getMaterialExtensionSet();
            }
            if (matExts == null) {
                throw new IOException("Must specify MaterialExtensionSet when loading\n" + "Ogre3D materials with extended materials");
            }
            list = new MaterialExtensionLoader().load(assetManager, key, matExts, statements);
            break;
        } else if (statement.getLine().startsWith("material")) {
            if (list == null) {
                list = new MaterialList();
            }
            String[] split = statement.getLine().split(" ", 2);
            matName = split[1].trim();
            if (matName.startsWith("\"") && matName.endsWith("\"")) {
                matName = matName.substring(1, matName.length() - 1);
            }
            readMaterial(statement);
            Material mat = compileMaterial();
            list.put(mat.getName(), mat);
        }
    }
    return list;
}
Also used : MaterialExtensionLoader(com.jme3.scene.plugins.ogre.matext.MaterialExtensionLoader) Statement(com.jme3.util.blockparser.Statement) MaterialList(com.jme3.material.MaterialList) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) Material(com.jme3.material.Material) IOException(java.io.IOException) MaterialExtensionSet(com.jme3.scene.plugins.ogre.matext.MaterialExtensionSet)

Aggregations

MaterialList (com.jme3.material.MaterialList)5 OgreMaterialKey (com.jme3.scene.plugins.ogre.matext.OgreMaterialKey)4 IOException (java.io.IOException)4 Material (com.jme3.material.Material)3 SAXException (org.xml.sax.SAXException)3 Statement (com.jme3.util.blockparser.Statement)2 InputStreamReader (java.io.InputStreamReader)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2 InputSource (org.xml.sax.InputSource)2 XMLReader (org.xml.sax.XMLReader)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 BulletAppState (com.jme3.bullet.BulletAppState)1 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)1 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)1 PhysicsCharacter (com.jme3.bullet.objects.PhysicsCharacter)1 AmbientLight (com.jme3.light.AmbientLight)1 DirectionalLight (com.jme3.light.DirectionalLight)1 Vector3f (com.jme3.math.Vector3f)1 Spatial (com.jme3.scene.Spatial)1