2
$\begingroup$

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()

VertexShader = """

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;
}
"""

FragmentShader = """

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:

shader = mat.getShader()

if shader != None:

    if not shader.isValid():

        shader.setSource(VertexShader, FragmentShader, 1)

        mat.setBlending(bge.logic.BL_SRC_ALPHA, bge.logic.BL_ONE_MINUS_SRC_ALPHA);
$\endgroup$
  • $\begingroup$ i have tried that (I dont know GLSL ) but what it does is make the object green with a white reflection even if i rotate the object is this the desired result ? $\endgroup$ – Chebhou Mar 29 '15 at 19:08
  • $\begingroup$ The result should be images.buystarwarstoys.com/products/9288/1-1/… $\endgroup$ – Daniele Gamba Mar 30 '15 at 19:00
  • $\begingroup$ sorry but is does not fade at the ends, also the white line is moving according to the view which shouldn't happen $\endgroup$ – Chebhou Mar 30 '15 at 19:04
  • $\begingroup$ can you get the local coordinates of the shading points ? $\endgroup$ – Chebhou Mar 30 '15 at 19:07
  • $\begingroup$ the local coordinates of each point are given by gl_Vertex, for each vertex $\endgroup$ – Daniele Gamba Mar 30 '15 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.