Skip to content

Commit 0908c6b

Browse files
author
Thayer J Andrews
committed
CCEffectLighting + CCLightNode - Light depth
Even though cocos2d scenes, nodes, and sprites are two dimensional, it is unavoidable that some lighting math involves a third dimension. Because normal maps define normal vectors in three dimensions, we need a way to specify a virtual Z value for our lights or otherwise all lights will appear to be coplanar with the sprites they are lighting. In this case all lighting would appear to be from the side (or above or below) and would be unpleasantly harsh looking. This change adds support for a light Z coordinate by adding a depth property (I've avoided using the name z or Z since this name is already used for render order sorting).
1 parent 58d38c8 commit 0908c6b

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

cocos2d/CCEffectLighting.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ -(void)buildRenderPasses
193193
GLKVector4 lightVector = GLKVector4Make(0.0f, 0.0f, 0.0f, 0.0f);
194194
if (light.type == CCLightDirectional)
195195
{
196-
lightVector = GLKVector4Normalize(GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(0.0f, 1.0f, 1.0f, 0.0f)));
196+
lightVector = GLKVector4Normalize(GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(0.0f, 1.0f, light.depth, 0.0f)));
197197
}
198198
else
199199
{
200-
lightVector = GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(light.anchorPointInPoints.x, light.anchorPointInPoints.y, 500.0f, 1.0f));
200+
lightVector = GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(light.anchorPointInPoints.x, light.anchorPointInPoints.y, light.depth, 1.0f));
201201

202202
float falloff = (light.cutoffRadius > 0.0f) ? 1.0f / light.cutoffRadius : 0.0f;
203203
NSString *lightFalloffLabel = [NSString stringWithFormat:@"u_lightFalloff%lu", (unsigned long)lightIndex];

cocos2d/CCLightNode.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ typedef NS_ENUM(NSUInteger, CCLightType)
7474
*/
7575
@property (nonatomic, assign) float cutoffRadius;
7676

77+
/** The light's depth value within the scene. This value is independent of the node
78+
* z-order which is used for sorting and is instead used by the lighting equations
79+
* when computing the light's direction vector relative to the nodes it is lighting.
80+
* Only values greater than or equal to 0 are valid. A depth value of 0 makes the light
81+
* coplanar with any nodes it is lighting which results in a very hard looking side light.
82+
* Increasingly positive values move the light farther and farther out of the plane of
83+
* the lit nodes resulting in a softer looking front light.
84+
*/
85+
@property (nonatomic, assign) float depth;
86+
7787

7888

7989
/// -----------------------------------------------------------------------

cocos2d/CCLightNode.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ -(id)initWithType:(CCLightType)type color:(CCColor *)color intensity:(float)inte
2727
_ambientColor = ambientColor;
2828
_ambientIntensity = ambientIntensity;
2929
_cutoffRadius = 0.0f;
30+
if (type == CCLightDirectional)
31+
{
32+
_depth = 1.0f;
33+
}
34+
else
35+
{
36+
_depth = 500.0f;
37+
}
3038
}
3139

3240
return self;

0 commit comments

Comments
 (0)