Raymarch – Volume Entry coordinate
This display the volume entry postion coordonate, like if the mesh has a front face. This is made to confirm the entry point into the volume and ray direction are correct.
- Shoot a ray from camera to vertex.
- Get the world position of the entry point into the volume
- Draw the coordinate has uvw.
Setup:
Meshinstance – BoxMesh – Shadermaterial
Set uniform vec3 box_size = vec3(1.0); Should match the mesh size
The mesh can be moved but not rotated.
Warning, use :
vec3 cam_world = (inverse(VIEW_MATRIX) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
DO NOT USE CAMERA_POSITION_WORLD, it doesnt work
Shader code
shader_type spatial;
render_mode unshaded, cull_front;
uniform vec3 box_size = vec3(1.0);
varying vec3 v_ray_dir;
varying vec3 cam_world;
void vertex() {
vec3 world_vert = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
cam_world = (inverse(VIEW_MATRIX) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
v_ray_dir = normalize(world_vert - cam_world);
}
float box_intersect(vec3 ro, vec3 rd, vec3 box_min, vec3 box_max) {
vec3 tmin = (box_min - ro) / rd;
vec3 tmax = (box_max - ro) / rd;
vec3 t1 = min(tmin, tmax);
vec3 t2 = max(tmin, tmax);
float t_near = max(max(t1.x, t1.y), t1.z);
float t_far = min(min(t2.x, t2.y), t2.z);
if (t_near > t_far || t_far < 0.0) return -1.0;
return max(t_near, 0.0);
}
void fragment() {
vec3 center_world = (MODEL_MATRIX * vec4(vec3(0.0), 1.0)).xyz;
vec3 box_min_world = center_world - box_size * 0.5;
vec3 box_max_world = center_world + box_size * 0.5;
vec3 ro = cam_world;
vec3 rd = v_ray_dir;
float t_entry = box_intersect(ro, rd, box_min_world, box_max_world);
if (t_entry >= 0.0) {
vec3 entry_point = ro + rd * t_entry;
vec3 uvw = (entry_point - box_min_world) / box_size;
ALBEDO = uvw;
} else {
discard;
}
}

Comments 0
No comments yet.