Skip to content

Commit 7d57810

Browse files
author
Thayer J Andrews
committed
CCEffectLighting + CCLightNode - Initial support for point light falloff
The user can now specify a radius which is the distance at which a light's intensity has fallen off to 0%. Currently the falloff is linear and is disabled when the radius is set to values <= 0.0.
1 parent 616b4c4 commit 7d57810

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

cocos2d/CCEffectLighting.m

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

cocos2d/CCLightNode.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ typedef NS_ENUM(NSUInteger, CCLightType)
6767
*/
6868
@property (nonatomic, assign) float ambientIntensity;
6969

70+
/** The radius of influence of a point light. When the distance from a sprite to this light
71+
* is less than or equal to the radius, the sprite will be lit by this light. If the
72+
* distance is greater, the sprite will not be lit by this light. This distance is
73+
* measured in points. This propery has no effect on directional lights.
74+
*/
75+
@property (nonatomic, assign) float cutoffRadius;
76+
7077

7178

7279
/// -----------------------------------------------------------------------

cocos2d/CCLightNode.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ -(id)initWithType:(CCLightType)type color:(CCColor *)color intensity:(float)inte
2626
_intensity = intensity;
2727
_ambientColor = ambientColor;
2828
_ambientIntensity = ambientIntensity;
29+
_cutoffRadius = 0.0f;
2930
}
3031

3132
return self;

0 commit comments

Comments
 (0)