Using stencil testing for outline shaders

Outline effects are sometimes made using stencil testing. I wanted to learn how to do this in Defold using this method. In short, you draw the object with stencil testing enabled once, then scale the object up slightly before drawing it again, which creates a “cut out” effect on the second pass leaving just the outline.

But I can’t find a way to scale the object up before drawing it a second time. Stencil testing is enabled in a render script, but I’d have to scale up the object in a regular script or a shader, before returning to the render script and drawing it again.

Is there a way to do this? Maybe with messages?

I suppose that you want to make outlines for 3d models.

Add the outline tag to your models material. You’ll use it for a render predicate to be able to render models which should be outlined.

Make a copy of the material plus its vertex/fragment shaders and name it as outline.material (the name and tags of that material is not important). Put the material into your custom .render file with the my_outline_material id.

Then, in the init function of your render script, add a custom predicate like self.outline_pred = render.predicate({"outline"}). To render outlines of the models, enable the custom material, draw models, disable the material:


To scale the object up, write a custom vertex shader in the outline material.

attribute highp vec3 position;
attribute mediump vec3 normal;

uniform highp mat4 mtx_world;
uniform highp mat4 mtx_worldviewproj;

void main()
    float outline_width = 0.05;

    // We should find the scale of the model to scale the outline width accordingly
    float scale_x = length(mtx_world * vec4(1.0, 0.0, 0.0, 0.0));
    float scale_y = length(mtx_world * vec4(0.0, 1.0, 0.0, 0.0));
    float scale_z = length(mtx_world * vec4(0.0, 0.0, 1.0, 0.0));
    vec3 scaled_outline_width = outline_width / vec3(scale_x, scale_y, scale_z);

    // Scale vertices up, and there are two ways:
    // - by positions, but it works well only for cubes
    vec3 outline_offset = * scaled_outline_width;
    // - by normals, but they should be smoothed
    // vec3 outline_offset = * scaled_outline_width;

    // Homework: use camera distance to keep outlines consistent!

    vec3 new_position = position + outline_offset;
    gl_Position = mtx_worldviewproj * vec4(, 1.0);

Works great! Here’s the result (homework not done):


There’s a bit of an issue when I start overlapping models, however. The models appear to “share” the outline.

I render the outlines first with depth testing disabled, and then draw the models on top.



To fix this I think I need a way to detect the edges of a mesh, unless there’s something in @aglitchman’s post that I didn’t understand?


This is how the stencil-based object outlining algorithm works - look at the end of the mentioned article.

Try another method to make outlines - the inverted hull method. Remove all your custom stencil stuff from the render script, and render the models twice, with the second version of it flipped inside out and a little bit bigger (you do that in the custom outline material).

render.enable_state(render.STATE_DEPTH_TEST) -- this should be ON.



render.set_cull_face(render.FACE_BACK) -- revert back the default mode, i.e. FACE_BACK

That’s all!


Here’s the result of the new code, very cool!