Browse Source

minor script fixes

ncannasse 7 years ago
parent
commit
bd12e47790
2 changed files with 60 additions and 9 deletions
  1. 20 0
      hide/comp/Scene.hx
  2. 40 9
      hide/tools/MaterialScript.hx

+ 20 - 0
hide/comp/Scene.hx

@@ -95,6 +95,7 @@ class Scene extends Component implements h3d.IDrawable {
 		stage = @:privateAccess new hxd.Stage(canvas);
 		stage = @:privateAccess new hxd.Stage(canvas);
 		stage.setCurrent();
 		stage.setCurrent();
 		engine = new h3d.Engine();
 		engine = new h3d.Engine();
+		@:privateAccess engine.resCache.set(Scene, this);
 		engine.backgroundColor = 0xFF111111;
 		engine.backgroundColor = 0xFF111111;
 		canvas.id = null;
 		canvas.id = null;
 		engine.onReady = function() {
 		engine.onReady = function() {
@@ -282,6 +283,9 @@ class Scene extends Component implements h3d.IDrawable {
 				return fullPath;
 				return fullPath;
 		}
 		}
 
 
+		if( modelPath == null )
+			return null;
+
 		filePath = filePath.split("\\").join("/");
 		filePath = filePath.split("\\").join("/");
 		modelPath = ide.getPath(modelPath);
 		modelPath = ide.getPath(modelPath);
 
 
@@ -294,6 +298,18 @@ class Scene extends Component implements h3d.IDrawable {
 		return null;
 		return null;
 	}
 	}
 
 
+	public function loadTextureDotPath( path : String, ?onReady ) {
+		var path = path.split(".").join("/");
+		var t = resolvePath(null, path + ".png");
+		if( t == null )
+			t = resolvePath(null, path + ".jpg");
+		if( t == null )
+			t = resolvePath(null, path + ".jpeg");
+		if( t == null )
+			t = path;
+		return loadTexture("", t, onReady);
+	}
+
 	public function loadTexture( modelPath : String, texturePath : String, ?onReady : h3d.mat.Texture -> Void ) {
 	public function loadTexture( modelPath : String, texturePath : String, ?onReady : h3d.mat.Texture -> Void ) {
 		var path = resolvePath(modelPath, texturePath);
 		var path = resolvePath(modelPath, texturePath);
 		if( path == null ) {
 		if( path == null ) {
@@ -389,4 +405,8 @@ class Scene extends Component implements h3d.IDrawable {
 		return null;
 		return null;
 	}
 	}
 
 
+	public static function getCurrent() : Scene {
+		return @:privateAccess h3d.Engine.getCurrent().resCache.get(Scene);
+	}
+
 }
 }

+ 40 - 9
hide/tools/MaterialScript.hx

@@ -25,6 +25,31 @@ class RendererScript extends h3d.scene.Renderer {
 
 
 }
 }
 
 
+class ResourceLoader {
+
+	var __path : Array<String>;
+
+	public function new(p) {
+		__path = p;
+	}
+
+	public function toTexture() {
+		return hide.comp.Scene.getCurrent().loadTextureDotPath(__path.join("."));
+	}
+
+	public function hscriptGet( field : String ) {
+
+		var f = Reflect.field(this,field);
+		if( f != null )
+			return Reflect.makeVarArgs(function(args) return Reflect.callMethod(this, f, args));
+
+		var p = __path.copy();
+		p.push(field);
+		return new ResourceLoader(p);
+	}
+
+}
+
 class MaterialScript extends h3d.mat.MaterialScript {
 class MaterialScript extends h3d.mat.MaterialScript {
 
 
 	var ide : hide.ui.Ide;
 	var ide : hide.ui.Ide;
@@ -50,6 +75,19 @@ class MaterialScript extends h3d.mat.MaterialScript {
 		return objs;
 		return objs;
 	}
 	}
 
 
+	function lookupShader( shader : hxsl.Shader, ?passName : String ) {
+		var s = @:privateAccess shader.shader;
+		var scene = hide.comp.Scene.getCurrent();
+		for( m in scene.s3d.getMaterials() )
+			for( p in m.getPasses() ) {
+				if( passName != null && p.name != passName ) continue;
+				for( ss in p.getShaders() )
+					if( @:privateAccess ss.shader == s )
+						return ss;
+			}
+		return shader;
+	}
+
 	function makeClass( c : hscript.Expr.ClassDecl, ?args : Array<Dynamic> ) {
 	function makeClass( c : hscript.Expr.ClassDecl, ?args : Array<Dynamic> ) {
 		var interp = new Interp();
 		var interp = new Interp();
 		var obj = null;
 		var obj = null;
@@ -67,15 +105,8 @@ class MaterialScript extends h3d.mat.MaterialScript {
 			throw "Don't know what to do with " + c.name;
 			throw "Don't know what to do with " + c.name;
 
 
 		interp.variables.set("loadShader", function(name) return ide.shaderLoader.load(name));
 		interp.variables.set("loadShader", function(name) return ide.shaderLoader.load(name));
-
-		var debug = true;
-		interp.variables.set("loadTexture", function(name) return {
-			if( debug ) {
-				trace("TODO:" + name);
-				debug = false;
-			}
-			return h3d.mat.Texture.fromColor(0);
-		});
+		interp.variables.set("lookupShader", lookupShader);
+		interp.variables.set("hxd", { Res : new ResourceLoader([]) });
 
 
 		for( f in c.fields )
 		for( f in c.fields )
 			switch( f.kind ) {
 			switch( f.kind ) {