attribute vec3 a_position;
uniform mat4 u_projViewTrans;

#ifdef normalFlag
attribute vec3 a_normal;
uniform mat3 u_normalMatrix;
varying vec3 v_normal;
#endif // normalFlag

#if defined(tangentFlag) && defined(binormalFlag)
attribute vec3 a_tangent;
attribute vec3 a_binormal;
varying mat3 v_tbn;
#endif // tangentFlag && bitangentFlag

#ifdef colorFlag
varying vec4 v_color;
attribute vec4 a_color;
#endif // colorFlag

#if defined(diffuseTextureFlag) || defined(specularTextureFlag) || defined(emissiveTextureFlag)
#define textureFlag
#endif

#if	defined(ambientLightFlag) || defined(ambientCubemapFlag) || defined(sphericalHarmonicsFlag)
#define ambientFlag
#endif // ambientFlag

#ifdef textureFlag
attribute vec2 a_texCoord0;
#endif // textureFlag

#ifdef diffuseTextureFlag
uniform vec4 u_diffuseUVTransform;
varying vec2 v_diffuseUV;
#endif // diffuseTextureFlag

#ifdef normalTextureFlag
uniform vec4 u_normalUVTransform;
varying vec2 v_normalUV;
#endif // normalTextureFlag

#ifdef emissiveTextureFlag
uniform vec4 u_emissiveUVTransform;
varying vec2 v_emissiveUV;
#endif // emissiveTextureFlag

#ifdef specularTextureFlag
uniform vec4 u_specularUVTransform;
varying vec2 v_specularUV;
#endif // specularTextureFlag

uniform mat4 u_worldTrans;

#ifdef lightingFlag

#ifdef ambientLightFlag
uniform vec3 u_ambientLight;
#endif // ambientLightFlag

#ifdef ambientLightFlag
uniform vec3 u_ambientLight;
#endif // ambientLightFlag

#ifdef ambientCubemapFlag
uniform vec3 u_ambientCubemap[6];
#endif // ambientCubemapFlag

/*
#ifdef sphericalHarmonicsFlag
uniform vec3 u_sphericalHarmonics[9];
#endif // sphericalHarmonicsFlag
*/

#ifdef ambientFlag
varying vec3 v_ambient;
#endif // ambientFlag

#endif // lightingFlag

#if defined(lightingFlag) || defined(fogFlag)
varying vec4 v_worldPosition;
#endif // lightingFlag || fogFlag

void main()
{
#ifdef normalFlag
	//vec3 normal = (transpose(inverse(u_normalMatrix)) * a_normal);
	vec3 normal = normalize(u_normalMatrix * a_normal);
	v_normal = normal;

#if defined(tangentFlag) && defined(binormalFlag)
	vec3 tangent = normalize(u_normalMatrix * a_tangent);
	vec3 bitangent = normalize(u_normalMatrix * a_binormal);
	v_tbn = mat3(tangent, bitangent, normal);
#endif // tangentFlag && bitangentFlag
#endif // normalFlag

#ifdef colorFlag
	v_color = a_color;
#endif // colorFlag

#ifdef diffuseTextureFlag
	v_diffuseUV = u_diffuseUVTransform.xy + a_texCoord0 * u_diffuseUVTransform.zw;
#endif // diffuseTextureFlag

#ifdef normalTextureFlag
	v_normalUV = u_normalUVTransform.xy + a_texCoord0 * u_normalUVTransform.zw;
#endif // normalTextureFlag

#ifdef emissiveTextureFlag
	v_emissiveUV = u_emissiveUVTransform.xy + a_texCoord0 * u_emissiveUVTransform.zw;
#endif // emissiveTextureFlag

#ifdef specularTextureFlag
	v_specularUV = u_specularUVTransform.xy + a_texCoord0 * u_specularUVTransform.zw;
#endif // specularTextureFlag

#ifdef ambientLightFlag
	v_ambient = u_ambientLight;
#else // !ambientLightFlag
	v_ambient = vec3(0.0);
#endif // ambientLightFlag

#ifdef ambientCubemapFlag
	vec3 norm2 = normal * normal;
	vec3 isPositive = step(0.0, normal);
	v_ambient +=
		norm2.x * mix(u_ambientCubemap[0], u_ambientCubemap[1], isPositive.x) +
		norm2.y * mix(u_ambientCubemap[2], u_ambientCubemap[3], isPositive.y) +
		norm2.z * mix(u_ambientCubemap[4], u_ambientCubemap[5], isPositive.z);
#endif // ambientCubemapFlag

	vec4 worldPos = u_worldTrans * vec4(a_position, 1.0);
#if defined(lightingFlag) || defined(fogFlag)
	v_worldPosition = worldPos;
	//v_cameraPosition = u_cameraPosition;
#endif // lightingFlag || fogFlag
	gl_Position = u_projViewTrans * worldPos;
}
