Ver Fonte

Shader properties working

trethaller há 7 anos atrás
pai
commit
4f38327174
4 ficheiros alterados com 111 adições e 10 exclusões
  1. 9 1
      bin/style.css
  2. 10 1
      bin/style.less
  3. 2 2
      hide/comp/PropsEditor.hx
  4. 90 6
      hide/prefab/Shader.hx

+ 9 - 1
bin/style.css

@@ -259,7 +259,7 @@ input[type=checkbox]:checked:after {
   width: 145px;
 }
 .hide-range input[type=range].small {
-  width: 133px;
+  width: 125px;
 }
 .hide-range input[type=text] {
   display: inline-block;
@@ -465,6 +465,11 @@ input[type=checkbox]:checked:after {
   overflow: hidden;
   margin-top: 4px;
 }
+.hide-properties dd span,
+.hide-properties dt span {
+  display: inline-block;
+  width: 20px;
+}
 .hide-properties dt {
   width: 80px;
   text-align: right;
@@ -476,6 +481,9 @@ input[type=checkbox]:checked:after {
 .hide-properties input {
   width: 190px;
 }
+.hide-properties input.small {
+  width: 170px;
+}
 .hide-properties input[type=checkbox] {
   width: 13px;
 }

+ 10 - 1
bin/style.less

@@ -276,7 +276,7 @@ input[type=checkbox] {
 	}
 
 	input[type=range].small {
-		width: 133px;
+		width: 125px;
 	}
 
 	input[type=text] {
@@ -494,6 +494,11 @@ input[type=checkbox] {
 		display:inline-block;
 		overflow:hidden;
 		margin-top:4px;
+
+		span {
+			display: inline-block;
+			width: 20px;
+		}	
 	}
 
 	dt {
@@ -509,6 +514,10 @@ input[type=checkbox] {
 		width:190px;
 	}
 
+	input.small {
+		width:170px;
+	}
+
 	input[type=checkbox] {
 		width:13px;
 	}

+ 2 - 2
hide/comp/PropsEditor.hx

@@ -42,7 +42,7 @@ class PropsEditor extends Component {
 			def.appendTo(parent);
 	}
 
-	public function addProps( props : Array<{ name : String, t : PropType }>, context : Dynamic ) {
+	public function addProps( props : Array<{ name : String, t : PropType }>, context : Dynamic, ?onChange : String -> Void) {
 		var e = new Element('<dl>');
 		for( p in props ) {
 			new Element('<dt>${p.name}</dt>').appendTo(e);
@@ -74,7 +74,7 @@ class PropsEditor extends Component {
 				}
 			}
 		}
-		return add(e, context);
+		return add(e, context, onChange);
 	}
 
 	public function add( e : Element, ?context : Dynamic, ?onChange : String -> Void ) {

+ 90 - 6
hide/prefab/Shader.hx

@@ -18,35 +18,119 @@ class Shader extends Prefab {
 		};
 	}
 
+	public function applyVars(ctx: Context) {
+		var shader = Std.instance(ctx.custom, hxsl.DynamicShader);
+		if(shader == null || shaderDef == null)
+			return;
+		for(v in shaderDef.shader.data.vars) {
+			if(v.kind != Param)
+				continue;
+			var val : Dynamic = Reflect.field(props, v.name);
+			switch(v.type) {
+				case TVec( size, VFloat ):
+					var a = Std.instance(val, Array);
+					if(a == null)
+						continue;
+					val = h3d.Vector.fromArray(a);
+				default:
+			}
+			if(val == null)
+				continue;
+			shader.setParamValue(v, val);
+		}
+	}
+
 	override function makeInstance(ctx:Context):Context {
 		if(source == null)
 			return ctx;
 		if(ctx.local3d == null)
 			return ctx;
 		ctx = ctx.clone(this);
-		if(shaderDef == null)
-			shaderDef = ctx.loadShader("shaders/TestShader");
+		loadShaderDef(ctx);
 		if(shaderDef == null)
 			return ctx;
 		var shader = new hxsl.DynamicShader(shaderDef.shader);
-		for( v in shaderDef.inits )
-			shader.hscriptSet(v.v.name, hxsl.Ast.Tools.evalConst(v.e));
+		for( v in shaderDef.inits ) {
+			var defVal = hxsl.Ast.Tools.evalConst(v.e);
+			shader.hscriptSet(v.v.name, defVal);
+		}
 		ctx.custom = shader;
 		if(shader != null) {
 			for(m in ctx.local3d.getMaterials()) {
 				m.mainPass.addShader(shader);
 			}
 		}
+		applyVars(ctx);
 		return ctx;
 	}
 
+	function loadShaderDef(ctx: Context) {
+		if(shaderDef == null)
+			shaderDef = ctx.loadShader("shaders/TestShader");
+
+		// TODO: Where to init prefab default values?
+		for( v in shaderDef.inits ) {
+			if(!Reflect.hasField(props, v.v.name)) {
+				var defVal = hxsl.Ast.Tools.evalConst(v.e);
+				Reflect.setField(props, v.v.name, defVal);
+			}
+		}
+		for(v in shaderDef.shader.data.vars) {
+			if(v.kind != Param)
+				continue;
+			if(!Reflect.hasField(props, v.name)) {
+				Reflect.setField(props, v.name, getDefault(v.type));
+			}
+		}
+	}
+
+	static function getDefault(type: hxsl.Ast.Type): Dynamic {
+		switch(type) {
+			case TBool:
+				return false;
+			case TInt:
+				return 0;
+			case TFloat:
+				return 0.0;
+			case TVec( size, VFloat ):
+				return [for(i in 0...size) 0];
+			default:
+				return null;
+		}
+		return null;
+	}
+
 	override function edit( ctx : EditContext ) {
 		super.edit(ctx);
 
-		var shader = shctx.shader;
+		loadShaderDef(ctx.rootContext);
+		if(shaderDef == null)
+			return;
+
+		var props = [];
 		for(v in shaderDef.shader.data.vars) {
-			// TODO
+			if(v.kind != Param)
+				continue;
+			var prop : hide.comp.PropsEditor.PropType;
+			switch(v.type) {
+				case TBool:
+					prop = PBool;
+				case TInt:
+					prop = PInt(0, 10);
+				case TFloat:
+					prop = PFloat(-1.0, 1.0);
+				case TVec( size, VFloat ):
+					prop = PVec(size);
+				default:
+					prop = PUnsupported('${v.type}');
+			}
+			props.push({name: v.name, t: prop});
 		}
+		ctx.properties.addProps(props, this.props, function(pname) {
+			ctx.onChange(this, pname);
+			var inst = ctx.getContext(this);
+			applyVars(inst);
+		});
 	}
 
 	override function getHideProps() {