From 86143acee2c52bd02d000755249d2a2dbf676f23 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Mon, 14 Sep 2020 16:02:59 +0200 Subject: [PATCH] Node preview for individual sockets --- .../Sources/arm/shader/MaterialParser.hx | 7 +-- Sources/arm/Context.hx | 1 + Sources/arm/node/MakeNodePreview.hx | 14 +++--- Sources/arm/ui/UINodes.hx | 45 +++++++++++++------ Sources/arm/util/RenderUtil.hx | 42 ++++++++++------- 5 files changed, 71 insertions(+), 38 deletions(-) diff --git a/Libraries/shaderlib/Sources/arm/shader/MaterialParser.hx b/Libraries/shaderlib/Sources/arm/shader/MaterialParser.hx index cbd7c6bc..1879cdb1 100644 --- a/Libraries/shaderlib/Sources/arm/shader/MaterialParser.hx +++ b/Libraries/shaderlib/Sources/arm/shader/MaterialParser.hx @@ -175,10 +175,11 @@ class MaterialParser { } } if (frag.wtangent) { - con.add_elem("tang", "short4norm"); - vert.add_uniform("mat3 N", "_normalMatrix"); + // con.add_elem("tang", "short4norm"); + // vert.add_uniform("mat3 N", "_normalMatrix"); vert.add_out("vec3 wtangent"); - vert.write_attrib('wtangent = normalize(mul(tang.xyz, N));'); + // vert.write_attrib('wtangent = normalize(mul(tang.xyz, N));'); + vert.write_attrib('wtangent = vec3(0.0, 0.0, 0.0);'); } if (frag.vVecCam) { vert.add_uniform("mat4 WV", "_worldViewMatrix"); diff --git a/Sources/arm/Context.hx b/Sources/arm/Context.hx index b4807f68..f1c06f98 100644 --- a/Sources/arm/Context.hx +++ b/Sources/arm/Context.hx @@ -45,6 +45,7 @@ class Context { public static var layerPreviewDirty = true; public static var layersPreviewDirty = false; public static var nodePreviewDirty = false; + public static var nodePreviewSocket = 0; public static var colorIdPicked = false; public static var splitView = false; diff --git a/Sources/arm/node/MakeNodePreview.hx b/Sources/arm/node/MakeNodePreview.hx index 1e929c25..83e386d1 100644 --- a/Sources/arm/node/MakeNodePreview.hx +++ b/Sources/arm/node/MakeNodePreview.hx @@ -17,28 +17,28 @@ class MakeNodePreview { depth_write: true, compare_mode: "less", cull_mode: "clockwise", - vertex_elements: [{name: "pos", data: "float3"}], + vertex_elements: [{name: "pos", data: "short4norm"}, {name: "nor", data: "short2norm"}, {name: "tex", data: "short2norm"}, {name: "col", data: "short4norm"}], color_attachments: ["RGBA32"] }); + con_mesh.allow_vcols = true; var vert = con_mesh.make_vert(); var frag = con_mesh.make_frag(); frag.ins = vert.outs; - vert.write_attrib('gl_Position = vec4(pos.xy, 0.0, 1.0);'); - vert.add_out('vec2 texCoord'); + vert.write_attrib('gl_Position = vec4(pos.xy * 3.0, 0.0, 1.0);'); // Pos unpack vert.write_attrib('const vec2 madd = vec2(0.5, 0.5);'); - vert.write_attrib('texCoord = pos.xy * madd + madd;'); + vert.add_out('vec2 texCoord'); + vert.write_attrib('texCoord = gl_Position.xy * madd + madd;'); #if (!kha_opengl) vert.write_attrib('texCoord.y = 1.0 - texCoord.y;'); #end - vert.write_attrib('vec3 nor = vec3(0.0, 0.0, 1.0);'); var canvas_nodes = Context.material.canvas.nodes; var canvas_links = Context.material.canvas.links; var nodes = Context.material.nodes; - var link: TNodeLink = { id: nodes.getLinkId(canvas_links), from_id: nodes.nodesSelected[0].id, from_socket: 0, to_id: 0, to_socket: 0 }; + var link: TNodeLink = { id: nodes.getLinkId(canvas_links), from_id: nodes.nodesSelected[0].id, from_socket: Context.nodePreviewSocket, to_id: 0, to_socket: 0 }; canvas_links.push(link); MaterialParser.init(); @@ -63,7 +63,7 @@ class MakeNodePreview { frag.ndcpos = true; vert.add_out('vec4 ndc'); - vert.write_attrib('ndc = vec4(pos.xyz * vec3(0.5, 0.5, 0.0) + vec3(0.5, 0.5, 0.0), 1.0);'); + vert.write_attrib('ndc = vec4(gl_Position.xyz * vec3(0.5, 0.5, 0.0) + vec3(0.5, 0.5, 0.0), 1.0);'); MaterialParser.finalize(con_mesh); diff --git a/Sources/arm/ui/UINodes.hx b/Sources/arm/ui/UINodes.hx index 01315a27..87eaa4f7 100644 --- a/Sources/arm/ui/UINodes.hx +++ b/Sources/arm/ui/UINodes.hx @@ -15,6 +15,7 @@ import arm.ui.UIHeader; import arm.Enums; @:access(zui.Zui) +@:access(zui.Nodes) class UINodes { public static var inst: UINodes; @@ -66,19 +67,36 @@ class UINodes { function onLinkDrag(linkDrag: TNodeLink, isNewLink: Bool) { if (isNewLink) { - nodeSearch(-1, -1, function() { - var n = getNodes().nodesSelected[0]; - if (linkDrag.to_id == -1 && n.inputs.length > 0) { - linkDrag.to_id = n.id; - linkDrag.to_socket = 0; - getCanvas().links.push(linkDrag); - } - else if (linkDrag.from_id == -1 && n.outputs.length > 0) { - linkDrag.from_id = n.id; - linkDrag.from_socket = 0; - getCanvas().links.push(linkDrag); - } - }); + var nodes = getNodes(); + var node = nodes.getNode(getCanvas().nodes, linkDrag.from_id > -1 ? linkDrag.from_id : linkDrag.to_id); + var linkX = ui._windowX + nodes.NODE_X(node); + var linkY = ui._windowY + nodes.NODE_Y(node); + if (linkDrag.from_id > -1) { + linkX += nodes.NODE_W(); + linkY += nodes.SOCKET_Y(linkDrag.from_socket); + } + else { + linkY += nodes.SOCKET_Y(linkDrag.to_socket + node.outputs.length) + nodes.BUTTONS_H(node); + } + var mouse = Input.getMouse(); + if (Math.abs(mouse.x - linkX) > 5 || Math.abs(mouse.y - linkY) > 5) { // Link length + nodeSearch(-1, -1, function() { + var n = nodes.nodesSelected[0]; + if (linkDrag.to_id == -1 && n.inputs.length > 0) { + linkDrag.to_id = n.id; + linkDrag.to_socket = 0; + getCanvas().links.push(linkDrag); + } + else if (linkDrag.from_id == -1 && n.outputs.length > 0) { + linkDrag.from_id = n.id; + linkDrag.from_socket = 0; + getCanvas().links.push(linkDrag); + } + }); + } + else { + Context.nodePreviewSocket = linkDrag.from_id > -1 ? linkDrag.from_socket : 0; + } } } @@ -298,6 +316,7 @@ class UINodes { if (nodes.nodesSelected.length > 0 && nodes.nodesSelected[0] != lastNodeSelected) { lastNodeSelected = nodes.nodesSelected[0]; Context.nodePreviewDirty = true; + Context.nodePreviewSocket = 0; } if (Config.raw.node_preview && Context.nodePreviewDirty) RenderUtil.makeNodePreview(); diff --git a/Sources/arm/util/RenderUtil.hx b/Sources/arm/util/RenderUtil.hx index c51fa1aa..04726ffa 100644 --- a/Sources/arm/util/RenderUtil.hx +++ b/Sources/arm/util/RenderUtil.hx @@ -28,8 +28,8 @@ class RenderUtil { public static inline var matPreviewSize = 256; public static inline var decalPreviewSize = 512; - static var screenAligned3VB: VertexBuffer = null; - static var screenAligned3IB: IndexBuffer = null; + static var screenAlignedFullVB: VertexBuffer = null; + static var screenAlignedFullIB: IndexBuffer = null; public static function makeMaterialPreview() { Context.materialPreview = true; @@ -414,23 +414,28 @@ class RenderUtil { if (current != null) current.begin(false); } - static function createScreenAligned3Data() { + static function createScreenAlignedFullData() { // Over-sized triangle - var data = [-1.0, -1.0, 0.0, 3.0, -1.0, 0.0, -1.0, 3.0, 0.0]; + var data = [-Std.int(32767 / 3), -Std.int(32767 / 3), 0, 32767, 0, 0, 0, 0, 0, 0, 0, 0, + 32767, -Std.int(32767 / 3), 0, 32767, 0, 0, 0, 0, 0, 0, 0, 0, + -Std.int(32767 / 3), 32767, 0, 32767, 0, 0, 0, 0, 0, 0, 0, 0]; var indices = [0, 1, 2]; // Mandatory vertex data names and sizes var structure = new VertexStructure(); - structure.add("pos", VertexData.Float3); - screenAligned3VB = new VertexBuffer(Std.int(data.length / Std.int(structure.byteSize() / 4)), structure, Usage.StaticUsage); - var vertices = screenAligned3VB.lock(); + structure.add("pos", VertexData.Short4Norm); + structure.add("nor", VertexData.Short2Norm); + structure.add("tex", VertexData.Short2Norm); + structure.add("col", VertexData.Short4Norm); + screenAlignedFullVB = new VertexBuffer(Std.int(data.length / Std.int(structure.byteSize() / 4)), structure, Usage.StaticUsage); + var vertices = screenAlignedFullVB.lockInt16(); for (i in 0...vertices.length) vertices.set(i, data[i]); - screenAligned3VB.unlock(); + screenAlignedFullVB.unlock(); - screenAligned3IB = new IndexBuffer(indices.length, Usage.StaticUsage); - var id = screenAligned3IB.lock(); + screenAlignedFullIB = new IndexBuffer(indices.length, Usage.StaticUsage); + var id = screenAlignedFullIB.lock(); for (i in 0...id.length) id[i] = indices[i]; - screenAligned3IB.unlock(); + screenAlignedFullIB.unlock(); } public static function makeNodePreview() { @@ -457,17 +462,24 @@ class RenderUtil { if (scon == null) return; var g4 = UINodes.inst.nodePreview.g4; - if (screenAligned3VB == null) { - createScreenAligned3Data(); + if (screenAlignedFullVB == null) { + createScreenAlignedFullData(); } + var _scaleWorld = Context.paintObject.transform.scaleWorld; + Context.paintObject.transform.scaleWorld = 3.0; + Context.paintObject.transform.buildMatrix(); + g4.begin(); g4.setPipeline(scon.pipeState); iron.object.Uniforms.setContextConstants(g4, scon, [""]); iron.object.Uniforms.setObjectConstants(g4, scon, Context.paintObject); - g4.setVertexBuffer(screenAligned3VB); - g4.setIndexBuffer(screenAligned3IB); + g4.setVertexBuffer(screenAlignedFullVB); + g4.setIndexBuffer(screenAlignedFullIB); g4.drawIndexedVertices(); g4.end(); + + Context.paintObject.transform.scaleWorld = _scaleWorld; + Context.paintObject.transform.buildMatrix(); } }