shaderlib: Implement place_texture() and provide an example, replace mix.gdshader example; implements #17

This commit is contained in:
ChaoticByte 2024-12-28 00:00:46 +01:00
parent 8b5a1cc37d
commit 1aa217776c
No known key found for this signature in database
8 changed files with 48 additions and 21 deletions

View file

@ -31,18 +31,22 @@ The image file will be read and available as the `TEXTURE` variable.
uniform sampler2D <name>; uniform sampler2D <name>;
``` ```
Have a look at the `mix.gdshader` example: Have a look at the `place_texture.gdshader` example:
```glsl ```glsl
shader_type canvas_item; shader_type canvas_item;
//!load ./swamp.jpg #include "res://shaderlib/transform.gdshaderinc"
//!load+ img2 ./overlay.jpg //!load ./swamp.jpg
uniform sampler2D img2: repeat_enable, filter_nearest; //!load+ img2 ./grass.png
uniform sampler2D img2: repeat_disable, filter_nearest;
void fragment() { 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);
} }
``` ```

View file

@ -1,5 +1,5 @@
# Example Images # Example Images
- swamp.jpg by [clfr21](https://pixabay.com/de/users/clfr21-6530007/) - swamp.jpg by [clfr21 on Pixabay](https://pixabay.com/de/users/clfr21-6530007/)
- overlay.jpg by [Humusak](https://pixabay.com/de/users/humusak-137455/) - grass.png by [GDJ on Pixabay](https://pixabay.com/users/gdj-1086657/)

BIN
examples/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

View file

@ -2,16 +2,16 @@
importer="texture" importer="texture"
type="CompressedTexture2D" type="CompressedTexture2D"
uid="uid://8101nnc8cx4o" uid="uid://c1mh1d2f3u4ju"
path="res://.godot/imported/overlay.jpg-b6d13e0def59877969c9eda02fa4244e.ctex" path="res://.godot/imported/grass.png-30af7213ed2d70b810b4ae788314a9e9.ctex"
metadata={ metadata={
"vram_texture": false "vram_texture": false
} }
[deps] [deps]
source_file="res://examples/overlay.jpg" source_file="res://examples/grass.png"
dest_files=["res://.godot/imported/overlay.jpg-b6d13e0def59877969c9eda02fa4244e.ctex"] dest_files=["res://.godot/imported/grass.png-30af7213ed2d70b810b4ae788314a9e9.ctex"]
[params] [params]

View file

@ -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

View 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);
}

View 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
}