Skip to content

Commit

Permalink
Add more parameters to the Shape node
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwych committed Nov 19, 2023
1 parent 0f3c0c4 commit 8a8620f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ in vec2 a_texCoord;
uniform int p_shape;
uniform float p_rectWidth;
uniform float p_rectHeight;
uniform float p_rectRoundness;
uniform float p_circleRadius;
uniform float p_triangleSize;
uniform float p_outline;
uniform float p_cutoff;

out vec4 out_color;

Expand All @@ -18,8 +21,9 @@ out vec4 out_color;
// SDFs adapted from https://iquilezles.org/

float sdfRect(vec2 uv, vec2 size) {
size -= vec2(p_rectRoundness);
vec2 d = abs(uv) - size;
return length(max(d, 0.0f)) + min(max(d.x, d.y), 0.0f);
return (length(max(d, 0.0f)) + min(max(d.x, d.y), 0.0f)) - p_rectRoundness;
}

float sdfCircle(vec2 uv, float radius) {
Expand All @@ -34,7 +38,7 @@ float sdfTriangle(vec2 uv, float r) {
uv = vec2(uv.x - k * uv.y, -k * uv.x - uv.y) / 2.0f;
}
uv.x -= clamp(uv.x, -2.0f * r, 0.0f);
return -length(uv) * sign(uv.y);
return (-length(uv) * sign(uv.y));
}

float shape(vec2 uv) {
Expand All @@ -51,7 +55,17 @@ float shape(vec2 uv) {
}

void main(void) {
float d = step(0.0f, shape(a_texCoord - vec2(0.5f, 0.5f)));
out_color = vec4(1.0f - d);
out_color.a = 1.0f;
float d = shape(a_texCoord - vec2(0.5f, 0.5f));

if(p_outline > 0.0f) {
float d2 = d * 100.0f;
if(d2 <= p_outline) {
d2 = 0.0f;
}
d = d2 - d;
}

d = 1.0f - smoothstep(0.0f, p_cutoff, d);

out_color = vec4(vec3(d), 1.0f);
}
36 changes: 35 additions & 1 deletion resources/nodes/generate/shape.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MaterialNodeBlueprint } from "../../../packages/material/node";
import glsl from "./shape.glsl?raw";
import glsl from "./shape.fs?raw";

export default {
id: "shape",
Expand Down Expand Up @@ -44,6 +44,18 @@ export default {
},
when: "params.shape === 0",
},
rectRoundness: {
id: "rectRoundness",
name: "Roundness",
default: 0,
inputType: "number",
valueType: "float",
inputProps: {
min: 0,
max: 1,
},
when: "params.shape === 0",
},
circleRadius: {
id: "circleRadius",
name: "Radius",
Expand All @@ -68,6 +80,28 @@ export default {
},
when: "params.shape === 2",
},
cutoff: {
id: "cutoff",
name: "Cutoff",
default: 0,
inputType: "number",
valueType: "float",
inputProps: {
min: 0,
max: 1,
},
},
outline: {
id: "outline",
name: "Outline Thickness",
default: 0,
inputType: "number",
valueType: "float",
inputProps: {
min: 0,
max: 1,
},
},
},
inputs: {},
outputs: {
Expand Down

0 comments on commit 8a8620f

Please sign in to comment.