# Making a "flashlight"

**URL:** <https://forum.defold.com/t/making-a-flashlight/286>\
**Category:** Questions\
**Created:** [October 3, 2015, 10:18am UTC](https://forum.defold.com/t/making-a-flashlight/286 "2015-10-03T10:18:58Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![sven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sven/32/1086_2.png) [@sven](https://forum.defold.com/u/sven)\
**Post date:** [October 3, 2015, 11:34am UTC](https://forum.defold.com/t/making-a-flashlight/286/3 "2015-10-03T11:34:06Z")

</div>

Hey Linus!

As Mikael mentioned, you most likely need to do some changes to your rendering script.

I think there are a couple of ways to do this, one way is to utilise something called “stencil masks”. I did a quick test and created a custom rendering script that does similar to what you are asking.

 ![](https://forum-defold-uploads.b-cdn.net/original/1X/f6e4224174c7b141e5be2f42b2db5e45bb7f36f7.png)

 ![](https://forum-defold-uploads.b-cdn.net/original/1X/f0e85e1c26a240f3050f3e6a0f3fe99a5bfe2bae.png)

So essentially I do a game object with a custom material, a material with a special tag I called “mask”. Then I create my own render script (I copied the one in builtins/render/default.render\_script) that renders this “mask” tag into a stencil mask before I render the rest of the game.

I added this to my `init(self)` function:

```
self.mask_pred = render.predicate({"mask"})

```

And these changed the `update(self, dt)` function into this:

```
function update(self)
    render.set_depth_mask(true)
    render.clear({[render.BUFFER_COLOR_BIT] = self.clear_color, [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})

    render.set_viewport(0, 0, render.get_window_width(), render.get_window_height())
    render.set_view(self.view)

    render.set_depth_mask(false)
    render.disable_state(render.STATE_DEPTH_TEST)
    render.set_projection(vmath.matrix4_orthographic(0, render.get_width(), 0, render.get_height(), -1, 1))
    
    -- 1. We need to disable color drawing while rendering the mask
    render.set_color_mask(false, false, false, false)
    
    -- 2. Enable stencil test and setup stencil mask parameters
    render.enable_state(render.STATE_STENCIL_TEST)
    render.set_stencil_func(render.COMPARE_FUNC_ALWAYS, 1, 255)
    render.set_stencil_op(render.STENCIL_OP_KEEP, render.STENCIL_OP_KEEP, render.STENCIL_OP_REPLACE)
    render.set_stencil_mask(255)
    
    -- 3. Draw the mask
    render.draw(self.mask_pred)
	
    -- 4. Update the stencil function to only let pixel pass that are equal to the mask result
    render.set_stencil_func(render.COMPARE_FUNC_EQUAL, 1, 255)
	
    -- 5. Re-enable color drawing
    render.set_color_mask(true, true, true, true)
	
    -- 6. Continue as rendering usual! :)
    render.enable_state(render.STATE_BLEND)
    render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
    render.disable_state(render.STATE_CULL_FACE)

    render.draw(self.tile_pred)
    render.draw(self.particle_pred)
    render.draw_debug3d()

    render.set_view(vmath.matrix4())
    render.set_projection(vmath.matrix4_orthographic(0, render.get_window_width(), 0, render.get_window_height(), -1, 1))

    render.enable_state(render.STATE_STENCIL_TEST)
    render.draw(self.gui_pred)
    render.draw(self.text_pred)
    render.disable_state(render.STATE_STENCIL_TEST)

    render.set_depth_mask(false)
    render.draw_debug2d()
end

```

I hope you can use this as some kind of base or just as inspiration! 🙂

---

_[View the full topic](https://forum.defold.com/t/making-a-flashlight/286)._
