Skip to content

Instantly share code, notes, and snippets.

@elementbound
Last active July 17, 2024 10:12
Show Gist options
  • Save elementbound/8436220b38bdd3ed4cf38906b778349f to your computer and use it in GitHub Desktop.
Save elementbound/8436220b38bdd3ed4cf38906b778349f to your computer and use it in GitHub Desktop.
Custom defines for Godot shaders
#define STAGE 0
/**
* Stages:
* - Slices, no extra shader
* - Calculate UVW + unshaded
* - Voronoi
* - Root texture
* - Height gradient
* - Additive
* - Height * Root
* - Height * Root * Voronoi
* - Color
*/
shader_type spatial;
#if STAGE >= 5 && STAGE != 8
render_mode unshaded, cull_disabled, depth_draw_never, blend_add;
#else
#if STAGE >= 1
render_mode unshaded;
#endif
#endif
uniform float FADE = 0.;
uniform sampler2D crackle_texture : hint_default_white;
uniform vec2 crackle_size = vec2(16.);
uniform float crackle_width : hint_range(0.0, 1.0, 0.05);
uniform sampler2D height_gradient: hint_default_white;
uniform sampler2D root_gradient : hint_default_white;
uniform sampler2D color_texture : hint_default_white;
uniform vec2 color_size = vec2(16.);
uniform float strength = 1.;
varying vec3 UVW;
varying vec3 WORLD_POS;
void vertex() {
float W = .5 + VERTEX.y; // Assuming a 1x1x1 box
UVW = vec3(UV, W);
WORLD_POS = (MODEL_MATRIX * vec4(VERTEX, 1.)).xyz;
}
void fragment() {
#if STAGE == 1
ALBEDO = UVW;
#endif
#if STAGE == 2
float crackle_f = texture(crackle_texture, WORLD_POS.xz / crackle_size).r;
crackle_f = smoothstep(crackle_f, 1., crackle_width);
ALBEDO = vec3(crackle_f);
#endif
#if STAGE == 3
float root_f = texture(root_gradient, UVW.xy).r;
ALBEDO = vec3(root_f);
#endif
#if STAGE == 4 || STAGE == 5
float height_f = texture(height_gradient, clamp(UVW.zz, 0., 0.99)).r;
ALBEDO = vec3(height_f);
#endif
#if STAGE == 6
float root_f = texture(root_gradient, UVW.xy).r;
float height_f = texture(height_gradient, clamp(UVW.zz, 0., 0.99)).r;
ALBEDO = vec3(mix(height_f, root_f * height_f, FADE));
#endif
#if STAGE == 7
float crackle_f = texture(crackle_texture, WORLD_POS.xz / crackle_size).r;
crackle_f = smoothstep(crackle_f, 1., crackle_width);
float root_f = texture(root_gradient, UVW.xy).r;
float height_f = texture(height_gradient, clamp(UVW.zz, 0., 0.99)).r;
ALBEDO = vec3(mix(root_f * height_f, crackle_f * root_f * height_f, 1. - FADE));
#endif
#if STAGE == 8
vec3 color = texture(color_texture, WORLD_POS.xz / color_size).rgb;
color = pow(color, vec3(2.2));
ALBEDO = color.rgb;
#endif
#if STAGE >= 9
float crackle_f = texture(crackle_texture, WORLD_POS.xz / crackle_size).r;
crackle_f = smoothstep(crackle_f, 1., crackle_width);
float root_f = texture(root_gradient, UVW.xy).r;
float height_f = texture(height_gradient, clamp(UVW.zz, 0., 0.99)).r;
float f = crackle_f * root_f * height_f * strength;
f *= ALPHA;
f = clamp(f, 0., 1.);
vec3 color = texture(color_texture, WORLD_POS.xz / color_size).rgb;
color = pow(color, vec3(2.2));
ALBEDO = mix(vec3(f), color * f, FADE);
ALPHA = 1.;
#endif
}
@tool
extends ShaderMaterial
class_name StagedShaderMaterial
@export var base_shader: Shader:
get: return base_shader
set(v):
if v != base_shader:
base_shader = v
_update()
@export var stage = 0:
get: return stage
set(v):
if v != stage:
stage = v
_update()
func _update():
var code = base_shader.code
if code.begins_with("#define STAGE"):
code = code.erase(0, code.find("\n") + 1)
code = "#define STAGE %d\n%s" % [stage, code]
shader = Shader.new()
shader.code = code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment