- Home /
Broken specular shader code (CG)
Recently I've been making shaders, and I put this on a capsule
Shader "Example/Specular" {
Properties
{
_Color ("Color", Color) = (1, 1, 1, 1)
_Shininess ("Shininess", Range (0, 1)) = 0.5
}
SubShader
{
Pass
{
Tags
{
"LightMode" = "ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
float4 _LightColor;
float4 _Shininess;
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 pos : POSITION;
float4 col : COLOR;
};
vertexOutput vert (vertexInput input) {
vertexOutput output;
float3 normalDirection = normalize (mul (float4 (input.normal, 0), _World2Object).xyz);
float3 lightDirection = normalize (_WorldSpaceLightPos0.xyz);
float3 viewDirection = float4 (_WorldSpaceCameraPos.xyz, 1) - mul (UNITY_MATRIX_MVP, input.vertex);
float atten = 1;
float3 diffuseReflection = atten * _LightColor.xyz * _Color.rgb * max (0, dot (normalDirection, lightDirection));
float3 specularReflection = max (0, dot (normalDirection, lightDirection)) * pow (max (0, dot (reflect (-lightDirection, normalDirection), viewDirection)), _Shininess);
float3 lightFinal = diffuseReflection + specularReflection + UNITY_LIGHTMODEL_AMBIENT;
output.col = float4 (lightFinal, 1);
output.pos = mul (UNITY_MATRIX_MVP, input.vertex);
return output;
}
float4 frag (vertexOutput output) : COLOR {
return output.col;
}
ENDCG
}
}
Fallback "Diffuse"
}
and it made a bunch of vertices facing the light white, and the rest midnight black. Im so overwhelmed with learning this new language, its like when i was trying to learn C#, it was like no one could help me. Im afraid there really is no one to teach me this language though.
I dont understand any of the code im using either, and im trying to avoid scary code. I know from my past C# experience that avoiding code is not the answer if i want to learn, so can someone please help me? ill look back in the morning, but its in the A$$anonymous$$ hours now. goodnight.
Your answer
Follow this Question
Related Questions
Using custom keywords in cg shaders 1 Answer
Shader works differently on Android (OpenGL) and Editor (DX11) 0 Answers
_LightMatrix0 and Directional Light 1 Answer
Shader time related questions 1 Answer
Shader - UNITY_SAMPLE_DEPTH function 2 Answers