Skip to content

Commit

Permalink
Add random block
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Orangemoon committed Jul 18, 2024
1 parent 4ab1097 commit 1ab5afd
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Source/blockly/patches/fragmentSafeifier.js
Original file line number Diff line number Diff line change
@@ -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");
};
11 changes: 11 additions & 0 deletions Source/blockly/patches/vertexSafeifier.js
Original file line number Diff line number Diff line change
@@ -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, "")
Expand Down
77 changes: 77 additions & 0 deletions Source/blocks/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Source/editor/compileTime/shaders.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
40 changes: 37 additions & 3 deletions Source/render/defaultShader.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit 1ab5afd

Please sign in to comment.