- Home /
Can I retrieve per face normals instead of per vertex normals in vertex shader?
I'm learning shaders and want to render a cube with each face lit based on the normal of the face rather than the normal of the vertex.
The cube I am using is a mesh I generate in code that only has 1 vertex per corner of the cube which is why the per vertex normals will face outward from the center of the cube, rather than normal of the face. One solution is to of course have 4 vertices per face but that would be unnecessary cost if I can just retrieve the normal of the face.
I'm currently using this shader:
Shader "Custom/AmbientLambertColorShader" {
Properties {
}
SubShader {
Tags{ "LightMode"="ForwardBase"}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//unity defined variable
uniform float4 _LightColor0;
//input structs
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 col : COLOR;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 col : COLOR;
};
//vertex function
vertexOutput vert(vertexInput v) {
vertexOutput o;
float3 normalDirection = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
float3 lightDirection;
float atten = 1.0;
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuseReflection = atten * _LightColor0.xyz * max( 0.0, dot(normalDirection, lightDirection));
float3 lightFinal = diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
// o.col = float4(lightFinal * v.col.rgb,1.0);
o.col = float4(normalDirection,1.0);
return o;
}
//Fragment function
float4 frag(vertexOutput i) : COLOR {
return i.col;
}
ENDCG
}
}
// FallBack "Diffuse"
}
Thank you for any thoughts.
Answer by RobotRocker · Jun 29, 2014 at 06:31 AM
For posterity I will say I did not find an answer to this so I assume it's no.
Your answer
Follow this Question
Related Questions
Reflections not seamless 1 Answer
How to write unlit surface shader? 6 Answers
How to calculate light from behind a quad? (and other light positions) 1 Answer
Double sided geometry. 3 Answers