From 8a8620fd16513008b1f21b4c6ac7a08b0d6ca2e8 Mon Sep 17 00:00:00 2001 From: Adam Wychowaniec Date: Sun, 19 Nov 2023 07:03:22 +0100 Subject: [PATCH] Add more parameters to the Shape node --- .../nodes/generate/{shape.glsl => shape.fs} | 24 ++++++++++--- resources/nodes/generate/shape.ts | 36 ++++++++++++++++++- 2 files changed, 54 insertions(+), 6 deletions(-) rename resources/nodes/generate/{shape.glsl => shape.fs} (68%) diff --git a/resources/nodes/generate/shape.glsl b/resources/nodes/generate/shape.fs similarity index 68% rename from resources/nodes/generate/shape.glsl rename to resources/nodes/generate/shape.fs index 5dc9391..2fd0ff4 100644 --- a/resources/nodes/generate/shape.glsl +++ b/resources/nodes/generate/shape.fs @@ -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; @@ -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) { @@ -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) { @@ -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); } \ No newline at end of file diff --git a/resources/nodes/generate/shape.ts b/resources/nodes/generate/shape.ts index 922f2d8..812fdf1 100644 --- a/resources/nodes/generate/shape.ts +++ b/resources/nodes/generate/shape.ts @@ -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", @@ -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", @@ -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: {