LittleJS 1.29.0

Draw

Shader

Shader - A custom fragment shader for objects and draws, 2D or 3D

  • Write a mainImage function in the post processing style, the renderer wraps it with its own program
  • It gives the surface color, then the object's color and additive color apply in 2D, and the lighting, shadows and fog in 3D; set emissive to 1 on a 3D object for the snippet's color to be final
  • Set it as obj.shader, or use setShader for 2D draws and render3D.shader for 3D draws
  • Draws that share a Shader share a batch; with no Shader set nothing changes
  • In 2D it shades textured draws, untextured ones like drawRect draw as they are
  • A render target, like a tile layer drawn in WebGL, holds premultiplied color, and so does every image when tilesPixelated is false; there iChannel0 reads premultiplied texels and the snippet's color is taken as premultiplied too. premultipliedTexture is true there, so a snippet that changes the alpha, or makes a see through color of its own, scales the rgb with it: if (premultipliedTexture) c.rgb *= k;
  • Compiled once per renderer by the first draw that needs it; a bad snippet throws with the GLSL log in debug
  • Make each Shader once, at init, and share it; every one made lives for the session with its programs
  • Names in both renderers: iChannel0 the texture, iTime, iResolution, premultipliedTexture, and localUV, 0 to 1 across the sprite or the mesh's own uv
  • Names in 3D only: worldPos, worldNormal, cameraPos, sunDirection, sunColor, ambientColor, ambientGroundColor, lightCount, lights[i], lightColors[i] and shadow(), and viewProjection, the camera's matrix from the world to the screen, for a snippet that sets its own depth: vec4 c = viewProjection * vec4(p, 1.); gl_FragDepth = c.z / c.w * .5 + .5 puts it at world point p; only on the screen, the shadow map and sceneDepth draw the mesh where it is
  • In 3D the snippet may also define void mainNormal(inout vec3 n), given the normal facing the camera after the normal map, in world space, to bend it per pixel for waves or ripples; the lighting, specular, reflection and fog then all use it, as they use a normal map
  • In 3D the shadow map is drawn without the Shader, cut only by the texture's alpha, so a snippet that removes parts of a surface still shadows with the whole of it

Constructor

new Shader(fragmentCode)engineDraw.js:631

Create a shader from a fragment snippet that defines void mainImage(out vec4 c, vec2 uv)

fragmentCode string
const fade = new Shader(`
void mainImage(out vec4 c, vec2 uv)
{
    c = texture(iChannel0, uv);
    float fade = .5 + .5*sin(iTime);
    c.a *= fade;
    if (premultipliedTexture) c.rgb *= fade; // a tile layer, or any image with tilesPixelated false
}`);
obj.shader = fade;

Members

Methods

Members

fragmentCode : stringengineDraw.js:639

The mainImage snippet

noTint : booleanengineDraw.js:649

In 2D, write what mainImage gives as it is, with the draw's alpha, leaving out the draw's color and additive color, for data rather than a picture, as the light system's normal pass does; set it before the shader first draws, its program is built then

program : WebGLProgram|undefinedengineDraw.js:642

The 2D program, compiled by the first draw that needs it, read only

program3D : WebGLProgram|undefinedengineDraw.js:645

The 3D program, compiled by the 3D plugin the same way, read only

Methods

Let go of this shader: its compiled programs are freed and it leaves the engine's list of shaders, which keeps every one made for a lost context, so a scene made again and again, or an editor trying snippets, does not keep them all; calling it again does nothing, and a draw with it afterwards compiles it again and takes it back, so only let go of one no object still draws with when it should stay freed