浏览代码

working basic SCN viewer

Nicolas Cannasse 8 年之前
父节点
当前提交
77b3e9c94e
共有 5 个文件被更改,包括 80 次插入14 次删除
  1. 2 0
      hide.hxml
  2. 1 1
      hide.hxproj
  3. 70 8
      hide/comp/Scene.hx
  4. 6 4
      hide/comp/SceneTree.hx
  5. 1 1
      hide/view/Model.hx

+ 2 - 0
hide.hxml

@@ -4,4 +4,6 @@
 -lib hxnodejs
 -lib heaps
 -dce full
+--macro keep('h3d.prim')
 --macro include('hide.view')
+--macro include('h3d.prim')

+ 1 - 1
hide.hxproj

@@ -24,7 +24,7 @@
     <option noInlineOnDebug="False" />
     <option mainClass="hide.ui.Ide" />
     <option enabledebug="False" />
-    <option additional="-lib hxnodejs&#xA;-lib heaps&#xA;-dce full&#xA;&#xA;--macro include('hide.view')" />
+    <option additional="-lib hxnodejs&#xA;-lib heaps&#xA;-dce full&#xA;&#xA;--macro keep('h3d.prim')&#xA;--macro include('hide.view')&#xA;--macro include('h3d.prim')" />
   </build>
   <!-- haxelib libraries -->
   <haxelib>

+ 70 - 8
hide/comp/Scene.hx

@@ -1,8 +1,40 @@
 package hide.comp;
 
+@:access(hide.comp.Scene)
+class SceneLoader extends h3d.impl.Serializable.SceneSerializer {
+
+	var scnPath : String;
+	var projectPath : String;
+	var scene : Scene;
+
+	public function new(scnPath,scene) {
+		super();
+		this.scnPath = scnPath;
+		this.scene = scene;
+	}
+
+	override function initSCNPaths(resPath:String, projectPath:String) {
+		this.resPath = resPath.split("\\").join("/");
+		this.projectPath = projectPath == null ? null : projectPath.split("\\").join("/");
+		trace(this.resPath, this.projectPath);
+	}
+
+	override function resolveTexture(path:String) {
+		var t = null;
+		if( projectPath != null )
+			t = scene.loadTextureFile(projectPath + resPath + "/" + scnPath.split("/").pop(), path);
+		if( t == null )
+			t = scene.loadTextureFile(scnPath, path);
+		if( t == null )
+			t = h3d.mat.Texture.fromColor(0xFF00FF);
+		return t;
+	}
+
+}
+
 class Scene extends Component implements h3d.IDrawable {
 
-	static var UID = 0; 
+	static var UID = 0;
 
 	var id = ++UID;
 	var stage : hxd.Stage;
@@ -44,6 +76,7 @@ class Scene extends Component implements h3d.IDrawable {
 			sevents.addScene(s2d);
 			sevents.addScene(s3d);
 			onReady();
+			sync();
 			ide.registerUpdate(sync);
 		};
 		engine.onResized = function() {
@@ -88,24 +121,53 @@ class Scene extends Component implements h3d.IDrawable {
 		});
 	}
 
+	function loadSCN( path : String ) {
+		var ctx = new SceneLoader(path,this);
+		var fullPath = ide.getPath(path);
+		var bytes = sys.io.File.getBytes(fullPath);
+		var root = new h3d.scene.Object();
+		for( o in ctx.loadSCN(bytes).content )
+			root.addChild(o);
+		return root;
+	}
+
 	public function loadModel( path : String ) {
+		if( StringTools.endsWith(path.toLowerCase(), ".scn") )
+			return loadSCN(path);
 		var lib = loadHMD(path,false);
-		return lib.makeObject(loadHMDTexture.bind(path));
+		return lib.makeObject(loadTextureFile.bind(path));
 	}
 
 	public function loadAnimation( path : String ) {
 		var lib = loadHMD(path,true);
 		return lib.loadAnimation();
 	}
- 
-	function loadHMDTexture( basePath : String, texturePath : String ) {
-		if( sys.FileSystem.exists(texturePath) ) {
+
+	function resolveTexturePath( modelPath : String, texturePath : String ) {
+		inline function exists(path) return sys.FileSystem.exists(path);
+		if( exists(texturePath) )
+			return texturePath;
+		texturePath = texturePath.split("\\").join("/");
+		modelPath = ide.getPath(modelPath);
+
+		var path = modelPath.split("/");
+		path.pop();
+		var relToModel = path.join("/") + "/" + texturePath.split("/").pop();
+		if( exists(relToModel) )
+			return relToModel;
+
+		return null;
+	}
+
+	function loadTextureFile( modelPath : String, texturePath : String ) {
+		var path = resolveTexturePath(modelPath, texturePath);
+		if( path != null ) {
 			var t = new h3d.mat.Texture(1,1);
 			t.clear(0x102030);
-			loadTexture(texturePath, function(_) {}, t);
+			loadTexture(path, function(_) {}, t);
 			return t;
 		}
-		trace("TODO:",basePath, texturePath);
+		trace("Could not load texture " + { modelPath : modelPath, texturePath : texturePath });
 		return null;
 	}
 
@@ -129,7 +191,7 @@ class Scene extends Component implements h3d.IDrawable {
 		s2d.render(e);
 	}
 
-	public dynamic function onUpdate(dt:Float) {	
+	public dynamic function onUpdate(dt:Float) {
 	}
 
 	public dynamic function onReady() {

+ 6 - 4
hide/comp/SceneTree.hx

@@ -2,16 +2,18 @@ package hide.comp;
 
 class SceneTree extends IconTree {
 
+	var showRoot : Bool;
 	public var obj : h3d.scene.Object;
 
-	public function new(obj, root) {
+	public function new(obj, root, showRoot : Bool) {
 		super(root);
+		this.showRoot = showRoot;
 		this.obj = obj;
 		init();
 	}
 
 	override function get( id : String ) {
-		var root = obj.parent;
+		var root = showRoot ? obj.parent : obj;
 		var path = id == null ? "" : id+"/";
 		if( id != null ) {
 			var parts = [for(p in id.split("/")) Std.parseInt(p)];
@@ -23,7 +25,7 @@ class SceneTree extends IconTree {
 				var c = root.getChildAt(i);
 				{
 					id : path+i,
-					text : c.name,
+					text : c.name == null ? c.toString()+"@"+i : c.name,
 					icon : "fa fa-" + (c.isMesh() ? (Std.is(c,h3d.scene.Skin) ? "male" : "cube") : "circle-o"),
 					children : c.isMesh() || c.numChildren > 0,
 				}
@@ -33,7 +35,7 @@ class SceneTree extends IconTree {
 			function makeMaterial( m : h3d.mat.Material, index : Int ) : IconTree.IconTreeItem {
 				return {
 					id : path+"mat"+index,
-					text : m.name,
+					text : m.name == null ? "Material@"+index : m.name,
 					icon : "fa fa-photo",
 				};
 			}

+ 1 - 1
hide/view/Model.hx

@@ -21,7 +21,7 @@ class Model extends FileView {
 		obj = scene.loadModel(state.path);
 		new h3d.scene.Object(scene.s3d).addChild(obj);
 		control = new h3d.scene.CameraController(scene.s3d);
-		tree = new hide.comp.SceneTree(obj,scroll.content);
+		tree = new hide.comp.SceneTree(obj,scroll.content, obj.name != null);
 		resetCamera();
 
 		var anims = listAnims();