Additive Linear Volume

Simple method for increasing overdraw volumetric lighting effects, inspired by the logic behind stencil shadows:

Back face distance – front face distance = total distance through mesh

This works with any manifold mesh, whether the camera is inside or outside.

The shader as given is mainly useful for retro/NPR visuals. For more advanced use cases you can take the same method and generate a separate accumulation buffer, then use that as input to a physically based model.

Shader code
shader_type spatial;
render_mode unshaded, blend_add, cull_disabled, depth_test_disabled;

uniform sampler2D depth_texture : hint_depth_texture;
uniform vec3 color : source_color = vec3(1.0, 0.0, 0.0);
uniform float density = 1.0;

void fragment() {
	// Distance from depth buffer
	vec3 scene_clip_pos = vec3(SCREEN_UV * 2.0 - 1.0, texture(depth_texture, SCREEN_UV).r);
	vec4 scene_view_pos = INV_PROJECTION_MATRIX * vec4(scene_clip_pos, 1.0);
	scene_view_pos.xyz /= scene_view_pos.w;
	float scene_dist = length(scene_view_pos.xyz);
	
	// Distance to mesh
	float dist = length(VERTEX);
	
	// Add distance for back faces and subtract for front, checking against the depth buffer in each case
	if (!FRONT_FACING) {
		dist = min(dist, scene_dist);
	}
	else {
		dist = -dist;
		dist = max(dist, -scene_dist);
	}
    ALBEDO = dist * color * density;
}
Live Preview
Tags
accumulation, hologram, laser, light shafts, Volume, volumetric
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 tentabrobpy

Related shaders

guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
lamb
17 days ago

this looks SICK