Voxel Shader

A sudo working voxel shader

Shader code
shader_type spatial;

uniform float voxel_size : hint_range(0.01, 1.0) = 0.15; // Size of each voxel

void vertex() {
    // Get world position of the vertex
    vec3 world_pos = VERTEX * mat3(MODEL_MATRIX) + MODEL_MATRIX[3].xyz;

    // Snap to voxel grid
    world_pos = floor(world_pos / voxel_size) * voxel_size;

    // Convert back to object space
    VERTEX = (inverse(MODEL_MATRIX) * vec4(world_pos, 1.0)).xyz;
}
Live Preview
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.

Related shaders

guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
LohaGames
LohaGames
3 months ago

I made some adjustments that fix some world position errors and also adds texture input

shader_type spatial;

uniform float voxel_size : hint_range(0.01, 1.0) = 0.15; // Size of each voxel
uniform sampler2D color : hint_default_white;

void vertex() {
  // Get world position of the vertex
  vec3 world_pos = VERTEX * mat3(MODEL_MATRIX) + MODEL_MATRIX[3].xyz;

  // Snap to voxel grid
  world_pos = floor(world_pos / voxel_size) * voxel_size;

  // Convert back to object space
  VERTEX = (MODEL_MATRIX * vec4(world_pos, 1.0)).xyz;
}

void fragment() {
vec3 alb = texture(color,UV).rgb;

ALBEDO = alb;
}