Per-pixel spotlight shader

I found a per-pixel spotlight shader here: http://www.lighthouse3d.com/opengl/glsl/index.php?spotlight 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:

Attachments



hmmmm coo Iā€™ll check it out

Hereā€™s the FPS template with flashlight using the shader: http://users.tkk.fi/~pkangass/blender/FPSTemplateFlashLight.blend
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:

Attachments



wow thatā€™s coolā€¦

looks cool, too band i cant use it

according to the console
ā€œFragment shaders not supportedā€

:frowning:

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ā€¦ http://www.ozone3d.net/tutorials/index.php#concepts_3d

have you seen this site about how to use the GLSL inside of blender
http://snailrose_dir.home.bresnan.net/material.html

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

http://img505.imageshack.us/img505/7577/perpixellightingld7.jpg

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'] ]