attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
attribute float aInShade;
attribute vec4 aVertexColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;
uniform vec3 uAmbientColor;
uniform vec3 uLightingDirection;
uniform vec3 uDirectionalColor;
uniform bool uUseLighting;
uniform bool uUseShadows;
uniform bool uUseColor;
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
varying vec4 vColor;
vec3 halfLambertDiffuse(void) {
vec4 transformedLightingDirection = uMVMatrix * vec4(uLightingDirection, 0.0);
vec4 transformedNormal = uNMatrix * vec4(aVertexNormal, 1.0);
float directionalLightWeighting = max(sqrt(0.5 * dot(transformedNormal.xyz, transformedLightingDirection.xyz) + 0.5), 0.0);
//float directionalLightWeighting = max(dot(transformedNormal.xyz, transformedLightingDirection.xyz), 0.6);
vLightWeighting = uAmbientColor + uDirectionalColor * directionalLightWeighting;
return vLightWeighting;
}
void main(void)
{
vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * mvPosition;
if (!uUseColor) {
vTextureCoord = aTextureCoord;
if (!uUseLighting) {
vLightWeighting = vec3(1.0, 1.0, 1.0);
} else {
//shading with shadows
if (uUseShadows) {
if (aInShade == 0.0) {
vLightWeighting = halfLambertDiffuse();
} else {
vLightWeighting = uAmbientColor;
}
} else {
//shading only
vLightWeighting = halfLambertDiffuse();
}
}
} else {
vColor = aVertexColor;
if (uUseLighting) {
vLightWeighting = halfLambertDiffuse();
}
}
}