From 1ab5afd243ff32a357303f6da88ef2a8dc3d746e Mon Sep 17 00:00:00 2001 From: Obvious Alex C <76855369+David-Orangemoon@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:58:11 -0400 Subject: [PATCH] Add random block --- Source/blockly/patches/fragmentSafeifier.js | 13 ++++ Source/blockly/patches/vertexSafeifier.js | 11 +++ Source/blocks/operators.js | 77 +++++++++++++++++++++ Source/editor/compileTime/shaders.js | 2 +- Source/render/defaultShader.js | 40 ++++++++++- 5 files changed, 139 insertions(+), 4 deletions(-) diff --git a/Source/blockly/patches/fragmentSafeifier.js b/Source/blockly/patches/fragmentSafeifier.js index 5a5b43a..3fb41e7 100644 --- a/Source/blockly/patches/fragmentSafeifier.js +++ b/Source/blockly/patches/fragmentSafeifier.js @@ -1,8 +1,21 @@ penPlus.makeFragmentSafe = (shaderText) => { + if (shaderText.includes("#version 300 es")) { + return shaderText + .replaceAll(/(gl_Position\.*[xyzw]*\s*[+*/-]*=.*;)/g, "") + .replaceAll(/(gl_Position)/g, "vec4(1)") + .replaceAll(/(v_color\.*[xyzw]*\s*[+*/-]*=.*;)/g, "") + .replaceAll("varying", "in") + .replaceAll(/attribute.*;/g,"") + .replaceAll(/(penPlus_isFragment)/g, "false") + .replaceAll("gl_FragColor","fragColor"); + } + + //Old 2.22 spec return shaderText .replaceAll() .replaceAll(/(gl_Position\.*[xyzw]*\s*[+*/-]*=.*;)/g, "") .replaceAll(/(gl_Position)/g, "vec4(1)") .replaceAll(/(v_color\.*[xyzw]*\s*[+*/-]*=.*;)/g, "") + .replaceAll(/attribute.*;/g,"") .replaceAll(/(penPlus_isFragment)/g, "true"); }; diff --git a/Source/blockly/patches/vertexSafeifier.js b/Source/blockly/patches/vertexSafeifier.js index 71c070e..a989a96 100644 --- a/Source/blockly/patches/vertexSafeifier.js +++ b/Source/blockly/patches/vertexSafeifier.js @@ -1,4 +1,15 @@ penPlus.makeVertexSafe = (shaderText) => { + if (shaderText.includes("#version 300 es")) { + return shaderText + .replaceAll(/(gl_FragColor\.*[xyzw]*\s*[+*/-]*=.*;)/g, "") + .replaceAll(/(gl_FragColor)/g, "vec4(1)") + .replaceAll(/(gl_FragCoord)/g, "vec2(1)") + .replaceAll("attribute", "in") + .replaceAll("varying", "out") + .replaceAll(/(penPlus_isFragment)/g, "false"); + } + + //Old 2.22 spec return shaderText .replaceAll() .replaceAll(/(gl_FragColor\.*[xyzw]*\s*[+*/-]*=.*;)/g, "") diff --git a/Source/blocks/operators.js b/Source/blocks/operators.js index fb27170..91ae1e7 100644 --- a/Source/blocks/operators.js +++ b/Source/blocks/operators.js @@ -474,6 +474,69 @@ }, ], }, + { + opcode:"clamp", + type: "reporter", + text: "clamp %1 between %2 and %3", + tooltip: "if A < B then A = B, if A > C then A = C", + + arguments: [ + { + type: "input_value", + name: "A", + shadow: { + type: "number_reporter", + }, + }, + { + type: "input_value", + name: "B", + shadow: { + type: "number_reporter", + }, + }, + { + type: "input_value", + name: "C", + shadow: { + type: "number_reporter", + }, + }, + ], + }, + "---", + { + opcode:"random", + type: "reporter", + text: "random %1 between %2 and %3", + tooltip: "if A < B then A = B, if A > C then A = C", + + arguments: [ + penPlus.createMenu( + [ + ["float", "x"], + ["vector 2", "xy"], + ["vector 3", "xyz"], + ["vector 4", "xyzw"], + ], + "A" + ), + { + type: "input_value", + name: "B", + shadow: { + type: "number_reporter", + }, + }, + { + type: "input_value", + name: "C", + shadow: { + type: "number_reporter", + }, + }, + ], + }, "---", { opcode: "cast", @@ -629,6 +692,20 @@ return [`max(${A},${B})` + nextBlockToCode(block, generator), Order.ATOMIC]; } + clamp(block, generator) { + const A = generator.valueToCode(block, "A", Order.ATOMIC); + const B = generator.valueToCode(block, "B", Order.ATOMIC); + const C = generator.valueToCode(block, "C", Order.ATOMIC); + return [`clamp(${A},${B},${C})` + nextBlockToCode(block, generator), Order.ATOMIC]; + } + + random(block, generator) { + const A = block.getFieldValue("A"); + const B = generator.valueToCode(block, "B", Order.ATOMIC); + const C = generator.valueToCode(block, "C", Order.ATOMIC); + return [`daveRandomRange(${B},${C}).${A}` + nextBlockToCode(block, generator), Order.ATOMIC]; + } + cast(block, generator) { const to = block.getFieldValue("to"); const A = generator.valueToCode(block, "A", Order.ATOMIC); diff --git a/Source/editor/compileTime/shaders.js b/Source/editor/compileTime/shaders.js index e720cbe..a5f666b 100644 --- a/Source/editor/compileTime/shaders.js +++ b/Source/editor/compileTime/shaders.js @@ -1,4 +1,4 @@ -const gl = document.getElementById("shaderpreview").getContext("webgl", { antialias: false, preserveDrawingBuffer: true }); +const gl = document.getElementById("shaderpreview").getContext("webgl2", { antialias: false, preserveDrawingBuffer: true }); function replacementShader() { penPlus.Generated_GLSL = penPlus.defaultShader + penPlus.defaultVert + penPlus.defaultFrag; diff --git a/Source/render/defaultShader.js b/Source/render/defaultShader.js index 9302dd7..bbff393 100644 --- a/Source/render/defaultShader.js +++ b/Source/render/defaultShader.js @@ -1,10 +1,13 @@ (function () { - penPlus.defaultShader = `//replacement shader -//Base Variables + penPlus.defaultShader = /*`#version 300 es + //This is the default shader for the shader editor! +//These functions are here and are written for the GLSL 3.0 specification. +//You can revert it to GLSL 2.0 by removing the version number//our output for color +//out highp vec4 fragColor;*/`//Base Variables attribute highp vec4 a_position; attribute highp vec4 a_color; attribute highp vec2 a_texCoord; - + varying highp vec4 v_color; varying highp vec2 v_texCoord; @@ -16,6 +19,8 @@ uniform highp mat4 u_transform; uniform mediump vec2 u_res; //Base functions + +//Some missing math functions highp float log10(highp float a) { return log(a)/log(10.0); } @@ -24,6 +29,35 @@ highp float eulernum(highp float a) { return 2.718 * a; } +//Psuedorandomness +highp vec4 pcg4d(highp vec4 v) +{ + v = v * 1664525.0 + 1013904223.0; + + v.x += v.y*v.w; + v.y += v.z*v.x; + v.z += v.x*v.y; + v.w += v.y*v.z; + + v.x += v.z*v.w; + v.y += v.y*v.x; + v.z += v.x*v.w; + v.w += v.y*v.x; + + return vec4(v); +} + +highp vec4 daveRandomRange(highp float lowR, highp float highR) +{ + highp float randomizer = (gl_FragCoord.x * 50.25313532) + (gl_FragCoord.y * 21.5453) + u_timer; + return clamp(vec4( + fract(sin(randomizer*(91.3458)) * 47453.5453), + fract(sin(randomizer*(80.3458)) * 48456.5453), + fract(sin(randomizer*(95.3458)) * 42457.5453), + fract(sin(randomizer*(85.3458)) * 47553.5453) + ), lowR, highR); +} + highp vec4 HSVToRGB(highp float hue, highp float saturation, highp float value, highp float a) { highp float huePrime = mod(hue,360.0); highp float c = (value/100.0) * (saturation/100.0);