Matcap

View stable material capture.
Use ‘edge_fix’ to sample further away from the edges to avoid artifacts.

Shader code
shader_type spatial;
render_mode unshaded;

uniform sampler2D texture_albedo : source_color, repeat_disable;
uniform float edge_fix: hint_range(0.0, 0.03, 0.001) = 0.01;

vec2 matcap_uv(vec3 view, vec3 normal) {
	normal = normalize(normal);
    vec3 up = abs(view.y) > 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
    vec3 tangent = normalize(cross(up, view));
    vec3 bitangent = cross(view, tangent);
    vec2 uv = vec2(dot(tangent, normal), -dot(bitangent, normal));
    return uv * (0.5 - edge_fix) + 0.5;
}

void fragment() {
    vec2 uv = matcap_uv(VIEW, NORMAL);
    ALBEDO = texture(texture_albedo, uv).rgb;
}
Tags
matcap, Material Capture
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 jacob

Transparency Dither

Related shaders

I-Can’t-Believe-It’s-Not-Matcap (modular view-based shading)

Matcap + NormalMap + Triplanar

Matcap + NormalMap

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments