Skip to content

Commit eb18943

Browse files
committed
Merge pull request #866 from thayerandrews/develop
The normal map is now a sprite frame and CCSprite has one too
2 parents 5f2db98 + 3683535 commit eb18943

28 files changed

+302
-74
lines changed

Resources/Images/sphere-256.png

41.6 KB
Loading

Resources/Images/torus-256.png

47.2 KB
Loading

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ -(void)setupRefractionEffectTest
3939
planet.position = p2;
4040

4141
[planet runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
42-
[CCActionMoveTo actionWithDuration:4.0 position:ccp(p1.x, p1.y)],
43-
[CCActionMoveTo actionWithDuration:4.0 position:ccp(p2.x, p2.y)],
42+
[CCActionMoveTo actionWithDuration:4.0 position:ccp(p1.x, p1.y)],
43+
[CCActionMoveTo actionWithDuration:4.0 position:ccp(p2.x, p2.y)],
4444
nil
4545
]]];
4646

@@ -57,13 +57,16 @@ -(void)setupRefractionEffectTest
5757

5858
[self.contentNode addChild:renderTexture];
5959

60-
61-
CCTexture *sphereNormalMap = [CCTexture textureWithFile:@"Images/sphere-normal-256.png"];
62-
CCEffect *sphereRefraction = [[CCEffectRefraction alloc] initWithRefraction:0.1f environment:renderTexture.sprite normalMap:sphereNormalMap];
60+
NSString *sphereTextureFile = @"Images/sphere-256.png";
61+
CCTexture *sphereTexture = [CCTexture textureWithFile:sphereTextureFile];
62+
CCSpriteFrame *sphereNormalMap = [CCSpriteFrame frameWithImageNamed:@"Images/sphere-normal-256.png"];
63+
CCEffectRefraction *sphereRefraction = [[CCEffectRefraction alloc] initWithRefraction:0.1f environment:renderTexture.sprite normalMap:nil];
6364

6465
p1 = CGPointMake(0.1f, 0.8f);
6566
p2 = CGPointMake(0.35f, 0.2f);
66-
CCSprite *sprite1 = [self spriteWithEffects:@[sphereRefraction] image:@"Images/blocks-hd.png" atPosition:p1];
67+
CCSprite *sprite1 = [self spriteWithEffects:@[sphereRefraction] image:sphereTextureFile atPosition:p1];
68+
sprite1.normalMapSpriteFrame = sphereNormalMap;
69+
sprite1.scale = 0.5f;
6770
[sprite1 runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
6871
[CCActionMoveTo actionWithDuration:2.0 position:ccp(p1.x, p2.y)],
6972
[CCActionMoveTo actionWithDuration:1.0 position:ccp(p2.x, p2.y)],
@@ -72,14 +75,16 @@ -(void)setupRefractionEffectTest
7275
nil
7376
]]];
7477
[self.contentNode addChild:sprite1];
75-
76-
77-
CCTexture *torusNormalMap = [CCTexture textureWithFile:@"Images/torus-normal-256.png"];
78-
CCEffect *torusRefraction = [[CCEffectRefraction alloc] initWithRefraction:0.1f environment:renderTexture.sprite normalMap:torusNormalMap];
78+
79+
NSString *torusTextureFile = @"Images/torus-256.png";
80+
CCTexture *torusTexture = [CCTexture textureWithFile:torusTextureFile];
81+
CCSpriteFrame *torusNormalMap = [CCSpriteFrame frameWithImageNamed:@"Images/torus-normal-256.png"];
82+
CCEffectRefraction *torusRefraction = [[CCEffectRefraction alloc] initWithRefraction:0.1f environment:renderTexture.sprite normalMap:torusNormalMap];
7983

8084
p1 = CGPointMake(0.65f, 0.2f);
8185
p2 = CGPointMake(0.9f, 0.8f);
82-
CCSprite *sprite2 = [self spriteWithEffects:@[torusRefraction] image:@"Images/blocks-hd.png" atPosition:p1];
86+
CCSprite *sprite2 = [self spriteWithEffects:@[torusRefraction] image:torusTextureFile atPosition:p1];
87+
sprite2.scale = 0.5f;
8388
[sprite2 runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
8489
[CCActionMoveTo actionWithDuration:2.0 position:ccp(p1.x, p2.y)],
8590
[CCActionMoveTo actionWithDuration:2.0 position:ccp(p2.x, p2.y)],
@@ -88,6 +93,89 @@ -(void)setupRefractionEffectTest
8893
nil
8994
]]];
9095
[self.contentNode addChild:sprite2];
96+
97+
__block NSUInteger spriteConfig = 0;
98+
99+
CCActionCallBlock *blockAction = [CCActionCallBlock actionWithBlock:^{
100+
spriteConfig++;
101+
if (spriteConfig > 8)
102+
{
103+
spriteConfig = 0;
104+
}
105+
106+
switch (spriteConfig)
107+
{
108+
case 0:
109+
sprite1.normalMapSpriteFrame = nil;
110+
sphereRefraction.normalMap = nil;
111+
sprite1.texture = [CCTexture none];
112+
113+
NSLog(@"Sprite: nil Effect: nil - You should see a rectangle.");
114+
break;
115+
case 1:
116+
sprite1.normalMapSpriteFrame = sphereNormalMap;
117+
sphereRefraction.normalMap = nil;
118+
sprite1.texture = sphereTexture;
119+
120+
NSLog(@"Sprite: Sphere Effect: nil - You should see a sphere.");
121+
break;
122+
case 2:
123+
sprite1.normalMapSpriteFrame = torusNormalMap;
124+
sphereRefraction.normalMap = nil;
125+
sprite1.texture = torusTexture;
126+
127+
NSLog(@"Sprite: Torus Effect: nil - You should see a torus.");
128+
break;
129+
case 3:
130+
sprite1.normalMapSpriteFrame = nil;
131+
sphereRefraction.normalMap = sphereNormalMap;
132+
sprite1.texture = sphereTexture;
133+
134+
NSLog(@"Sprite: nil Effect: Sphere - You should see a sphere.");
135+
break;
136+
case 4:
137+
sprite1.normalMapSpriteFrame = sphereNormalMap;
138+
sphereRefraction.normalMap = sphereNormalMap;
139+
sprite1.texture = sphereTexture;
140+
141+
NSLog(@"Sprite: Sphere Effect: Sphere - You should see a sphere.");
142+
break;
143+
case 5:
144+
sprite1.normalMapSpriteFrame = torusNormalMap;
145+
sphereRefraction.normalMap = sphereNormalMap;
146+
sprite1.texture = sphereTexture;
147+
148+
NSLog(@"Sprite: Torus Effect: Sphere - You should see a sphere.");
149+
break;
150+
case 6:
151+
sprite1.normalMapSpriteFrame = nil;
152+
sphereRefraction.normalMap = torusNormalMap;
153+
sprite1.texture = torusTexture;
154+
155+
NSLog(@"Sprite: nil Effect: Torus - You should see a torus.");
156+
break;
157+
case 7:
158+
sprite1.normalMapSpriteFrame = sphereNormalMap;
159+
sphereRefraction.normalMap = torusNormalMap;
160+
sprite1.texture = torusTexture;
161+
162+
NSLog(@"Sprite: Sphere Effect: Torus - You should see a torus.");
163+
break;
164+
case 8:
165+
sprite1.normalMapSpriteFrame = torusNormalMap;
166+
sphereRefraction.normalMap = torusNormalMap;
167+
sprite1.texture = torusTexture;
168+
169+
NSLog(@"Sprite: Torus Effect: Torus - You should see a torus.");
170+
break;
171+
}
172+
}];
173+
[sprite2 runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
174+
[CCActionDelay actionWithDuration:4.0f],
175+
blockAction,
176+
nil
177+
]]];
178+
91179
}
92180

93181
-(void)setupBlurEffectNodeTest

cocos2d/CCEffect.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ typedef NS_ENUM(NSUInteger, CCEffectFunctionStitchFlags)
2222
CCEffectFunctionStitchBoth = (CCEffectFunctionStitchBefore | CCEffectFunctionStitchAfter),
2323
};
2424

25+
typedef NS_ENUM(NSUInteger, CCEffectPrepareStatus)
26+
{
27+
CCEffectPrepareNothingToDo = 0,
28+
CCEffectPrepareFailure = 1,
29+
CCEffectPrepareSuccess = 2,
30+
};
2531

2632
@interface CCEffectFunction : NSObject
2733

@@ -110,14 +116,15 @@ typedef void (^CCEffectRenderPassEndBlock)(CCEffectRenderPass *pass);
110116
@property (nonatomic, readonly) NSMutableDictionary* shaderUniforms;
111117
@property (nonatomic, readonly) NSInteger renderPassesRequired;
112118
@property (nonatomic, readonly) BOOL supportsDirectRendering;
119+
@property (nonatomic, readonly) BOOL readyForRendering;
113120
@property (nonatomic, copy) NSString *debugName;
114121

115122

116123
-(id)initWithFragmentUniforms:(NSArray*)fragmentUniforms vertextUniforms:(NSArray*)vertexUniforms varying:(NSArray*)varying;
117124
-(id)initWithFragmentFunction:(NSMutableArray*) fragmentFunctions fragmentUniforms:(NSArray*)fragmentUniforms vertextUniforms:(NSArray*)vertexUniforms varying:(NSArray*)varying;
118125
-(id)initWithFragmentFunction:(NSMutableArray*) fragmentFunctions vertexFunctions:(NSMutableArray*)vertextFunctions fragmentUniforms:(NSArray*)fragmentUniforms vertextUniforms:(NSArray*)vertexUniforms varying:(NSArray*)varying;
119126

120-
-(BOOL)prepareForRendering;
127+
-(CCEffectPrepareStatus)prepareForRendering;
121128
-(CCEffectRenderPass *)renderPassAtIndex:(NSInteger)passIndex;
122129

123130
-(BOOL)stitchSupported:(CCEffectFunctionStitchFlags)stitch;

cocos2d/CCEffect.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,15 @@ -(id)initWithFragmentFunction:(NSMutableArray*) fragmentFunctions vertexFunction
388388

389389
-(void)buildShaderUniforms:(NSArray*)fragmentUniforms vertexUniforms:(NSArray*)vertexUniforms
390390
{
391-
[_shaderUniforms removeAllObjects];
391+
_shaderUniforms = [[NSMutableDictionary alloc] init];
392392

393393
for(CCEffectUniform* uniform in fragmentUniforms)
394394
{
395-
if(_shaderUniforms == nil)
396-
_shaderUniforms = [[NSMutableDictionary alloc] init];
397-
398395
[_shaderUniforms setObject:uniform.value forKey:uniform.name];
399396
}
400397

401398
for(CCEffectUniform* uniform in vertexUniforms)
402399
{
403-
if(_shaderUniforms == nil)
404-
_shaderUniforms = [[NSMutableDictionary alloc] init];
405-
406400
[_shaderUniforms setObject:uniform.value forKey:uniform.name];
407401
}
408402
}
@@ -541,11 +535,16 @@ - (BOOL)supportsDirectRendering
541535
return YES;
542536
}
543537

544-
- (BOOL)prepareForRendering
538+
- (BOOL)readyForRendering
545539
{
546540
return YES;
547541
}
548542

543+
- (CCEffectPrepareStatus)prepareForRendering
544+
{
545+
return CCEffectPrepareNothingToDo;
546+
}
547+
549548
-(CCEffectRenderPass *)renderPassAtIndex:(NSInteger)passIndex
550549
{
551550
NSAssert((passIndex >= 0) && (passIndex < _renderPasses.count), @"Pass index out of range.");

cocos2d/CCEffectBloom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @param luminanceThreshold ranges between 0.0-1.0 - defines which part of the image should be glown via a luminance factor (brightness).
2020
* A value of 0.0 will apply bloom to the whole image, a value of 1.0 will only apply bloom to the brightest part of the image.
2121
*/
22+
-(id)init;
2223
-(id)initWithPixelBlurRadius:(NSUInteger)blurRadius intensity:(float)intensity luminanceThreshold:(float)luminanceThreshold;
2324
+(id)effectWithPixelBlurRadius:(NSUInteger)blurRadius intensity:(float)intensity luminanceThreshold:(float)luminanceThreshold;
2425

cocos2d/CCEffectBloom.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ -(void)buildRenderPasses
279279

280280
CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init];
281281
pass0.shader = self.shader;
282-
pass0.shaderUniforms = self.shaderUniforms;
283282
pass0.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture){
284283

285284
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
@@ -294,7 +293,6 @@ -(void)buildRenderPasses
294293

295294
CCEffectRenderPass *pass1 = [[CCEffectRenderPass alloc] init];
296295
pass1.shader = self.shader;
297-
pass1.shaderUniforms = self.shaderUniforms;
298296
pass1.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture){
299297

300298
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
@@ -308,7 +306,6 @@ -(void)buildRenderPasses
308306

309307
CCEffectRenderPass *pass2 = [[CCEffectRenderPass alloc] init];
310308
pass2.shader = self.shader;
311-
pass2.shaderUniforms = self.shaderUniforms;
312309
pass2.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture){
313310
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
314311
pass.shaderUniforms[self.uniformTranslationTable[@"u_enableGlowMap"]] = [NSNumber numberWithFloat:1.0f];

cocos2d/CCEffectBrightness.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
@property (nonatomic) float brightness;
1515

16+
-(id)init;
1617
-(id)initWithBrightness:(float)brightness;
1718
+(id)effectWithBrightness:(float)brightness;
1819

cocos2d/CCEffectBrightness.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ -(id)initWithBrightness:(float)brightness
3939
{
4040
if((self = [self init]))
4141
{
42+
_brightness = brightness;
4243
_conditionedBrightness = conditionBrightness(brightness);
4344
}
4445
return self;
@@ -67,7 +68,6 @@ -(void)buildRenderPasses
6768

6869
CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init];
6970
pass0.shader = self.shader;
70-
pass0.shaderUniforms = self.shaderUniforms;
7171
pass0.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture){
7272
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
7373
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
@@ -79,6 +79,7 @@ -(void)buildRenderPasses
7979

8080
-(void)setBrightness:(float)brightness
8181
{
82+
_brightness = brightness;
8283
_conditionedBrightness = conditionBrightness(brightness);
8384
}
8485

cocos2d/CCEffectContrast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
@property (nonatomic) float contrast;
1515

16+
-(id)init;
1617
-(id)initWithContrast:(float)contrast;
1718
+(id)effectWithContrast:(float)contrast;
1819

0 commit comments

Comments
 (0)