Skip to content

Commit 9d5a832

Browse files
committed
Merge pull request #1025 from thayerandrews/develop
CCEffectLighting
2 parents 426e6a6 + 5b2dcd8 commit 9d5a832

16 files changed

+914
-12
lines changed

Resources/Images/ShinyTorusColor.png

-57.4 KB
Loading

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,246 @@ - (void)enableOutline:(id)sender
249249
_distanceFieldEffect.outline = !_distanceFieldEffect.outline;
250250
}
251251

252+
-(void)setupSimpleLightingTest
253+
{
254+
self.subTitle = @"Simple Lighting Test";
255+
256+
NSString *normalMapImage = @"Images/ShinyTorusNormals.png";
257+
NSString *diffuseImage = @"Images/ShinyTorusColor.png";
258+
259+
void (^setupBlock)(CGPoint position, CCLightType type, float lightDepth, NSString *title) = ^void(CGPoint position, CCLightType type, float lightDepth, NSString *title)
260+
{
261+
CCLightNode *light = [[CCLightNode alloc] init];
262+
light.type = type;
263+
light.positionType = CCPositionTypeNormalized;
264+
light.position = ccp(0.5f, 0.5f);
265+
light.anchorPoint = ccp(0.5f, 0.5f);
266+
light.intensity = 1.0f;
267+
light.ambientIntensity = 0.2f;
268+
light.cutoffRadius = 0.0f;
269+
light.depth = lightDepth;
270+
271+
CCSprite *lightSprite = [CCSprite spriteWithImageNamed:@"Images/snow.png"];
272+
273+
CCEffectLighting *lightingEffect = [[CCEffectLighting alloc] initWithLights:@[light]];
274+
lightingEffect.shininess = 10.0f;
275+
276+
CCSprite *sprite = [CCSprite spriteWithImageNamed:diffuseImage];
277+
sprite.positionType = CCPositionTypeNormalized;
278+
sprite.position = ccp(0.5f, 0.5f);
279+
sprite.normalMapSpriteFrame = [CCSpriteFrame frameWithImageNamed:normalMapImage];
280+
sprite.effect = lightingEffect;
281+
sprite.scale = 0.5f;
282+
283+
CCNode *root = [[CCNode alloc] init];
284+
root.positionType = CCPositionTypeNormalized;
285+
root.position = position;
286+
root.anchorPoint = ccp(0.5f, 0.5f);
287+
root.contentSizeType = CCSizeTypePoints;
288+
root.contentSize = CGSizeMake(200.0f, 200.0f);
289+
290+
CCLabelTTF *label = [CCLabelTTF labelWithString:title fontName:@"HelveticaNeue-Light" fontSize:12 * [CCDirector sharedDirector].UIScaleFactor];
291+
label.color = [CCColor whiteColor];
292+
label.positionType = CCPositionTypeNormalized;
293+
label.position = ccp(0.5f, 1.0f);
294+
label.horizontalAlignment = CCTextAlignmentCenter;
295+
296+
[self.contentNode addChild:root];
297+
[root addChild:label];
298+
[root addChild:sprite];
299+
[root addChild:light];
300+
[light addChild:lightSprite];
301+
302+
[light runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
303+
[CCActionMoveTo actionWithDuration:1.0 position:ccp(1.0f, 1.0f)],
304+
[CCActionMoveTo actionWithDuration:2.0 position:ccp(0.0f, 0.0f)],
305+
[CCActionRotateBy actionWithDuration:5.0 angle:360.0],
306+
[CCActionMoveTo actionWithDuration:1.0 position:ccp(0.5f, 0.5f)],
307+
nil
308+
]]];
309+
};
310+
setupBlock(ccp(0.25f, 0.5f), CCLightPoint, 250.0f, @"Point Light\nPosition matters, orientation does not.");
311+
setupBlock(ccp(0.75f, 0.5f), CCLightDirectional, 1.0f, @"Directional Light\nPosition does not matter, orientation does.");
312+
}
313+
314+
-(void)setupLightingParameterTest
315+
{
316+
self.subTitle = @"Lighting Parameter Test";
317+
318+
NSString *normalMapImage = @"Images/ShinyTorusNormals.png";
319+
NSString *diffuseImage = @"Images/ShinyTorusColor.png";
320+
321+
CCLightNode* (^setupBlock)(CGPoint position, NSString *title, CCAction *action) = ^CCLightNode*(CGPoint position, NSString *title, CCAction *action)
322+
{
323+
CCLightNode *light = [[CCLightNode alloc] init];
324+
light.positionType = CCPositionTypeNormalized;
325+
light.position = ccp(1.0f, 1.0f);
326+
light.anchorPoint = ccp(0.5f, 0.5f);
327+
light.intensity = 1.0f;
328+
light.ambientIntensity = 0.2f;
329+
light.cutoffRadius = 0.0f;
330+
light.depth = 100.0f;
331+
332+
CCSprite *lightSprite = [CCSprite spriteWithImageNamed:@"Images/snow.png"];
333+
[light addChild:lightSprite];
334+
335+
CCEffectLighting *lightingEffect = [[CCEffectLighting alloc] initWithLights:@[light]];
336+
337+
CCSprite *sprite = [CCSprite spriteWithImageNamed:diffuseImage];
338+
sprite.positionType = CCPositionTypeNormalized;
339+
sprite.position = position;
340+
sprite.normalMapSpriteFrame = [CCSpriteFrame frameWithImageNamed:normalMapImage];
341+
sprite.effect = lightingEffect;
342+
sprite.scale = 0.3f;
343+
344+
[self.contentNode addChild:sprite];
345+
346+
[sprite addChild:light];
347+
348+
CCLabelTTF *label = [CCLabelTTF labelWithString:title fontName:@"HelveticaNeue-Light" fontSize:36 * [CCDirector sharedDirector].UIScaleFactor];
349+
label.color = [CCColor whiteColor];
350+
label.positionType = CCPositionTypeNormalized;
351+
label.position = ccp(0.5f, 1.1f);
352+
label.horizontalAlignment = CCTextAlignmentCenter;
353+
354+
[sprite addChild:label];
355+
356+
if (action)
357+
{
358+
[light runAction:action];
359+
}
360+
return light;
361+
};
362+
363+
364+
CCLightNode *light = nil;
365+
CCSprite *sprite = nil;
366+
CCEffectLighting *lighting = nil;
367+
368+
light = setupBlock(ccp(0.2f, 0.65f), @"Varying Intensity", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
369+
[CCActionTween actionWithDuration:2 key:@"intensity" from:0.0f to:1.0f],
370+
[CCActionDelay actionWithDuration:2],
371+
[CCActionTween actionWithDuration:2 key:@"intensity" from:1.0f to:0.0f],
372+
nil
373+
]]);
374+
light.ambientIntensity = 0.0f;
375+
light = setupBlock(ccp(0.2f, 0.25f), @"Varying Color", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
376+
[CCActionTintTo actionWithDuration:2 color:[CCColor redColor]],
377+
[CCActionDelay actionWithDuration:1],
378+
[CCActionTintTo actionWithDuration:2 color:[CCColor greenColor]],
379+
[CCActionDelay actionWithDuration:1],
380+
[CCActionTintTo actionWithDuration:2 color:[CCColor blueColor]],
381+
[CCActionDelay actionWithDuration:1],
382+
[CCActionTintTo actionWithDuration:2 color:[CCColor whiteColor]],
383+
[CCActionDelay actionWithDuration:1],
384+
nil
385+
]]);
386+
387+
388+
light = setupBlock(ccp(0.4f, 0.65f), @"Varying Ambient Intensity", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
389+
[CCActionTween actionWithDuration:2 key:@"ambientIntensity" from:0.0f to:1.0f],
390+
[CCActionDelay actionWithDuration:2],
391+
[CCActionTween actionWithDuration:2 key:@"ambientIntensity" from:1.0f to:0.0f],
392+
nil
393+
]]);
394+
light = setupBlock(ccp(0.4f, 0.25f), @"Varying Ambient Color", nil);
395+
light.intensity = 0.5f;
396+
light.ambientIntensity = 0.5f;
397+
398+
const float timeStep = 0.017f;
399+
const float duration = 2.0f;
400+
const float delta = timeStep / duration;
401+
402+
typedef void (^AmbientLerpBlock)();
403+
typedef void (^AmbientLerpBuilderBlock)(ccColor4F deltaC);
404+
405+
__weak CCLightNode *weakLight = light;
406+
AmbientLerpBlock (^ambientLerpBuilder)(ccColor4F deltaC) = ^AmbientLerpBlock(ccColor4F deltaC)
407+
{
408+
AmbientLerpBlock lerpBlock = ^{
409+
ccColor4F c = weakLight.ambientColor.ccColor4f;
410+
c.r += deltaC.r;
411+
c.g += deltaC.g;
412+
c.b += deltaC.b;
413+
weakLight.ambientColor = [CCColor colorWithCcColor4f:c];
414+
};
415+
return lerpBlock;
416+
};
417+
418+
AmbientLerpBlock whiteRedLerp = ambientLerpBuilder(ccc4f(0.0f, -delta, -delta, 0.0f));
419+
CCActionInterval *whiteRedLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:whiteRedLerp]] times:120];
420+
421+
AmbientLerpBlock redGreenLerp = ambientLerpBuilder(ccc4f(-delta, delta, 0.0f, 0.0f));
422+
CCActionInterval *redGreenLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:redGreenLerp]] times:120];
423+
424+
AmbientLerpBlock greenBlueLerp = ambientLerpBuilder(ccc4f(0.0f, -delta, delta, 0.0f));
425+
CCActionInterval *greenBlueLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:greenBlueLerp]] times:120];
426+
427+
AmbientLerpBlock blueWhiteLerp = ambientLerpBuilder(ccc4f(delta, delta, 0.0f, 0.0f));
428+
CCActionInterval *blueWhiteLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:blueWhiteLerp]] times:120];
429+
430+
CCAction *ambientLerpAction = [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
431+
whiteRedLerpAction,
432+
[CCActionDelay actionWithDuration:1],
433+
redGreenLerpAction,
434+
[CCActionDelay actionWithDuration:1],
435+
greenBlueLerpAction,
436+
[CCActionDelay actionWithDuration:1],
437+
blueWhiteLerpAction,
438+
[CCActionDelay actionWithDuration:1],
439+
nil
440+
]];
441+
[light runAction:ambientLerpAction];
442+
443+
light = setupBlock(ccp(0.6f, 0.65f), @"Varying Cutoff", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
444+
[CCActionTween actionWithDuration:2 key:@"cutoffRadius" from:1.0f to:500.0f],
445+
[CCActionTween actionWithDuration:2 key:@"cutoffRadius" from:500.0f to:1.0f],
446+
nil
447+
]]);
448+
light.cutoffRadius = 1.0f;
449+
light = setupBlock(ccp(0.6f, 0.25f), @"Varying Depth", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
450+
[CCActionTween actionWithDuration:2 key:@"depth" from:1.0f to:500.0f],
451+
[CCActionTween actionWithDuration:2 key:@"depth" from:500.0f to:1.0f],
452+
nil
453+
]]);
454+
light.depth = 1.0f;
455+
sprite = (CCSprite *)light.parent;
456+
lighting = (CCEffectLighting *)sprite.effect;
457+
lighting.shininess = 20.0f;
458+
459+
460+
light = setupBlock(ccp(0.8f, 0.65f), @"Varying Shininess", nil);
461+
sprite = (CCSprite *)light.parent;
462+
lighting = (CCEffectLighting *)sprite.effect;
463+
lighting.shininess = 4.0f;
464+
465+
typedef void (^ShininessLerpBlock)();
466+
typedef void (^ShininessLerpBuilderBlock)(float delta);
467+
ShininessLerpBlock (^shininessLerpBuilder)(float delta) = ^ShininessLerpBlock(float delta)
468+
{
469+
ShininessLerpBlock lerpBlock = ^{
470+
lighting.shininess += delta;
471+
};
472+
return lerpBlock;
473+
};
474+
475+
ShininessLerpBlock shininessRampUp = shininessLerpBuilder(delta * 50.0f);
476+
CCActionInterval *shininessRampUpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:shininessRampUp]] times:120];
477+
478+
ShininessLerpBlock shininessRampDown = shininessLerpBuilder(-delta * 50.0f);
479+
CCActionInterval *shininessRampDownAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:shininessRampDown]] times:120];
480+
481+
[light runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
482+
shininessRampUpAction,
483+
[CCActionDelay actionWithDuration:1],
484+
shininessRampDownAction,
485+
nil
486+
]]];
487+
}
488+
252489
#endif
253490

491+
254492
-(void)setupPaddingEffectTest
255493
{
256494
self.subTitle = @"Effect Padding Test";

0 commit comments

Comments
 (0)