use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.
the class SceneWithAnimationLoader method load.
@Override
public Object load(AssetInfo assetInfo) throws IOException {
AssetKey<?> key = assetInfo.getKey();
if (!(key instanceof ModelKey))
throw new AssetLoadException("Invalid asset key");
InputStream stream = assetInfo.openStream();
Scanner scanner = new Scanner(stream);
AnimationList animList = new AnimationList();
String modelName = null;
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#"))
continue;
if (modelName == null) {
modelName = line;
continue;
}
String[] split = split(line);
if (split.length < 3)
throw new IOException("Unparseable string \"" + line + "\"");
int start;
int end;
try {
start = Integer.parseInt(split[0]);
end = Integer.parseInt(split[1]);
} catch (NumberFormatException e) {
throw new IOException("Unparseable string \"" + line + "\"", e);
}
animList.add(split[2], split.length > 3 ? split[3] : null, start, end);
}
} finally {
scanner.close();
stream.close();
}
return assetInfo.getManager().loadAsset(new SceneKey(key.getFolder() + modelName, animList));
}
use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.
the class AssetLinkNode method attachLinkedChildren.
/**
* Loads the linked children AssetKeys from the AssetManager and attaches them to the Node<br>
* If they are already attached, they will be reloaded.
* @param manager
*/
public void attachLinkedChildren(AssetManager manager) {
detachLinkedChildren();
for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext(); ) {
ModelKey assetKey = it.next();
Spatial curChild = assetChildren.get(assetKey);
if (curChild != null) {
curChild.removeFromParent();
}
Spatial child = manager.loadAsset(assetKey);
attachChild(child);
assetChildren.put(assetKey, child);
}
}
use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.
the class AssetLinkNode method read.
@Override
public void read(JmeImporter e) throws IOException {
super.read(e);
InputCapsule capsule = e.getCapsule(this);
BinaryImporter importer = BinaryImporter.getInstance();
AssetManager loaderManager = e.getAssetManager();
assetLoaderKeys = (ArrayList<ModelKey>) capsule.readSavableArrayList("assetLoaderKeyList", new ArrayList<ModelKey>());
for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext(); ) {
ModelKey modelKey = it.next();
AssetInfo info = loaderManager.locateAsset(modelKey);
Spatial child = null;
if (info != null) {
child = (Spatial) importer.load(info);
}
if (child != null) {
child.parent = this;
children.add(child);
assetChildren.put(modelKey, child);
} else {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot locate {0} for asset link node {1}", new Object[] { modelKey, key });
}
}
}
use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.
the class BlenderLoader method setup.
/**
* This method sets up the loader.
* @param assetInfo
* the asset info
* @throws BlenderFileException
* an exception is throw when something wrong happens with blender file
*/
protected BlenderContext setup(AssetInfo assetInfo) throws BlenderFileException {
// registering loaders
ModelKey modelKey = (ModelKey) assetInfo.getKey();
BlenderKey blenderKey;
if (modelKey instanceof BlenderKey) {
blenderKey = (BlenderKey) modelKey;
} else {
blenderKey = new BlenderKey(modelKey.getName());
}
// opening stream
BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream());
// reading blocks
List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
FileBlockHeader fileBlock;
BlenderContext blenderContext = new BlenderContext();
blenderContext.setBlenderVersion(inputStream.getVersionNumber());
blenderContext.setAssetManager(assetInfo.getManager());
blenderContext.setInputStream(inputStream);
blenderContext.setBlenderKey(blenderKey);
// creating helpers
blenderContext.putHelper(AnimationHelper.class, new AnimationHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber(), blenderContext));
blenderContext.putHelper(LandscapeHelper.class, new LandscapeHelper(inputStream.getVersionNumber(), blenderContext));
// reading the blocks (dna block is automatically saved in the blender context when found)
FileBlockHeader sceneFileBlock = null;
do {
fileBlock = new FileBlockHeader(inputStream, blenderContext);
if (!fileBlock.isDnaBlock()) {
blocks.add(fileBlock);
// save the scene's file block
if (fileBlock.getCode() == BlockCode.BLOCK_SC00) {
sceneFileBlock = fileBlock;
}
}
} while (!fileBlock.isLastBlock());
if (sceneFileBlock != null) {
blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext));
}
// adding locator for linked content
assetInfo.getManager().registerLocator(assetInfo.getKey().getName(), LinkedContentLocator.class);
return blenderContext;
}
use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.
the class TestAssetLinkNode method simpleInitApp.
@Override
public void simpleInitApp() {
AssetLinkNode loaderNode = new AssetLinkNode();
loaderNode.addLinkedChild(new ModelKey("Models/MonkeyHead/MonkeyHead.mesh.xml"));
//save and load the loaderNode
try {
//export to byte array
ByteArrayOutputStream bout = new ByteArrayOutputStream();
BinaryExporter.getInstance().save(loaderNode, bout);
//import from byte array, automatically loads the monkeyhead from file
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
BinaryImporter imp = BinaryImporter.getInstance();
imp.setAssetManager(assetManager);
Node newLoaderNode = (Node) imp.load(bin);
//attach to rootNode
rootNode.attachChild(newLoaderNode);
} catch (IOException ex) {
Logger.getLogger(TestAssetLinkNode.class.getName()).log(Level.SEVERE, null, ex);
}
rootNode.attachChild(loaderNode);
lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
lightMdl.setMaterial((Material) assetManager.loadAsset(new AssetKey("Common/Materials/RedColor.j3m")));
rootNode.attachChild(lightMdl);
// flourescent main light
pl = new PointLight();
pl.setColor(new ColorRGBA(0.88f, 0.92f, 0.95f, 1.0f));
rootNode.addLight(pl);
// sunset light
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.1f, -0.7f, 1).normalizeLocal());
dl.setColor(new ColorRGBA(0.44f, 0.30f, 0.20f, 1.0f));
rootNode.addLight(dl);
// skylight
dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.6f, -1, -0.6f).normalizeLocal());
dl.setColor(new ColorRGBA(0.10f, 0.22f, 0.44f, 1.0f));
rootNode.addLight(dl);
// white ambient light
dl = new DirectionalLight();
dl.setDirection(new Vector3f(1, -0.5f, -0.1f).normalizeLocal());
dl.setColor(new ColorRGBA(0.50f, 0.40f, 0.50f, 1.0f));
rootNode.addLight(dl);
}
Aggregations