Shader

core.shader.create_from_raw

Client

Creates a new shader instance directly from a GLSL source string

Syntax

local instance = core.shader.create_from_raw(
    raw,
    mode
)

Parameters

TypeNameDescription
stringrawRaw GLSL shader source code
core.shader.shader_modemodeRendering pipeline to target:
• CANVAS_ITEM - 2D canvas shader
• SPATIAL - 3D material shader
• POST_PROCESS - full-screen effect over the rendered scene

Returns

TypeNameDescription
shaderinstanceCreated shader instance

Examples

Compile a canvas-item shader from an inline source string
local raw = [[
    void fragment() {
        COLOR = vec4(UV, 0.0, 1.0);
    }
]]

local entity = core.shader.create_from_raw(raw, core.shader.shader_mode.CANVAS_ITEM)

util.event.on("sandbox:draw", function()
    core.engine.draw_material(
        {0, 0}, 
        core.display.get_resolution(), 
        entity
    )
end)
Compile a spatial shader from a raw source string
local raw = [[
    uniform float dissolve_amount : hint_range(0.0, 1.0) = 0.0;

    void fragment() {
        ALBEDO = vec3(dissolve_amount);
    }
]]

local entity = core.shader.create_from_raw(raw, core.shader.shader_mode.SPATIAL)

entity:set_param("dissolve_amount", 0.5)
Compile a post-process shader from a raw source string
local raw = [[
    uniform sampler2D screen_texture : hint_screen_texture, filter_linear_mipmap;
    uniform float desaturation : hint_range(0.0, 1.0) = 0.5;

    void vertex() {
        POSITION = vec4(VERTEX.xy, 1.0, 1.0);
    }

    void fragment() {
        vec3 color = texture(screen_texture, SCREEN_UV).rgb;
        float grey = dot(color, vec3(0.299, 0.587, 0.114));
        ALBEDO = mix(color, vec3(grey), desaturation);
    }
]]

local entity = core.shader.create_from_raw(raw, core.shader.shader_mode.POST_PROCESS)

util.event.on("sandbox:draw", function()
    core.engine.draw_material({0, 0}, {0, 0}, entity)
end)

On this page