diff --git a/README.md b/README.md index 331bc53..fa7db0c 100644 --- a/README.md +++ b/README.md @@ -31,18 +31,22 @@ The image file will be read and available as the `TEXTURE` variable. uniform sampler2D ; ``` -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); } ``` diff --git a/examples/CREDITS.md b/examples/CREDITS.md index 99df0e2..ed427c8 100644 --- a/examples/CREDITS.md +++ b/examples/CREDITS.md @@ -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/) diff --git a/examples/grass.png b/examples/grass.png new file mode 100644 index 0000000..bb7e120 Binary files /dev/null and b/examples/grass.png differ diff --git a/examples/overlay.jpg.import b/examples/grass.png.import similarity index 69% rename from examples/overlay.jpg.import rename to examples/grass.png.import index 7d189f0..f9a52a6 100644 --- a/examples/overlay.jpg.import +++ b/examples/grass.png.import @@ -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] diff --git a/examples/mix.gdshader b/examples/mix.gdshader deleted file mode 100644 index a043856..0000000 --- a/examples/mix.gdshader +++ /dev/null @@ -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); -} diff --git a/examples/overlay.jpg b/examples/overlay.jpg deleted file mode 100644 index b305379..0000000 Binary files a/examples/overlay.jpg and /dev/null differ diff --git a/examples/place_texture.gdshader b/examples/place_texture.gdshader new file mode 100644 index 0000000..95a26f0 --- /dev/null +++ b/examples/place_texture.gdshader @@ -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); +} diff --git a/shaderlib/transform.gdshaderinc b/shaderlib/transform.gdshaderinc new file mode 100644 index 0000000..c888732 --- /dev/null +++ b/shaderlib/transform.gdshaderinc @@ -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 +}