shaderlib: Implement place_texture() and provide an example, replace mix.gdshader example; implements #17
This commit is contained in:
parent
8b5a1cc37d
commit
1aa217776c
8 changed files with 48 additions and 21 deletions
14
README.md
14
README.md
|
@ -31,18 +31,22 @@ The image file will be read and available as the `TEXTURE` variable.
|
|||
uniform sampler2D <name>;
|
||||
```
|
||||
|
||||
Have a look at the `mix.gdshader` example:
|
||||
Have a look at the `place_texture.gdshader` example:
|
||||
|
||||
```glsl
|
||||
shader_type canvas_item;
|
||||
|
||||
//!load ./swamp.jpg
|
||||
#include "res://shaderlib/transform.gdshaderinc"
|
||||
|
||||
//!load+ img2 ./overlay.jpg
|
||||
uniform sampler2D img2: repeat_enable, filter_nearest;
|
||||
//!load ./swamp.jpg
|
||||
//!load+ img2 ./grass.png
|
||||
|
||||
uniform sampler2D img2: repeat_disable, filter_nearest;
|
||||
|
||||
void fragment() {
|
||||
COLOR = mix(COLOR, texture(img2, UV), .2);
|
||||
vec4 grass = place_texture(img2, UV, TEXTURE_PIXEL_SIZE, vec2(0, .47), vec2(1));
|
||||
grass.rgb += (vec3(0.03, 0.07, 0.11) - ((UV.y - .8) * 0.15)); // color correction
|
||||
COLOR.rgb = mix(COLOR.rgb, grass.rgb, grass.a);
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
# Example Images
|
||||
|
||||
- swamp.jpg by [clfr21](https://pixabay.com/de/users/clfr21-6530007/)
|
||||
- overlay.jpg by [Humusak](https://pixabay.com/de/users/humusak-137455/)
|
||||
- swamp.jpg by [clfr21 on Pixabay](https://pixabay.com/de/users/clfr21-6530007/)
|
||||
- grass.png by [GDJ on Pixabay](https://pixabay.com/users/gdj-1086657/)
|
||||
|
|
BIN
examples/grass.png
Normal file
BIN
examples/grass.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 150 KiB |
|
@ -2,16 +2,16 @@
|
|||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://8101nnc8cx4o"
|
||||
path="res://.godot/imported/overlay.jpg-b6d13e0def59877969c9eda02fa4244e.ctex"
|
||||
uid="uid://c1mh1d2f3u4ju"
|
||||
path="res://.godot/imported/grass.png-30af7213ed2d70b810b4ae788314a9e9.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://examples/overlay.jpg"
|
||||
dest_files=["res://.godot/imported/overlay.jpg-b6d13e0def59877969c9eda02fa4244e.ctex"]
|
||||
source_file="res://examples/grass.png"
|
||||
dest_files=["res://.godot/imported/grass.png-30af7213ed2d70b810b4ae788314a9e9.ctex"]
|
||||
|
||||
[params]
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
shader_type canvas_item;
|
||||
|
||||
//!load ./swamp.jpg
|
||||
|
||||
//!load+ img2 ./overlay.jpg
|
||||
uniform sampler2D img2: repeat_enable, filter_nearest;
|
||||
|
||||
void fragment() {
|
||||
COLOR = mix(COLOR, texture(img2, UV), .2);
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 896 KiB |
14
examples/place_texture.gdshader
Normal file
14
examples/place_texture.gdshader
Normal file
|
@ -0,0 +1,14 @@
|
|||
shader_type canvas_item;
|
||||
|
||||
#include "res://shaderlib/transform.gdshaderinc"
|
||||
|
||||
//!load ./swamp.jpg
|
||||
//!load+ img2 ./grass.png
|
||||
|
||||
uniform sampler2D img2: repeat_disable, filter_nearest;
|
||||
|
||||
void fragment() {
|
||||
vec4 grass = place_texture(img2, UV, TEXTURE_PIXEL_SIZE, vec2(0, .47), vec2(1));
|
||||
grass.rgb += (vec3(0.03, 0.07, 0.11) - ((UV.y - .8) * 0.15)); // color correction
|
||||
COLOR.rgb = mix(COLOR.rgb, grass.rgb, grass.a);
|
||||
}
|
19
shaderlib/transform.gdshaderinc
Normal file
19
shaderlib/transform.gdshaderinc
Normal file
|
@ -0,0 +1,19 @@
|
|||
|
||||
// Load in a texture from a sampler2D with an offset and scale
|
||||
// See examples/place_texture.gdshader
|
||||
vec4 place_texture(sampler2D sampler, vec2 uv, vec2 texture_pixel_size, vec2 offset, vec2 scale) {
|
||||
vec2 texture_size = vec2(textureSize(sampler, 0));
|
||||
// position of current pixel; sample color c
|
||||
vec2 pos = (uv - offset) / (texture_size*texture_pixel_size) / scale;
|
||||
vec4 c = texture(sampler, pos);
|
||||
// top-left bounds
|
||||
vec2 a = offset;
|
||||
// bottom-right bounds
|
||||
vec2 b = offset + (texture_size*texture_pixel_size) * scale;
|
||||
// check bounds
|
||||
if (
|
||||
a.x < uv.x && a.y < uv.y
|
||||
&& b.x > uv.x && b.y > uv.y
|
||||
) { return c; } // within bounds -> return color
|
||||
return vec4(0); // not within bounds -> return transparency
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue