Skip to content

Commit

Permalink
Modified shape functions to return floats and moved grid to uv as uv_…
Browse files Browse the repository at this point in the history
…grid_tiler given it cannot return a float and more
  • Loading branch information
mikeysax committed Mar 10, 2023
1 parent db3b355 commit 8431f70
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ All these shader functions are based on publicly available shader functions (ope
* `uv_polar_coord_*`
* `uv_flipbook`
* `uv_twirl`
* `uv_grid_tiler`

### Wave
* `sawtooth_wave`
Expand All @@ -99,6 +100,5 @@ All these shader functions are based on publicly available shader functions (ope
* `square_rounded`
* `swirl`
* `line`
* `grid`

</details>
24 changes: 8 additions & 16 deletions addons/ShaderFunction-Extras/Shapes/shapes.gdshaderinc
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
vec4 polygon(vec2 uv, float width, int sides) {
float polygon(vec2 uv, float width, int sides) {
uv = uv * 2.0 - 1.0;
float angle = atan(uv.x, uv.y);
float radius = 6.28318530718 / float(sides);
float dist = cos(floor(0.5 + angle / radius) * radius - angle) * length(uv);
float poly = step(width, dist);
return vec4(vec3(poly), 1.0);
return step(width, dist);
}

float circle(vec2 uv, float radius, float feather) {
return smoothstep(radius, radius + feather, dot(position, position) * 4.0);
}

vec4 square(vec2 uv, float width) {
float square(vec2 uv, float width) {
uv = uv * 2.0 - 1.0;
vec2 abs_uv = abs(uv.xy);
float square = step(width, max(abs_uv.x, abs_uv.y));
return vec4(vec3(square), 1.0);
return step(width, max(abs_uv.x, abs_uv.y));
}

vec4 square_stroke(vec2 uv, float width, float stroke_width) {
float square_stroke(vec2 uv, float width, float stroke_width) {
uv = uv * 2.0 - 1.0;
vec2 abs_uv = abs(uv.xy);
float dist = max(abs_uv.x, abs_uv.y);
vec3 stroke = 1.0 - vec3(step(width, dist) - step(width + stroke_width, dist));
return vec4(vec3(stroke), 1.0);
return 1.0 - (step(width, dist) - step(width + stroke_width, dist));
}

vec4 square_rounded(vec2 uv, float width, float radius) {
float square_rounded(vec2 uv, float width, float radius) {
uv = uv * 2.0 - 1.0;
radius *= width;
vec2 abs_uv = abs(uv.xy) - radius;
vec2 dist = vec2(max(abs_uv.xy, 0.0));
float square = step(width - radius, length(dist));
return vec4(vec3(square), 1.0);
return step(width - radius, length(dist));
}

float swirl(vec2 uv, float size, int arms) {
Expand All @@ -53,7 +49,3 @@ float border(vec2 uv, float border_width) {
return bottom_left.x * bottom_left.y * top_right.x * top_right.y;
}

vec2 grid(vec2 uv, float columns, float rows) {
return fract(vec2(uv.x * columns, uv.y * rows));
}

4 changes: 4 additions & 0 deletions addons/ShaderFunction-Extras/UV/uv.gdshaderinc
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ vec2 uv_twirl(vec2 uv, vec2 center, float strength, vec2 offset) {
float __y = sin(__angle) * __delta.x + cos(__angle) * __delta.y;
return vec2(__x + center.x + offset.x, __y + center.y + offset.y);
}

vec2 uv_grid_tiler(vec2 uv, float columns, float rows) {
return fract(vec2(uv.x * columns, uv.y * rows));
}

0 comments on commit 8431f70

Please sign in to comment.