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;
}
}
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;
}
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;
}
}
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;
}
Aggregations