Skip to content

Commit 850ab72

Browse files
author
Thayer J Andrews
committed
CCEffectsTest - Add lighting parameter test
Tests manipulation of various light parameters including primary and ambient colors, primary and ambient intensities, cutoff, and depth.
1 parent 19f1956 commit 850ab72

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ -(void)setupSimpleLightingTest
294294

295295
[sprite addChild:label];
296296

297-
298297
[light runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
299298
[CCActionMoveTo actionWithDuration:1.0 position:ccp(1.0f, 1.0f)],
300299
[CCActionMoveTo actionWithDuration:2.0 position:ccp(0.0f, 0.0f)],
@@ -307,6 +306,142 @@ -(void)setupSimpleLightingTest
307306
setupBlock(ccp(0.75f, 0.5f), CCLightDirectional, 1.0f, @"Directional Light\nPosition does not matter, orientation does.");
308307
}
309308

309+
-(void)setupLightingParameterTest
310+
{
311+
self.subTitle = @"Lighting Parameter Test";
312+
313+
NSString *normalMapImage = @"Images/ShinyTorusNormals.png";
314+
NSString *diffuseImage = @"Images/ShinyTorusColor.png";
315+
316+
CCLightNode* (^setupBlock)(CGPoint position, NSString *title, CCAction *action) = ^CCLightNode*(CGPoint position, NSString *title, CCAction *action)
317+
{
318+
CCLightNode *light = [[CCLightNode alloc] init];
319+
light.positionType = CCPositionTypeNormalized;
320+
light.position = ccp(1.0f, 1.0f);
321+
light.anchorPoint = ccp(0.5f, 0.5f);
322+
light.intensity = 1.0f;
323+
light.ambientIntensity = 0.2f;
324+
light.cutoffRadius = 0.0f;
325+
light.depth = 100.0f;
326+
327+
CCSprite *lightSprite = [CCSprite spriteWithImageNamed:@"Images/snow.png"];
328+
[light addChild:lightSprite];
329+
330+
CCEffectLighting *lightingEffect = [[CCEffectLighting alloc] initWithLights:@[light]];
331+
332+
CCSprite *sprite = [CCSprite spriteWithImageNamed:diffuseImage];
333+
sprite.positionType = CCPositionTypeNormalized;
334+
sprite.position = position;
335+
sprite.normalMapSpriteFrame = [CCSpriteFrame frameWithImageNamed:normalMapImage];
336+
sprite.effect = lightingEffect;
337+
sprite.scale = 0.3f;
338+
339+
[self.contentNode addChild:sprite];
340+
341+
[sprite addChild:light];
342+
343+
CCLabelTTF *label = [CCLabelTTF labelWithString:title fontName:@"HelveticaNeue-Light" fontSize:36 * [CCDirector sharedDirector].UIScaleFactor];
344+
label.color = [CCColor whiteColor];
345+
label.positionType = CCPositionTypeNormalized;
346+
label.position = ccp(0.5f, 1.1f);
347+
label.horizontalAlignment = CCTextAlignmentCenter;
348+
349+
[sprite addChild:label];
350+
351+
if (action)
352+
{
353+
[light runAction:action];
354+
}
355+
return light;
356+
};
357+
358+
CCLightNode *light = nil;
359+
light = setupBlock(ccp(0.25f, 0.65f), @"Varying Intensity", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
360+
[CCActionTween actionWithDuration:2 key:@"intensity" from:0.0f to:1.0f],
361+
[CCActionDelay actionWithDuration:2],
362+
[CCActionTween actionWithDuration:2 key:@"intensity" from:1.0f to:0.0f],
363+
nil
364+
]]);
365+
light.ambientIntensity = 0.0f;
366+
light = setupBlock(ccp(0.25f, 0.25f), @"Varying Color", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
367+
[CCActionTintTo actionWithDuration:2 color:[CCColor redColor]],
368+
[CCActionDelay actionWithDuration:1],
369+
[CCActionTintTo actionWithDuration:2 color:[CCColor greenColor]],
370+
[CCActionDelay actionWithDuration:1],
371+
[CCActionTintTo actionWithDuration:2 color:[CCColor blueColor]],
372+
[CCActionDelay actionWithDuration:1],
373+
[CCActionTintTo actionWithDuration:2 color:[CCColor whiteColor]],
374+
[CCActionDelay actionWithDuration:1],
375+
nil
376+
]]);
377+
378+
379+
light = setupBlock(ccp(0.5f, 0.65f), @"Varying Ambient Intensity", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
380+
[CCActionTween actionWithDuration:2 key:@"ambientIntensity" from:0.0f to:1.0f],
381+
[CCActionDelay actionWithDuration:2],
382+
[CCActionTween actionWithDuration:2 key:@"ambientIntensity" from:1.0f to:0.0f],
383+
nil
384+
]]);
385+
light = setupBlock(ccp(0.5f, 0.25f), @"Varying Ambient Color", nil);
386+
light.intensity = 0.5f;
387+
light.ambientIntensity = 0.5f;
388+
389+
const float timeStep = 0.017f;
390+
const float duration = 2.0f;
391+
const float delta = timeStep / duration;
392+
393+
typedef void (^AmbientLerpBlock)();
394+
typedef void (^AmbientLerpBuilderBlock)(ccColor4F deltaC);
395+
AmbientLerpBlock (^lerpBuilderBlock)(ccColor4F deltaC) = ^AmbientLerpBlock(ccColor4F deltaC)
396+
{
397+
AmbientLerpBlock lerpBlock = ^{
398+
ccColor4F c = light.ambientColor.ccColor4f;
399+
c.r += deltaC.r;
400+
c.g += deltaC.g;
401+
c.b += deltaC.b;
402+
light.ambientColor = [CCColor colorWithCcColor4f:c];
403+
};
404+
return lerpBlock;
405+
};
406+
407+
AmbientLerpBlock whiteRedLerp = lerpBuilderBlock(ccc4f(0.0f, -delta, -delta, 0.0f));
408+
CCActionInterval *whiteRedLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:whiteRedLerp]] times:120];
409+
410+
AmbientLerpBlock redGreenLerp = lerpBuilderBlock(ccc4f(-delta, delta, 0.0f, 0.0f));
411+
CCActionInterval *redGreenLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:redGreenLerp]] times:120];
412+
413+
AmbientLerpBlock greenBlueLerp = lerpBuilderBlock(ccc4f(0.0f, -delta, delta, 0.0f));
414+
CCActionInterval *greenBlueLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:greenBlueLerp]] times:120];
415+
416+
AmbientLerpBlock blueWhiteLerp = lerpBuilderBlock(ccc4f(delta, delta, 0.0f, 0.0f));
417+
CCActionInterval *blueWhiteLerpAction = [CCActionRepeat actionWithAction:[CCActionSequence actionOne:[CCActionDelay actionWithDuration:timeStep] two:[CCActionCallBlock actionWithBlock:blueWhiteLerp]] times:120];
418+
419+
CCAction *ambientLerpAction = [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
420+
whiteRedLerpAction,
421+
[CCActionDelay actionWithDuration:1],
422+
redGreenLerpAction,
423+
[CCActionDelay actionWithDuration:1],
424+
greenBlueLerpAction,
425+
[CCActionDelay actionWithDuration:1],
426+
blueWhiteLerpAction,
427+
[CCActionDelay actionWithDuration:1],
428+
nil
429+
]];
430+
[light runAction:ambientLerpAction];
431+
432+
light = setupBlock(ccp(0.75f, 0.65f), @"Varying Cutoff", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
433+
[CCActionTween actionWithDuration:2 key:@"cutoffRadius" from:1.0f to:500.0f],
434+
[CCActionTween actionWithDuration:2 key:@"cutoffRadius" from:500.0f to:1.0f],
435+
nil
436+
]]);
437+
light = setupBlock(ccp(0.75f, 0.25f), @"Varying Depth", [CCActionRepeatForever actionWithAction:[CCActionSequence actions:
438+
[CCActionTween actionWithDuration:2 key:@"depth" from:1.0f to:500.0f],
439+
[CCActionTween actionWithDuration:2 key:@"depth" from:500.0f to:1.0f],
440+
nil
441+
]]);
442+
}
443+
444+
310445
-(void)setupPaddingEffectTest
311446
{
312447
self.subTitle = @"Effect Padding Test";

0 commit comments

Comments
 (0)