# GLSL Lightsaber (per vertex lighting)

Here's my script, which I activate in the game engine with a python controller, applied to a cylinder.

I managed to achieve a pretty good result by following the tutorial on http://en.wikibooks.org/wiki/GLSL_Programming/Blender/Silhouette_Enhancement at least when the tube is vertical. when I rotate it by ninety degrees, strange thing happen. someone wanna give it a look?

To me, it looked like the best way to draw a lightsaber without billboards, though perhaps there are smartest ones.

import bge

cont = bge.logic.getCurrentController()

varying vec3 varyingNormalDirection;
varying vec3 varyingViewDirection;

void main()
{
varyingNormalDirection = normalize(gl_NormalMatrix * gl_Normal);
varyingViewDirection = normalize(gl_ModelViewMatrix * gl_Vertex);

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""

varying vec3 varyingNormalDirection;
varying vec3 varyingViewDirection;

void main()
{
// I project the two vectors on the plane y = 0
vec2 normalDirection = normalize(varyingNormalDirection.xz);
vec2 viewDirection = normalize(varyingViewDirection.xz);

const vec4 color = vec4(1.0,1.0,1.0,0.3);

//      opacity equals infinity when view and normal have an angle of 0,
//      equals 0.001 when they have an angle of 90
//      I also multiply for the dot product between view and normal
//      which is 1 at center, 0 at edge, to increase the falloff
float dotProduct = abs(dot(viewDirection, normalDirection));
float opacity = min(1.0, dotProduct * 0.01 / (1.0 - dotProduct) );

//      with opacity I'm also varying the color, from green to white
gl_FragColor = vec4(1.*opacity,1.0,1.*opacity,opacity*2.);
}
"""

mesh = cont.owner.meshes[0]

for mat in mesh.materials: