Skip to content

Commit e82679c

Browse files
committed
Merge remote-tracking branch 'upstream/effects' into develop
2 parents 00b167b + 4aa30d5 commit e82679c

17 files changed

+1209
-300
lines changed

Resources/Images/output.png

7.95 KB
Loading

cocos2d-ios.xcodeproj/project.pbxproj

Lines changed: 44 additions & 24 deletions
Large diffs are not rendered by default.

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 169 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
#import "CCEffectStack_Private.h"
99

1010
@interface CCEffectsTest : TestBase @end
11-
@implementation CCEffectsTest
11+
@implementation CCEffectsTest {
12+
#if CC_EFFECTS_EXPERIMENTAL
13+
CCEffectDistanceField* _distanceFieldEffect;
14+
#endif
15+
CCEffectDFOutline* _outlineEffect;
16+
}
1217

1318
-(id)init
1419
{
@@ -20,29 +25,179 @@ -(id)init
2025
return self;
2126
}
2227

23-
-(void)setupOuterGlowEffectTest
28+
#pragma mark Distance Fields
29+
30+
-(void)setupDFOutlineEffectTest
2431
{
25-
self.subTitle = @"OuterGlow Effect Test";
32+
self.subTitle = @"Distance Field Outline Test";
2633

27-
// CCNodeColor* environment = [CCNodeColor nodeWithColor:[CCColor whiteColor]];
2834
CCSprite *environment = [CCSprite spriteWithImageNamed:@"Images/MountainPanorama.jpg"];
2935
environment.positionType = CCPositionTypeNormalized;
3036
environment.anchorPoint = ccp(0.5, 0.5);
3137
environment.position = ccp(0.5f, 0.5f);
3238

39+
_outlineEffect = [CCEffectDFOutline effectWithOutlineColor:[CCColor redColor] fillColor:[CCColor blackColor] outlineWidth:3 fieldScale:32];
40+
41+
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/output.png"];
42+
sampleSprite.position = ccp(0.5, 0.5);
43+
sampleSprite.positionType = CCPositionTypeNormalized;
44+
sampleSprite.effect = _outlineEffect;
45+
sampleSprite.scale = 2.0f;
46+
47+
CCSpriteFrame* background = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-background.png"];
48+
CCSpriteFrame* backgroundHilite = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-background-hilite.png"];
49+
CCSpriteFrame* handle = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-handle.png"];
50+
51+
CCSlider* slider = [[CCSlider alloc] initWithBackground:background andHandleImage:handle];
52+
[slider setBackgroundSpriteFrame:backgroundHilite forState:CCControlStateHighlighted];
53+
slider.positionType = CCPositionTypeNormalized;
54+
slider.position = ccp(0.1f, 0.5f);
55+
56+
slider.preferredSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
57+
slider.preferredSize = CGSizeMake(0.5f, 10);
58+
slider.rotation = 90;
59+
slider.anchorPoint = ccp(0.5f, 0.5f);
60+
slider.scale = 0.8;
61+
62+
[slider setTarget:self selector:@selector(outlineWidthChagne:)];
63+
3364
[self.contentNode addChild:environment];
65+
[self.contentNode addChild:slider];
66+
[self.contentNode addChild:sampleSprite];
3467

35-
CCColor *glowColor = [CCColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5];
36-
CCEffectOuterGlow* effect = [CCEffectOuterGlow effectWithGlowColor:glowColor];
68+
// 6 pixel block used for comparison;
69+
CCNodeColor* block = [CCNodeColor nodeWithColor:[CCColor greenColor]];
70+
block.contentSize = CGSizeMake(6.0, 6.0);
71+
block.position = ccp(0.424, 0.324);
72+
block.positionType = CCPositionTypeNormalized;
73+
block.rotation = 32;
74+
// [self.contentNode addChild:block];
75+
}
76+
77+
- (void)outlineWidthChagne:(id)sender
78+
{
79+
const int outlineWidthMax = 6;
80+
CCSlider* slider = sender;
81+
_outlineEffect.outlineWidth = slider.sliderValue * outlineWidthMax;
82+
}
83+
84+
#if CC_EFFECTS_EXPERIMENTAL
85+
-(void)setupDistanceFieldEffectTest
86+
{
87+
self.subTitle = @"Distance Field Effect Test";
3788

38-
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/DistanceFieldX.png"];
89+
// CCNodeColor* environment = [CCNodeColor nodeWithColor:[CCColor whiteColor]];
90+
CCSprite *environment = [CCSprite spriteWithImageNamed:@"Images/MountainPanorama.jpg"];
91+
environment.positionType = CCPositionTypeNormalized;
92+
environment.anchorPoint = ccp(0.5, 0.5);
93+
environment.position = ccp(0.5f, 0.5f);
94+
95+
[self.contentNode addChild:environment];
96+
97+
CCColor *glowColor = [CCColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
98+
_distanceFieldEffect = [CCEffectDistanceField effectWithGlowColor:glowColor outlineColor:[CCColor redColor]];
99+
100+
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/output.png"];
39101
sampleSprite.position = ccp(0.5, 0.5);
40102
sampleSprite.positionType = CCPositionTypeNormalized;
41-
sampleSprite.effect = effect;
42-
103+
sampleSprite.effect = _distanceFieldEffect;
104+
sampleSprite.scale = 2.0f;
105+
106+
CCSpriteFrame* background = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-background.png"];
107+
CCSpriteFrame* backgroundHilite = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-background-hilite.png"];
108+
CCSpriteFrame* handle = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-handle.png"];
109+
110+
CCSlider* slider = [[CCSlider alloc] initWithBackground:background andHandleImage:handle];
111+
[slider setBackgroundSpriteFrame:backgroundHilite forState:CCControlStateHighlighted];
112+
slider.positionType = CCPositionTypeNormalized;
113+
slider.position = ccp(0.1f, 0.5f);
114+
115+
slider.preferredSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
116+
slider.preferredSize = CGSizeMake(0.5f, 10);
117+
slider.rotation = 90;
118+
slider.anchorPoint = ccp(0.5f, 0.5f);
119+
slider.scale = 0.8;
120+
121+
[slider setTarget:self selector:@selector(outlineInnerWidthChange:)];
122+
123+
CCSlider* slider2 = [[CCSlider alloc] initWithBackground:background andHandleImage:handle];
124+
[slider2 setBackgroundSpriteFrame:backgroundHilite forState:CCControlStateHighlighted];
125+
slider2.positionType = CCPositionTypeNormalized;
126+
slider2.position = ccp(0.15f, 0.5f);
127+
128+
slider2.preferredSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
129+
slider2.preferredSize = CGSizeMake(0.5f, 10);
130+
slider2.rotation = 90;
131+
slider2.anchorPoint = ccp(0.5f, 0.5f);
132+
slider2.scale = 0.8;
133+
134+
[slider2 setTarget:self selector:@selector(outlineOuterWidthChange:)];
135+
136+
CCSlider* slider3 = [[CCSlider alloc] initWithBackground:background andHandleImage:handle];
137+
[slider3 setBackgroundSpriteFrame:backgroundHilite forState:CCControlStateHighlighted];
138+
slider3.positionType = CCPositionTypeNormalized;
139+
slider3.position = ccp(0.20f, 0.5f);
140+
141+
slider3.preferredSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
142+
slider3.preferredSize = CGSizeMake(0.5f, 10);
143+
slider3.rotation = 90;
144+
slider3.anchorPoint = ccp(0.5f, 0.5f);
145+
slider3.scale = 0.8;
146+
147+
[slider3 setTarget:self selector:@selector(glowWidthChange:)];
148+
149+
CCButton* enableGlow = [CCButton buttonWithTitle:@"Outer Glow"];
150+
enableGlow.positionType = CCPositionTypeNormalized;
151+
enableGlow.anchorPoint = ccp(0.5f, 0.5f);
152+
enableGlow.position = ccp(0.9, 0.8);
153+
[enableGlow setTarget:self selector:@selector(enableGlow:)];
154+
155+
CCButton* enableOutline = [CCButton buttonWithTitle:@"Outline"];
156+
enableOutline.positionType = CCPositionTypeNormalized;
157+
enableOutline.anchorPoint = ccp(0.5f, 0.5f);
158+
enableOutline.position = ccp(0.9, 0.7);
159+
[enableOutline setTarget:self selector:@selector(enableOutline:)];
160+
161+
[self.contentNode addChild:enableOutline];
162+
[self.contentNode addChild:enableGlow];
43163
[self.contentNode addChild:sampleSprite];
164+
[self.contentNode addChild:slider];
165+
[self.contentNode addChild:slider2];
166+
[self.contentNode addChild:slider3];
167+
}
168+
169+
- (void)outlineInnerWidthChange:(id)sender
170+
{
171+
CCSlider* slider = sender;
172+
_distanceFieldEffect.outlineInnerWidth = slider.sliderValue;
173+
}
174+
175+
- (void)outlineOuterWidthChange:(id)sender
176+
{
177+
CCSlider* slider = sender;
178+
_distanceFieldEffect.outlineOuterWidth = slider.sliderValue;
179+
}
180+
181+
- (void)glowWidthChange:(id)sender
182+
{
183+
CCSlider* slider = sender;
184+
_distanceFieldEffect.glowWidth = slider.sliderValue;
44185
}
45186

187+
- (void)enableGlow:(id)sender
188+
{
189+
_distanceFieldEffect.glow = !_distanceFieldEffect.glow;
190+
}
191+
192+
- (void)enableOutline:(id)sender
193+
{
194+
_distanceFieldEffect.outline = !_distanceFieldEffect.outline;
195+
}
196+
197+
#endif
198+
199+
#pragma mark DropShadow
200+
46201
-(void)setupDropShadowEffectTest
47202
{
48203
self.subTitle = @"DropShadow Effect Test";
@@ -54,7 +209,7 @@ -(void)setupDropShadowEffectTest
54209
[self.contentNode addChild:environment];
55210

56211
CCColor *shadowColor = [CCColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:0.5];
57-
CCEffectDropShadow* effect = [CCEffectDropShadow effectWithShadowOffset:GLKVector2Make(2.0, -2.0) shadowColor:shadowColor];
212+
CCEffectDropShadow* effect = [CCEffectDropShadow effectWithShadowOffset:GLKVector2Make(2.0, -2.0) shadowColor:shadowColor blurRadius:5];
58213

59214
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/Ohm.png"];
60215
sampleSprite.position = ccp(0.5, 0.5);
@@ -71,6 +226,8 @@ -(void)setupDropShadowEffectTest
71226
[self.contentNode addChild:effectNode];
72227
}
73228

229+
#pragma mark Glass
230+
74231
-(void)setupGlassEffectTest
75232
{
76233
self.subTitle = @"Glass Effect Test";
@@ -453,7 +610,7 @@ -(void)setupBloomEffectTest
453610
sampleSprite3.positionType = CCPositionTypeNormalized;
454611

455612
// Blend glow maps test
456-
CCEffectBloom* glowEffect3 = [CCEffectBloom effectWithBlurRadius:8 intensity:1.0f luminanceThreshold:1.0f - ((float)i/(float)(steps-1))];
613+
CCEffectBloom* glowEffect3 = [CCEffectBloom effectWithBlurRadius:3 intensity:1.0f luminanceThreshold:1.0f - ((float)i/(float)(steps-1))];
457614
sampleSprite3.effect = glowEffect3;
458615

459616
[self.contentNode addChild:sampleSprite3];
@@ -1338,7 +1495,6 @@ - (CCSprite *)spriteWithEffects:(NSArray *)effects image:(NSString *)spriteImage
13381495
return sprite;
13391496
}
13401497

1341-
13421498
-(void)renderTextureHelper:(CCNode *)stage size:(CGSize)size
13431499
{
13441500
CCColor *color = [CCColor colorWithRed:0.0 green:0.0 blue:0.5 alpha:0.5];
@@ -1364,5 +1520,5 @@ -(void)renderTextureHelper:(CCNode *)stage size:(CGSize)size
13641520
]]];
13651521
[node addChild:sprite];
13661522
}
1367-
@end
13681523

1524+
@end

cocos2d/CCEffectBloom.m

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
@implementation CCEffectBloom {
4949
NSUInteger _numberOfOptimizedOffsets;
5050
GLfloat _sigma;
51+
NSUInteger _trueBlurRadius;
5152
BOOL _shaderDirty;
5253
float _transformedIntensity;
5354
}
@@ -73,13 +74,13 @@ -(id)initWithPixelBlurRadius:(NSUInteger)blurRadius intensity:(float)intensity l
7374
CCEffectUniform* u_intensity = [CCEffectUniform uniform:@"float" name:@"u_intensity" value:[NSNumber numberWithFloat:_transformedIntensity]];
7475
CCEffectUniform* u_luminanceThreshold = [CCEffectUniform uniform:@"float" name:@"u_luminanceThreshold" value:[NSNumber numberWithFloat:_luminanceThreshold]];
7576
CCEffectUniform* u_enableGlowMap = [CCEffectUniform uniform:@"float" name:@"u_enableGlowMap" value:[NSNumber numberWithFloat:0.0f]];
76-
CCEffectUniform* u_blurDirection = [CCEffectUniform uniform:@"vec2" name:@"u_blurDirection"
77+
CCEffectUniform* u_blurDirection = [CCEffectUniform uniform:@"highp vec2" name:@"u_blurDirection"
7778
value:[NSValue valueWithGLKVector2:GLKVector2Make(0.0f, 0.0f)]];
7879

7980
unsigned long count = (unsigned long)(1 + (_numberOfOptimizedOffsets * 2));
8081
CCEffectVarying* v_blurCoords = [CCEffectVarying varying:@"vec2" name:@"v_blurCoordinates" count:count];
8182

82-
if(self = [super initWithFragmentUniforms:@[u_enableGlowMap, u_luminanceThreshold, u_intensity]
83+
if(self = [super initWithFragmentUniforms:@[u_enableGlowMap, u_luminanceThreshold, u_intensity, u_blurDirection]
8384
vertexUniforms:@[u_blurDirection]
8485
varyings:@[v_blurCoords]])
8586
{
@@ -121,9 +122,10 @@ -(void)setBlurRadius:(NSUInteger)blurRadius
121122

122123
- (void)setBlurRadiusAndDependents:(NSUInteger)blurRadius
123124
{
125+
_trueBlurRadius = blurRadius;
124126
blurRadius = MIN(blurRadius, BLUR_OPTIMIZED_RADIUS_MAX);
125127
_blurRadius = blurRadius;
126-
_sigma = blurRadius / 2;
128+
_sigma = _trueBlurRadius / 2;
127129
if(_sigma == 0.0)
128130
_sigma = 1.0f;
129131

@@ -134,9 +136,9 @@ -(void)buildFragmentFunctions
134136
{
135137
self.fragmentFunctions = [[NSMutableArray alloc] init];
136138

137-
GLfloat *standardGaussianWeights = calloc(_blurRadius + 1, sizeof(GLfloat));
139+
GLfloat *standardGaussianWeights = calloc(_trueBlurRadius + 1, sizeof(GLfloat));
138140
GLfloat sumOfWeights = 0.0;
139-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _blurRadius + 1; currentGaussianWeightIndex++)
141+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
140142
{
141143
standardGaussianWeights[currentGaussianWeightIndex] = (1.0 / sqrt(2.0 * M_PI * pow(_sigma, 2.0))) * exp(-pow(currentGaussianWeightIndex, 2.0) / (2.0 * pow(_sigma, 2.0)));
142144

@@ -151,14 +153,14 @@ -(void)buildFragmentFunctions
151153
}
152154

153155
// Next, normalize these weights to prevent the clipping of the Gaussian curve at the end of the discrete samples from reducing luminance
154-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _blurRadius + 1; currentGaussianWeightIndex++)
156+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
155157
{
156158
standardGaussianWeights[currentGaussianWeightIndex] = standardGaussianWeights[currentGaussianWeightIndex] / sumOfWeights;
157159
}
158160

159161
// From these weights we calculate the offsets to read interpolated values from
160162
NSUInteger numberOfOptimizedOffsets = MIN(_blurRadius / 2 + (_blurRadius % 2), BLUR_OPTIMIZED_RADIUS_MAX);
161-
NSUInteger trueNumberOfOptimizedOffsets = _blurRadius / 2 + (_blurRadius % 2);
163+
NSUInteger trueNumberOfOptimizedOffsets = _trueBlurRadius / 2 + (_trueBlurRadius % 2);
162164

163165
NSMutableString *shaderString = [[NSMutableString alloc] init];
164166

@@ -244,9 +246,9 @@ -(void)buildVertexFunctions
244246
{
245247
self.vertexFunctions = [[NSMutableArray alloc] init];
246248

247-
GLfloat* standardGaussianWeights = calloc(_blurRadius + 1, sizeof(GLfloat));
249+
GLfloat* standardGaussianWeights = calloc(_trueBlurRadius + 1, sizeof(GLfloat));
248250
GLfloat sumOfWeights = 0.0;
249-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _blurRadius + 1; currentGaussianWeightIndex++)
251+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
250252
{
251253
standardGaussianWeights[currentGaussianWeightIndex] = (1.0 / sqrt(2.0 * M_PI * pow(_sigma, 2.0))) * exp(-pow(currentGaussianWeightIndex, 2.0) / (2.0 * pow(_sigma, 2.0)));
252254

@@ -261,7 +263,7 @@ -(void)buildVertexFunctions
261263
}
262264

263265
// Next, normalize these weights to prevent the clipping of the Gaussian curve at the end of the discrete samples from reducing luminance
264-
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _blurRadius + 1; currentGaussianWeightIndex++)
266+
for (NSUInteger currentGaussianWeightIndex = 0; currentGaussianWeightIndex < _trueBlurRadius + 1; currentGaussianWeightIndex++)
265267
{
266268
standardGaussianWeights[currentGaussianWeightIndex] = standardGaussianWeights[currentGaussianWeightIndex] / sumOfWeights;
267269
}

0 commit comments

Comments
 (0)