Modified Light Shader

This commit is contained in:
songchengjiang 2014-09-22 18:08:58 +08:00
parent 61def04ca6
commit 216c40fe9e
9 changed files with 239 additions and 176 deletions

View File

@ -30,10 +30,10 @@
NS_CC_BEGIN
#define CC_MAX_DIRECTIONAL_LIGHT_NUM 1
#define CC_MAX_POINT_LIGHT_NUM 1
#define CC_MAX_SPOT_LIGHT_NUM 1
#define CC_MAX_AMBIENT_LIGHT_NUM 1
#define MAX_DIRECTIONAL_LIGHT_NUM 1
#define MAX_POINT_LIGHT_NUM 1
#define MAX_SPOT_LIGHT_NUM 1
#define MAX_AMBIENT_LIGHT_NUM 1
enum class LightType
{

View File

@ -517,7 +517,7 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
GLuint textureID = mesh->getTexture() ? mesh->getTexture()->getName() : 0;
meshCommand.init(_globalZOrder, textureID, programstate, _blend, mesh->getVertexBuffer(), mesh->getIndexBuffer(), mesh->getPrimitiveType(), mesh->getIndexFormat(), mesh->getIndexCount(), transform);
meshCommand.init(_globalZOrder, textureID, programstate, _blend, mesh->getVertexBuffer(), mesh->getIndexBuffer(), mesh->getPrimitiveType(), mesh->getIndexFormat(), mesh->getIndexCount(), _lightMask, transform);
auto skin = mesh->getSkin();
if (skin)

View File

@ -425,49 +425,11 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source
{
return false;
}
GLchar def[256];
sprintf(def
, "#define CC_MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"#define CC_MAX_POINT_LIGHT_NUM %d \n"
"#define CC_MAX_SPOT_LIGHT_NUM %d \n"
"#define CC_MAX_AMBIENT_LIGHT_NUM %d \n"
, CC_MAX_DIRECTIONAL_LIGHT_NUM, CC_MAX_POINT_LIGHT_NUM, CC_MAX_SPOT_LIGHT_NUM, CC_MAX_AMBIENT_LIGHT_NUM);
GLchar lightStruct[] = {
"#if CC_MAX_DIRECTIONAL_LIGHT_NUM \n"
"uniform vec3 CC_DirLightSourceColor[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n"
"uniform vec3 CC_DirLightSourceDirection[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n"
"uniform float CC_DirLightSourceIntensity[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n"
"#endif \n"
"#if CC_MAX_POINT_LIGHT_NUM \n"
"uniform vec3 CC_PointLightSourceColor[CC_MAX_POINT_LIGHT_NUM]; \n"
"uniform vec3 CC_PointLightSourcePosition[CC_MAX_POINT_LIGHT_NUM]; \n"
"uniform float CC_PointLightSourceRangeInverse[CC_MAX_POINT_LIGHT_NUM]; \n"
"uniform float CC_PointLightSourceIntensity[CC_MAX_POINT_LIGHT_NUM]; \n"
"#endif \n"
"#if CC_MAX_SPOT_LIGHT_NUM \n"
"uniform vec3 CC_SpotLightSourceColor[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform vec3 CC_SpotLightSourcePosition[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform vec3 CC_SpotLightSourceDirection[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform float CC_SpotLightSourceInnerAngleCos[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform float CC_SpotLightSourceOuterAngleCos[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform float CC_SpotLightSourceRangeInverse[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform float CC_SpotLightSourceIntensity[CC_MAX_SPOT_LIGHT_NUM]; \n"
"#endif \n"
//"#if CC_MAX_AMBIENT_LIGHT_NUM \n"
"uniform vec3 CC_AmbientLightSourceColor[CC_MAX_AMBIENT_LIGHT_NUM]; \n"
"uniform float CC_AmbientLightSourceIntensity[CC_MAX_AMBIENT_LIGHT_NUM]; \n"
//"#endif \n"
};
const GLchar *sources[] = {
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
(type == GL_VERTEX_SHADER ? "precision highp float;\n precision highp int;\n" : "precision mediump float;\n precision mediump int;\n"),
#endif
def,
lightStruct,
"uniform mat4 CC_PMatrix;\n"
"uniform mat4 CC_MVMatrix;\n"
"uniform mat4 CC_MVPMatrix;\n"

View File

@ -60,7 +60,8 @@ MeshCommand::MeshCommand()
, _matrixPaletteSize(0)
, _materialID(0)
, _vao(0)
, _useLights(false)
, _lightMask(-1)
, _preProgram(-1)
{
_type = RenderCommand::Type::MESH_COMMAND;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
@ -79,6 +80,7 @@ void MeshCommand::init(float globalOrder,
GLenum primitive,
GLenum indexFormat,
ssize_t indexCount,
unsigned short lightMask,
const Mat4 &mv)
{
CCASSERT(glProgramState, "GLProgramState cannot be nill");
@ -93,12 +95,10 @@ void MeshCommand::init(float globalOrder,
_primitive = primitive;
_indexFormat = indexFormat;
_indexCount = indexCount;
_lightMask = lightMask;
_mv.set(mv);
_useLights = (0 < CC_MAX_DIRECTIONAL_LIGHT_NUM) ||
(0 < CC_MAX_POINT_LIGHT_NUM) ||
(0 < CC_MAX_SPOT_LIGHT_NUM) ||
(0 < CC_MAX_AMBIENT_LIGHT_NUM);
updateLightUniforms();
}
void MeshCommand::setCullFaceEnabled(bool enable)
@ -230,7 +230,7 @@ void MeshCommand::batchDraw()
_glProgramState->applyGLProgram(_mv);
_glProgramState->applyUniforms();
if (_useLights)
if (_lightMask)
{
applyLightUniforms();
}
@ -274,7 +274,7 @@ void MeshCommand::execute()
_glProgramState->apply(_mv);
if (_useLights)
if (_lightMask)
{
applyLightUniforms();
}
@ -331,9 +331,7 @@ void MeshCommand::applyLightUniforms()
if (scene)
{
GLProgram *glProgram = _glProgramState->getGLProgram();
GLint programID = glProgram->getProgram();
auto &lights = scene->getLights();
char str[64];
GLint enabledDirLightNum = 0;
GLint enabledPointLightNum = 0;
GLint enabledSpotLightNum = 0;
@ -341,76 +339,57 @@ void MeshCommand::applyLightUniforms()
for (unsigned int i = 0; i < lights.size(); ++i)
{
BaseLight3D *light = lights[i];
float intensity = light->isEnabled() == true? light->getIntensity() : 0.0f;
bool useLight = light->isEnabled();
useLight = useLight && ((unsigned short)light->getLightFlag() & _lightMask);
float intensity = useLight == true? light->getIntensity() : 0.0f;
switch (light->getLightType())
{
case LightType::DIRECTIONAL:
{
CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, "");
CCASSERT(enabledDirLightNum < MAX_DIRECTIONAL_LIGHT_NUM, "");
DirectionLight3D *dirLight = static_cast<DirectionLight3D *>(light);
Vec3 dir = dirLight->getDirectionInWorld();
dir.normalize();
const Color3B &col = dirLight->getDisplayedColor();
sprintf(str, "CC_DirLightSourceColor[%d]", enabledDirLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_DirLightSourceDirection[%d]", enabledDirLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), dir.x, dir.y, dir.z);
sprintf(str, "CC_DirLightSourceIntensity[%d]", enabledDirLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_DIRECTIONAL_COLOR] + enabledDirLightNum, col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_DIRECTIONAL_DIR] + enabledDirLightNum, dir.x, dir.y, dir.z);
++enabledDirLightNum;
}
break;
case LightType::POINT:
{
CCASSERT(enabledPointLightNum < CC_MAX_POINT_LIGHT_NUM, "");
CCASSERT(enabledPointLightNum < MAX_POINT_LIGHT_NUM, "");
PointLight3D *pointLight = static_cast<PointLight3D *>(light);
Mat4 mat= pointLight->getNodeToWorldTransform();
const Color3B &col = pointLight->getDisplayedColor();
sprintf(str, "CC_PointLightSourceColor[%d]", enabledPointLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_PointLightSourcePosition[%d]", enabledPointLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), mat.m[12], mat.m[13], mat.m[14]);
sprintf(str, "CC_PointLightSourceRangeInverse[%d]", enabledPointLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), 1.0f / pointLight->getRange());
sprintf(str, "CC_PointLightSourceIntensity[%d]", enabledPointLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_POINT_COLOR] + enabledPointLightNum, col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_POINT_POSITION] + enabledPointLightNum, mat.m[12], mat.m[13], mat.m[14]);
glProgram->setUniformLocationWith1f(_lightUniforms[UNIFORM_POINT_RANGE_INVERSE] + enabledPointLightNum, 1.0f / pointLight->getRange());
++enabledPointLightNum;
}
break;
case LightType::SPOT:
{
CCASSERT(enabledSpotLightNum < CC_MAX_SPOT_LIGHT_NUM, "");
CCASSERT(enabledSpotLightNum < MAX_SPOT_LIGHT_NUM, "");
SpotLight3D *spotLight = static_cast<SpotLight3D *>(light);
Vec3 dir = spotLight->getDirectionInWorld();
dir.normalize();
Mat4 mat= light->getNodeToWorldTransform();
const Color3B &col = spotLight->getDisplayedColor();
sprintf(str, "CC_SpotLightSourceColor[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_SpotLightSourcePosition[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), mat.m[12], mat.m[13], mat.m[14]);
sprintf(str, "CC_SpotLightSourceDirection[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), dir.x, dir.y, dir.z);
sprintf(str, "CC_SpotLightSourceInnerAngleCos[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), spotLight->getCosInnerAngle());
sprintf(str, "CC_SpotLightSourceOuterAngleCos[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), spotLight->getCosOuterAngle());
sprintf(str, "CC_SpotLightSourceRangeInverse[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), 1.0f / spotLight->getRange());
sprintf(str, "CC_SpotLightSourceIntensity[%d]", enabledSpotLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_SPOT_COLOR] + enabledSpotLightNum, col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_SPOT_POSITION] + enabledSpotLightNum, mat.m[12], mat.m[13], mat.m[14]);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_SPOT_DIR] + enabledSpotLightNum, dir.x, dir.y, dir.z);
glProgram->setUniformLocationWith1f(_lightUniforms[UNIFORM_SPOT_INNER_ANGLE_COS] + enabledSpotLightNum, spotLight->getCosInnerAngle());
glProgram->setUniformLocationWith1f(_lightUniforms[UNIFORM_SPOT_OUTER_ANGLE_COS] + enabledSpotLightNum, spotLight->getCosOuterAngle());
glProgram->setUniformLocationWith1f(_lightUniforms[UNIFORM_SPOT_RANGE_INVERSE] + enabledSpotLightNum, 1.0f / spotLight->getRange());
++enabledSpotLightNum;
}
break;
case LightType::AMBIENT:
{
CCASSERT(enabledAmbientLightNum < CC_MAX_AMBIENT_LIGHT_NUM, "");
AmbientLight3D *ambLight = static_cast<AmbientLight3D *>(light);
const Color3B &col = ambLight->getDisplayedColor();
sprintf(str, "CC_AmbientLightSourceColor[%d]", enabledAmbientLightNum);
glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_AmbientLightSourceIntensity[%d]", enabledAmbientLightNum);
glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity);
glProgram->setUniformLocationWith3f(_lightUniforms[UNIFORM_AMBIENT_COLOR] + enabledAmbientLightNum, col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
++enabledAmbientLightNum;
}
break;
@ -421,6 +400,33 @@ void MeshCommand::applyLightUniforms()
}
}
void MeshCommand::updateLightUniforms()
{
GLProgram *glProgram = _glProgramState->getGLProgram();
GLint programID = glProgram->getProgram();
if (programID != _preProgram)
{
_lightUniforms[UNIFORM_DIRECTIONAL_COLOR] = glGetUniformLocation(programID, "u_DirLightSourceColor");
_lightUniforms[UNIFORM_DIRECTIONAL_DIR] = glGetUniformLocation(programID, "u_DirLightSourceDirection");
_lightUniforms[UNIFORM_POINT_COLOR] = glGetUniformLocation(programID, "u_PointLightSourceColor");
_lightUniforms[UNIFORM_POINT_POSITION] = glGetUniformLocation(programID, "u_PointLightSourcePosition");
_lightUniforms[UNIFORM_POINT_RANGE_INVERSE] = glGetUniformLocation(programID, "u_PointLightSourceRangeInverse");
_lightUniforms[UNIFORM_SPOT_COLOR] = glGetUniformLocation(programID, "u_SpotLightSourceColor");
_lightUniforms[UNIFORM_SPOT_POSITION] = glGetUniformLocation(programID, "u_SpotLightSourcePosition");
_lightUniforms[UNIFORM_SPOT_DIR] = glGetUniformLocation(programID, "u_SpotLightSourceDirection");
_lightUniforms[UNIFORM_SPOT_INNER_ANGLE_COS] = glGetUniformLocation(programID, "u_SpotLightSourceInnerAngleCos");
_lightUniforms[UNIFORM_SPOT_OUTER_ANGLE_COS] = glGetUniformLocation(programID, "u_SpotLightSourceOuterAngleCos");
_lightUniforms[UNIFORM_SPOT_RANGE_INVERSE] = glGetUniformLocation(programID, "u_SpotLightSourceRangeInverse");
_lightUniforms[UNIFORM_AMBIENT_COLOR] = glGetUniformLocation(programID, "u_AmbientLightSourceColor");
_preProgram = programID;
}
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
void MeshCommand::listenRendererRecreated(EventCustom* event)
{

View File

@ -46,7 +46,7 @@ public:
MeshCommand();
~MeshCommand();
void init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, GLuint vertexBuffer, GLuint indexBuffer, GLenum primitive, GLenum indexType, ssize_t indexCount, const Mat4 &mv);
void init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, GLuint vertexBuffer, GLuint indexBuffer, GLenum primitive, GLenum indexType, ssize_t indexCount, unsigned short lightMask, const Mat4 &mv);
void setCullFaceEnabled(bool enable);
@ -123,7 +123,35 @@ protected:
// ModelView transform
Mat4 _mv;
bool _useLights;
unsigned short _lightMask;
protected:
void updateLightUniforms();
enum LightUniform
{
UNIFORM_DIRECTIONAL_COLOR,
UNIFORM_DIRECTIONAL_DIR,
UNIFORM_POINT_COLOR,
UNIFORM_POINT_POSITION,
UNIFORM_POINT_RANGE_INVERSE,
UNIFORM_SPOT_COLOR,
UNIFORM_SPOT_POSITION,
UNIFORM_SPOT_DIR,
UNIFORM_SPOT_INNER_ANGLE_COS,
UNIFORM_SPOT_OUTER_ANGLE_COS,
UNIFORM_SPOT_RANGE_INVERSE,
UNIFORM_AMBIENT_COLOR,
UNIFORM_MAX,
};
GLint _lightUniforms[UNIFORM_MAX];
GLint _preProgram;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
EventListenerCustom* _rendererRecreatedListener;

View File

@ -1,29 +1,51 @@
const char* cc3D_ColorNormal_frag = STRINGIFY(
\n#define MAX_DIRECTIONAL_LIGHT_NUM 1 \n
\n#define MAX_POINT_LIGHT_NUM 1 \n
\n#define MAX_SPOT_LIGHT_NUM 1 \n
\n#define MAX_AMBIENT_LIGHT_NUM 1 \n
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
uniform vec3 u_DirLightSourceColor[MAX_DIRECTIONAL_LIGHT_NUM];
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
uniform vec3 u_PointLightSourceColor[MAX_POINT_LIGHT_NUM];
uniform float u_PointLightSourceRangeInverse[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
uniform vec3 u_SpotLightSourceColor[MAX_SPOT_LIGHT_NUM];
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceInnerAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceOuterAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceRangeInverse[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_AMBIENT_LIGHT_NUM > 0)\n
uniform vec3 u_AmbientLightSourceColor[MAX_AMBIENT_LIGHT_NUM];
\n#endif\n
\n#ifdef GL_ES\n
varying mediump vec2 TextureCoordOut;
\n#if CC_MAX_POINT_LIGHT_NUM\n
varying mediump vec3 v_vertexToPointLightDirection[CC_MAX_POINT_LIGHT_NUM];
\n#if MAX_POINT_LIGHT_NUM\n
varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if CC_MAX_SPOT_LIGHT_NUM\n
varying mediump vec3 v_vertexToSpotLightDirection[CC_MAX_SPOT_LIGHT_NUM];
\n#if MAX_SPOT_LIGHT_NUM\n
varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying mediump vec3 v_normal;
\n#endif\n
\n#else\n
varying vec2 TextureCoordOut;
\n#if CC_MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[CC_MAX_POINT_LIGHT_NUM];
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if CC_MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[CC_MAX_SPOT_LIGHT_NUM];
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying vec3 v_normal;
\n#endif\n
@ -41,59 +63,59 @@ vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, fl
void main(void)
{
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 normal = normalize(v_normal);
\n#endif\n
vec4 combinedColor = vec4(0.0, 0.0, 0.0, 1.0);
// Directional light contribution
\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_DIRECTIONAL_LIGHT_NUM; ++i)
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
vec3 lightDirection = normalize(CC_DirLightSourceDirection[i] * 2.0);
combinedColor.xyz += computeLighting(normal, -lightDirection, CC_DirLightSourceColor[i] * CC_DirLightSourceIntensity[i], 1.0);
vec3 lightDirection = normalize(u_DirLightSourceDirection[i] * 2.0);
combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0);
}
\n#endif\n
// Point light contribution
\n#if (CC_MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_POINT_LIGHT_NUM; ++i)
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
vec3 ldir = v_vertexToPointLightDirection[i] * CC_PointLightSourceRangeInverse[i];
vec3 ldir = v_vertexToPointLightDirection[i] * u_PointLightSourceRangeInverse[i];
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), CC_PointLightSourceColor[i] * CC_PointLightSourceIntensity[i], attenuation);
combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), u_PointLightSourceColor[i], attenuation);
}
\n#endif\n
// Spot light contribution
\n#if (CC_MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_SPOT_LIGHT_NUM; ++i)
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
// Compute range attenuation
vec3 ldir = v_vertexToSpotLightDirection[i] * CC_SpotLightSourceRangeInverse[i];
vec3 ldir = v_vertexToSpotLightDirection[i] * u_SpotLightSourceRangeInverse[i];
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection[i]);
vec3 spotLightDirection = normalize(CC_SpotLightSourceDirection[i] * 2.0);
vec3 spotLightDirection = normalize(u_SpotLightSourceDirection[i] * 2.0);
// "-lightDirection" is used because light direction points in opposite direction to spot direction.
float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
// Apply spot attenuation
attenuation *= smoothstep(CC_SpotLightSourceOuterAngleCos[i], CC_SpotLightSourceInnerAngleCos[i], spotCurrentAngleCos);
combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, CC_SpotLightSourceColor[i] * CC_SpotLightSourceIntensity[i], attenuation);
attenuation *= smoothstep(u_SpotLightSourceOuterAngleCos[i], u_SpotLightSourceInnerAngleCos[i], spotCurrentAngleCos);
combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, u_SpotLightSourceColor[i], attenuation);
}
\n#endif\n
\n#if (CC_MAX_AMBIENT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_AMBIENT_LIGHT_NUM; ++i)
\n#if (MAX_AMBIENT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_AMBIENT_LIGHT_NUM; ++i)
{
combinedColor.xyz += CC_AmbientLightSourceColor[i] * CC_AmbientLightSourceIntensity[i];
combinedColor.xyz += u_AmbientLightSourceColor[i];
}
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0) || (CC_MAX_AMBIENT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0) || (MAX_AMBIENT_LIGHT_NUM > 0))\n
gl_FragColor = u_color * combinedColor;
\n#else\n
gl_FragColor = u_color;

View File

@ -1,29 +1,51 @@
const char* cc3D_ColorNormalTex_frag = STRINGIFY(
\n#define MAX_DIRECTIONAL_LIGHT_NUM 1 \n
\n#define MAX_POINT_LIGHT_NUM 1 \n
\n#define MAX_SPOT_LIGHT_NUM 1 \n
\n#define MAX_AMBIENT_LIGHT_NUM 1 \n
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
uniform vec3 u_DirLightSourceColor[MAX_DIRECTIONAL_LIGHT_NUM];
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
\n#endif\n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
uniform vec3 u_PointLightSourceColor[MAX_POINT_LIGHT_NUM];
uniform float u_PointLightSourceRangeInverse[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
uniform vec3 u_SpotLightSourceColor[MAX_SPOT_LIGHT_NUM];
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceInnerAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceOuterAngleCos[MAX_SPOT_LIGHT_NUM];
uniform float u_SpotLightSourceRangeInverse[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_AMBIENT_LIGHT_NUM > 0)\n
uniform vec3 u_AmbientLightSourceColor[MAX_AMBIENT_LIGHT_NUM];
\n#endif\n
\n#ifdef GL_ES\n
varying mediump vec2 TextureCoordOut;
\n#if CC_MAX_POINT_LIGHT_NUM\n
varying mediump vec3 v_vertexToPointLightDirection[CC_MAX_POINT_LIGHT_NUM];
\n#if MAX_POINT_LIGHT_NUM\n
varying mediump vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if CC_MAX_SPOT_LIGHT_NUM\n
varying mediump vec3 v_vertexToSpotLightDirection[CC_MAX_SPOT_LIGHT_NUM];
\n#if MAX_SPOT_LIGHT_NUM\n
varying mediump vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying mediump vec3 v_normal;
\n#endif\n
\n#else\n
varying vec2 TextureCoordOut;
\n#if CC_MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[CC_MAX_POINT_LIGHT_NUM];
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if CC_MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[CC_MAX_SPOT_LIGHT_NUM];
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying vec3 v_normal;
\n#endif\n
@ -41,59 +63,59 @@ vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, fl
void main(void)
{
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec3 normal = normalize(v_normal);
\n#endif\n
vec4 combinedColor = vec4(0.0, 0.0, 0.0, 1.0);
// Directional light contribution
\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_DIRECTIONAL_LIGHT_NUM; ++i)
\n#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
vec3 lightDirection = normalize(CC_DirLightSourceDirection[i] * 2.0);
combinedColor.xyz += computeLighting(normal, -lightDirection, CC_DirLightSourceColor[i] * CC_DirLightSourceIntensity[i], 1.0);
vec3 lightDirection = normalize(u_DirLightSourceDirection[i] * 2.0);
combinedColor.xyz += computeLighting(normal, -lightDirection, u_DirLightSourceColor[i], 1.0);
}
\n#endif\n
// Point light contribution
\n#if (CC_MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_POINT_LIGHT_NUM; ++i)
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
vec3 ldir = v_vertexToPointLightDirection[i] * CC_PointLightSourceRangeInverse[i];
vec3 ldir = v_vertexToPointLightDirection[i] * u_PointLightSourceRangeInverse[i];
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), CC_PointLightSourceColor[i] * CC_PointLightSourceIntensity[i], attenuation);
combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), u_PointLightSourceColor[i], attenuation);
}
\n#endif\n
// Spot light contribution
\n#if (CC_MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_SPOT_LIGHT_NUM; ++i)
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
// Compute range attenuation
vec3 ldir = v_vertexToSpotLightDirection[i] * CC_SpotLightSourceRangeInverse[i];
vec3 ldir = v_vertexToSpotLightDirection[i] * u_SpotLightSourceRangeInverse[i];
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection[i]);
vec3 spotLightDirection = normalize(CC_SpotLightSourceDirection[i] * 2.0);
vec3 spotLightDirection = normalize(u_SpotLightSourceDirection[i] * 2.0);
// "-lightDirection" is used because light direction points in opposite direction to spot direction.
float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
// Apply spot attenuation
attenuation *= smoothstep(CC_SpotLightSourceOuterAngleCos[i], CC_SpotLightSourceInnerAngleCos[i], spotCurrentAngleCos);
combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, CC_SpotLightSourceColor[i] * CC_SpotLightSourceIntensity[i], attenuation);
attenuation *= smoothstep(u_SpotLightSourceOuterAngleCos[i], u_SpotLightSourceInnerAngleCos[i], spotCurrentAngleCos);
combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, u_SpotLightSourceColor[i], attenuation);
}
\n#endif\n
\n#if (CC_MAX_AMBIENT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_AMBIENT_LIGHT_NUM; ++i)
\n#if (MAX_AMBIENT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_AMBIENT_LIGHT_NUM; ++i)
{
combinedColor.xyz += CC_AmbientLightSourceColor[i] * CC_AmbientLightSourceIntensity[i];
combinedColor.xyz += u_AmbientLightSourceColor[i];
}
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0) || (CC_MAX_AMBIENT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0) || (MAX_AMBIENT_LIGHT_NUM > 0))\n
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color * combinedColor;
\n#else\n
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color;

View File

@ -1,39 +1,50 @@
const char* cc3D_PositionNormalTex_vert = STRINGIFY(
\n#define MAX_DIRECTIONAL_LIGHT_NUM 1 \n
\n#define MAX_POINT_LIGHT_NUM 1 \n
\n#define MAX_SPOT_LIGHT_NUM 1 \n
\n#define MAX_AMBIENT_LIGHT_NUM 1 \n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
\n#endif\n
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec3 a_normal;
varying vec2 TextureCoordOut;
\n#if CC_MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[CC_MAX_POINT_LIGHT_NUM];
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if CC_MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[CC_MAX_SPOT_LIGHT_NUM];
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
varying vec3 v_normal;
\n#endif\n
void main(void)
{
vec4 ePosition = CC_MVMatrix * a_position;
\n#if (CC_MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_POINT_LIGHT_NUM; ++i)
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = CC_PointLightSourcePosition[i].xyz - ePosition.xyz;
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
}
\n#endif\n
\n#if (CC_MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_SPOT_LIGHT_NUM; ++i)
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = CC_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
v_normal = CC_NormalMatrix * a_normal;
\n#endif\n
@ -44,6 +55,18 @@ void main(void)
);
const char* cc3D_SkinPositionNormalTex_vert = STRINGIFY(
\n#define MAX_DIRECTIONAL_LIGHT_NUM 1 \n
\n#define MAX_POINT_LIGHT_NUM 1 \n
\n#define MAX_SPOT_LIGHT_NUM 1 \n
\n#define MAX_AMBIENT_LIGHT_NUM 1 \n
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
\n#endif\n
attribute vec3 a_position;
attribute vec4 a_blendWeight;
@ -60,11 +83,11 @@ uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
// Varyings
varying vec2 TextureCoordOut;
\n#if CC_MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[CC_MAX_POINT_LIGHT_NUM];
\n#if MAX_POINT_LIGHT_NUM\n
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
\n#endif\n
\n#if CC_MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[CC_MAX_SPOT_LIGHT_NUM];
\n#if MAX_SPOT_LIGHT_NUM\n
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
\n#endif\n
varying vec3 v_normal;
@ -114,7 +137,7 @@ void getPositionAndNormal(out vec4 position, out vec3 normal)
position.z = dot(p, matrixPalette3);
position.w = p.w;
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
vec4 n = vec4(a_normal, 0.0);
normal.x = dot(n, matrixPalette1);
normal.y = dot(n, matrixPalette2);
@ -129,21 +152,21 @@ void main()
getPositionAndNormal(position, normal);
vec4 ePosition = CC_MVMatrix * position;
\n#if (CC_MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_POINT_LIGHT_NUM; ++i)
\n#if (MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = CC_PointLightSourcePosition[i].xyz- ePosition.xyz;
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz;
}
\n#endif\n
\n#if (CC_MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_SPOT_LIGHT_NUM; ++i)
\n#if (MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = CC_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n
\n#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))\n
v_normal = CC_NormalMatrix * normal;
\n#endif\n

@ -1 +1 @@
Subproject commit af131ebbdefe0f34ac53f59f4ac7719879e56c5b
Subproject commit 0fd9899d8797dad6fd68d99b5dc41a5548219a97