@@ -85,36 +85,49 @@ +(NSMutableArray *)buildFragmentFunctionsWithLights:(NSArray*)lights
85
85
[effectBody appendString: CC_GLSL (
86
86
// Index the normal map and expand the color value from [0..1] to [-1..1]
87
87
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 ) ;
89
89
90
90
if (normalMap.a == 0.0 )
91
91
{
92
92
return vec4 (0 ,0 ,0 ,0 );
93
93
}
94
94
95
95
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
+
98
102
float lightDist;
103
+ float falloffTerm;
104
+ float diffuseTerm;
105
+ float specularTerm;
99
106
)];
100
107
101
108
for (NSUInteger lightIndex = 0 ; lightIndex < lights.count ; lightIndex++)
102
109
{
103
110
CCLightNode *light = lights[lightIndex];
104
111
if (light.type == CCLightDirectional)
105
112
{
106
- [effectBody appendFormat: @" tangentSpaceLightDir = v_tangentSpaceLightDir%lu ;\n " , (unsigned long )lightIndex];
113
+ [effectBody appendFormat: @" tangentSpaceLightDir = v_tangentSpaceLightDir%lu .xyz ;\n " , (unsigned long )lightIndex];
107
114
[effectBody appendFormat: @" lightColor = u_lightColor%lu ;\n " , (unsigned long )lightIndex];
108
115
}
109
116
else
110
117
{
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];
114
122
}
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 " ];
116
129
}
117
- [effectBody appendString: @" return resultColor * inputValue;\n " ];
130
+ [effectBody appendString: @" return diffuseLightColor * inputValue + specularLightColor ;\n " ];
118
131
119
132
CCEffectFunction* fragmentFunction = [[CCEffectFunction alloc ] initWithName: @" lightingEffectFrag" body: effectBody inputs: @[input] returnType: @" vec4" ];
120
133
return [NSMutableArray arrayWithObject: fragmentFunction];
0 commit comments