2
0
Эх сурвалжийг харах

[shgraph] Previews, plugged default values

Clément Espeute 1 жил өмнө
parent
commit
e9f338b3f5

+ 99 - 1
hide/view/GraphEditor.hx

@@ -31,6 +31,11 @@ class GraphEditor extends hide.comp.Component {
 	var statusBar : JQuery;
 	var statusClose : JQuery;
 
+	public var config : hide.Config;
+
+
+	var previewsScene : hide.comp.Scene;
+
 	var boxes : Map<Int, Box> = [];
 
 	var transformMatrix : Array<Float> = [1, 0, 0, 1, 0, 0];
@@ -79,7 +84,7 @@ class GraphEditor extends hide.comp.Component {
 	// Maps a packIO of an input to it's visual link in the graph
 	var edges : Map<Int, JQuery> = [];
 
-	public function new(editor: hide.view.GraphInterface.IGraphEditor, parent: Element = null) {
+	public function new(config: hide.Config, editor: hide.view.GraphInterface.IGraphEditor, parent: Element = null) {
 		super(parent, new Element('
 		<div class="flex vertical" >
 			<div class="flex-elt graph-view" tabindex="0" >
@@ -89,6 +94,7 @@ class GraphEditor extends hide.comp.Component {
 				</div>
 			</div>
 		</div>'));
+		this.config = config;
 		this.editor = editor;
 	}
 
@@ -137,6 +143,11 @@ class GraphEditor extends hide.comp.Component {
 		var keys = new hide.ui.Keys(element);
 		keys.register("delete", deleteSelection);
 
+		var miniPreviews = new Element('<div class="mini-preview"></div>');
+		heapsScene.prepend(miniPreviews);
+		previewsScene = new hide.comp.Scene(config, null, miniPreviews);
+		previewsScene.onReady = onMiniPreviewReady;
+		previewsScene.onUpdate = onMiniPreviewUpdate;
 
 		// rectangle Selection
 		var rawheaps = heapsScene.get(0);
@@ -248,6 +259,93 @@ class GraphEditor extends hide.comp.Component {
 		}
 	}
 
+	var boxToPreview : Map<Box, h2d.Bitmap>;
+	var miniPreviewInitTimeout = 0;
+	function onMiniPreviewReady() {
+		if (previewsScene.s2d == null) {
+			miniPreviewInitTimeout ++;
+			if (miniPreviewInitTimeout > 10)
+				throw "Couldn't initialize background previews";
+			haxe.Timer.delay(() -> onMiniPreviewReady, 100);
+			return;
+		}
+		var bg = new h2d.Flow(previewsScene.s2d);
+		bg.fillHeight = true;
+		bg.fillWidth = true;
+		bg.backgroundTile = h2d.Tile.fromColor(0x333333);
+		boxToPreview = [];
+
+		var identity : h3d.Matrix = new h3d.Matrix();
+		identity.identity();
+		@:privateAccess previewsScene.s2d.renderer.globals.set("camera.viewProj", identity);
+		@:privateAccess previewsScene.s2d.renderer.globals.set("camera.position", identity.getPosition());
+	}
+
+	/** If this function returns false, the preview update will be skipped**/
+	public dynamic function onPreviewUpdate() : Bool {
+		return true;
+	}
+
+	/** Called for each visible preview in the graph editor **/
+	public dynamic function onNodePreviewUpdate(node: IGraphNode, bitmap: h2d.Bitmap) : Void {
+
+	}
+
+	function onMiniPreviewUpdate(dt: Float) {
+		@:privateAccess
+		/*if (sceneEditor?.scene?.s3d?.renderer?.ctx?.time != null) {
+			sceneEditor.scene.s3d.renderer.ctx.time = previewsScene.s3d.renderer.ctx.time;
+		}*/
+
+		if (!onPreviewUpdate())
+			return;
+
+		var newBoxToPreview : Map<Box, h2d.Bitmap> = [];
+		for (box in boxes) {
+			if (box.info.preview == null) {
+				continue;
+			}
+			var preview = boxToPreview.get(box);
+			if (preview == null) {
+				var bmp = new h2d.Bitmap(h2d.Tile.fromColor(0xFF00FF,1,1), previewsScene.s2d);
+				bmp.blendMode = None;
+				preview = bmp;
+			} else {
+				boxToPreview.remove(box);
+			}
+			newBoxToPreview.set(box,preview);
+		}
+
+		for (preview in boxToPreview) {
+			preview.remove();
+		}
+		boxToPreview = newBoxToPreview;
+
+		var sceneW = editorDisplay.element.width();
+		var sceneH = editorDisplay.element.height();
+
+		for (box => preview in boxToPreview) {
+			preview.visible = box.info.preview.getVisible();
+			if (!preview.visible)
+				continue;
+
+			var pos = Box.tmpPoint;
+			box.node.getPos(pos);
+			preview.x = gX(pos.x);
+			preview.y = gY(pos.y + box.getHeight());
+			preview.scaleX = transformMatrix[0] * box.width;
+			preview.scaleY = transformMatrix[3] * box.width;
+
+			if (preview.x + preview.scaleX < 0 || preview.x > sceneW || preview.y + preview.scaleY < 0 || preview.y > sceneH) {
+				preview.visible = false;
+				continue;
+			}
+
+			onNodePreviewUpdate(box.node, preview);
+		}
+	}
+
+
 	function deleteSelection() {
 		cleanupCreateEdge();
 

+ 42 - 44
hide/view/shadereditor/ShaderEditor.hx

@@ -158,53 +158,13 @@ typedef ClassRepoEntry =
 	args: Array<Dynamic>
 };
 
-class TestNode implements IGraphNode {
-	var id : Int;
-	var x : Float = 0;
-	var y : Float = 0;
-	public function new(id: Int) {
-		this.id = id;
-	}
-	public function getInfo() : GraphNodeInfo {
-		return {
-			name: "Test",
-			inputs: [
-				{
-					name :"a",
-					color: 0xFF0000,
-				}
-			],
-			outputs: [
-				{
-					name: "out",
-					color: 0x00FF00,
-				}
-			]
-		}
-	}
-
-	public function getId() : Int {
-		return id;
-	}
-
-	public function getPos(pt: h2d.col.Point) : Void {
-		pt.set(x,y);
-	}
-
-	public function setPos(pt: h2d.col.Point) : Void {
-		x = pt.x;
-		y = pt.y;
-	}
-
-	public function getPropertiesHTML(w : Float) : Array<hide.Element> {
-		return [];
-	}
-}
-
 class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEditor {
 	var graphEditor : hide.view.GraphEditor;
 	var shaderGraph : hrt.shgraph.ShaderGraph;
 	var currentGraph : hrt.shgraph.ShaderGraph.Graph;
+
+	var compiledShader : hrt.prefab.Cache.ShaderDef;
+	var previewVar : hxsl.Ast.TVar;
 	
 	override function onDisplay() {
 		super.onDisplay();
@@ -213,8 +173,32 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 
 		if (graphEditor != null)
 			graphEditor.remove();
-		graphEditor = new hide.view.GraphEditor(this, this.element);
+		graphEditor = new hide.view.GraphEditor(config, this, this.element);
 		graphEditor.onDisplay();
+
+		graphEditor.onNodePreviewUpdate = onNodePreviewUpdate;
+	}
+
+	var bitmapToShader : Map<h2d.Bitmap, hxsl.DynamicShader> = [];
+	public function onNodePreviewUpdate(node: IGraphNode, bitmap: h2d.Bitmap) {
+		if (compiledShader == null) {
+			bitmap.visible = false;
+			return;
+		}
+		var shader = bitmapToShader.get(bitmap);
+		if (shader == null) {
+			for (s in bitmap.getShaders()) {
+				bitmap.removeShader(s);
+			}
+			shader = new DynamicShader(compiledShader.shader);
+			bitmapToShader.set(bitmap, shader);
+			bitmap.addShader(shader);
+		}
+		setParamValue(shader, previewVar, node.getId() + 1);
+	}
+
+	function setParamValue(shader : DynamicShader, variable : hxsl.Ast.TVar, value : Dynamic) {
+		@:privateAccess ShaderGraph.setParamValue(shader, variable, value);
 	}
 
 	/** IGraphEditor interface **/
@@ -269,10 +253,12 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 
 	public function addNode(node: IGraphNode) : Void {
 		currentGraph.addNode(cast node);
+		compileShader();
 	}
 
 	public function removeNode(id: Int) : Void {
 		currentGraph.removeNode(id);
+		compileShader();
 	}
 
 	public function canAddEdge(edge: Edge) : Bool {
@@ -282,11 +268,13 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 	public function addEdge(edge: Edge) : Void {
 		var input = currentGraph.getNode(edge.nodeToId);
 		input.connections[edge.inputToId] = {from: currentGraph.getNode(edge.nodeFromId), outputId: edge.outputFromId};
+		compileShader();
 	}
 
 	public function removeEdge(nodeToId: Int, inputToId: Int) : Void {
 		var input = currentGraph.getNode(nodeToId);
 		input.connections[inputToId] = null;
+		compileShader();
 	}
 
 	public override function save() {
@@ -304,6 +292,16 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 		var p = (new hrt.shgraph.ShaderGraph(null, null)).serialize();
 		return haxe.io.Bytes.ofString(ide.toJSON(p));
 	}
+
+	public function compileShader() {
+		try {
+			compiledShader = shaderGraph.compile(Fragment);
+			bitmapToShader.clear();
+			previewVar = compiledShader.inits.find((e) -> e.variable.name == hrt.shgraph.Variables.previewSelectName).variable;
+		} catch (err) {
+			Ide.inst.quickError(err);
+		}
+	}
 	
 	static var _ = FileTree.registerExtension(ShaderEditor,["shgraph"],{ icon : "scribd", createNew: "Shader Graph" });
 }

+ 16 - 1
hrt/shgraph/ShaderNode.hx

@@ -64,9 +64,19 @@ implements hide.view.GraphInterface.IGraphNode
 			name: nameOverride ?? (metas.name != null ? metas.name[0] : "undefined"),
 			inputs: [
 				for (i in getInputs()) {
+					var defaultParam = null;
+					switch (i.def) {
+						case Const(intialValue):
+							defaultParam = {
+								get: () -> Std.string(Reflect.getProperty(defaults, i.name) ?? intialValue),
+								set: (s:String) -> Reflect.setField(defaults, i.name, Std.parseFloat(s)),
+							};
+						default:
+					}
 					{
 						name: i.name,
 						color: 0xFF0000,
+						defaultParam: defaultParam,
 					}
 				}
 			],
@@ -77,7 +87,12 @@ implements hide.view.GraphInterface.IGraphNode
 						color: 0xFF0000,
 					}
 				}
-			]
+			],
+			preview: {
+				getVisible: () -> showPreview,
+				setVisible: (b:Bool) -> showPreview = b,
+				fullSize: false,
+			},
 		};
 	}