Bladeren bron

Merge pull request #169 from GreenCubes/master

Fix and extend FBX file loader
normen 11 jaren geleden
bovenliggende
commit
d921847f35

+ 1 - 0
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

+ 5 - 5
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 {

+ 67 - 0
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<String> list = new ArrayList<String>();
+		Matcher m = splitStrings.matcher(src);
+		while(m.find())
+			list.add(m.group(1).replace("\"", ""));
+		return list.toArray(new String[list.size()]);
+	}
+	
+}