Browse Source

ShaderGraph: various fixes + add UV Scroll node

Tom Spira 4 years ago
parent
commit
fccae1b5a1

+ 34 - 7
hide/view/shadereditor/ShaderEditor.hx

@@ -135,7 +135,7 @@ class ShaderEditor extends hide.view.Graph {
 				return;
 			} else if (e.shiftKey) {
 				if (addMenu == null || !addMenu.is(":visible"))
-					openAddMenu();
+					openAddMenu(-40, -70);
 
 				return;
 			}
@@ -473,7 +473,9 @@ class ShaderEditor extends hide.view.Graph {
 					elt.attr("draggable", "true");
 					afterChange();
 				});
-				if (value != null) range.value = value;
+				if (value == null) value = 0;
+				range.value = value;
+				shaderGraph.setParameterDefaultValue(id, value);
 				range.onChange = function(moving) {
 					if (!shaderGraph.setParameterDefaultValue(id, range.value))
 						return;
@@ -485,11 +487,11 @@ class ShaderEditor extends hide.view.Graph {
 				var parentPicker = new Element('<div style="width: 35px; height: 25px; display: inline-block;"></div>').appendTo(defaultValue);
 				var picker = new hide.comp.ColorPicker(true, parentPicker);
 
-				var start : h3d.Vector;
-				if (value != null)
-					start = h3d.Vector.fromArray(value);
-				else
-					start = h3d.Vector.fromArray([0, 0, 0, 1]);
+				
+				if (value == null)
+					value = [0, 0, 0, 1];
+				var start : h3d.Vector = h3d.Vector.fromArray(value);
+				shaderGraph.setParameterDefaultValue(id, value);
 				picker.value = start.toColor();
 				picker.onChange = function(move) {
 					var vecColor = h3d.Vector.fromColor(picker.value);
@@ -648,6 +650,7 @@ class ShaderEditor extends hide.view.Graph {
 		var paramShader = shaderGraph.getParameter(paramShaderID);
 
 		var elt = addParameter(paramShaderID, paramShader.name, type, null);
+		updateParam(paramShaderID);
 
 		elt.find(".input-title").focus();
 	}
@@ -861,6 +864,9 @@ class ShaderEditor extends hide.view.Graph {
 
 			addMenu.css("left", posCursor.x);
 			addMenu.css("top", posCursor.y);
+			for (c in addMenu.find("#results").children().elements()) {
+				c.show();
+			}
 			return;
 		}
 
@@ -1190,6 +1196,27 @@ class ShaderEditor extends hide.view.Graph {
 		return haxe.io.Bytes.ofString(ide.toJSON(p));
 	}
 
+	override function onDragDrop(items : Array<String>, isDrop : Bool) {
+		var valid = false;
+		var offset = 0;
+		for (i in items) {
+			if (i.indexOf("hlshader") != -1 && i != state.path) {
+				if (isDrop) {
+					var posCursor = new Point(lX(ide.mouseX - 25 + offset), lY(ide.mouseY - 10 + offset));
+					var node : SubGraph = cast addNode(posCursor, SubGraph);
+					@:privateAccess node.pathShaderGraph = i;
+					node.loadGraphShader();
+					offset += 25;
+				}
+				valid = true;
+			}
+		}
+		if (valid && isDrop) {
+			refreshShaderGraph();
+		}
+		return valid;
+	}
+
 	static var _ = FileTree.registerExtension(ShaderEditor,["hlshader"],{ icon : "scribd", createNew: "Shader Graph" });
 
 }

+ 4 - 4
hrt/shgraph/ShaderGlobalInput.hx

@@ -8,10 +8,10 @@ using hxsl.Ast;
 @color("#0e8826")
 class ShaderGlobalInput extends ShaderInput {
 
-	static var globalInputs = [	{ parent: null, id: 0, kind: Global, name: "global.time", type: TFloat },
-								{ parent: null, id: 0, kind: Global, name: "global.pixelSize", type: TVec(2, VFloat) },
-								{ parent: null, id: 0, kind: Global, name: "global.modelView", type: TMat4 },
-								{ parent: null, id: 0, kind: Global, name: "global.modelViewInverse", type: TMat4 } ];
+	static public var globalInputs = [	{ parent: null, id: 0, kind: Global, name: "global.time", type: TFloat },
+										{ parent: null, id: 0, kind: Global, name: "global.pixelSize", type: TVec(2, VFloat) },
+										{ parent: null, id: 0, kind: Global, name: "global.modelView", type: TMat4 },
+										{ parent: null, id: 0, kind: Global, name: "global.modelViewInverse", type: TMat4 } ];
 
 	override public function loadProperties(props : Dynamic) {
 		var paramVariable : String = Reflect.field(props, "variable");

+ 4 - 0
hrt/shgraph/ShaderGraph.hx

@@ -300,6 +300,10 @@ class ShaderGraph {
 
 		var outputs : Array<String> = [];
 
+		for (g in ShaderGlobalInput.globalInputs) {
+			allVariables.push(g);
+		}
+
 		for (n in nodes) {
 			var shaderNode;
 			var variable;

+ 1 - 1
hrt/shgraph/nodes/FloatConst.hx

@@ -2,7 +2,7 @@ package hrt.shgraph.nodes;
 
 using hxsl.Ast;
 
-@name("Number")
+@name("Value")
 @description("Number input (static)")
 @group("Property")
 @width(100)

+ 1 - 1
hrt/shgraph/nodes/Mod.hx

@@ -8,7 +8,7 @@ using hxsl.Ast;
 @group("Math")
 class Mod extends ShaderFunction {
 
-	@input("x") var x = SType.Float;
+	@input("x") var x = SType.Variant;
 	@input("mod", true) var mod = SType.Float;
 
 	public function new() {

+ 6 - 5
hrt/shgraph/nodes/Preview.hx

@@ -103,10 +103,13 @@ class Preview extends ShaderNode {
 			for (m in cube.getMaterials()) {
 				m.mainPass.removeShader(currentShaderPreview);
 			}
+			currentShaderPreview = null;
 		}
 
 		if (scene == null || input == null || input.isEmpty()) return;
 
+		if (@:privateAccess scene.window == null)
+			return;
 		scene.setCurrent();
 
 		var shader : hxsl.DynamicShader = null;
@@ -146,12 +149,10 @@ class Preview extends ShaderNode {
 			switch (variable.type) {
 				case TSampler2D:
 					shader.setParamValue(variable, scene.loadTexture("", value));
+				case TVec(size, _):
+					shader.setParamValue(variable, h3d.Vector.fromArray(value));
 				default:
-					if (variable.name.toLowerCase().indexOf("color") != -1) {
-						shader.setParamValue(variable, h3d.Vector.fromArray(value));
-					} else {
-						shader.setParamValue(variable, value);
-					}
+					shader.setParamValue(variable, value);
 			}
 		} catch (e : Dynamic) {
 			// variable not used

+ 109 - 0
hrt/shgraph/nodes/UVScroll.hx

@@ -0,0 +1,109 @@
+package hrt.shgraph.nodes;
+
+using hxsl.Ast;
+
+@name("UV Scroll")
+@description("Scroll UV according to U & V speed")
+@group("Specials")
+class UVScroll extends ShaderNode {
+
+	@input("uv") var uv = SType.Vec2;
+	@input("uSpeed", true) var uSpeed = SType.Number;
+	@input("vSpeed", true) var vSpeed = SType.Number;
+
+	@output("") var output = SType.Vec2;
+
+	var operation : Binop;
+
+	public function new(operation : Binop) {
+		this.operation = operation;
+	}
+
+	override public function computeOutputs() {
+		if (uv != null && !uv.isEmpty())
+			addOutput("output", uv.getType());
+		else
+			removeOutput("output");
+	}
+
+	override public function build(key : String) : TExpr {
+		
+		var globalTime : TVar = @:privateAccess ShaderGlobalInput.globalInputs.filter(i -> i.name.indexOf("time") != -1)[0];
+		var timeExpr : TExpr = { e: TVar(globalTime), p: null, t: globalTime.type };
+
+		return { e: TBinop(OpAssign, {
+						e: TVar(output),
+						p: null,
+						t: output.type
+					}, {
+						// uv % 1 (wrap)
+						e: TCall({
+							e: TGlobal(Mod),
+							p: null,
+							t: TFun([
+								{
+									ret: output.type,
+									args: [
+										{ name: "uv", type : output.type }, 
+										{ name: "mod", type : TFloat }
+									]
+								}
+							])
+						}, [
+							{
+								// uv + speed * time
+								e: TBinop(OpAdd,
+									uv.getVar(),
+									{
+										e: TCall({
+											e: TGlobal(Vec2),
+											p: null,
+											t: TFun([
+												{
+													ret: output.type,
+													args: [
+														{ name: "u", type : TFloat }, 
+														{ name: "v", type : TFloat }
+													]
+												}
+											])
+										}, [
+											// uSpeed * time
+											{
+												e: TBinop(OpMult,
+													uSpeed.getVar(),
+													timeExpr),
+												p: null,
+												t: uSpeed.getType()
+											},
+											// vSpeed * time
+											{
+												e: TBinop(OpMult,
+													vSpeed.getVar(),
+													timeExpr),
+												p: null,
+												t: vSpeed.getType()
+											}
+										]
+										),
+										p: null,
+										t: output.type
+									}),
+								p: null,
+								t: uSpeed.getType()
+							},
+							{
+								e: TConst(CFloat(1)),
+								p: null,
+								t: TFloat
+							}
+						]),
+						p: null,
+						t: output.type
+					}),
+					p: null,
+					t: output.type
+				};
+	}
+
+}