Examples
Hologram chams
Enemies drawn as scanning holograms, and your held weapon in a pulsing rim light. Both are materials with their own HLSL pixel shader.
Needs: Allow custom shaders, and Allow game scripting for the player list. Custom shaders explains materials in full.
Lua
-- @name Hologram chams
-- @author evasive
-- @version 1.1
-- @description Enemies as scanning holograms, the held weapon in a pulsing rim light: two HLSL materials of its own (needs "Allow custom shaders").
if not scene then error("enable 'Allow custom shaders' in Scripts > Security", 0) end
local speed = ui.slider("Scan speed", 0.1, 3, 0.6, {decimals = 1})
local weapon_glow = ui.checkbox("Rim light on the held weapon", true)
local holo, why = scene.material{ color = "#4fd8ff", params = { speed = 0.6, lineSpacing = 6, rimColor = "#bff4ff" }, hlsl = [[
float4 Surface(SurfaceInput s, MaterialParams p) {
const float band = frac(s.position.z / max(p.lineSpacing, 0.5f) - s.time * p.speed);
const float scan = smoothstep(0.0f, 0.08f, band) * (1.0f - smoothstep(0.35f, 0.5f, band));
const float rim = pow(1.0f - s.facing, 2.5f);
const float3 col = s.color.rgb * (0.3f + scan) + p.rimColor.rgb * rim * 1.5f;
return float4(col, s.color.a * saturate(0.12f + scan * 0.55f + rim)); }]] }
-- A soft body with a bright edge that breathes once a second.
local rim, why2 = scene.material{ color = "#7cff9a", layer = "visible", params = { rate = 1 }, hlsl = [[
float4 Surface(SurfaceInput s, MaterialParams p) {
const float pulse = 0.6f + 0.4f * sin(s.time * 6.2832f * p.rate);
const float edge = pow(1.0f - s.facing, 3.0f);
return float4(s.color.rgb * (0.4f + edge * 2.0f * pulse), s.color.a * saturate(0.15f + edge * pulse)); }]] }
if not holo then print("hologram: " .. tostring(why)) end
if not rim then print("rim: " .. tostring(why2)) end
events.on("tick", function()
if holo then
holo:set("speed", speed:get())
-- a player assignment lasts 1 s: renewing it each tick keeps it while the player is listed
for _, p in ipairs(entities.players("enemies")) do
p:set_material(holo)
end
end
scene.viewmodel(weapon_glow:get() and rim or nil, "weapon")
end)
events.on("unload", function()
if holo then
local ms = holo:gpu_ms()
print(string.format("hologram GPU %s, error %s", ms and string.format("%.3f ms", ms) or "-", tostring(holo:error())))
end
end)How it works
- The first line stops the script with a clear message when the shader switch is off, because
sceneis nil then. scene.material{...}takes the shader source, a base colour and named params. The shader compiles in the background. Until it is ready the material draws flat in its colour.- The hologram shader makes bands that move up the body over time (
s.position.zands.time) and adds a bright edge where the surface turns away from the camera (s.facing). holo:set("speed", ...)passes the slider's value to the shader'sspeedparam every tick.p:set_material(holo)lasts one second. Renewing it every tick keeps it on while the player is listed, and lets it drop by itself when they are not.scene.viewmodel(rim, "weapon")draws your first person weapon with the rim material. Passing nil gives it back to your chams settings.- On unload the script prints how much GPU time the hologram took per frame, and any error it had.
Try this
- Draw teammates in a calmer material: make a second one with
layer = "occluded"so it only shows through walls. - Use
scene.item(index, material)onentities.items()to light up weapons on the ground.