UV light (2D and 3D)
This is an example of a “UV light” effect, useful for secret objects and messages. It works both with 3D geometry (where the shape is defined by a mesh) and with flat objects (where the shape/pattern is defined by texture’s alpha).
Requirements:
- put all secret objects in a separate layer (property
layers
, in this example it’s “Layer 2”) - remove that layer from normal light sources (property
light_cull_mask
) - make sure that UV light sources affect that layer (property
light_cull_mask
)
Shader code
// 2D objects, e.g. text on a wall, where the shape is defined by a texture
shader_type spatial;
uniform sampler2D tex;
uniform vec4 color: source_color = vec4(0, 1, 1, 1);
uniform float energy: hint_range(0, 16) = 1;
void fragment() {
EMISSION = color.rgb * energy;
ALPHA = 0.0;
}
void light() {
vec4 pixel = textureLod(tex, UV, 1.0);
DIFFUSE_LIGHT = vec3(0.0);
SPECULAR_LIGHT = vec3(0.0);
ALPHA = ATTENUATION * pixel.a;
if (ATTENUATION == 1.0) {
ALPHA = 0.0;
}
}
// 3D objects, e.g. a ghost, where the shape is defined by geometry
shader_type spatial;
uniform vec4 color: source_color = vec4(0, 1, 1, 1);
uniform float energy: hint_range(0, 16) = 1;
void fragment() {
EMISSION = color.rgb * energy;
ALPHA = 0.0;
}
void light() {
DIFFUSE_LIGHT = vec3(0.0);
SPECULAR_LIGHT = vec3(0.0);
ALPHA = ATTENUATION;
if (ATTENUATION == 1.0) {
ALPHA = 0.0;
}
}
Love this shader! It has one issue I need to solve though… If you aim a non-UV light that doesn’t reveal the message, it stays dark and doesn’t react to the light because you cannot set different cull masks for different materials in one object.