Cast Texture To Surface

Extremly basic and simple shader for casting a image texture onto a surface.

It features scale, offset and basic color mixing.

It is a nice starting point for learning or more complex shaders.

 

Use:

–  Create new Shader Material

– Create new shader and paste the shader code

– add your texture of choice and play with other parameters

 

Note:

The texture used in screenshot is low-res so it looks so bad. Use higher quality images for better result. (duh)

 

Shader code
shader_type spatial;

uniform sampler2D tex : filter_linear_mipmap, repeat_enable; 
//texture to be casted. Change to repeat_disable if needed
uniform vec2 tex_offset = vec2(0.0);
//texture image offset
uniform vec2 tex_scale = vec2(1.0);
//texture image scale
uniform vec3 tex_color_shift : source_color = vec3(1.0);
//color to be mixed with texture image

vec2 scale(vec2 uv, float x, float y)
{
	//scale function taken from godotshader.com snippets page
	//https://godotshaders.com/snippet/scale/
	
	mat2 scale = mat2(vec2(x, 0.0), vec2(0.0, y));
	
	uv -= 0.5;
	uv = uv * scale;
	uv += 0.5;
	return uv;
}

void fragment() {
	vec2 uv = vec2(1.0 - UV.y, UV.x);
	//rotate UV correctly (change if image is facing wrong way)
	uv = scale(uv, tex_scale.x, tex_scale.y) + (tex_offset * 0.5); 
	//apply scale and offset
	vec3 tex_uv = texture(tex,  uv).xyz; 
	//extract color from texture on uv cordinates
	ALBEDO = tex_uv * tex_color_shift;
	//apply mix of extracted color and tex_color_shift color
}
Tags
cast texture offset scale surface 3d spatial vulkan
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Batboil

Image Warp Shader

Related shaders

PSX Style Water Surface – Pixelation, Waves, Scrolling Textures

Surface Embedded Sprites

Extrude 2D Texture in 3D

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments