@@ -53,9 +53,16 @@ -(id)initWithLights:(NSArray *)lights
5353
5454 for (NSUInteger lightIndex = 0 ; lightIndex < lights.count ; lightIndex++)
5555 {
56+ CCLightNode *light = lights[lightIndex];
57+
5658 [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 )]]];
5759 [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 )]]];
5860
61+ if (light.type != CCLightDirectional)
62+ {
63+ [fragUniforms addObject: [CCEffectUniform uniform: @" float" name: [NSString stringWithFormat: @" u_lightFalloff%lu " , (unsigned long )lightIndex] value: [NSNumber numberWithFloat: 1 .0f ]]];
64+ }
65+
5966 [varyings addObject: [CCEffectVarying varying: @" vec4" name: [NSString stringWithFormat: @" v_tangentSpaceLightDir%lu " , (unsigned long )lightIndex]]];
6067 }
6168
@@ -102,12 +109,28 @@ +(NSMutableArray *)buildFragmentFunctionsWithLights:(NSArray*)lights
102109 {
103110 return vec4 (0 ,0 ,0 ,0 );
104111 }
112+
113+ vec4 lightColor;
114+ vec4 tangentSpaceLightDir;
105115 vec4 resultColor = u_globalAmbientColor;
116+ float lightDist;
106117 )];
107118
108119 for (NSUInteger lightIndex = 0 ; lightIndex < lights.count ; lightIndex++)
109120 {
110- [effectBody appendFormat: @" resultColor += u_lightColor%lu * dot(tangentSpaceNormal, v_tangentSpaceLightDir%lu );\n " , (unsigned long )lightIndex, (unsigned long )lightIndex];
121+ CCLightNode *light = lights[lightIndex];
122+ if (light.type == CCLightDirectional)
123+ {
124+ [effectBody appendFormat: @" tangentSpaceLightDir = v_tangentSpaceLightDir%lu ;\n " , (unsigned long )lightIndex];
125+ [effectBody appendFormat: @" lightColor = u_lightColor%lu ;\n " , (unsigned long )lightIndex];
126+ }
127+ else
128+ {
129+ [effectBody appendFormat: @" tangentSpaceLightDir = normalize(v_tangentSpaceLightDir%lu );\n " , (unsigned long )lightIndex];
130+ [effectBody appendFormat: @" lightDist = length(v_tangentSpaceLightDir%lu );\n " , (unsigned long )lightIndex];
131+ [effectBody appendFormat: @" lightColor = u_lightColor%lu * max(0.0, (1.0 - lightDist * u_lightFalloff%lu ));\n " , (unsigned long )lightIndex, (unsigned long )lightIndex];
132+ }
133+ [effectBody appendFormat: @" resultColor += lightColor * max(0.0, dot(tangentSpaceNormal, tangentSpaceLightDir));\n " ];
111134 }
112135 [effectBody appendString: @" return resultColor * inputValue;\n " ];
113136
@@ -128,7 +151,7 @@ +(NSMutableArray *)buildVertexFunctionsWithLights:(NSArray*)lights
128151 }
129152 else
130153 {
131- [effectBody appendFormat: @" v_tangentSpaceLightDir%lu = normalize( u_lightVector%lu - u_ndcToTangentSpace * cc_Position) ;" , (unsigned long )lightIndex, (unsigned long )lightIndex];
154+ [effectBody appendFormat: @" v_tangentSpaceLightDir%lu = u_lightVector%lu - u_ndcToTangentSpace * cc_Position;" , (unsigned long )lightIndex, (unsigned long )lightIndex];
132155 }
133156 }
134157 [effectBody appendString: @" return cc_Position;" ];
@@ -175,15 +198,19 @@ -(void)buildRenderPasses
175198 else
176199 {
177200 lightVector = GLKMatrix4MultiplyVector4 (lightNodeToEffectNode, GLKVector4Make (light.anchorPointInPoints .x , light.anchorPointInPoints .y , 500 .0f , 1 .0f ));
201+
202+ float falloff = (light.cutoffRadius > 0 .0f ) ? 1 .0f / light.cutoffRadius : 0 .0f ;
203+ NSString *lightFalloffLabel = [NSString stringWithFormat: @" u_lightFalloff%lu " , (unsigned long )lightIndex];
204+ pass.shaderUniforms [weakSelf.uniformTranslationTable[lightFalloffLabel]] = [NSNumber numberWithFloat: falloff];
178205 }
179206
180207 // Compute the real light color based on color and intensity.
181208 GLKVector4 lightColor = GLKVector4MultiplyScalar (light.color .glkVector4 , light.intensity );
182209
183210 NSString *lightColorLabel = [NSString stringWithFormat: @" u_lightColor%lu " , (unsigned long )lightIndex];
184- NSString *lightVectorLabel = [NSString stringWithFormat: @" u_lightVector%lu " , (unsigned long )lightIndex];
185-
186211 pass.shaderUniforms [weakSelf.uniformTranslationTable[lightColorLabel]] = [NSValue valueWithGLKVector4: lightColor];
212+
213+ NSString *lightVectorLabel = [NSString stringWithFormat: @" u_lightVector%lu " , (unsigned long )lightIndex];
187214 pass.shaderUniforms [weakSelf.uniformTranslationTable[lightVectorLabel]] = [NSValue valueWithGLKVector4: lightVector];
188215 }
189216
0 commit comments