Skip to content

Commit 7ec7276

Browse files
author
Thayer J Andrews
committed
CCEffectLighting - Directional light support
For point lights we compute the light vector from each vertex position to the light position and interpolate this across the sprite. For directional lights we set the light vector to be the inverse of the light's direction (because light direction comes "from" the light but we want a vector "to" the light). This is the same for all vertices so the direction is constant across the sprite (and therefore independent of light position).
1 parent 78af1e6 commit 7ec7276

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cocos2d/CCEffectLighting.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ -(void)buildRenderPasses
167167
GLKMatrix4 lightNodeToEffectNode = CCEffectUtilsTransformFromNodeToNode(light, pass.node, nil);
168168

169169
// Compute the light's position in the effect node's coordinate system.
170-
GLKVector4 lightPosition = GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(light.anchorPointInPoints.x, light.anchorPointInPoints.y, 500.0f, 1.0f));
170+
GLKVector4 lightVector = GLKVector4Make(0.0f, 0.0f, 0.0f, 0.0f);
171+
if (light.type == CCLightNodeDirectional)
172+
{
173+
lightVector = GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(0.0f, 1.0f, 1.0f, 0.0f));
174+
}
175+
else
176+
{
177+
lightVector = GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(light.anchorPointInPoints.x, light.anchorPointInPoints.y, 50.0f, 1.0f));
178+
}
171179

172180
// Compute the real light color based on color and intensity.
173181
GLKVector4 lightColor = GLKVector4MultiplyScalar(light.color.glkVector4, light.intensity);
@@ -176,7 +184,7 @@ -(void)buildRenderPasses
176184
NSString *lightVectorLabel = [NSString stringWithFormat:@"u_lightVector%lu", (unsigned long)lightIndex];
177185

178186
pass.shaderUniforms[weakSelf.uniformTranslationTable[lightColorLabel]] = [NSValue valueWithGLKVector4:lightColor];
179-
pass.shaderUniforms[weakSelf.uniformTranslationTable[lightVectorLabel]] = [NSValue valueWithGLKVector4:lightPosition];
187+
pass.shaderUniforms[weakSelf.uniformTranslationTable[lightVectorLabel]] = [NSValue valueWithGLKVector4:lightVector];
180188
}
181189

182190
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_globalAmbientColor"]] = [NSValue valueWithGLKVector4:globalAmbientColor];

0 commit comments

Comments
 (0)