Skip to content

Commit bf0732d

Browse files
authored
Merge pull request #12 from Mikeysax/main
Added shapes, uv_grid, uv_rotate, fresnel, and fresnel_glow functions
2 parents 3b4c93e + d50ce88 commit bf0732d

5 files changed

Lines changed: 117 additions & 5 deletions

File tree

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
This project aims to help Godot developers writing custom written shaders by providing high-level functions which are often used.
1818

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

2323
## 🔢 Versioning
@@ -73,19 +73,32 @@ All these shader functions are based on publicly available shader functions (ope
7373
* `proximity_fade_*`
7474
* `random_range`
7575
* `remap`
76+
* `fresnel`
77+
* `fresnel_glow`
7678

7779
### UV
7880
* `uv_panning`
7981
* `uv_scaling`
82+
* `uv_rotate`
8083
* `uv_polar_coord_*`
8184
* `uv_flipbook`
8285
* `uv_twirl`
86+
* `uv_grid_tiler`
8387

8488
### Wave
8589
* `sawtooth_wave`
8690
* `sine_wave`
8791
* `sine_wave_angular`
8892
* `square_wave`
8993
* `triangle_wave`
90-
94+
95+
### Shapes
96+
* `polygon`
97+
* `circle`
98+
* `square`
99+
* `square_stroke`
100+
* `square_rounded`
101+
* `swirl`
102+
* `line`
103+
91104
</details>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# License Information
2+
3+
## shapes.gdshaderinc
4+
```md
5+
/**************************************************************************/
6+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
7+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
8+
/* */
9+
/* Permission is hereby granted, free of charge, to any person obtaining */
10+
/* a copy of this software and associated documentation files (the */
11+
/* "Software"), to deal in the Software without restriction, including */
12+
/* without limitation the rights to use, copy, modify, merge, publish, */
13+
/* distribute, sublicense, and/or sell copies of the Software, and to */
14+
/* permit persons to whom the Software is furnished to do so, subject to */
15+
/* the following conditions: */
16+
/* */
17+
/* The above copyright notice and this permission notice shall be */
18+
/* included in all copies or substantial portions of the Software. */
19+
/* */
20+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
21+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
22+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
23+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
24+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
25+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
26+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27+
/**************************************************************************/
28+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
float polygon(vec2 uv, float width, int sides) {
2+
uv = uv * 2.0 - 1.0;
3+
float angle = atan(uv.x, uv.y);
4+
float radius = 6.28318530718 / float(sides);
5+
float dist = cos(floor(0.5 + angle / radius) * radius - angle) * length(uv);
6+
return step(width, dist);
7+
}
8+
9+
float circle(vec2 uv, float radius, float feather) {
10+
return smoothstep(radius, radius + feather, dot(position, position) * 4.0);
11+
}
12+
13+
float square(vec2 uv, float width) {
14+
uv = uv * 2.0 - 1.0;
15+
vec2 abs_uv = abs(uv.xy);
16+
return step(width, max(abs_uv.x, abs_uv.y));
17+
}
18+
19+
float square_stroke(vec2 uv, float width, float stroke_width) {
20+
uv = uv * 2.0 - 1.0;
21+
vec2 abs_uv = abs(uv.xy);
22+
float dist = max(abs_uv.x, abs_uv.y);
23+
return 1.0 - (step(width, dist) - step(width + stroke_width, dist));
24+
}
25+
26+
float square_rounded(vec2 uv, float width, float radius) {
27+
uv = uv * 2.0 - 1.0;
28+
radius *= width;
29+
vec2 abs_uv = abs(uv.xy) - radius;
30+
vec2 dist = vec2(max(abs_uv.xy, 0.0));
31+
return step(width - radius, length(dist));
32+
}
33+
34+
float swirl(vec2 uv, float size, int arms) {
35+
float angle = atan(-uv.y + 0.5, uv.x - 0.5);
36+
float len = length(uv - vec2(0.5, 0.5));
37+
return sin(len * size + angle * float(arms));
38+
}
39+
40+
float line(vec2 uv, vec2 p1, vec2 p2, float width) {
41+
float dist = distance(p1, p2);
42+
float dist_uv = distance(p1, uv);
43+
return 1.0 - floor(1.0 - (0.001 * width) + distance (mix(p1, p2, clamp(dist_uv / dist, 0.0, 1.0)), uv));
44+
}
45+
46+
float border(vec2 uv, float border_width) {
47+
vec2 bottom_left = step(vec2(border_width), uv);
48+
vec2 top_right = step(vec2(border_width), 1.0 - uv);
49+
return bottom_left.x * bottom_left.y * top_right.x * top_right.y;
50+
}
51+

addons/ShaderFunction-Extras/UV/uv.gdshaderinc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ vec2 uv_scaling(vec2 uv, vec2 scale, vec2 pivot) {
66
return (uv - pivot) * scale + pivot;
77
}
88

9+
vec2 uv_rotate(vec2 uv, vec2 pivot, float angle) {
10+
mat2 rotation = mat2(vec2(sin(angle), -cos(angle)), vec2(cos(angle), sin(angle)));
11+
uv -= pivot;
12+
uv = uv * rotation;
13+
uv += pivot;
14+
return uv;
15+
}
16+
917
vec2 uv_polar_coord_canvas(vec2 uv, vec2 center, float zoom, float repeat) {
1018
vec2 dir = uv - center;
1119
float radius = length(dir) * 2.0;
@@ -24,17 +32,17 @@ vec2 uv_flipbook(vec2 uv, int columns, int rows, int starting_frame, int ending_
2432
starting_frame += int(fract(TIME * anim_speed) * float(ending_frame));
2533
float frame = float(clamp(starting_frame, 0, ending_frame));
2634
vec2 offPerFrame = vec2((1.0 / float(columns)), (1.0 / float(rows)));
27-
35+
2836
vec2 sprite_size = vec2(uv.x / float(columns), uv.y / float(rows));
2937
vec2 current_sprite = vec2(0.0, 1.0 - offPerFrame.y);
3038
current_sprite.x += frame * offPerFrame.x;
3139
float rowIndex;
3240
float _mod = modf(frame / float(columns), rowIndex);
3341
current_sprite.y -= rowIndex * offPerFrame.y;
3442
current_sprite.x -= rowIndex * float(columns) * offPerFrame.x;
35-
43+
3644
vec2 sprite_uv = (sprite_size + current_sprite);
37-
45+
3846
return sprite_uv;
3947
}
4048

@@ -45,3 +53,7 @@ vec2 uv_twirl(vec2 uv, vec2 center, float strength, vec2 offset) {
4553
float __y = sin(__angle) * __delta.x + cos(__angle) * __delta.y;
4654
return vec2(__x + center.x + offset.x, __y + center.y + offset.y);
4755
}
56+
57+
vec2 uv_grid_tiler(vec2 uv, float columns, float rows) {
58+
return fract(vec2(uv.x * columns, uv.y * rows));
59+
}

addons/ShaderFunction-Extras/Utility/utility.gdshaderinc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ float remap(float value, float input_min, float input_max, float output_min, flo
4343
float _output_range = output_max - output_min;
4444
return output_min + _output_range * ((value - input_min) / _input_range);
4545
}
46+
47+
float fresnel(float amount, vec3 normal, vec3 view) {
48+
return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0 )), amount);
49+
}
50+
51+
vec3 fresnel_glow(float amount, float intensity, vec3 color, vec3 normal, vec3 view) {
52+
return pow((1.0 - dot(normalize(normal), normalize(view))), amount) * color * intensity;
53+
}

0 commit comments

Comments
 (0)