Hi everyone,
I’m having a small issue with doing a GLSL shader, for some reason, I can’t get the correct direction of the directional light in my scene using Unity functions. Right now I’m “fixing it” by multiplying it by (-1,1,1), but I’d like know what is going wrong.
Anyone have any ideas?
Here is the code -
Shader "Fast" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "Queue" = "Geometry" }
Pass {
GLSLPROGRAM
#include "UnityCG.glslinc"
#ifdef VERTEX
varying vec3 EyespaceNormal;
varying vec3 EyeVector;
varying vec3 LightVector;
varying vec2 TextureCoordinate;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
EyespaceNormal = normalize(gl_NormalMatrix * gl_Normal);
EyeVector = WorldSpaceViewDir( gl_Vertex );
// Here - not sure why this doesn't work.
LightVector = normalize(WorldSpaceLightDir( gl_Vertex ));
TextureCoordinate = gl_MultiTexCoord0.xy;
}
#endif
#ifdef FRAGMENT
uniform sampler2D _MainTex;
varying vec2 TextureCoordinate;
varying vec3 EyespaceNormal;
varying vec3 EyeVector;
varying vec3 LightVector;
void main()
{
vec3 N = EyespaceNormal;
vec3 L = LightVector;
vec3 E = EyeVector;
vec3 H = normalize(L + E);
float ldn = max(0.0, dot(N, L));
float hdn = max(0.0, dot(N, H));
vec4 texColor = texture2D(_MainTex, TextureCoordinate);
vec2 litv = vec2(max( 0.0, ldn), pow( hdn, 0.0 ));
vec3 diff = vec3(texColor.xyz * (litv.x * vec3( .35, .662, 0.996) + vec3( .4, .4, .4 )));
vec3 spec = litv.x*litv.y*vec3( .5, .5, .5) * texColor.a;
gl_FragColor = vec4(diff + spec, 1.0);
}
#endif
ENDGLSL
}
}
}