Lit mesh

glint · Examples · Tutorials · Gallery

A torus with a vertex-displaced surface and simple lighting. Try the other shapes in Pass settings.

Lit mesh: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

vertex shader

#version 300 es
in vec4 a_position;
in vec3 a_normal;
in vec2 a_texcoord;

uniform mat4 mvp;
uniform mat4 modelMatrix;
uniform mat3 normalMatrix;
uniform float time;
uniform float wobble;

out vec3 v_normal;
out vec3 v_world;
out vec2 v_texcoord;

void main() {
    vec3 p = a_position.xyz + a_normal * wobble * sin(a_texcoord.x * 40.0 + time * 3.0) * sin(a_texcoord.y * 12.0);
    v_normal = normalMatrix * a_normal;
    v_world = (modelMatrix * vec4(p, 1.0)).xyz;
    v_texcoord = a_texcoord;
    gl_Position = mvp * vec4(p, 1.0);
}

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec3 baseColor;

in vec3 v_normal;
in vec3 v_world;
in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec3 n = normalize(v_normal);
    vec3 l = normalize(vec3(0.5, 0.8, 0.6));
    vec3 v = normalize(vec3(0.0, 0.0, 2.2) - v_world);
    float dif = max(dot(n, l), 0.0);
    float spec = pow(max(dot(reflect(-l, n), v), 0.0), 48.0);
    float rim = pow(1.0 - max(dot(n, v), 0.0), 3.0);
    float stripes = 0.85 + 0.15 * step(0.5, fract(v_texcoord.x * 24.0));
    vec3 col = baseColor * stripes * (0.15 + dif) + spec + rim * vec3(0.3, 0.5, 1.0);
    fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}

More Starters examples

Raymarcher · Feedback trails · Audio spectrum · Vertex art · Webcam kaleidoscope · Shadertoy style · Plasma · Tunnel