From cc2e26ac747270e6fcc1549b7ea75a11b21a3850 Mon Sep 17 00:00:00 2001 From: tamat Date: Wed, 8 May 2019 15:15:44 +0200 Subject: [PATCH] fix horizontal nodes --- src/litegraph.js | 2 +- src/nodes/gltextures.js | 61 +++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/litegraph.js b/src/litegraph.js index 96a295c48a..5cf8813a3f 100755 --- a/src/litegraph.js +++ b/src/litegraph.js @@ -3168,7 +3168,7 @@ LGraphNode.prototype.isPointInside = function(x, y, margin, skip_title) { margin = margin || 0; - var margin_top = this.graph && this.graph.isLive() ? 0 : 20; + var margin_top = this.graph && this.graph.isLive() ? 0 : LiteGraph.NODE_TITLE_HEIGHT; if (skip_title) { margin_top = 0; } diff --git a/src/nodes/gltextures.js b/src/nodes/gltextures.js index 57b393305d..33f2a9c809 100755 --- a/src/nodes/gltextures.js +++ b/src/nodes/gltextures.js @@ -366,6 +366,15 @@ ]; }; + //used to replace shader code + LGraphTexture.replaceCode = function( code, context ) + { + return code.replace(/\{\{[a-zA-Z0-9_]*\}\}/g, function(v){ + v = v.replace( /[\{\}]/g, "" ); + return context[v] || ""; + }); + } + LiteGraph.registerNodeType("texture/texture", LGraphTexture); //************************** @@ -458,7 +467,7 @@ this.help = "

pixelcode must be vec3

\

uvcode must be vec2, is optional

\ -

uv: tex. coords

color: texture

colorB: textureB

time: scene time

value: input value

"; +

uv: tex. coords

color: texture

colorB: textureB

time: scene time

value: input value

For multiline you must type: result = ...

"; this.properties = { value: 1, @@ -466,6 +475,8 @@ pixelcode: "color + colorB * value", precision: LGraphTexture.DEFAULT }; + + this.has_error = false; } LGraphTextureOperation.widgets_info = { @@ -492,6 +503,11 @@ ]; }; + LGraphTextureOperation.prototype.onPropertyChanged = function() + { + this.has_error = false; + } + LGraphTextureOperation.prototype.onDrawBackground = function(ctx) { if ( this.flags.collapsed || @@ -583,30 +599,21 @@ var shader = this._shader; - if (!shader || this._shader_code != uvcode + "|" + pixelcode) { + if ( !this.has_error && (!shader || this._shader_code != uvcode + "|" + pixelcode) ) { + + var final_pixel_code = LGraphTexture.replaceCode( LGraphTextureOperation.pixel_shader, { UV_CODE:uvcode, PIXEL_CODE:pixelcode }); + try { - this._shader = new GL.Shader( - Shader.SCREEN_VERTEX_SHADER, - LGraphTextureOperation.pixel_shader, - { UV_CODE: uvcode, PIXEL_CODE: pixelcode } - ); + shader = new GL.Shader( Shader.SCREEN_VERTEX_SHADER, final_pixel_code ); this.boxcolor = "#00FF00"; } catch (err) { - console.log("Error compiling shader: ", err); + console.log("Error compiling shader: ", err, final_pixel_code ); this.boxcolor = "#FF0000"; + this.has_error = true; return; } - this.boxcolor = "#FF0000"; - + this._shader = shader; this._shader_code = uvcode + "|" + pixelcode; - shader = this._shader; - } - - if (!shader) { - this.boxcolor = "red"; - return; - } else { - this.boxcolor = "green"; } var value = this.getInputData(2); @@ -655,14 +662,14 @@ \n\ void main() {\n\ vec2 uv = v_coord;\n\ - UV_CODE;\n\ + {{UV_CODE}};\n\ vec4 color4 = texture2D(u_texture, uv);\n\ vec3 color = color4.rgb;\n\ vec4 color4B = texture2D(u_textureB, uv);\n\ vec3 colorB = color4B.rgb;\n\ vec3 result = color;\n\ float alpha = 1.0;\n\ - PIXEL_CODE;\n\ + {{PIXEL_CODE}};\n\ gl_FragColor = vec4(result, alpha);\n\ }\n\ "; @@ -1556,11 +1563,12 @@ GL.Shader.SCREEN_VERTEX_SHADER, LGraphTextureAverage.pixel_shader ); - //creates 32 random numbers and stores the, in two mat4 - var samples = new Float32Array(32); - for (var i = 0; i < 32; ++i) { - samples[i] = Math.random(); + //creates 256 random numbers and stores them in two mat4 + var samples = new Float32Array(16); + for (var i = 0; i < samples.length; ++i) { + samples[i] = Math.random(); //poorly distributed samples } + //upload only once LGraphTextureAverage._shader.uniforms({ u_samples_a: samples.subarray(0, 16), u_samples_b: samples.subarray(16, 32) @@ -1620,8 +1628,9 @@ \n\ void main() {\n\ vec4 color = vec4(0.0);\n\ - for(int i = 0; i < 4; ++i)\n\ - for(int j = 0; j < 4; ++j)\n\ + //random average\n\ + for(int i = 0; i <= 4; ++i)\n\ + for(int j = 0; j <= 4; ++j)\n\ {\n\ color += texture2D(u_texture, vec2( u_samples_a[i][j], u_samples_b[i][j] ), u_mipmap_offset );\n\ color += texture2D(u_texture, vec2( 1.0 - u_samples_a[i][j], 1.0 - u_samples_b[i][j] ), u_mipmap_offset );\n\