@@ -53,9 +53,16 @@ -(id)initWithLights:(NSArray *)lights
53
53
54
54
for (NSUInteger lightIndex = 0 ; lightIndex < lights.count ; lightIndex++)
55
55
{
56
+ CCLightNode *light = lights[lightIndex];
57
+
56
58
[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 )]]];
57
59
[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 )]]];
58
60
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
+
59
66
[varyings addObject: [CCEffectVarying varying: @" vec4" name: [NSString stringWithFormat: @" v_tangentSpaceLightDir%lu " , (unsigned long )lightIndex]]];
60
67
}
61
68
@@ -102,12 +109,28 @@ +(NSMutableArray *)buildFragmentFunctionsWithLights:(NSArray*)lights
102
109
{
103
110
return vec4 (0 ,0 ,0 ,0 );
104
111
}
112
+
113
+ vec4 lightColor;
114
+ vec4 tangentSpaceLightDir;
105
115
vec4 resultColor = u_globalAmbientColor;
116
+ float lightDist;
106
117
)];
107
118
108
119
for (NSUInteger lightIndex = 0 ; lightIndex < lights.count ; lightIndex++)
109
120
{
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 " ];
111
134
}
112
135
[effectBody appendString: @" return resultColor * inputValue;\n " ];
113
136
@@ -128,7 +151,7 @@ +(NSMutableArray *)buildVertexFunctionsWithLights:(NSArray*)lights
128
151
}
129
152
else
130
153
{
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];
132
155
}
133
156
}
134
157
[effectBody appendString: @" return cc_Position;" ];
@@ -175,15 +198,19 @@ -(void)buildRenderPasses
175
198
else
176
199
{
177
200
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];
178
205
}
179
206
180
207
// Compute the real light color based on color and intensity.
181
208
GLKVector4 lightColor = GLKVector4MultiplyScalar (light.color .glkVector4 , light.intensity );
182
209
183
210
NSString *lightColorLabel = [NSString stringWithFormat: @" u_lightColor%lu " , (unsigned long )lightIndex];
184
- NSString *lightVectorLabel = [NSString stringWithFormat: @" u_lightVector%lu " , (unsigned long )lightIndex];
185
-
186
211
pass.shaderUniforms [weakSelf.uniformTranslationTable[lightColorLabel]] = [NSValue valueWithGLKVector4: lightColor];
212
+
213
+ NSString *lightVectorLabel = [NSString stringWithFormat: @" u_lightVector%lu " , (unsigned long )lightIndex];
187
214
pass.shaderUniforms [weakSelf.uniformTranslationTable[lightVectorLabel]] = [NSValue valueWithGLKVector4: lightVector];
188
215
}
189
216
0 commit comments