Jelajahi Sumber

Preview working

Clement Espeute 2 tahun lalu
induk
melakukan
f59716045f
3 mengubah file dengan 180 tambahan dan 148 penghapusan
  1. 22 5
      hide/view/shadereditor/ShaderEditor.hx
  2. 16 12
      hrt/shgraph/ShaderGraph.hx
  3. 142 131
      hrt/shgraph/nodes/Preview.hx

+ 22 - 5
hide/view/shadereditor/ShaderEditor.hx

@@ -280,7 +280,15 @@ class ShaderEditor extends hide.view.Graph {
 		element.find("#displayGlsl").on("click", () -> displayCompiled("glsl"));
 		element.find("#displayGlsl").on("click", () -> displayCompiled("glsl"));
 		element.find("#displayHlsl").on("click", () -> displayCompiled("hlsl"));
 		element.find("#displayHlsl").on("click", () -> displayCompiled("hlsl"));
 
 
-		element.find("#display2").on("click", () -> {@:privateAccess info(hxsl.Printer.shaderToString(shaderGraph.compile2().shader.data, true));});
+		var preview : hrt.shgraph.nodes.Preview = null;
+		for (selected in listOfBoxesSelected) {
+			var asPrev = Std.downcast(selected.getInstance(), hrt.shgraph.nodes.Preview);
+			if (asPrev != null) {
+				preview = asPrev;
+				break;
+			}
+		}
+		element.find("#display2").on("click", () -> {@:privateAccess info(hxsl.Printer.shaderToString(shaderGraph.compile2(preview).shader.data, true));});
 
 
 		editorMatrix.on("click", "input, select", function(ev) {
 		editorMatrix.on("click", "input, select", function(ev) {
 			beforeChange();
 			beforeChange();
@@ -465,13 +473,13 @@ class ShaderEditor extends hide.view.Graph {
 		updateMatrix();
 		updateMatrix();
 
 
 		for (node in shaderGraph.getNodes()) {
 		for (node in shaderGraph.getNodes()) {
-			/*var shaderPreview = Std.downcast(node.instance, hrt.shgraph.nodes.Preview);
+			var shaderPreview = Std.downcast(node.instance, hrt.shgraph.nodes.Preview);
 			if (shaderPreview != null) {
 			if (shaderPreview != null) {
 				shaderPreview.config = config;
 				shaderPreview.config = config;
 				shaderPreview.shaderGraph = shaderGraph;
 				shaderPreview.shaderGraph = shaderGraph;
 				addBox(new Point(node.x, node.y), std.Type.getClass(node.instance), shaderPreview);
 				addBox(new Point(node.x, node.y), std.Type.getClass(node.instance), shaderPreview);
 				continue;
 				continue;
-			}*/
+			}
 			var paramNode = Std.downcast(node.instance, ShaderParam);
 			var paramNode = Std.downcast(node.instance, ShaderParam);
 			if (paramNode != null) {
 			if (paramNode != null) {
 				var paramShader = shaderGraph.getParameter(paramNode.parameterId);
 				var paramShader = shaderGraph.getParameter(paramNode.parameterId);
@@ -893,6 +901,15 @@ class ShaderEditor extends hide.view.Graph {
 			sceneEditor.scene.render(sceneEditor.scene.engine);
 			sceneEditor.scene.render(sceneEditor.scene.engine);
 			currentShader = newShader;
 			currentShader = newShader;
 			currentShaderDef = shaderGraphDef;//{shader: shaderGraphDef, inits:[]};
 			currentShaderDef = shaderGraphDef;//{shader: shaderGraphDef, inits:[]};
+
+			for (node in shaderGraph.getNodes()) {
+				var preview = Std.downcast(node.instance, hrt.shgraph.nodes.Preview);
+				if (preview != null) {
+					preview.config = config;
+					preview.shaderGraph = shaderGraph;
+					preview.update();
+				}
+			}
 			info('Shader compiled in  ${Date.now().getTime() - timeStart}ms');
 			info('Shader compiled in  ${Date.now().getTime() - timeStart}ms');
 
 
 		} catch (e : Dynamic) {
 		} catch (e : Dynamic) {
@@ -982,12 +999,12 @@ class ShaderEditor extends hide.view.Graph {
 	function initSpecifics(node : Null<ShaderNode>) {
 	function initSpecifics(node : Null<ShaderNode>) {
 		if( node == null )
 		if( node == null )
 			return;
 			return;
-		/*var shaderPreview = Std.downcast(node, hrt.shgraph.nodes.Preview);
+		var shaderPreview = Std.downcast(node, hrt.shgraph.nodes.Preview);
 		if (shaderPreview != null) {
 		if (shaderPreview != null) {
 			shaderPreview.config = config;
 			shaderPreview.config = config;
 			shaderPreview.shaderGraph = shaderGraph;
 			shaderPreview.shaderGraph = shaderGraph;
 			return;
 			return;
-		}*/
+		}
 		// var subGraphNode = Std.downcast(node, hrt.shgraph.nodes.SubGraph);
 		// var subGraphNode = Std.downcast(node, hrt.shgraph.nodes.SubGraph);
 		// if (subGraphNode != null) {
 		// if (subGraphNode != null) {
 		// 	subGraphNode.loadGraphShader();
 		// 	subGraphNode.loadGraphShader();

+ 16 - 12
hrt/shgraph/ShaderGraph.hx

@@ -241,14 +241,13 @@ class ShaderGraph {
 			};
 			};
 	}
 	}
 
 
-	public function generate2(?getNewVarId: () -> Int) : ShaderNodeDef {
-		if (getNewVarId == null) {
-			var varIdCount = 0;
-			getNewVarId = function()
-				{
-					return varIdCount++;
-				};
-		}
+	public function generate2(?specificOutput: ShaderNode) : ShaderNodeDef {
+
+		var varIdCount = 0;
+		var getNewVarId = function()
+		{
+			return varIdCount++;
+		};
 
 
 		inline function getNewVarName(node: Node, id: Int) : String {
 		inline function getNewVarName(node: Node, id: Int) : String {
 			return '_sg_${(node.type).split(".").pop()}_var_$id';
 			return '_sg_${(node.type).split(".").pop()}_var_$id';
@@ -389,10 +388,15 @@ class ShaderGraph {
 		// Actually build the final shader expression
 		// Actually build the final shader expression
 		var exprsReverse : Array<TExpr> = [];
 		var exprsReverse : Array<TExpr> = [];
 		for (currentNode in sortedNodes) {
 		for (currentNode in sortedNodes) {
+
 			// Skip nodes with no outputs that arent a final node
 			// Skip nodes with no outputs that arent a final node
-			if (Std.downcast(currentNode.instance, ShaderOutput)==null) {
-				if (!nodeHasOutputs.get(currentNode))
+			if (!nodeHasOutputs.get(currentNode))
+			{
+				if (specificOutput != null && currentNode.instance != specificOutput)
 					continue;
 					continue;
+				if ( currentNode.instance != specificOutput && Std.downcast(currentNode.instance, ShaderOutput)==null) {
+					continue;
+				}
 			}
 			}
 
 
 
 
@@ -486,10 +490,10 @@ class ShaderGraph {
 		};
 		};
 	}
 	}
 
 
-	public function compile2() : hrt.prefab.ContextShared.ShaderDef {
+	public function compile2(?specificOutput: ShaderNode) : hrt.prefab.ContextShared.ShaderDef {
 		var start = haxe.Timer.stamp();
 		var start = haxe.Timer.stamp();
 
 
-		var gen = generate2();
+		var gen = generate2(specificOutput);
 
 
 		var shaderData : ShaderData = {
 		var shaderData : ShaderData = {
 			name: "",
 			name: "",

+ 142 - 131
hrt/shgraph/nodes/Preview.hx

@@ -11,6 +11,19 @@ using hxsl.Ast;
 @noheader()
 @noheader()
 class Preview extends ShaderNode {
 class Preview extends ShaderNode {
 
 
+	override function getShaderDef():hrt.shgraph.ShaderGraph.ShaderNodeDef {
+		var pos : Position = {file: "", min: 0, max: 0};
+
+		var inVar : TVar = {name: "input", id:0, type: TVec(4, VFloat), kind: Param, qualifiers: [SgInput]};
+		var output : TVar = {name: "pixelColor", id:1, type: TVec(4, VFloat), kind: Local, qualifiers: [SgOutput]};
+		var finalExpr : TExpr = {e: TBinop(OpAssign, {e:TVar(output), p:pos, t:output.type}, {e: TVar(inVar), p: pos, t: output.type}), p: pos, t: output.type};
+
+		//var param = getParameter(inputNode.parameterId);
+		//inits.push({variable: inVar, value: param.defaultValue});
+
+		return {expr: finalExpr, inVars: [inVar], outVars:[], externVars: [inVar, output], inits: []};
+	}
+
 	// @input("Input") var input = SType.Vec4;
 	// @input("Input") var input = SType.Vec4;
 
 
 	// public var variable : TVar;
 	// public var variable : TVar;
@@ -29,135 +42,133 @@ class Preview extends ShaderNode {
 
 
 	// }
 	// }
 
 
-	// #if editor
-	// public var shaderGraph : ShaderGraph;
-	// var nodePreview : js.jquery.JQuery;
-	// var cube : Mesh;
-	// var scene : hide.comp.Scene;
-	// var currentShaderPreview : hxsl.DynamicShader;
-	// var currentShaderDef : hrt.prefab.ContextShared.ShaderDef;
-	// public var config : hide.Config;
-
-	// override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
-	// 	var elements = super.getPropertiesHTML(width);
-	// 	for (c in ShaderNode.availableVariables) {
-	// 		if (c.name == "pixelColor") {
-	// 			this.variable = c;
-	// 		}
-	// 	}
-
-	// 	if (this.variable == null) {
-	// 		throw ShaderException.t("The preview is not available", this.id);
-	// 	}
-	// 	var element = new hide.Element('<div style="width: 100px; height: 100px"><div class="preview-parent" top="-10px" ><div class="node-preview" style="height: 100px" ></div></div></div>');
-	// 	nodePreview = element.find(".node-preview");
-	// 	scene = new hide.comp.Scene(config, null, nodePreview);
-
-	// 	scene.onReady = function() {
-	// 		var prim = new h3d.prim.Cube();
-	// 		prim.addUVs();
-	// 		prim.addNormals();
-	// 		cube = new Mesh(prim, scene.s3d);
-	// 		scene.s3d.camera.pos = new h3d.Vector(0.5, 3.4, 0.5);
-	// 		scene.s3d.camera.target = new h3d.Vector(0.5, 0.5, 0.5);
-	// 		var light = new h3d.scene.pbr.DirLight(scene.s3d.camera.target.sub(scene.s3d.camera.pos), scene.s3d);
-	// 		light.setPosition(scene.s3d.camera.pos.x, scene.s3d.camera.pos.y, scene.s3d.camera.pos.z);
-	// 		scene.s3d.camera.zoom = 1;
-	// 		scene.init();
-	// 		onMove();
-	// 		computeOutputs();
-	// 	};
-
-	// 	elements.push(element);
-	// 	return elements;
-	// }
-
-	// public function onMove(?x : Float, ?y : Float, zoom : Float = 1.) {
-	// 	// var top : Float;
-	// 	// var left : Float;
-	// 	// var parent = nodePreview.parent();
-	// 	// if (x != null && y != null) {
-	// 	// 	left = x;
-	// 	// 	top = y;
-	// 	// } else {
-	// 	// 	var offsetWindow = nodePreview.closest(".heaps-scene").offset();
-	// 	// 	var offset = nodePreview.closest("foreignObject").offset();
-	// 	// 	if (offsetWindow == null || offset == null) return;
-	// 	// 	top = offset.top - offsetWindow.top - 32;
-	// 	// 	left = offset.left - offsetWindow.left;
-	// 	// }
-	// 	// nodePreview.closest(".prop-group").attr("transform", 'translate(0, -5)');
-	// 	nodePreview.closest(".properties-group").children().first().css("fill", "#000");
-	// 	// parent.css("top", top/zoom + 17);
-	// 	// parent.css("left", left/zoom);
-	// 	// parent.css("zoom", zoom);
-	// }
-
-	// function onResize() {
-	// 	if( cube == null ) return;
-
-	// }
-
-	// override public function computeOutputs() {
-	// 	if (currentShaderPreview != null) {
-	// 		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;
-	// 	try {
-	// 		var shaderGraphDef = shaderGraph.compile2(/*this*/);
-	// 		shader = new hxsl.DynamicShader(shaderGraphDef.shader);
-	// 		for (init in shaderGraphDef.inits) {
-	// 			setParamValue(init.variable, init.value, shader);
-	// 		}
-	// 		for (m in cube.getMaterials()) {
-	// 			m.mainPass.addShader(shader);
-	// 		}
-	// 		currentShaderPreview = shader;
-	// 		currentShaderDef = shaderGraphDef;
-	// 	} catch(e : Dynamic) {
-	// 		if (shader != null) {
-	// 			for (m in cube.getMaterials()) {
-	// 				m.mainPass.removeShader(shader);
-	// 			}
-	// 		}
-	// 	}
-	// }
-
-	// public function setParamValueByName(varName : String, value : Dynamic) {
-	// 	if (currentShaderDef == null) return;
-	// 	for (init in currentShaderDef.inits) {
-	// 		if (init.variable.name == varName) {
-	// 			setParamValue(init.variable, value, currentShaderPreview);
-	// 			return;
-	// 		}
-	// 	}
-	// }
-
-	// public function setParamValue(variable : TVar, value : Dynamic, shader : hxsl.DynamicShader) {
-	// 	scene.setCurrent();
-	// 	try {
-	// 		switch (variable.type) {
-	// 			case TSampler2D:
-	// 				shader.setParamValue(variable, scene.loadTexture("", value));
-	// 			case TVec(size, _):
-	// 				shader.setParamValue(variable, h3d.Vector.fromArray(value));
-	// 			default:
-	// 				shader.setParamValue(variable, value);
-	// 		}
-	// 	} catch (e : Dynamic) {
-	// 		// variable not used
-	// 	}
-	// }
-
-	// #end
+	#if editor
+	var nodePreview : js.jquery.JQuery;
+	var element : js.jquery.JQuery;
+	public var shaderGraph: ShaderGraph;
+	var cube : Mesh;
+	var scene : hide.comp.Scene;
+	var currentShaderPreview : hxsl.DynamicShader;
+	var currentShaderDef : hrt.prefab.ContextShared.ShaderDef;
+	var inited = false;
+	public var config : hide.Config;
+
+	override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
+		var elements = super.getPropertiesHTML(width);
+
+		if (element == null) {
+			element = new hide.Element('<div style="width: 100px; height: 100px"><div class="preview-parent" top="-10px" ><div class="node-preview" style="height: 100px" ></div></div></div>');
+			nodePreview = element.find(".node-preview");
+
+			scene = new hide.comp.Scene(config, null, nodePreview);
+
+			scene.onReady = function() {
+				var prim = new h3d.prim.Cube();
+				prim.addUVs();
+				prim.addNormals();
+				cube = new Mesh(prim, scene.s3d);
+				scene.s3d.camera.pos = new h3d.Vector(0.5, 3.4, 0.5);
+				scene.s3d.camera.target = new h3d.Vector(0.5, 0.5, 0.5);
+				var light = new h3d.scene.pbr.DirLight(scene.s3d.camera.target.sub(scene.s3d.camera.pos), scene.s3d);
+				light.setPosition(scene.s3d.camera.pos.x, scene.s3d.camera.pos.y, scene.s3d.camera.pos.z);
+				scene.s3d.camera.zoom = 1;
+				scene.init();
+				onMove();
+				inited = true;
+
+				update();
+			};
+		}
+
+		elements.push(element);
+		return elements;
+	}
+
+	public function onMove(?x : Float, ?y : Float, zoom : Float = 1.) {
+		// var top : Float;
+		// var left : Float;
+		// var parent = nodePreview.parent();
+		// if (x != null && y != null) {
+		// 	left = x;
+		// 	top = y;
+		// } else {
+		// 	var offsetWindow = nodePreview.closest(".heaps-scene").offset();
+		// 	var offset = nodePreview.closest("foreignObject").offset();
+		// 	if (offsetWindow == null || offset == null) return;
+		// 	top = offset.top - offsetWindow.top - 32;
+		// 	left = offset.left - offsetWindow.left;
+		// }
+		// nodePreview.closest(".prop-group").attr("transform", 'translate(0, -5)');
+		nodePreview.closest(".properties-group").children().first().css("fill", "#000");
+		// parent.css("top", top/zoom + 17);
+		// parent.css("left", left/zoom);
+		// parent.css("zoom", zoom);
+	}
+
+	function onResize() {
+		if( cube == null ) return;
+	}
+
+	public function update() {
+		if (!inited)
+			return;
+		if (currentShaderPreview != null) {
+			for (m in cube.getMaterials()) {
+				m.mainPass.removeShader(currentShaderPreview);
+			}
+			currentShaderPreview = null;
+		}
+
+		if (@:privateAccess scene.window == null)
+			return;
+		scene.setCurrent();
+
+		var shader : hxsl.DynamicShader = null;
+		try {
+			var shaderGraphDef = shaderGraph.compile2(this);
+			shader = new hxsl.DynamicShader(shaderGraphDef.shader);
+			for (init in shaderGraphDef.inits) {
+				setParamValue(init.variable, init.value, shader);
+			}
+			for (m in cube.getMaterials()) {
+				m.mainPass.addShader(shader);
+			}
+			currentShaderPreview = shader;
+			currentShaderDef = shaderGraphDef;
+		} catch(e : Dynamic) {
+			if (shader != null) {
+				for (m in cube.getMaterials()) {
+					m.mainPass.removeShader(shader);
+				}
+			}
+		}
+	}
+
+	public function setParamValueByName(varName : String, value : Dynamic) {
+		if (currentShaderDef == null) return;
+		for (init in currentShaderDef.inits) {
+			if (init.variable.name == varName) {
+				setParamValue(init.variable, value, currentShaderPreview);
+				return;
+			}
+		}
+	}
+
+	public function setParamValue(variable : TVar, value : Dynamic, shader : hxsl.DynamicShader) {
+		scene.setCurrent();
+		try {
+			switch (variable.type) {
+				case TSampler2D:
+					shader.setParamValue(variable, scene.loadTexture("", value));
+				case TVec(size, _):
+					shader.setParamValue(variable, h3d.Vector.fromArray(value));
+				default:
+					shader.setParamValue(variable, value);
+			}
+		} catch (e : Dynamic) {
+			// variable not used
+		}
+	}
+
+	#end
 }
 }