Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added shapes, uv_grid, uv_rotate, fresnel, and fresnel_glow functions #12

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
This project aims to help Godot developers writing custom written shaders by providing high-level functions which are often used.

## :tada: Using the addon
Download the source (releases and AssetLib version are still WIP) and put the `ShaderFunction-Extras` inside
Download the source (releases and AssetLib version are still WIP) and put the `ShaderFunction-Extras` inside
your `addons` folder. If you don't have one, create it or put the whole addons folder inside your project directory.

## 🔢 Versioning
Expand Down Expand Up @@ -73,19 +73,32 @@ All these shader functions are based on publicly available shader functions (ope
* `proximity_fade_*`
* `random_range`
* `remap`
* `fresnel`
* `fresnel_glow`

### UV
* `uv_panning`
* `uv_scaling`
* `uv_rotate`
* `uv_polar_coord_*`
* `uv_flipbook`
* `uv_twirl`
* `uv_grid_tiler`

### Wave
* `sawtooth_wave`
* `sine_wave`
* `sine_wave_angular`
* `square_wave`
* `triangle_wave`


### Shapes
* `polygon`
* `circle`
* `square`
* `square_stroke`
* `square_rounded`
* `swirl`
* `line`

</details>
28 changes: 28 additions & 0 deletions addons/ShaderFunction-Extras/Shapes/LicenseInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# License Information

## shapes.gdshaderinc
```md
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
```
51 changes: 51 additions & 0 deletions addons/ShaderFunction-Extras/Shapes/shapes.gdshaderinc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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);
return step(width, dist);
}

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

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

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);
return 1.0 - (step(width, dist) - step(width + stroke_width, dist));
}

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));
return step(width - radius, length(dist));
}

float swirl(vec2 uv, float size, int arms) {
float angle = atan(-uv.y + 0.5, uv.x - 0.5);
float len = length(uv - vec2(0.5, 0.5));
return sin(len * size + angle * float(arms));
}

float line(vec2 uv, vec2 p1, vec2 p2, float width) {
float dist = distance(p1, p2);
float dist_uv = distance(p1, uv);
return 1.0 - floor(1.0 - (0.001 * width) + distance (mix(p1, p2, clamp(dist_uv / dist, 0.0, 1.0)), uv));
}

float border(vec2 uv, float border_width) {
vec2 bottom_left = step(vec2(border_width), uv);
vec2 top_right = step(vec2(border_width), 1.0 - uv);
return bottom_left.x * bottom_left.y * top_right.x * top_right.y;
}

18 changes: 15 additions & 3 deletions addons/ShaderFunction-Extras/UV/uv.gdshaderinc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ vec2 uv_scaling(vec2 uv, vec2 scale, vec2 pivot) {
return (uv - pivot) * scale + pivot;
}

vec2 uv_rotate(vec2 uv, vec2 pivot, float angle) {
mat2 rotation = mat2(vec2(sin(angle), -cos(angle)), vec2(cos(angle), sin(angle)));
uv -= pivot;
uv = uv * rotation;
uv += pivot;
return uv;
}

vec2 uv_polar_coord_canvas(vec2 uv, vec2 center, float zoom, float repeat) {
vec2 dir = uv - center;
float radius = length(dir) * 2.0;
Expand All @@ -24,17 +32,17 @@ vec2 uv_flipbook(vec2 uv, int columns, int rows, int starting_frame, int ending_
starting_frame += int(fract(TIME * anim_speed) * float(ending_frame));
float frame = float(clamp(starting_frame, 0, ending_frame));
vec2 offPerFrame = vec2((1.0 / float(columns)), (1.0 / float(rows)));

vec2 sprite_size = vec2(uv.x / float(columns), uv.y / float(rows));
vec2 current_sprite = vec2(0.0, 1.0 - offPerFrame.y);
current_sprite.x += frame * offPerFrame.x;
float rowIndex;
float _mod = modf(frame / float(columns), rowIndex);
current_sprite.y -= rowIndex * offPerFrame.y;
current_sprite.x -= rowIndex * float(columns) * offPerFrame.x;

vec2 sprite_uv = (sprite_size + current_sprite);

return sprite_uv;
}

Expand All @@ -45,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));
}
8 changes: 8 additions & 0 deletions addons/ShaderFunction-Extras/Utility/utility.gdshaderinc
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ float remap(float value, float input_min, float input_max, float output_min, flo
float _output_range = output_max - output_min;
return output_min + _output_range * ((value - input_min) / _input_range);
}

float fresnel(float amount, vec3 normal, vec3 view) {
return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0 )), amount);
}

vec3 fresnel_glow(float amount, float intensity, vec3 color, vec3 normal, vec3 view) {
return pow((1.0 - dot(normalize(normal), normalize(view))), amount) * color * intensity;
}