Skip to content

Commit 195bf2d

Browse files
committed
Merge pull request #919 from thayerandrews/develop
CCEffects - Stack constructors, bloom fixes, and stacking fixes
2 parents 228f091 + 91bd04a commit 195bf2d

File tree

10 files changed

+312
-135
lines changed

10 files changed

+312
-135
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ -(void)setupReflectEffectTest
8181
[self.contentNode addChild:environment];
8282

8383
CCSpriteFrame *normalMap = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyBallNormals.png"];
84-
CCEffectReflection *reflection = [[CCEffectReflection alloc] initWithShininess:0.4f environment:environment];
84+
CCEffectReflection *reflection = [[CCEffectReflection alloc] initWithShininess:1.0f environment:environment];
8585
reflection.fresnelBias = 0.0f;
8686
reflection.fresnelPower = 0.0f;
8787

@@ -333,9 +333,9 @@ -(void)setupBlurEffectNodeTest
333333
[self.contentNode addChild:effectNode4];
334334
}
335335

336-
-(void)setupGlowEffectNodeTest
336+
-(void)setupBloomEffectTest
337337
{
338-
self.subTitle = @"Glow Effect Node Test";
338+
self.subTitle = @"Bloom Effect Test";
339339

340340
CCSprite *sampleSprite_base = [CCSprite spriteWithImageNamed:@"Images/sample_hollow_circle.png"];
341341
sampleSprite_base.anchorPoint = ccp(0.0, 0.0);
@@ -389,6 +389,22 @@ -(void)setupGlowEffectNodeTest
389389
glowEffectNode2.effect = glowEffect2;
390390

391391
[self.contentNode addChild:glowEffectNode2];
392+
393+
// Create a sprite to blur
394+
const int steps = 5;
395+
for (int i = 0; i < steps; i++)
396+
{
397+
CCSprite *sampleSprite3 = [CCSprite spriteWithImageNamed:@"Images/grossini_dance_08.png"];
398+
sampleSprite3.anchorPoint = ccp(0.5, 0.5);
399+
sampleSprite3.position = ccp(0.1f + i * (0.8f / (steps - 1)), 0.2f);
400+
sampleSprite3.positionType = CCPositionTypeNormalized;
401+
402+
// Blend glow maps test
403+
CCEffectBloom* glowEffect3 = [CCEffectBloom effectWithBlurRadius:8 intensity:1.0f luminanceThreshold:1.0f - ((float)i/(float)(steps-1))];
404+
sampleSprite3.effect = glowEffect3;
405+
406+
[self.contentNode addChild:sampleSprite3];
407+
}
392408
}
393409

394410
-(void)setupBrightnessAndContrastEffectNodeTest
@@ -454,15 +470,15 @@ -(void)setupSaturationEffectNodeTest
454470
];
455471

456472
// Effect nodes that use the effects in different combinations.
457-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[0]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.1, 0.5)]];
458-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[1]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.2, 0.5)]];
459-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[2]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.3, 0.5)]];
460-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[3]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.4, 0.5)]];
461-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[4]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.5, 0.5)]];
462-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[5]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.6, 0.5)]];
463-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[6]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.7, 0.5)]];
464-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[7]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.8, 0.5)]];
465-
[self.contentNode addChild:[self effectNodeWithEffects:@[effects[8]] appliedToSpriteWithImage:@"Images/grossini.png" atPosition:ccp(0.9, 0.5)]];
473+
[self.contentNode addChild:[self spriteWithEffects:@[effects[0]] image:@"Images/grossini.png" atPosition:ccp(0.1, 0.5)]];
474+
[self.contentNode addChild:[self spriteWithEffects:@[effects[1]] image:@"Images/grossini.png" atPosition:ccp(0.2, 0.5)]];
475+
[self.contentNode addChild:[self spriteWithEffects:@[effects[2]] image:@"Images/grossini.png" atPosition:ccp(0.3, 0.5)]];
476+
[self.contentNode addChild:[self spriteWithEffects:@[effects[3]] image:@"Images/grossini.png" atPosition:ccp(0.4, 0.5)]];
477+
[self.contentNode addChild:[self spriteWithEffects:@[effects[4]] image:@"Images/grossini.png" atPosition:ccp(0.5, 0.5)]];
478+
[self.contentNode addChild:[self spriteWithEffects:@[effects[5]] image:@"Images/grossini.png" atPosition:ccp(0.6, 0.5)]];
479+
[self.contentNode addChild:[self spriteWithEffects:@[effects[6]] image:@"Images/grossini.png" atPosition:ccp(0.7, 0.5)]];
480+
[self.contentNode addChild:[self spriteWithEffects:@[effects[7]] image:@"Images/grossini.png" atPosition:ccp(0.8, 0.5)]];
481+
[self.contentNode addChild:[self spriteWithEffects:@[effects[8]] image:@"Images/grossini.png" atPosition:ccp(0.9, 0.5)]];
466482
}
467483

468484
-(void)setupHueEffectTest
@@ -527,8 +543,7 @@ -(void)setupStackTest
527543
sprite.position = ccp(0.5f, 0.5f);
528544
sprite.scale = 0.5f;
529545

530-
sprite.effect = [CCEffectStack effectStackWithEffects:@[effects[8], effects[9]]];
531-
// sprite.effect = [CCEffectStack effectStackWithEffects:@[effects[7]]];
546+
sprite.effect = [CCEffectStack effects:effects[7], effects[4], nil];
532547

533548
sprite.normalMapSpriteFrame = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyBallNormals.png"];
534549
sprite.colorRGBA = [CCColor colorWithRed:0.75f green:0.75f blue:0.75f alpha:0.75f];
@@ -662,7 +677,7 @@ - (CCNode *)effectNodeWithEffects:(NSArray *)effects appliedToSpriteWithImage:(N
662677
}
663678
else if (effects.count > 1)
664679
{
665-
CCEffectStack *stack = [CCEffectStack effectStackWithEffects:effects];
680+
CCEffectStack *stack = [CCEffectStack effectWithArray:effects];
666681
effectNode.effect = stack;
667682
}
668683

@@ -683,7 +698,7 @@ - (CCSprite *)spriteWithEffects:(NSArray *)effects image:(NSString *)spriteImage
683698
}
684699
else if (effects.count > 1)
685700
{
686-
CCEffectStack *stack = [CCEffectStack effectStackWithEffects:effects];
701+
CCEffectStack *stack = [CCEffectStack effectWithArray:effects];
687702
sprite.effect = stack;
688703
}
689704

cocos2d/CCEffectBloom.m

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ -(void)setLuminanceThreshold:(float)luminanceThreshold
105105
-(void)setIntensity:(float)intensity
106106
{
107107
_intensity = clampf(intensity, 0.0f, 1.0f);
108-
_transformedIntensity = 1.0f - _intensity;
108+
_transformedIntensity = _intensity;
109109
}
110110

111111
-(void)setBlurRadius:(NSUInteger)blurRadius
@@ -169,17 +169,27 @@ -(void)buildFragmentFunctions
169169

170170
[shaderString appendString:@"if(u_enableGlowMap == 0.0) {\n"];
171171

172+
[shaderString appendString:@"const vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);\n"];
173+
[shaderString appendString:@"vec4 srcPixel; float luminanceCheck;\n"];
174+
172175
// Inner texture loop
173-
[shaderString appendFormat:@"src += texture2D(cc_PreviousPassTexture, v_blurCoordinates[0]) * %f;\n", standardGaussianWeights[0]];
176+
[shaderString appendFormat:@"srcPixel = texture2D(cc_PreviousPassTexture, v_blurCoordinates[0]);\n"];
177+
[shaderString appendString:@"luminanceCheck = step(u_luminanceThreshold, dot(srcPixel.rgb, luminanceWeighting));\n"];
178+
[shaderString appendFormat:@"src += luminanceCheck * srcPixel * %f;\n", standardGaussianWeights[0]];
174179

175180
for (NSUInteger currentBlurCoordinateIndex = 0; currentBlurCoordinateIndex < numberOfOptimizedOffsets; currentBlurCoordinateIndex++)
176181
{
177182
GLfloat firstWeight = standardGaussianWeights[currentBlurCoordinateIndex * 2 + 1];
178183
GLfloat secondWeight = standardGaussianWeights[currentBlurCoordinateIndex * 2 + 2];
179184
GLfloat optimizedWeight = firstWeight + secondWeight;
180-
181-
[shaderString appendFormat:@"src += texture2D(cc_PreviousPassTexture, v_blurCoordinates[%lu]) * %f;\n", (unsigned long)((currentBlurCoordinateIndex * 2) + 1), optimizedWeight];
182-
[shaderString appendFormat:@"src += texture2D(cc_PreviousPassTexture, v_blurCoordinates[%lu]) * %f;\n", (unsigned long)((currentBlurCoordinateIndex * 2) + 2), optimizedWeight];
185+
186+
[shaderString appendFormat:@"srcPixel = texture2D(cc_PreviousPassTexture, v_blurCoordinates[%lu]);\n", (unsigned long)((currentBlurCoordinateIndex * 2) + 1)];
187+
[shaderString appendString:@"luminanceCheck = step(u_luminanceThreshold, dot(srcPixel.rgb, luminanceWeighting));\n"];
188+
[shaderString appendFormat:@"src += luminanceCheck * srcPixel * %f;\n", optimizedWeight];
189+
190+
[shaderString appendFormat:@"srcPixel = texture2D(cc_PreviousPassTexture, v_blurCoordinates[%lu]);\n", (unsigned long)((currentBlurCoordinateIndex * 2) + 2)];
191+
[shaderString appendString:@"luminanceCheck = step(u_luminanceThreshold, dot(srcPixel.rgb, luminanceWeighting));\n"];
192+
[shaderString appendFormat:@"src += luminanceCheck * srcPixel * %f;\n", optimizedWeight];
183193
}
184194

185195
// If the number of required samples exceeds the amount we can pass in via varyings, we have to do dependent texture reads in the fragment shader
@@ -194,16 +204,17 @@ -(void)buildFragmentFunctions
194204

195205
GLfloat optimizedWeight = firstWeight + secondWeight;
196206
GLfloat optimizedOffset = (firstWeight * (currentOverlowTextureRead * 2 + 1) + secondWeight * (currentOverlowTextureRead * 2 + 2)) / optimizedWeight;
197-
198-
[shaderString appendFormat:@"src += texture2D(cc_PreviousPassTexture, v_blurCoordinates[0] + singleStepOffset * %f) * %f;\n", optimizedOffset, optimizedWeight];
199-
[shaderString appendFormat:@"src += texture2D(cc_PreviousPassTexture, v_blurCoordinates[0] - singleStepOffset * %f) * %f;\n", optimizedOffset, optimizedWeight];
207+
208+
[shaderString appendFormat:@"srcPixel = texture2D(cc_PreviousPassTexture, v_blurCoordinates[0] + singleStepOffset * %f);\n", optimizedOffset];
209+
[shaderString appendString:@"luminanceCheck = step(u_luminanceThreshold, dot(srcPixel.rgb, luminanceWeighting));\n"];
210+
[shaderString appendFormat:@"src += luminanceCheck * srcPixel * %f;\n", optimizedWeight];
211+
212+
[shaderString appendFormat:@"srcPixel = texture2D(cc_PreviousPassTexture, v_blurCoordinates[0] - singleStepOffset * %f);\n", optimizedOffset];
213+
[shaderString appendString:@"luminanceCheck = step(u_luminanceThreshold, dot(srcPixel.rgb, luminanceWeighting));\n"];
214+
[shaderString appendFormat:@"src += luminanceCheck * srcPixel * %f;\n", optimizedWeight];
200215
}
201216
}
202217

203-
[shaderString appendString:@"const vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);\n\
204-
float luminance = dot(src.rgb, luminanceWeighting);\n\
205-
if(luminance < u_luminanceThreshold)\n discard;\n"];
206-
207218
[shaderString appendString:@"} else {\n"];
208219
[shaderString appendString:@"\
209220
dst = texture2D(cc_MainTexture, cc_FragTexCoord1);\
@@ -214,7 +225,7 @@ -(void)buildFragmentFunctions
214225
// Choose one?
215226
// TODO: try using min(src, dst) to create a gloomEffect
216227
// NSString* additiveBlending = @"src + dst";
217-
NSString* screenBlending = @"(src + dst) - ((src * dst) * u_intensity)";
228+
NSString* screenBlending = @"(src * u_intensity + dst) - ((src * dst) * u_intensity)";
218229

219230
[shaderString appendFormat:@"\
220231
return %@;\n", screenBlending];
@@ -328,7 +339,7 @@ -(void)buildRenderPasses
328339

329340
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
330341
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_enableGlowMap"]] = [NSNumber numberWithFloat:0.0f];
331-
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_luminanceThreshold"]] = [NSNumber numberWithFloat:_luminanceThreshold];
342+
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_luminanceThreshold"]] = [NSNumber numberWithFloat:0.0f];
332343
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_intensity"]] = [NSNumber numberWithFloat:_transformedIntensity];
333344

334345
GLKVector2 dur = GLKVector2Make(0.0, 1.0 / (previousPassTexture.pixelHeight / previousPassTexture.contentScale));
@@ -344,7 +355,7 @@ -(void)buildRenderPasses
344355

345356
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
346357
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_enableGlowMap"]] = [NSNumber numberWithFloat:1.0f];
347-
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_luminanceThreshold"]] = [NSNumber numberWithFloat:_luminanceThreshold];
358+
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_luminanceThreshold"]] = [NSNumber numberWithFloat:0.0f];
348359
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_intensity"]] = [NSNumber numberWithFloat:_transformedIntensity];
349360

350361
} copy]];

0 commit comments

Comments
 (0)