Hi
I’m a complete noob writing shaders, I’m following nvidia’s Cg Tutorial, so far I’ve been able to run all the examples in unity, but I got stuck in chapter 5: Lighting, more than anything, because I have to replace some variables for its unity equivalencies, the easy parts (emissive and ambient) are runing, but I’m having problems with both diffuse and specular.
this is the tutorial code:
void C5E1v_basicLight(float4 position : POSITION,
float3 normal : NORMAL,
out float4 oPosition : POSITION,
out float4 color : COLOR,
uniform float4x4 modelViewProj,
uniform float3 globalAmbient,
uniform float3 lightColor,
uniform float3 lightPosition,
uniform float3 eyePosition,
uniform float3 Ke,
uniform float3 Ka,
uniform float3 Kd,
uniform float3 Ks,
uniform float shininess)
{
oPosition = mul(modelViewProj, position);
float3 P = position.xyz;
float3 N = normal;
// Compute the emissive term
float3 emissive = Ke;
// Compute the ambient term
float3 ambient = Ka * globalAmbient;
// Compute the diffuse term
float3 L = normalize(lightPosition - P);
float diffuseLight = max(dot(N, L), 0);
float3 diffuse = Kd * lightColor * diffuseLight;
// Compute the specular term
float3 V = normalize(eyePosition - P);
float3 H = normalize(L + V);
float specularLight = pow(max(dot(N, H), 0), shininess);
if (diffuseLight <= 0) specularLight = 0;
float3 specular = Ks * lightColor * specularLight;
color.xyz = emissive + ambient + diffuse + specular;
color.w = 1;
}
and this is my code (please forgive any dumb error, I’ve spent years without programming), I’ve replace the kx variable with _something from the material to be able to change them from the unity editor:
void vert(float4 position : POSITION,
float3 normal : NORMAL,
out float4 oPosition : POSITION,
out float4 color : COLOR)
{
oPosition = mul(UNITY_MATRIX_MVP, position);
float3 P = position.xyz;
float3 N = normal;
// Compute the emissive term
float3 emisivo = _Emissive.xyz;
// Compute the ambient term
float3 ambiente = _Ambient.xyz * UNITY_LIGHTMODEL_AMBIENT.xyz;
// Compute the diffuse term
float3 dirLuz = P - _WorldSpaceLightPos0.xyz;
float3 L = normalize(dirLuz);
float diffuseLight = max(dot(N, L), 0);
float3 difuso = _Diffuse.rgb * unity_LightColor0.xyz * diffuseLight;
// Compute the specular term
float3 V = normalize(_WorldSpaceCameraPos - P);
float3 H = normalize(L + V);
float specularLight = pow(max(dot(N, H), 0), _Shininess);
if (diffuseLight <= 0) specularLight = 0;
float3 especular = _Specular * unity_LightColor[0].xyz * specularLight;
color.xyz = emisivo + ambiente + difuso + especular;
color.w = 1;
}
I’m having problems with the equivalencies, all I get is a shade in the mesh, but it’s not responding to actual light position, I’ve taken the equivalencies from this source, but it doesn’t work, it says that doesn’t find neither _ObjectSpaceLightPos nor _ModelLightColor; and yes, I’ve included UnityCG.cginc:
http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html
The variables I’m finding hard to replace are lightColor, lightPosition and eyePosition.
Thanks in advance