Shaped Glow Post-Processing Effect

This is a post-processing effect based on my previous Sci-Fi pulse depth buffer shader that is inteded to show an example of how to use Signed Distance Functions to create a different shaped glow that follows the Transform of a Spatial node in the scene.

Credit for the Signed Distance Function goes to Inigo Quilez, and other shapes can be found on his website

Like the Sci-Fi Scanner Pulse, there is a bit of setup required to get this post-processing effect to work.

  1. A quad mesh (size (2,2) is required to be visible to the camera (you might have to disable backface culling). I would recommend making it a child of the camera to keep track of it.
  2. Give the quad mesh a new shader material and give it this shader.
  3. Give the quad mesh the following GDScript to update all of the Shader Parameters and to track a Spatial node’s transform.
extends MeshInstance

# Get the reference to the material to pass data to shader parameters.
onready var SHADER:ShaderMaterial = self.get_active_material(0)

# Don't forget to assign the start point node to this variable.
export(NodePath) onready var origin_point = get_node(origin_point) as Spatial


func _ready():
	# Set the start point of the effect in the shader to the world position of
	# of the origin_point.
	SHADER.set_shader_param("shape_transform", origin_point.get_global_transform())


func _process(delta):
	SHADER.set_shader_param("shape_transform", origin_point.get_global_transform())
	
	# If you want to use the size parameter (maybe a sine wave pulse using time?):
	# SHADER.set_shader_param("size", variable)
Shader code
/* 
Basic shape glow post-processing effect.
Required to be put on a quad mesh that is rendered in the scene.
Required associated script to control the transform of the shape.
Parts of the code are created by Inigo Quilez and used under the MIT license:
https://iquilezles.org/articles/

Copyright 2022 Michael Watt and Inigo Quilez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to 
do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Video tutorial on YouTube: https://youtu.be/0BXBXy8zaaI
*/

shader_type spatial;
render_mode unshaded;

// Settings to play with
uniform float pulse_width = 2.0;
uniform vec4 color :hint_color;
uniform vec3 box_dimensions = vec3(1.0);
uniform float roundness = 0.5;

// Updated by Script
uniform mat4 shape_transform = mat4(1.0);
uniform float size = 1.0;

// Not sure this is actually needed, but used in Documentation.
varying mat4 CAMERA;

// Signed Distance Function by Inigo Quilez
float sdRoundBox( vec3 p, vec3 b, float r )
{
  vec3 q = abs(p) - b;
  return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r;
}

void vertex() {
	// Set the Quad to cover the entire screen.
	// If it doesn't, resize the quad (x,y) to (2,2)
	POSITION = vec4(VERTEX, 1.0);
	
	// Again, from the docs, we need the interpolated camera matrix
	// Which I think you can just get in the fragment shader?
	CAMERA = CAMERA_MATRIX;
}


void fragment() {
	// Get the original screen rendered texture at the screen uv coordinates.
	vec4 original = texture(SCREEN_TEXTURE, SCREEN_UV);
	
	// Get the depth value form the depth buffer. Stored in the x value
	// of the depth texture.
	float depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;
	
	// Normalized Device Coordinates (clip space... mostly)
	vec3 ndc = vec3(SCREEN_UV, depth) * 2.0 - 1.0;
	
	// Convert to world space from NDC
	vec4 world = CAMERA * INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
	vec3 world_position = world.xyz / world.w;
	
	// Transform the world_position of the fragment based on the shape's transform.
	// In the case of SDFs, the inverse of the shape's transform is required.
	vec3 adjusted_position = (inverse(shape_transform) * vec4(world_position, 1.0)).xyz;
	
	// Use the Signed Distance Function to calculate how far away the position is from the
	// desired shape.
	float dist = sdRoundBox(adjusted_position, box_dimensions * size, roundness * size);

	float mix_ratio = 0.0;
	
	if (dist <= 0.0 && dist > -pulse_width){
		float percentage = abs(dist) / abs(pulse_width);
		mix_ratio = 1.0 - percentage;
		
		// Clamp the mix ratio to avoid any unexpected visual bugs.
		mix_ratio = clamp(mix_ratio, 0.0, 1.0);
	}
	
	ALBEDO = mix(original.rgb, color.rgb, mix_ratio);
}
Tags
SDF, Signed Distance Field, Signed Distance Function
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Watt Interactive

X-ray Vision Effect

Sci-Fi Scanner Pulse

Related shaders

PS1 Post-processing

Post Effect Outline Shader for GLES2

Post Effect Outline Shader

guest

0 Comments
Inline Feedbacks
View all comments