From f28c31d8512a9c9bd5dbfff33f21b1874f40aebc Mon Sep 17 00:00:00 2001 From: Rena4ka Date: Mon, 18 Aug 2014 06:19:28 +0400 Subject: [PATCH] Fix and extend FBX file loader Fix using AssetManager.loadModel() with fbx file causing to throw AssetLoadException. Implemented loading of new file format .fba - .fbx file with animation mapping. --- .../main/resources/com/jme3/asset/Desktop.cfg | 1 + .../jme3/scene/plugins/fbx/SceneLoader.java | 10 +-- .../plugins/fbx/SceneWithAnimationLoader.java | 67 +++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneWithAnimationLoader.java diff --git a/jme3-core/src/main/resources/com/jme3/asset/Desktop.cfg b/jme3-core/src/main/resources/com/jme3/asset/Desktop.cfg index 01bc5045e..60b86fbc1 100644 --- a/jme3-core/src/main/resources/com/jme3/asset/Desktop.cfg +++ b/jme3-core/src/main/resources/com/jme3/asset/Desktop.cfg @@ -23,3 +23,4 @@ LOADER com.jme3.scene.plugins.ogre.SceneLoader : scene LOADER com.jme3.scene.plugins.blender.BlenderModelLoader : blend LOADER com.jme3.shader.plugins.GLSLLoader : vert, frag, glsl, glsllib LOADER com.jme3.scene.plugins.fbx.SceneLoader : fbx +LOADER com.jme3.scene.plugins.fbx.SceneWithAnimationLoader : fba diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneLoader.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneLoader.java index 545d4cc92..db0925643 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneLoader.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneLoader.java @@ -55,6 +55,7 @@ import com.jme3.asset.AssetKey; import com.jme3.asset.AssetLoadException; import com.jme3.asset.AssetLoader; import com.jme3.asset.AssetManager; +import com.jme3.asset.ModelKey; import com.jme3.material.Material; import com.jme3.material.RenderState.BlendMode; import com.jme3.math.ColorRGBA; @@ -92,7 +93,7 @@ public class SceneLoader implements AssetLoader { private static final Logger logger = Logger.getLogger(SceneLoader.class.getName()); private AssetManager assetManager; - private SceneKey key; + private AnimationList animList; private String sceneName; private String sceneFilename; @@ -132,8 +133,8 @@ public class SceneLoader implements AssetLoader { this.assetManager = assetInfo.getManager(); AssetKey assetKey = assetInfo.getKey(); if(assetKey instanceof SceneKey) - key = (SceneKey) assetKey; - else + animList = ((SceneKey) assetKey).getAnimations(); + else if(!(assetKey instanceof ModelKey)) throw new AssetLoadException("Invalid asset key"); InputStream stream = assetInfo.openStream(); Node sceneNode = null; @@ -1264,7 +1265,6 @@ public class SceneLoader implements AssetLoader { private void linkAnimations() { if(skeleton == null) return; - AnimationList animList = key.getAnimations(); if(animList == null || animList.list.size() == 0) return; // Link curves to nodes @@ -1449,7 +1449,7 @@ public class SceneLoader implements AssetLoader { imgMap.clear(); skeleton = null; animControl = null; - key = null; + animList = null; } private class MeshData { diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneWithAnimationLoader.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneWithAnimationLoader.java new file mode 100644 index 000000000..349bbb478 --- /dev/null +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/SceneWithAnimationLoader.java @@ -0,0 +1,67 @@ +package com.jme3.scene.plugins.fbx; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.jme3.asset.AssetInfo; +import com.jme3.asset.AssetKey; +import com.jme3.asset.AssetLoadException; +import com.jme3.asset.AssetLoader; +import com.jme3.asset.ModelKey; + +public class SceneWithAnimationLoader implements AssetLoader { + + private Pattern splitStrings = Pattern.compile("([^\"]\\S*|\".+?\")\\s*"); + + @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)); + } + + private String[] split(String src) { + List list = new ArrayList(); + Matcher m = splitStrings.matcher(src); + while(m.find()) + list.add(m.group(1).replace("\"", "")); + return list.toArray(new String[list.size()]); + } + +}