Rim Shader V1

Recently I played a lot with the Flash 3D framework Flare3D, an awesome piece of work. Special with Flare3D is that they created an own shading language called FLSL, which has similarities with Adobes PixelBender pixelshader language. Thus making it easy to get into the language, if you are familiar with Pixelbender. I played with FLSL a lot and I will post here some of my FLSL shaders I wrote. First of all a simple fake rim shader.

Rim shading is what occurs, if an object is lighted from behind, creating a shine around it. Its often used in movies and videogames to exagerate objects in the scene. Simple but effective.

There is really no big magic behind it. The main work is done in two steps. First of all, in the Scene3D.RENDER_EVENT the position of the rim shaded object is subtracted from the camera position:


// subtract the target objects position from the cameras position
var camPos:Vector3D = Device3D.camera.getPosition().subtract(_targetObject.getPosition());
// make it length 1
 camPos.normalize();
// then set the <strong>camNormal </strong>variable in the FLSL shader
 this.params.camNormal.value = Vector.<Number>([camPos.x,camPos.y,camPos.z]);

The second step is done in the shader itself. With

float1 dotProduct = dot(camNormal,iNormal.xyz);

the dot product of the two vectors (camdirection and surface normal) is calculated. The dot product correlates to the angle between those two.

Then, the result is inverted (the bigger the angle, the smaller the dot product, but we want it the opposite way) and intensified (just for aesthetic reasons) and returned as the fragment color value.

Here is the complete shader code. Download here: RimShaderV1


/**
 * Flare3D Layer Shader Language v1.0.
 * @author Jonas Volger
 */

< namespace:"flare", name:"Rim Filter" >

input NORMAL normal;          // vertex normal
interpolated float4 iNormal;  // buffer for interpolated fragment normal
public float3 camDirection = 0;  // direction of camera, set from external

private void vertexRimShaderV1() // vertex function
{
 iNormal = float4( normal, 1 );  // set the interpolated normal
}

private float4 fragmentRimShaderV1() //fragment function
{
// calculate angle between camera direction and surface normal
 float1 dotProduct = dot(camNormal,iNormal.xyz);
 dotProduct = 1 - dotProduct;  // invert the result
 return dotProduct * 1.5;      // intensify the outcome a little
 delete dotProduct;            // delete internal use variables as registers are sparse
}

technique "perVertex"
{
 vertex vertexRimShaderV1();
 fragment fragmentRimShaderV1();
}

You can get the full code in the Sample Code Project. You find it in the Files section. Its also available on Github. So if you like to contribute, you are more than welcome!

If I didn’t explain anything well enough, made a mistake or explained something wrong, please let me know and leave a comment!

4 thoughts on “Rim Shader V1

  1. Schön, aber könnte man es noch so machen, dass unten ein Glas ist, das man hin und her bewegen kann, um damit von oben herunterfallende Zutaten in der richtigen Kombination auffangen kann?

  2. I’m truly enjoying the design and layout of your site. It’s
    a very easy on the eyes which makes it much more pleasant for me
    to come here and visit more often. Did you hire out a developer
    to create your theme? Exceptional work!

    • Thank you, but it’s just a basic WordPress Theme, which I slightly configured, thats all! So this should go out to the original designer of the WordPress Team ;)

Leave a Reply to eczema for baby Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>