* @return a ColladaStorage data object containing the Collada scene and other useful elements.
* @throws IOException
* if the resource can not be loaded for some reason.
*/
public ColladaStorage load(final ResourceSource resource) throws IOException {
final ColladaStorage colladaStorage = new ColladaStorage();
final DataCache dataCache = new DataCache();
if (_externalJointMapping != null) {
dataCache.getExternalJointMapping().putAll(_externalJointMapping);
}
final ColladaDOMUtil colladaDOMUtil = new ColladaDOMUtil(dataCache);
final ColladaMaterialUtils colladaMaterialUtils = new ColladaMaterialUtils(this, dataCache, colladaDOMUtil);
final ColladaMeshUtils colladaMeshUtils = new ColladaMeshUtils(dataCache, colladaDOMUtil, colladaMaterialUtils,
_optimizeMeshes, _optimizeSettings);
final ColladaAnimUtils colladaAnimUtils = new ColladaAnimUtils(colladaStorage, dataCache, colladaDOMUtil,
colladaMeshUtils);
final ColladaNodeUtils colladaNodeUtils = new ColladaNodeUtils(dataCache, colladaDOMUtil, colladaMaterialUtils,
colladaMeshUtils, colladaAnimUtils);
try {
// Pull in the DOM tree of the Collada resource.
final Element collada = readCollada(resource, dataCache);
// if we don't specify a texture locator, add a temporary texture locator at the location of this model
// resource..
final boolean addLocator = _textureLocator == null;
final RelativeResourceLocator loc;
if (addLocator) {
loc = new RelativeResourceLocator(resource);
ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, loc);
} else {
loc = null;
}
final AssetData assetData = colladaNodeUtils.parseAsset(collada.getChild("asset"));
// Collada may or may not have a scene, so this can return null.
final Node scene = colladaNodeUtils.getVisualScene(collada);
if (_loadAnimations) {
colladaAnimUtils.parseLibraryAnimations(collada);
}
// reattach attachments to scene
if (scene != null) {
colladaNodeUtils.reattachAttachments(scene);
}
// set our scene into storage
colladaStorage.setScene(scene);
// set our asset data into storage
colladaStorage.setAssetData(assetData);
// drop our added locator if needed.
if (addLocator) {
ResourceLocatorTool.removeResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, loc);
}
// copy across our mesh colors - only for objects with multiple channels
final Multimap<MeshData, FloatBuffer> colors = ArrayListMultimap.create();
final Multimap<MeshData, FloatBuffer> temp = dataCache.getParsedVertexColors();
for (final MeshData key : temp.keySet()) {
// only copy multiple channels since their data is lost
final Collection<FloatBuffer> val = temp.get(key);
if (val != null && val.size() > 1) {
colors.putAll(key, val);
}
}
colladaStorage.setParsedVertexColors(colors);
// copy across our mesh material info
colladaStorage.setMeshMaterialInfo(dataCache.getMeshMaterialMap());
colladaStorage.setMaterialMap(dataCache.getMaterialInfoMap());
// return storage
return colladaStorage;
} catch (final Exception e) {
throw new IOException("Unable to load collada resource from URL: " + resource, e);