Wireframe Shader (Godot 4.0)

Note: This only works in Godot 4.0 as you need the VERTEX_IDs in the shader to assign barycentric values.

Note: This only works consistently with flat-shaded models as the VERTED_IDs are not reliable for this when smoothshaded.

This was used as reference. https://catlikecoding.com/unity/tutorials/advanced-rendering/flat-and-wireframe-shading/

Shader code
shader_type spatial;

uniform vec4 albedo : source_color = vec4(1.0);
uniform vec4 wire_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);
uniform float wire_width : hint_range(0.0, 40.0) = 5.0;
uniform float wire_smoothness : hint_range(0.0, 0.1) = 0.01;

varying vec3 barys;

void vertex() {
	int index = VERTEX_ID % 3;
	switch (index) {
		case 0:
			barys = vec3(1.0, 0.0, 0.0);
			break;
		case 1:
			barys = vec3(0.0, 1.0, 0.0);
			break;
		case 2:
			barys = vec3(0.0, 0.0, 1.0);
			break;
	}
}

void fragment() {
	vec3 deltas = fwidth(barys);
	vec3 barys_s = smoothstep(deltas * wire_width - wire_smoothness, deltas * wire_width + wire_smoothness, barys);
	float wires = min(barys_s.x, min(barys_s.y, barys_s.z));
	ALBEDO = mix(wire_color.rgb, albedo.rgb, wires);
}
Tags
wireframe
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 Arnklit

World Normal Mix Shader

Related shaders

MToon Shader for Godot

Pixel Art Wind Sway (MeshInstance3D, Godot 4.0)

Billboard Sprite3D Sway (Godot 4.0)

guest

0 Comments
Inline Feedbacks
View all comments