Remove unnecessary light define checks

This commit is contained in:
halx99 2023-07-16 00:42:16 +08:00
parent 09d8f849fe
commit ac778081d1
3 changed files with 81 additions and 202 deletions

View File

@ -25,50 +25,32 @@
const char* CC3D_colorNormal_frag = R"( const char* CC3D_colorNormal_frag = R"(
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
uniform vec3 u_DirLightSourceColor[MAX_DIRECTIONAL_LIGHT_NUM]; uniform vec3 u_DirLightSourceColor[MAX_DIRECTIONAL_LIGHT_NUM];
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM]; uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif
#if (MAX_POINT_LIGHT_NUM > 0)
uniform vec3 u_PointLightSourceColor[MAX_POINT_LIGHT_NUM]; uniform vec3 u_PointLightSourceColor[MAX_POINT_LIGHT_NUM];
uniform float u_PointLightSourceRangeInverse[MAX_POINT_LIGHT_NUM]; uniform float u_PointLightSourceRangeInverse[MAX_POINT_LIGHT_NUM];
#endif
#if (MAX_SPOT_LIGHT_NUM > 0)
uniform vec3 u_SpotLightSourceColor[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourceColor[MAX_SPOT_LIGHT_NUM];
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceInnerAngleCos[MAX_SPOT_LIGHT_NUM]; uniform float u_SpotLightSourceInnerAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceOuterAngleCos[MAX_SPOT_LIGHT_NUM]; uniform float u_SpotLightSourceOuterAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceRangeInverse[MAX_SPOT_LIGHT_NUM]; uniform float u_SpotLightSourceRangeInverse[MAX_SPOT_LIGHT_NUM];
#endif
uniform vec3 u_AmbientLightSourceColor; uniform vec3 u_AmbientLightSourceColor;
#ifdef GL_ES #ifdef GL_ES
varying mediump vec2 TextureCoordOut; varying mediump vec2 TextureCoordOut;
#if MAX_POINT_LIGHT_NUM
varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM]; varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM]; varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
varying mediump vec3 v_normal; varying mediump vec3 v_normal;
#endif
#else #else
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
#if MAX_POINT_LIGHT_NUM varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
varying vec3 v_normal; varying vec3 v_normal;
#endif #endif
#endif
uniform vec4 u_color; uniform vec4 u_color;
vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, float attenuation) vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, float attenuation)
@ -81,33 +63,26 @@ vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, fl
void main(void) void main(void)
{ {
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
vec3 normal = normalize(v_normal); vec3 normal = normalize(v_normal);
#endif
vec4 combinedColor = vec4(u_AmbientLightSourceColor, 1.0); vec4 combinedColor = vec4(u_AmbientLightSourceColor, 1.0);
// Directional light contribution // Directional light contribution
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i) for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{ {
vec3 lightDirection = normalize(u_DirLightSourceDirection[i] * 2.0); vec3 lightDirection = normalize(u_DirLightSourceDirection[i] * 2.0);
combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0); combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0);
} }
#endif
// Point light contribution // Point light contribution
#if (MAX_POINT_LIGHT_NUM > 0)
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i) for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{ {
vec3 ldir = v_vertexToPointLightDirection[i] * u_PointLightSourceRangeInverse[i]; vec3 ldir = v_vertexToPointLightDirection[i] * u_PointLightSourceRangeInverse[i];
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0); float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), u_PointLightSourceColor[i], attenuation); combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), u_PointLightSourceColor[i], attenuation);
} }
#endif
// Spot light contribution // Spot light contribution
#if (MAX_SPOT_LIGHT_NUM > 0)
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i) for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{ {
// Compute range attenuation // Compute range attenuation
@ -125,13 +100,7 @@ void main(void)
attenuation = clamp(attenuation, 0.0, 1.0); attenuation = clamp(attenuation, 0.0, 1.0);
combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, u_SpotLightSourceColor[i], attenuation); combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, u_SpotLightSourceColor[i], attenuation);
} }
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
gl_FragColor = u_color * combinedColor; gl_FragColor = u_color * combinedColor;
#else
gl_FragColor = u_color;
#endif
} }
)"; )";

View File

@ -25,71 +25,52 @@
const char* CC3D_colorNormalTexture_frag = R"( const char* CC3D_colorNormalTexture_frag = R"(
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
uniform vec3 u_DirLightSourceColor[MAX_DIRECTIONAL_LIGHT_NUM]; uniform vec3 u_DirLightSourceColor[MAX_DIRECTIONAL_LIGHT_NUM];
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM]; uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif
#if (MAX_POINT_LIGHT_NUM > 0)
uniform vec3 u_PointLightSourceColor[MAX_POINT_LIGHT_NUM]; uniform vec3 u_PointLightSourceColor[MAX_POINT_LIGHT_NUM];
uniform float u_PointLightSourceRangeInverse[MAX_POINT_LIGHT_NUM]; uniform float u_PointLightSourceRangeInverse[MAX_POINT_LIGHT_NUM];
#endif
#if (MAX_SPOT_LIGHT_NUM > 0)
uniform vec3 u_SpotLightSourceColor[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourceColor[MAX_SPOT_LIGHT_NUM];
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceInnerAngleCos[MAX_SPOT_LIGHT_NUM]; uniform float u_SpotLightSourceInnerAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceOuterAngleCos[MAX_SPOT_LIGHT_NUM]; uniform float u_SpotLightSourceOuterAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceRangeInverse[MAX_SPOT_LIGHT_NUM]; uniform float u_SpotLightSourceRangeInverse[MAX_SPOT_LIGHT_NUM];
#endif
uniform vec3 u_AmbientLightSourceColor; uniform vec3 u_AmbientLightSourceColor;
#ifdef GL_ES #ifdef GL_ES
varying mediump vec2 TextureCoordOut; varying mediump vec2 TextureCoordOut;
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if MAX_DIRECTIONAL_LIGHT_NUM
varying mediump vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM]; varying mediump vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif #endif
#endif
#if MAX_POINT_LIGHT_NUM
varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM]; varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM]; varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
varying mediump vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM]; varying mediump vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif #endif
#endif
#ifndef USE_NORMAL_MAPPING #ifndef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
varying mediump vec3 v_normal; varying mediump vec3 v_normal;
#endif #endif
#endif
#else #else
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if MAX_DIRECTIONAL_LIGHT_NUM
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM]; varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif #endif
#endif
#if MAX_POINT_LIGHT_NUM
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM]; varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif #endif
#endif
#ifndef USE_NORMAL_MAPPING #ifndef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
varying vec3 v_normal; varying vec3 v_normal;
#endif #endif
#endif
#endif #endif
@ -112,19 +93,14 @@ void main(void)
{ {
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0)) vec3 normal = normalize(2.0 * texture2D(u_normalTex, TextureCoordOut).xyz - 1.0);
vec3 normal = normalize(2.0 * texture2D(u_normalTex, TextureCoordOut).xyz - 1.0);
#endif
#else #else
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0)) vec3 normal = normalize(v_normal);
vec3 normal = normalize(v_normal);
#endif
#endif #endif
vec4 combinedColor = vec4(u_AmbientLightSourceColor, 1.0); vec4 combinedColor = vec4(u_AmbientLightSourceColor, 1.0);
// Directional light contribution // Directional light contribution
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i) for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{ {
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
@ -134,20 +110,16 @@ void main(void)
#endif #endif
combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0); combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0);
} }
#endif
// Point light contribution // Point light contribution
#if (MAX_POINT_LIGHT_NUM > 0)
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i) for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{ {
vec3 ldir = v_vertexToPointLightDirection[i] * u_PointLightSourceRangeInverse[i]; vec3 ldir = v_vertexToPointLightDirection[i] * u_PointLightSourceRangeInverse[i];
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0); float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), u_PointLightSourceColor[i], attenuation); combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), u_PointLightSourceColor[i], attenuation);
} }
#endif
// Spot light contribution // Spot light contribution
#if (MAX_SPOT_LIGHT_NUM > 0)
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i) for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{ {
// Compute range attenuation // Compute range attenuation
@ -169,13 +141,7 @@ void main(void)
attenuation = clamp(attenuation, 0.0, 1.0); attenuation = clamp(attenuation, 0.0, 1.0);
combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, u_SpotLightSourceColor[i], attenuation); combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, u_SpotLightSourceColor[i], attenuation);
} }
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
gl_FragColor = texture2D(u_tex0, TextureCoordOut) * u_color * combinedColor; gl_FragColor = texture2D(u_tex0, TextureCoordOut) * u_color * combinedColor;
#else
gl_FragColor = texture2D(u_tex0, TextureCoordOut) * u_color;
#endif
} }
)"; )";

View File

@ -26,19 +26,13 @@
const char* CC3D_positionNormalTexture_vert = R"( const char* CC3D_positionNormalTexture_vert = R"(
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM]; uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif #endif
#endif
#if (MAX_POINT_LIGHT_NUM > 0)
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM]; uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
#endif
#if (MAX_SPOT_LIGHT_NUM > 0)
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
#endif #endif
#endif
attribute vec4 a_position; attribute vec4 a_position;
attribute vec2 a_texCoord; attribute vec2 a_texCoord;
@ -50,25 +44,17 @@ attribute vec3 a_binormal;
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if MAX_DIRECTIONAL_LIGHT_NUM
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM]; varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif #endif
#endif
#if MAX_POINT_LIGHT_NUM
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM]; varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif #endif
#endif
#ifndef USE_NORMAL_MAPPING #ifndef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
varying vec3 v_normal; varying vec3 v_normal;
#endif #endif
#endif
uniform mat4 u_MVPMatrix; uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix; uniform mat4 u_MVMatrix;
@ -79,61 +65,47 @@ void main(void)
{ {
vec4 ePosition = u_MVMatrix * a_position; vec4 ePosition = u_MVMatrix * a_position;
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0)) vec3 eTangent = normalize(u_NormalMatrix * a_tangent);
vec3 eTangent = normalize(u_NormalMatrix * a_tangent); vec3 eBinormal = normalize(u_NormalMatrix * a_binormal);
vec3 eBinormal = normalize(u_NormalMatrix * a_binormal); vec3 eNormal = normalize(u_NormalMatrix * a_normal);
vec3 eNormal = normalize(u_NormalMatrix * a_normal); for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
#endif {
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0) v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i) v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
{ v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]); }
v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
}
#endif
#if (MAX_POINT_LIGHT_NUM > 0) for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i) {
{ vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz; v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir); v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir); v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir); }
}
#endif
#if (MAX_SPOT_LIGHT_NUM > 0) for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i) {
{ vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz; v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir); v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir); v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]); v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]); v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]); v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
} }
#endif
#else #else
#if (MAX_POINT_LIGHT_NUM > 0) for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i) {
{ v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz; }
}
#endif
#if (MAX_SPOT_LIGHT_NUM > 0) for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i) {
{ v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz; }
}
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0)) v_normal = u_NormalMatrix * a_normal;
v_normal = u_NormalMatrix * a_normal;
#endif
#endif #endif
TextureCoordOut = a_texCoord; TextureCoordOut = a_texCoord;
@ -145,19 +117,14 @@ void main(void)
const char* CC3D_skinPositionNormalTexture_vert = R"( const char* CC3D_skinPositionNormalTexture_vert = R"(
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM]; uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif #endif
#endif
#if (MAX_POINT_LIGHT_NUM > 0)
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM]; uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
#endif
#if (MAX_SPOT_LIGHT_NUM > 0)
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM]; uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
#endif #endif
#endif
attribute vec3 a_position; attribute vec3 a_position;
@ -184,25 +151,18 @@ uniform mat4 u_PMatrix;
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if MAX_DIRECTIONAL_LIGHT_NUM
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM]; varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif #endif
#endif
#if MAX_POINT_LIGHT_NUM
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM]; varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM]; varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif #endif
#endif
#ifndef USE_NORMAL_MAPPING #ifndef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
varying vec3 v_normal; varying vec3 v_normal;
#endif #endif
#endif
void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent, out vec3 binormal) void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent, out vec3 binormal)
{ {
@ -247,7 +207,6 @@ void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent,
position.z = dot(p, matrixPalette3); position.z = dot(p, matrixPalette3);
position.w = p.w; position.w = p.w;
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
vec4 n = vec4(a_normal, 0.0); vec4 n = vec4(a_normal, 0.0);
normal.x = dot(n, matrixPalette1); normal.x = dot(n, matrixPalette1);
normal.y = dot(n, matrixPalette2); normal.y = dot(n, matrixPalette2);
@ -262,7 +221,6 @@ void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent,
binormal.y = dot(b, matrixPalette2); binormal.y = dot(b, matrixPalette2);
binormal.z = dot(b, matrixPalette3); binormal.z = dot(b, matrixPalette3);
#endif #endif
#endif
} }
void main() void main()
@ -275,62 +233,48 @@ void main()
vec4 ePosition = u_MVMatrix * position; vec4 ePosition = u_MVMatrix * position;
#ifdef USE_NORMAL_MAPPING #ifdef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0)) vec3 eTangent = normalize(u_NormalMatrix * tangent);
vec3 eTangent = normalize(u_NormalMatrix * tangent); vec3 eBinormal = normalize(u_NormalMatrix * binormal);
vec3 eBinormal = normalize(u_NormalMatrix * binormal); vec3 eNormal = normalize(u_NormalMatrix * normal);
vec3 eNormal = normalize(u_NormalMatrix * normal);
#endif
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0) for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i) {
{ v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]); v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]); v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]); }
}
#endif
#if (MAX_POINT_LIGHT_NUM > 0) for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i) {
{ vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz; v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir); v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir); v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir); }
}
#endif
#if (MAX_SPOT_LIGHT_NUM > 0) for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i) {
{ vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz; v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir); v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir); v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]); v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]); v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]); v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
} }
#endif
#else #else
#if (MAX_POINT_LIGHT_NUM > 0) for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i) {
{ v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz;
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz; }
}
#endif
#if (MAX_SPOT_LIGHT_NUM > 0) for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i) {
{ v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz; }
}
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0)) v_normal = u_NormalMatrix * normal;
v_normal = u_NormalMatrix * normal;
#endif
#endif #endif
TextureCoordOut = a_texCoord; TextureCoordOut = a_texCoord;