Voxel Volumetric Cloud (Base)
The idea: Use voxel traversal algorithm instead of raymarch.
Setup:
Add a Meshinstance -> boxmesh to the scene.
Add ShaderMaterial, create new NoiseTexture3D and assign it to the shader VolumeTex.
!!! Set your box size in the shader.
const vec3 box_size = vec3(size in meter);
!! Set the noisetexture3d resolution correctly
It must match the box scale/size.
ex: box is 10 x 5 x 20 meter and noisetexture3d res is 20 x 10 x 40 pixel.
! Subdivide the box mesh to avoid deformation.
ex: 16.
Made 100% with AI.
Shader code
shader_type spatial;
render_mode unshaded, cull_front, blend_mix, depth_draw_never;
uniform sampler3D volume_tex : filter_linear;
uniform float threshold = 0.6;
uniform int max_steps = 128;
const vec3 box_size = vec3(1.0, 1.0, 1.0);
varying vec3 v_ro_local;
varying vec3 v_rd_local;
void vertex() {
// 1. Transform camera world position to Local Space
mat4 world_to_local = inverse(MODEL_MATRIX);
vec3 cam_world_pos = (MAIN_CAM_INV_VIEW_MATRIX * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
v_ro_local = (world_to_local * vec4(cam_world_pos, 1.0)).xyz;
// 2. Calculate geometry-locked ray direction in Local Space
vec3 vertex_local = VERTEX;
v_rd_local = normalize(vertex_local - v_ro_local);
}
void fragment() {
ALPHA = 0.0;
vec3 ro = v_ro_local;
vec3 rd = normalize(v_rd_local);
// 3. Local Box Bounds centered at (0, 0, 0)
vec3 box_min = -0.5 * box_size;
vec3 box_max = 0.5 * box_size;
// 4. Analytic Ray-Box Intersection in Local Space
vec3 safe_rd = mix(sign(rd) * 1e-5, rd, greaterThanEqual(abs(rd), vec3(1e-5)));
vec3 inv_rd = 1.0 / safe_rd;
vec3 t0 = (box_min - ro) * inv_rd;
vec3 t1 = (box_max - ro) * inv_rd;
float t_enter = max(max(min(t0.x, t1.x), min(t0.y, t1.y)), min(t0.z, t1.z));
float t_exit = min(min(max(t0.x, t1.x), max(t0.y, t1.y)), max(t0.z, t1.z));
if (t_enter > t_exit || t_exit < 0.0) { discard; }
float t_start = max(t_enter, 0.0);
// 5. DDA Voxel Traversal Setup
vec3 res = vec3(textureSize(volume_tex, 0));
vec3 voxel_size = box_size / res;
vec3 p = ro + rd * t_start;
vec3 vox = clamp(floor((p - box_min) / voxel_size), vec3(0.0), res - 1.0);
vec3 step_dir = sign(safe_rd);
vec3 t_delta = abs(voxel_size * inv_rd);
vec3 next_bound = box_min + (vox + max(step_dir, 0.0)) * voxel_size;
vec3 t_side = max((next_bound - p) * inv_rd, vec3(0.0));
// 6. DDA Traversal Loop
for (int i = 0; i < max_steps; i++) {
vec3 uvw = (vox + 0.5) / res;
if (texture(volume_tex, uvw).r > threshold) {
ALBEDO = uvw;
ALPHA = 1.0;
break;
}
float t = min(t_side.x, min(t_side.y, t_side.z));
if (t > t_exit) { discard; }
bvec3 mask = lessThanEqual(t_side.xyz, min(t_side.yzx, t_side.zxy));
vox += vec3(mask) * step_dir;
t_side += vec3(mask) * t_delta;
if (any(lessThan(vox, vec3(0.0))) || any(greaterThanEqual(vox, res))) { discard; }
}
}

Comments 0
No comments yet.