@@ -105,8 +105,9 @@ +(NSMutableArray *)buildFragmentFunctionsWithLights:(NSArray*)lights normalMap:(
105105 NSMutableString *effectBody = [[NSMutableString alloc ] init ];
106106 [effectBody appendString: CC_GLSL (
107107 vec4 lightColor;
108- vec4 diffuseLightColor = u_globalAmbientColor;
109- vec4 specularLightColor = vec4 (0 ,0 ,0 ,0 );
108+ vec4 lightSpecularColor;
109+ vec4 diffuseSum = u_globalAmbientColor;
110+ vec4 specularSum = vec4 (0 ,0 ,0 ,0 );
110111
111112 vec3 tangentSpaceLightDir;
112113 vec3 halfAngleDir;
@@ -146,28 +147,30 @@ +(NSMutableArray *)buildFragmentFunctionsWithLights:(NSArray*)lights normalMap:(
146147 {
147148 [effectBody appendFormat: @" tangentSpaceLightDir = v_tangentSpaceLightDir%lu .xyz;\n " , (unsigned long )lightIndex];
148149 [effectBody appendFormat: @" lightColor = u_lightColor%lu ;\n " , (unsigned long )lightIndex];
150+ [effectBody appendFormat: @" lightSpecularColor = u_lightSpecularColor%lu ;\n " , (unsigned long )lightIndex];
149151 }
150152 else
151153 {
152154 [effectBody appendFormat: @" tangentSpaceLightDir = normalize(v_tangentSpaceLightDir%lu .xyz);\n " , (unsigned long )lightIndex];
153155 [effectBody appendFormat: @" lightDist = length(v_tangentSpaceLightDir%lu .xyz);\n " , (unsigned long )lightIndex];
154156 [effectBody appendFormat: @" falloffTerm = max(0.0, (1.0 - lightDist * u_lightFalloff%lu ));\n " , (unsigned long )lightIndex];
155157 [effectBody appendFormat: @" lightColor = u_lightColor%lu * falloffTerm;\n " , (unsigned long )lightIndex];
158+ [effectBody appendFormat: @" lightSpecularColor = u_lightSpecularColor%lu * falloffTerm;\n " , (unsigned long )lightIndex];
156159 }
157160 [effectBody appendString: @" diffuseTerm = max(0.0, dot(tangentSpaceNormal, tangentSpaceLightDir));\n " ];
158- [effectBody appendString: @" diffuseLightColor += lightColor * diffuseTerm;\n " ];
161+ [effectBody appendString: @" diffuseSum += lightColor * diffuseTerm;\n " ];
159162
160163 if (needsSpecular)
161164 {
162165 [effectBody appendString: @" halfAngleDir = (2.0 * dot(tangentSpaceLightDir, tangentSpaceNormal) * tangentSpaceNormal - tangentSpaceLightDir);\n " ];
163166 [effectBody appendString: @" specularTerm = max(0.0, dot(halfAngleDir, vec3(0,0,1))) * step(0.0, diffuseTerm);\n " ];
164- [effectBody appendString: @" specularLightColor += lightColor * pow(specularTerm, u_specularExponent);\n " ];
167+ [effectBody appendString: @" specularSum += lightSpecularColor * pow(specularTerm, u_specularExponent);\n " ];
165168 }
166169 }
167- [effectBody appendString: @" vec4 resultColor = diffuseLightColor * inputValue;\n " ];
170+ [effectBody appendString: @" vec4 resultColor = diffuseSum * inputValue;\n " ];
168171 if (needsSpecular)
169172 {
170- [effectBody appendString: @" resultColor += specularLightColor * u_specularColor;\n " ];
173+ [effectBody appendString: @" resultColor += specularSum * u_specularColor;\n " ];
171174 }
172175 [effectBody appendString: @" return vec4(resultColor.xyz, inputValue.a);\n " ];
173176
@@ -249,6 +252,14 @@ -(void)buildRenderPasses
249252
250253 NSString *lightVectorLabel = [NSString stringWithFormat: @" u_lightVector%lu " , (unsigned long )lightIndex];
251254 pass.shaderUniforms [weakSelf.uniformTranslationTable[lightVectorLabel]] = [NSValue valueWithGLKVector4: lightVector];
255+
256+ if (self.needsSpecular )
257+ {
258+ GLKVector4 lightSpecularColor = GLKVector4MultiplyScalar (light.specularColor .glkVector4 , light.specularIntensity );
259+
260+ NSString *lightSpecularColorLabel = [NSString stringWithFormat: @" u_lightSpecularColor%lu " , (unsigned long )lightIndex];
261+ pass.shaderUniforms [weakSelf.uniformTranslationTable[lightSpecularColorLabel]] = [NSValue valueWithGLKVector4: lightSpecularColor];
262+ }
252263 }
253264
254265 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_globalAmbientColor" ]] = [NSValue valueWithGLKVector4: globalAmbientColor];
@@ -290,6 +301,10 @@ - (CCEffectPrepareStatus)prepareForRenderingWithSprite:(CCSprite *)sprite
290301
291302 [vertUniforms addObject: [CCEffectUniform uniform: @" vec4" name: [NSString stringWithFormat: @" u_lightVector%lu " , (unsigned long )lightIndex] value: [NSValue valueWithGLKVector4: GLKVector4Make (0 .0f , 0 .0f , 0 .0f , 1 .0f )]]];
292303 [fragUniforms addObject: [CCEffectUniform uniform: @" vec4" name: [NSString stringWithFormat: @" u_lightColor%lu " , (unsigned long )lightIndex] value: [NSValue valueWithGLKVector4: GLKVector4Make (1 .0f , 1 .0f , 1 .0f , 1 .0f )]]];
304+ if (self.needsSpecular )
305+ {
306+ [fragUniforms addObject: [CCEffectUniform uniform: @" vec4" name: [NSString stringWithFormat: @" u_lightSpecularColor%lu " , (unsigned long )lightIndex] value: [NSValue valueWithGLKVector4: GLKVector4Make (1 .0f , 1 .0f , 1 .0f , 1 .0f )]]];
307+ }
293308
294309 if (light.type != CCLightDirectional)
295310 {
0 commit comments