toneburst
09-11-2008, 02:22 AM
I'm trying to implement a very simple rim-lighting shader, based on shader code extracted from the example here
http://www.catalinzima.com/?page_id=396
The problem is, it's not really working for me, unfortunately.
Here's what I get:
http://mcserver.gold.ac.uk/temp/rimlight_error.png
As you can see, the rim-lighting effect cuts off sharply, instead of going all the way around.
Here's the shader code:
Vertex Shader:
varying vec3 normal;
varying vec4 ecPos;
void main()
{
ecPos = gl_ModelViewMatrix * gl_Vertex;
gl_Position = gl_ProjectionMatrix * ecPos;
normal = gl_NormalMatrix * gl_Normal;
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}
Fragment Shader:
varying vec3 normal;
varying vec4 ecPos;
uniform sampler2D DiffuseMap; // Diffuse Map (base texture)
uniform float Rim_Start; // Rim Begin Range 0.0 > 1.0
uniform float Rim_End; // Rim End Range 0.0 > 1.0
uniform float Rim_Multiplier; // Rim Multiplier Range 0.0 > 5.0
uniform vec4 Rim_Color; // Rim Color
uniform vec3 Light_Direction; // Light Direction Vector
uniform vec4 Ambient_Color; // Ambient Light Color
uniform vec4 Light_Color; // Light Color
const vec3 CameraPosition = vec3(0.0);
void main()
{
vec4 tex = texture2D(DiffuseMap, gl_TexCoord[0].xy);
vec3 N = normalize(normal);
vec3 V = normalize(CameraPosition - (ecPos.xyz / ecPos.z));
float rim = smoothstep(Rim_Start, Rim_End, 1.0 - dot(N,V));
vec3 L = normalize(-Light_Direction);
float lightAmount = max(dot(N, L), 0.0);
vec4 lighting = Ambient_Color + vec4(lightAmount) * Light_Color;
lighting.a = 1.0;
//Multiply color by texture
gl_FragColor = tex * lighting + rim * Rim_Multiplier * Rim_Color;
}
Anything obvious I'm missing/getting wrong?
Cheers,
a|x
http://machinesdontcare.wordpress.com
http://www.catalinzima.com/?page_id=396
The problem is, it's not really working for me, unfortunately.
Here's what I get:
http://mcserver.gold.ac.uk/temp/rimlight_error.png
As you can see, the rim-lighting effect cuts off sharply, instead of going all the way around.
Here's the shader code:
Vertex Shader:
varying vec3 normal;
varying vec4 ecPos;
void main()
{
ecPos = gl_ModelViewMatrix * gl_Vertex;
gl_Position = gl_ProjectionMatrix * ecPos;
normal = gl_NormalMatrix * gl_Normal;
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}
Fragment Shader:
varying vec3 normal;
varying vec4 ecPos;
uniform sampler2D DiffuseMap; // Diffuse Map (base texture)
uniform float Rim_Start; // Rim Begin Range 0.0 > 1.0
uniform float Rim_End; // Rim End Range 0.0 > 1.0
uniform float Rim_Multiplier; // Rim Multiplier Range 0.0 > 5.0
uniform vec4 Rim_Color; // Rim Color
uniform vec3 Light_Direction; // Light Direction Vector
uniform vec4 Ambient_Color; // Ambient Light Color
uniform vec4 Light_Color; // Light Color
const vec3 CameraPosition = vec3(0.0);
void main()
{
vec4 tex = texture2D(DiffuseMap, gl_TexCoord[0].xy);
vec3 N = normalize(normal);
vec3 V = normalize(CameraPosition - (ecPos.xyz / ecPos.z));
float rim = smoothstep(Rim_Start, Rim_End, 1.0 - dot(N,V));
vec3 L = normalize(-Light_Direction);
float lightAmount = max(dot(N, L), 0.0);
vec4 lighting = Ambient_Color + vec4(lightAmount) * Light_Color;
lighting.a = 1.0;
//Multiply color by texture
gl_FragColor = tex * lighting + rim * Rim_Multiplier * Rim_Color;
}
Anything obvious I'm missing/getting wrong?
Cheers,
a|x
http://machinesdontcare.wordpress.com