How can I draw an outline around objects?

Is it possible to draw an outline around an object when the mouse is over it like in the blender viewport when you’ve selected an object? I want to highlight objects that the player can interact with by that, so only objects with a certain property should be highlighted.

Is there any script for this?


Easiest way would be to duplicate object and flip its normals, give new material to this duplicate and set it to shadeless. Other that that would be through custom shader i guess.

So I would have to duplicate every object? Or is there a way to duplicate and flip normals via python, so that I can delete the duplicate if the mouse isn’t over anymore?

You could use this script on object in question:

import bge


cont = bge.logic.getCurrentController()
own = cont.owner
sce = bge.logic.getCurrentScene()

mouse_sensor = cont.sensors['mouse_over']

hit_ob = mouse_sensor.hitObject

if hit_ob and hit_ob == own and own['spawned_ob'] == False:
    new = sce.addObject('objects_name',own)
    own['spawned_ob'] = new
elif not hit_ob and own['spawned_ob'] != False or \
     hit_ob and != 'objects_name' and != 'Suzanne' and own['spawned_ob'] != False:
    own['spawned_ob'] = False    

Though a better way would be to run script on single object and deal with all objects than running separate scripts on each object

blend: example.blend (596 KB)

No, you duplicate meshes. No, You duplicate meshes that should get an outline. (Alternative you can search for toon shader, or outline).

Yes, just write one.
Btw. scaling alone is fine at a sphere or a cube. You do not need scale, you need “fatten” = thicker objects. There might be a tool for that.

Yes, when it is a separate object.
(if you use a shader, deactivate/remove the shader)

I hope it helps

I guess you could flip normals of every vertex via python, but i haven’t tried that, and that would processor intensive.

At least with method that i suggested, yes you would need to duplicate every object that can have highlight.

Ok, thanks. If I duplicate every object and put it on a seperate layer, they wouldn’t be calculated all the time in my scene, right?

that will happen when placed on an active layer, take care to disable the layer where the “outlines” are.

Alternatively you can add (parent) the outline mesh to your player/enemy/object or whatever it is and set it to invisible. Then make it visible when mouseover. When it’s invisible it won’t affect your performance at all.

A benefit of this is that you can make the outline mesh animated along the regular mesh via an armature.

can you use a filter but only on certain items?

At the moment, only by using separate scenes, and duplicating the state of the main object to the state of a proxy object. This would not be performance friendly.