@@ -85,36 +85,49 @@ +(NSMutableArray *)buildFragmentFunctionsWithLights:(NSArray*)lights
8585 [effectBody appendString: CC_GLSL (
8686 // Index the normal map and expand the color value from [0..1] to [-1..1]
8787 vec4 normalMap = texture2D (cc_NormalMapTexture, cc_FragTexCoord2);
88- vec4 tangentSpaceNormal = normalMap * 2.0 - 1.0 ;
88+ vec3 tangentSpaceNormal = normalize ( normalMap.xyz * 2.0 - 1.0 ) ;
8989
9090 if (normalMap.a == 0.0 )
9191 {
9292 return vec4 (0 ,0 ,0 ,0 );
9393 }
9494
9595 vec4 lightColor;
96- vec4 tangentSpaceLightDir;
97- vec4 resultColor = u_globalAmbientColor;
96+ vec4 diffuseLightColor = u_globalAmbientColor;
97+ vec4 specularLightColor = vec4 (0 ,0 ,0 ,1 );
98+
99+ vec3 tangentSpaceLightDir;
100+ vec3 halfAngleDir;
101+
98102 float lightDist;
103+ float falloffTerm;
104+ float diffuseTerm;
105+ float specularTerm;
99106 )];
100107
101108 for (NSUInteger lightIndex = 0 ; lightIndex < lights.count ; lightIndex++)
102109 {
103110 CCLightNode *light = lights[lightIndex];
104111 if (light.type == CCLightDirectional)
105112 {
106- [effectBody appendFormat: @" tangentSpaceLightDir = v_tangentSpaceLightDir%lu ;\n " , (unsigned long )lightIndex];
113+ [effectBody appendFormat: @" tangentSpaceLightDir = v_tangentSpaceLightDir%lu .xyz ;\n " , (unsigned long )lightIndex];
107114 [effectBody appendFormat: @" lightColor = u_lightColor%lu ;\n " , (unsigned long )lightIndex];
108115 }
109116 else
110117 {
111- [effectBody appendFormat: @" tangentSpaceLightDir = normalize(v_tangentSpaceLightDir%lu );\n " , (unsigned long )lightIndex];
112- [effectBody appendFormat: @" lightDist = length(v_tangentSpaceLightDir%lu );\n " , (unsigned long )lightIndex];
113- [effectBody appendFormat: @" lightColor = u_lightColor%lu * max(0.0, (1.0 - lightDist * u_lightFalloff%lu ));\n " , (unsigned long )lightIndex, (unsigned long )lightIndex];
118+ [effectBody appendFormat: @" tangentSpaceLightDir = normalize(v_tangentSpaceLightDir%lu .xyz);\n " , (unsigned long )lightIndex];
119+ [effectBody appendFormat: @" lightDist = length(v_tangentSpaceLightDir%lu .xyz);\n " , (unsigned long )lightIndex];
120+ [effectBody appendFormat: @" falloffTerm = max(0.0, (1.0 - lightDist * u_lightFalloff%lu ));\n " , (unsigned long )lightIndex];
121+ [effectBody appendFormat: @" lightColor = u_lightColor%lu * falloffTerm;\n " , (unsigned long )lightIndex];
114122 }
115- [effectBody appendFormat: @" resultColor += lightColor * max(0.0, dot(tangentSpaceNormal, tangentSpaceLightDir));\n " ];
123+ [effectBody appendString: @" diffuseTerm = max(0.0, dot(tangentSpaceNormal, tangentSpaceLightDir));\n " ];
124+ [effectBody appendString: @" diffuseLightColor += lightColor * diffuseTerm;\n " ];
125+
126+ [effectBody appendString: @" halfAngleDir = (2.0 * dot(tangentSpaceLightDir, tangentSpaceNormal) * tangentSpaceNormal - tangentSpaceLightDir);\n " ];
127+ [effectBody appendString: @" specularTerm = max(0.0, dot(halfAngleDir, vec3(0,0,1))) * step(0.0, diffuseTerm);\n " ];
128+ [effectBody appendString: @" specularLightColor += lightColor * pow(specularTerm, 10.0);\n " ];
116129 }
117- [effectBody appendString: @" return resultColor * inputValue;\n " ];
130+ [effectBody appendString: @" return diffuseLightColor * inputValue + specularLightColor ;\n " ];
118131
119132 CCEffectFunction* fragmentFunction = [[CCEffectFunction alloc ] initWithName: @" lightingEffectFrag" body: effectBody inputs: @[input] returnType: @" vec4" ];
120133 return [NSMutableArray arrayWithObject: fragmentFunction];
0 commit comments