Skip to content

Commit 85212fc

Browse files
author
Thayer J Andrews
committed
CCEffectLighting - Add ambient color support
And add ambient color and intensity to CCLightNode. This also introduces the idea of light types (point, spot, directional) but this isn't actually implemented in the effect yet.
1 parent a1ff8af commit 85212fc

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

cocos2d/CCEffectLighting.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ -(id)init
2929
return [self initWithLight:nil];
3030
}
3131

32-
-(id)initWithLight:(CCNode *)light
32+
-(id)initWithLight:(CCLightNode *)light
3333
{
3434
NSArray *fragUniforms = @[
3535
[CCEffectUniform uniform:@"vec4" name:@"u_lightColor" value:[NSValue valueWithGLKVector4:GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)]],
36+
[CCEffectUniform uniform:@"vec4" name:@"u_ambientColor" value:[NSValue valueWithGLKVector4:GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)]],
3637
];
3738

3839
NSArray *vertUniforms = @[
@@ -53,7 +54,7 @@ -(id)initWithLight:(CCNode *)light
5354
return self;
5455
}
5556

56-
+(id)effectWithLight:(CCNode *)light
57+
+(id)effectWithLight:(CCLightNode *)light
5758
{
5859
return [[self alloc] initWithLight:light];
5960
}
@@ -72,7 +73,7 @@ -(void)buildFragmentFunctions
7273
float NdotL = dot(tangentSpaceNormal, v_tangentSpaceLightDir);
7374
if (normalMap.a > 0.0)
7475
{
75-
return inputValue * u_lightColor * NdotL;
76+
return inputValue * (u_lightColor * NdotL + u_ambientColor);
7677
}
7778
else
7879
{
@@ -123,9 +124,11 @@ -(void)buildRenderPasses
123124
GLKVector4 lightPosition = GLKMatrix4MultiplyVector4(lightNodeToEffectNode, GLKVector4Make(weakSelf.light.anchorPointInPoints.x, weakSelf.light.anchorPointInPoints.y, 500.0f, 1.0f));
124125

125126
GLKVector4 lightColor = GLKVector4MultiplyScalar(weakSelf.light.color.glkVector4, weakSelf.light.intensity);
127+
GLKVector4 ambientColor = GLKVector4MultiplyScalar(weakSelf.light.ambientColor.glkVector4, weakSelf.light.ambientIntensity);
126128

127129
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_lightColor"]] = [NSValue valueWithGLKVector4:lightColor];
128130
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_lightPosition"]] = [NSValue valueWithGLKVector4:lightPosition];
131+
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_ambientColor"]] = [NSValue valueWithGLKVector4:ambientColor];
129132
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_ndcToTangentSpace"]] = [NSValue valueWithGLKMatrix4:pass.ndcToNodeLocal];
130133

131134
} copy]];

cocos2d/CCLightNode.h

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
#import "CCNode.h"
1010

11+
12+
typedef NS_ENUM(NSUInteger, CCLightNodeType)
13+
{
14+
CCLightNodePoint = 0,
15+
CCLightNodeDirectional = 1,
16+
CCLightNodeSpot = 2,
17+
};
18+
1119
/**
1220
* CCLightNode allows the user to define lights that will be used with
1321
* CCEffectLighting.
@@ -19,27 +27,54 @@
1927
/// @name Accessing Node Attributes
2028
/// -----------------------------------------------------------------------
2129

22-
/** The color of the light. As described below, the color is modulated by the
30+
/** The type of the light. The contribution of point lights is dependent on
31+
* the relative positions of the light and the node it is lighting. The
32+
* contribution of directional lights is only dependent on the light's
33+
* orientation (as if it is infinitely far away). Spot lights behave like
34+
* point lights but they also have a direction vector and cutoff angle.
35+
* If the angle between the light's direction vector and the vector from
36+
* the light to the node exceeds the cutoff angle then the light no longer
37+
* contributes to the lighting of the node.
38+
*/
39+
@property (nonatomic, assign) CCLightNodeType type;
40+
41+
/** The primary color of the light. As described below, the color is modulated by the
2342
* intensity value to determine the contribution of the light to the lighting
24-
* effect.
43+
* effect. This color is used when computing the light's position and orientation
44+
* dependent contribution to the lighting effect.
2545
*/
2646
@property (nonatomic, strong) CCColor* color;
2747

28-
/** The overall brightness of the light. This value is in the range [0..1] with
29-
* 0 resulting in no contribution from this light in the final image (the light
48+
/** The brightness of the light's primary color. This value is in the range [0..1]
49+
* with 0 resulting in no contribution from this light in the final image (the light
3050
* effectively becomes black) and 1 resulting in full contribution from this
31-
* light (the light's full color contributes).
51+
* light.
3252
*/
3353
@property (nonatomic, assign) float intensity;
3454

55+
/** The ambient color of the light. As described below, the color is modulated by the
56+
* ambient intensity value to determine the contribution of the light to the lighting
57+
* effect. The ambient color contributes to the lighting effect independent of the light's
58+
* position and orientation relative to the affected node.
59+
*/
60+
@property (nonatomic, strong) CCColor* ambientColor;
61+
62+
/** The brightness of the light's ambient color. This value is in the range [0..1]
63+
* with 0 resulting in no contribution from the ambient color to the final image
64+
* (the ambient color effectively becomes black) and 1 resulting in full contribution
65+
* from this light.
66+
*/
67+
@property (nonatomic, assign) float ambientIntensity;
68+
69+
3570

3671
/// -----------------------------------------------------------------------
3772
/// @name Initializing a CCLightNode object
3873
/// -----------------------------------------------------------------------
3974

4075

4176
/**
42-
* Initializes a CCLightNode object with a white color and full intensity.
77+
* Initializes a point light with a white color and full intensity.
4378
*
4479
* @return The CCLightNode object.
4580
*/
@@ -48,9 +83,15 @@
4883
/**
4984
* Initializes a CCLightNode object with the specified parameters.
5085
*
86+
* @param type The type of the light.
87+
* @param color The primary color of the light.
88+
* @param intensity The brightness of the light's primary color.
89+
* @param ambientColor The ambient color of the light.
90+
* @param ambientIntensity The brightness of the light's ambient color.
91+
*
5192
* @return The CCLighttNode object.
5293
*/
53-
-(id)initWithColor:(CCColor *)color intensity:(float)intensity;
94+
-(id)initWithType:(CCLightNodeType)type color:(CCColor *)color intensity:(float)intensity ambientColor:(CCColor *)ambientColor ambientIntensity:(float)ambientIntensity;
5495

5596

5697
/// -----------------------------------------------------------------------
@@ -60,12 +101,15 @@
60101
/**
61102
* Creates a CCLightNode object with the specified parameters.
62103
*
63-
* @param color Color of the light.
64-
* @param intensity The overall brightness of the light.
104+
* @param type The type of the light.
105+
* @param color The primary color of the light.
106+
* @param intensity The brightness of the light's primary color.
107+
* @param ambientColor The ambient color of the light.
108+
* @param ambientIntensity The brightness of the light's ambient color.
65109
*
66110
* @return An initialized CCLightNode object.
67111
*/
68-
+(id)lightWithColor:(CCColor *)color intensity:(float)intensity;
112+
+(id)lightWithType:(CCLightNodeType)type color:(CCColor *)color intensity:(float)intensity ambientColor:(CCColor *)ambientColor ambientIntensity:(float)ambientIntensity;
69113

70114

71115
@end

cocos2d/CCLightNode.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@
88

99
#import "CCLightNode.h"
1010

11+
1112
@implementation CCLightNode
1213

13-
-(id)initWithColor:(CCColor *)color intensity:(float)intensity
14+
-(id)init
15+
{
16+
return [self initWithType:CCLightNodePoint color:[CCColor whiteColor] intensity:1.0f ambientColor:[CCColor whiteColor] ambientIntensity:0.5f];
17+
}
18+
19+
20+
-(id)initWithType:(CCLightNodeType)type color:(CCColor *)color intensity:(float)intensity ambientColor:(CCColor *)ambientColor ambientIntensity:(float)ambientIntensity
1421
{
1522
if ((self = [super init]))
1623
{
24+
_type = type;
1725
_color = color.ccColor4f;
1826
_intensity = intensity;
27+
_ambientColor = ambientColor;
28+
_ambientIntensity = ambientIntensity;
1929
}
2030

2131
return self;
2232
}
2333

24-
+(id)lightWithColor:(CCColor *)color intensity:(float)intensity
34+
+(id)lightWithType:(CCLightNodeType)type color:(CCColor *)color intensity:(float)intensity ambientColor:(CCColor *)ambientColor ambientIntensity:(float)ambientIntensity
2535
{
26-
return [[self alloc] initWithColor:color intensity:intensity];
36+
return [[self alloc] initWithType:type color:color intensity:intensity ambientColor:ambientColor ambientIntensity:ambientIntensity];
2737
}
2838

29-
3039
@end

0 commit comments

Comments
 (0)