Per-pixel spotlight shader

I found a per-pixel spotlight shader here: and put it in the BGE. To my surprise it actually works, although there are serious issues. The shader only works when 3d view is maximized :confused: and spotlight’s beam gets strange distortions when it hits polygon edges. I thought per-pixel lightning is fully independent from vertex/edge positions. Perhaps some shader guru here could make it work better… my experience with shaders is quite limited. I didn’t modify the actual shader code, just copypasted mainloop from Blender graphics demos.

With this shader things like flashlights, headlights etc. would be easy to do. BGE has always supported vertex lightning but it is dependent from vertex positions. So it looks awful if the faces of the lighted mesh aren’t evenly distributed or if the mesh is very low poly. Here’s a comparison of the spotlights lightning a subdivided plane. That cone in the center marks the position of the lamp. Left is vertex spotlight and right is per-pixel spotlight:


hmmmm coo I’ll check it out

Here’s the FPS template with flashlight using the shader:
Too bad all objects lose their textures when the shader is assigned to them. I guess texture handling must be added to the shader. Unfortunately I don’t know how.

Screenshots of the FPS template with flashlight:


wow that’s cool…

looks cool, too band i cant use it

according to the console
“Fragment shaders not supported”


verry nice, thanks for this :slight_smile:

I get that same message. It’s too bad. Would’ve been very useful in my project.

At least shader version 2.0 capable graphics card is required (GeForce FX and newer, Radeon 9500/9700 and newer). I don’t think that shader is very useful unless textures can be used with it. Untextured game might not look very professional…

it’s just a pixel shaded light instead of a vertex shaded light… so it should work with textures… but pixel shaders are slow

I gave it a try with and without the spot lamp, and unfortunately it looked better without. Textures are very important in making a game look nice.

Of course it COULD work with textures if someone (I don’t have the skills, yet) modified the shader code. Such lights are used in many commercial games like Doom 3, Far Cry etc. and they certainly have textures too :eyebrowlift2:

Just because commercial games do it doesn’t mean blender is capable of it.
There is also some major distortion when the faces are more parallel with the spotlight, versus the more accurate lighting when the faces are perpendicular to the spotlight.

I don’t see a good reason why it wouldn’t be possible when basic per pixel lamps can be used with textures (see ‘sebubla’ blend in graphics demos). I am more worried about the distortion because the shader worked perfectly in other engine. Oh well, another reason to hope for Ogre integration…

go look at the pipeline overview of GLSL… here’s a link to another site that might have some useful code snippets and demos…

have you seen this site about how to use the GLSL inside of blender

half of the problem is the room/level uses Texface with different maps on different polys… looks like there is a problem with using texface and GLSL… the crates use blender materials without tex face and they appear in GE with textures and the per pixel light works fine on those…

—the light affects the specular settings of the objects… so turn up the specular and set it to blinn and you will see the affect of the light… like you would do for a rendering… also, flat objects like crates generally arn’t that reflective… so put in a normal map and get specular on the bump edges… or put in a sphere…

also crank up the light intensity… it might actually be doing vertex shading on the boxes… can’t really tell… but you can make lights that attenuate… there are some examples of that on the ozone3d site

here’s a pic with the changed specular settings the real question would be how do you add in more lights? I tried doing that and it didn’t work out so well

aw man, thats brilliant! Just like in Halo or something!

Blender is so hardcore…

Unfortunately it’s just vertex light on the crates. All objects which are affected by the shader must be listed in the shader script. Look for line called ‘ShaderObjects’.

I too couldn’t succeed adding more lights. I tried to give each shader unique gl_LightSource[], but only the last added lamp worked. I guess I should start studying those GLSL docs…

if this works, I may never need viagra again!

Unfortunately it’s just vertex light on the crates. All objects which are affected by the shader must be listed in the shader script. Look for line called ‘ShaderObjects’.

hmmm then we should somehow be able to load all the objects… anyone who knows python better know how to do this… it is possible… it should make the pixel shader work on all objects…

You can easily add more objects after the first one by adding more objlist[“insert object name here”] entries. So the line should look like this:

ShaderObjects = [ objlist['OBMesh1'],  objlist['OBMesh2'], objlist['OBMesh3'] ]