Skip to content

Commit 9e4dd65

Browse files
committed
Merge pull request #992 from osinoleg/develop
Fixed overflow bug in bloom/blur effects - github issue #985
2 parents eb8d717 + 325983b commit 9e4dd65

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ -(void)setupBlurEffectNodeTest
693693
effectNode.positionType = CCPositionTypeNormalized;
694694
effectNode.position = ccp(0.1, 0.5);
695695
[effectNode addChild:sampleSprite];
696-
CCEffectBlur* effect = [CCEffectBlur effectWithBlurRadius:1.0];
696+
CCEffectBlur* effect = [CCEffectBlur effectWithBlurRadius:4.0];
697697
effectNode.effect = effect;
698698

699699
[self.contentNode addChild:effectNode];
@@ -707,12 +707,11 @@ -(void)setupBlurEffectNodeTest
707707
effectNode2.positionType = CCPositionTypeNormalized;
708708
effectNode2.position = ccp(0.21, 0.5);
709709
[effectNode2 addChild:sampleSprite2];
710-
CCEffectBlur* effect2 = [CCEffectBlur effectWithBlurRadius:7.0];
710+
CCEffectBlur* effect2 = [CCEffectBlur effectWithBlurRadius:10.0];
711711
effectNode2.effect = effect2;
712712

713713
[self.contentNode addChild:effectNode2];
714714

715-
// Tilt shift
716715
CCSprite *sampleSprite3 = [CCSprite spriteWithImageNamed:@"Images/sample_hollow_circle.png"];
717716
sampleSprite3.position = ccp(0.5, 0.5);
718717
sampleSprite3.positionType = CCPositionTypeNormalized;
@@ -728,7 +727,6 @@ -(void)setupBlurEffectNodeTest
728727

729728
[self.contentNode addChild:effectNode3];
730729

731-
// Tilt shift reversed
732730
CCSprite *sampleSprite4 = [CCSprite spriteWithImageNamed:@"Images/sample_hollow_circle.png"];
733731
sampleSprite4.position = ccp(0.5, 0.5);
734732
sampleSprite4.positionType = CCPositionTypeNormalized;
@@ -737,7 +735,7 @@ -(void)setupBlurEffectNodeTest
737735
effectNode4.positionType = CCPositionTypeNormalized;
738736
effectNode4.position = ccp(0.6, 0.5);
739737
[effectNode4 addChild:sampleSprite4];
740-
CCEffectBlur* effect4 = [CCEffectBlur effectWithBlurRadius:7.0];
738+
CCEffectBlur* effect4 = [CCEffectBlur effectWithBlurRadius:12.0];
741739
effectNode4.effect = effect4;
742740

743741
[self.contentNode addChild:effectNode4];

cocos2d/CCEffectBloom.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ -(void)buildFragmentFunctions
136136
{
137137
self.fragmentFunctions = [[NSMutableArray alloc] init];
138138

139-
GLfloat *standardGaussianWeights = calloc(_trueBlurRadius + 1, sizeof(GLfloat));
139+
GLfloat *standardGaussianWeights = calloc(_trueBlurRadius + 2, sizeof(GLfloat));
140140
GLfloat sumOfWeights = 0.0;
141-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
141+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 2; currentGaussianWeightIndex++)
142142
{
143143
standardGaussianWeights[currentGaussianWeightIndex] = (1.0 / sqrt(2.0 * M_PI * pow(_sigma, 2.0))) * exp(-pow(currentGaussianWeightIndex, 2.0) / (2.0 * pow(_sigma, 2.0)));
144144

@@ -153,14 +153,14 @@ -(void)buildFragmentFunctions
153153
}
154154

155155
// Next, normalize these weights to prevent the clipping of the Gaussian curve at the end of the discrete samples from reducing luminance
156-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
156+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 2; currentGaussianWeightIndex++)
157157
{
158158
standardGaussianWeights[currentGaussianWeightIndex] = standardGaussianWeights[currentGaussianWeightIndex] / sumOfWeights;
159159
}
160160

161161
// From these weights we calculate the offsets to read interpolated values from
162-
NSUInteger numberOfOptimizedOffsets = MIN(_blurRadius / 2 + (_blurRadius % 2), BLUR_OPTIMIZED_RADIUS_MAX);
163-
NSUInteger trueNumberOfOptimizedOffsets = _trueBlurRadius / 2 + (_trueBlurRadius % 2);
162+
NSUInteger numberOfOptimizedOffsets = _numberOfOptimizedOffsets;
163+
NSUInteger trueNumberOfOptimizedOffsets = _trueBlurRadius / 2;
164164

165165
NSMutableString *shaderString = [[NSMutableString alloc] init];
166166

@@ -183,7 +183,7 @@ -(void)buildFragmentFunctions
183183
[shaderString appendString:@"inBounds = step(0.0, min(compare.x, compare.y));"];
184184
[shaderString appendFormat:@"srcPixel = texture2D(cc_PreviousPassTexture, v_blurCoordinates[0]);\n"];
185185
[shaderString appendString:@"luminanceCheck = step(u_luminanceThreshold, dot(srcPixel.rgb, luminanceWeighting));\n"];
186-
[shaderString appendFormat:@"src += inBounds * luminanceCheck * srcPixel * %f;\n", standardGaussianWeights[0]];
186+
[shaderString appendFormat:@"src += inBounds * luminanceCheck * srcPixel * %f;\n", (_trueBlurRadius == 0) ? 1.0 : standardGaussianWeights[0]];
187187

188188
for (NSUInteger currentBlurCoordinateIndex = 0; currentBlurCoordinateIndex < numberOfOptimizedOffsets; currentBlurCoordinateIndex++)
189189
{

cocos2d/CCEffectBlur.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/// @name Accessing Effect Attributes
2121
/// -----------------------------------------------------------------------
2222

23-
/** The size of the blur. This value is in the range [0..6].
23+
/** The size of the blur. This value is in the range [0..n].
2424
*/
2525
@property (nonatomic, assign) NSUInteger blurRadius;
2626

cocos2d/CCEffectBlur.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ -(void)buildFragmentFunctions
118118
{
119119
self.fragmentFunctions = [[NSMutableArray alloc] init];
120120

121-
GLfloat *standardGaussianWeights = calloc(_trueBlurRadius + 1, sizeof(GLfloat));
121+
GLfloat *standardGaussianWeights = calloc(_trueBlurRadius + 2, sizeof(GLfloat));
122122
GLfloat sumOfWeights = 0.0;
123-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
123+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 2; currentGaussianWeightIndex++)
124124
{
125125
standardGaussianWeights[currentGaussianWeightIndex] = (1.0 / sqrt(2.0 * M_PI * pow(_sigma, 2.0))) * exp(-pow(currentGaussianWeightIndex, 2.0) / (2.0 * pow(_sigma, 2.0)));
126126

@@ -135,14 +135,14 @@ -(void)buildFragmentFunctions
135135
}
136136

137137
// Next, normalize these weights to prevent the clipping of the Gaussian curve at the end of the discrete samples from reducing luminance
138-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
138+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 2; currentGaussianWeightIndex++)
139139
{
140140
standardGaussianWeights[currentGaussianWeightIndex] = standardGaussianWeights[currentGaussianWeightIndex] / sumOfWeights;
141141
}
142142

143143
// From these weights we calculate the offsets to read interpolated values from
144-
NSUInteger numberOfOptimizedOffsets = MIN(_blurRadius / 2 + (_blurRadius % 2), BLUR_OPTIMIZED_RADIUS_MAX);
145-
NSUInteger trueNumberOfOptimizedOffsets = _trueBlurRadius / 2 + (_trueBlurRadius % 2);
144+
NSUInteger numberOfOptimizedOffsets = _numberOfOptimizedOffsets;
145+
NSUInteger trueNumberOfOptimizedOffsets = _trueBlurRadius / 2;
146146

147147
NSMutableString *shaderString = [[NSMutableString alloc] init];
148148

@@ -157,7 +157,7 @@ -(void)buildFragmentFunctions
157157
// Inner texture loop
158158
[shaderString appendString:@"compare = cc_FragTexCoord1Extents - abs(v_blurCoordinates[0] - cc_FragTexCoord1Center);"];
159159
[shaderString appendString:@"inBounds = step(0.0, min(compare.x, compare.y));"];
160-
[shaderString appendFormat:@"sum += texture2D(cc_PreviousPassTexture, v_blurCoordinates[0]) * inBounds * %f;\n", standardGaussianWeights[0]];
160+
[shaderString appendFormat:@"sum += texture2D(cc_PreviousPassTexture, v_blurCoordinates[0]) * inBounds * %f;\n", (_trueBlurRadius == 0) ? 1.0 : standardGaussianWeights[0]];
161161

162162
for (NSUInteger currentBlurCoordinateIndex = 0; currentBlurCoordinateIndex < numberOfOptimizedOffsets; currentBlurCoordinateIndex++)
163163
{

0 commit comments

Comments
 (0)