Bubble/Glass spatial shader with chromatic aberration

A fairly simple and lightweight bubble shader with chromatic aberration, background warping, homemade rim lighting which only really works on spheres (set `rim_lighting` to `0.0` to disable) and slight contrast adjustment of the background. It is compatible with Godot Engine 3 and 4 (in fact I initially wrote it in Godot 3). Definitely works on GLES 2, should work on different renderers (untested). Documentation of uniforms is in the shader.

Shader code
shader_type spatial;
render_mode unshaded;

/* Bubble or Glass shader, best applied to spheres.
 * It is not affected by lighting in the scene, only whatever's behind it.
 * 
 * Set rim_lightness to 0.0 to be more glass and less bubble.
 * Set it to 0.6 for full Good Witch Of The North vibes.
 * 
 * Setting chromatic_aberration to 0.0 removes the effect (no performance difference).
 * It gets ridiculous if you set it past around 0.25.
 * 
 * Warping affects the degree to which the backdrop is warped (not technically accurate).
 * 
 * Greyness is effectively a contrast control for whatever's behind it.
 * 0.0 is full contrast, 1.0 is pure 0.5 grey.
 *
 * Should be compatitable with Godot Engine 3 and 4.
 */

// comment out for godot >=4.0
#define OLD_GODOT

uniform float chromatic_aberration = 0.01;
uniform float warping = 0.1;
uniform float greyness = 0.2;
uniform float rim_lightness = 0.15;

#ifndef OLD_GODOT
uniform sampler2D SCREEN_TEXTURE: hint_screen_texture;
#endif

void vertex() {
	//
}
void fragment() {
	ALBEDO.r = texture(SCREEN_TEXTURE,  SCREEN_UV - (NORMAL.xz * warping + (0.5 * chromatic_aberration))).r;
	ALBEDO.g = texture(SCREEN_TEXTURE,  SCREEN_UV - (NORMAL.xz * warping)).g;
	ALBEDO.b = texture(SCREEN_TEXTURE,  SCREEN_UV - (NORMAL.xz * warping - (0.2 * chromatic_aberration))).b;
	ALBEDO = mix(ALBEDO, vec3(0.5), greyness);
	ALBEDO += (1.0 - dot(NORMAL, VIEW)) * rim_lightness;
}
Tags
Bubbles, chromatic aberration, glass
The shader code and all code snippets in this post are under GNU GPL v.3 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.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments