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

Particle Flipbook Flowmap Smoothing

Related shaders

Wireframe shader

Wireframe

The Best Darn Grid Shader (Yet) for Godot

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
leon
leon
9 months ago

This seems to work well for CSG shapes, but shapes imported from Blender seem to have entire faces shaded as the wire color. Any idea what could be causing this?