Skip to content

Commit 516ef6d

Browse files
committed
Added inner glow (using distance fields). Fixed minor bugs with outline effect. Cleaned up comments.
1 parent 7d1c580 commit 516ef6d

File tree

7 files changed

+335
-9
lines changed

7 files changed

+335
-9
lines changed

cocos2d-ios.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@
346346
D28A2E1D1954F7E000ADC03D /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D28A2E1C1954F7E000ADC03D /* OpenGLES.framework */; };
347347
D28A2E211954F85700ADC03D /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D28A2E201954F85700ADC03D /* AVFoundation.framework */; };
348348
D291DCC2195B2FA100278EC0 /* libObjectiveChipmunk-iPhone.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B759E52D1880C66900E8166C /* libObjectiveChipmunk-iPhone.a */; };
349+
D299CE7A19C2910B00519CBB /* CCEffectDFInnerGlow.h in Headers */ = {isa = PBXBuildFile; fileRef = D299CE7819C2910B00519CBB /* CCEffectDFInnerGlow.h */; };
350+
D299CE7B19C2910B00519CBB /* CCEffectDFInnerGlow.h in Headers */ = {isa = PBXBuildFile; fileRef = D299CE7819C2910B00519CBB /* CCEffectDFInnerGlow.h */; };
351+
D299CE7C19C2910B00519CBB /* CCEffectDFInnerGlow.m in Sources */ = {isa = PBXBuildFile; fileRef = D299CE7919C2910B00519CBB /* CCEffectDFInnerGlow.m */; };
352+
D299CE7D19C2910B00519CBB /* CCEffectDFInnerGlow.m in Sources */ = {isa = PBXBuildFile; fileRef = D299CE7919C2910B00519CBB /* CCEffectDFInnerGlow.m */; };
349353
D2DDB09319805E8400233D80 /* CCMathUtilsAndroid.h in Headers */ = {isa = PBXBuildFile; fileRef = D2DDB08819805E8400233D80 /* CCMathUtilsAndroid.h */; };
350354
D2DDB09419805E8400233D80 /* CCMathUtilsAndroid.h in Headers */ = {isa = PBXBuildFile; fileRef = D2DDB08819805E8400233D80 /* CCMathUtilsAndroid.h */; };
351355
D2DDB09519805E8400233D80 /* CCMatrix3.h in Headers */ = {isa = PBXBuildFile; fileRef = D2DDB08919805E8400233D80 /* CCMatrix3.h */; };
@@ -1074,6 +1078,8 @@
10741078
D28A2E1A1954F68D00ADC03D /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
10751079
D28A2E1C1954F7E000ADC03D /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; };
10761080
D28A2E201954F85700ADC03D /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
1081+
D299CE7819C2910B00519CBB /* CCEffectDFInnerGlow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCEffectDFInnerGlow.h; sourceTree = "<group>"; };
1082+
D299CE7919C2910B00519CBB /* CCEffectDFInnerGlow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCEffectDFInnerGlow.m; sourceTree = "<group>"; };
10771083
D2B840C31909F447008063EA /* CCRenderTexture_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CCRenderTexture_Private.h; sourceTree = "<group>"; };
10781084
D2DDB08819805E8400233D80 /* CCMathUtilsAndroid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCMathUtilsAndroid.h; path = Platforms/Android/CCMathUtilsAndroid.h; sourceTree = "<group>"; };
10791085
D2DDB08919805E8400233D80 /* CCMatrix3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCMatrix3.h; path = Platforms/Android/CCMatrix3.h; sourceTree = "<group>"; };
@@ -1852,6 +1858,8 @@
18521858
D27451B619AE5517006DA0A1 /* CCEffectDistanceField.m */,
18531859
D27451C419B111A9006DA0A1 /* CCEffectDFOutline.h */,
18541860
D27451C519B111A9006DA0A1 /* CCEffectDFOutline.m */,
1861+
D299CE7819C2910B00519CBB /* CCEffectDFInnerGlow.h */,
1862+
D299CE7919C2910B00519CBB /* CCEffectDFInnerGlow.m */,
18551863
);
18561864
name = DistanceField;
18571865
sourceTree = "<group>";
@@ -2018,6 +2026,7 @@
20182026
50F7B2780F28DE7C00057537 /* CCActionEase.h in Headers */,
20192027
571CD02819649E03003D460C /* CCPlatformTextField.h in Headers */,
20202028
B798D1451820305400E7BFCD /* CCSprite_Private.h in Headers */,
2029+
D299CE7A19C2910B00519CBB /* CCEffectDFInnerGlow.h in Headers */,
20212030
50BAF3990F33CCD8003F654C /* ccMacros.h in Headers */,
20222031
B798D13D181F2E4500E7BFCD /* CCNode_Private.h in Headers */,
20232032
509A79970F6188420032F449 /* CCSprite.h in Headers */,
@@ -2309,6 +2318,7 @@
23092318
D2FEB693194F6C9E00FC0574 /* CCProgressNode.h in Headers */,
23102319
D2DDB09419805E8400233D80 /* CCMathUtilsAndroid.h in Headers */,
23112320
D2FEB694194F6C9E00FC0574 /* CCLayout.h in Headers */,
2321+
D299CE7B19C2910B00519CBB /* CCEffectDFInnerGlow.h in Headers */,
23122322
D2FEB695194F6C9E00FC0574 /* NSThread+performBlock.h in Headers */,
23132323
D2FEB696194F6C9E00FC0574 /* CCTiledMapLayer_Private.h in Headers */,
23142324
D2FEB697194F6C9E00FC0574 /* ALSource.h in Headers */,
@@ -2496,6 +2506,7 @@
24962506
files = (
24972507
5018F26A0DFDEAFF00C013A5 /* CCAction.m in Sources */,
24982508
D3903B0B1995285B003AA81A /* CCEffectBlur.m in Sources */,
2509+
D299CE7C19C2910B00519CBB /* CCEffectDFInnerGlow.m in Sources */,
24992510
5018F26C0DFDEAFF00C013A5 /* CCNode.m in Sources */,
25002511
5018F26E0DFDEAFF00C013A5 /* CCDirector.m in Sources */,
25012512
5018F2700DFDEAFF00C013A5 /* CCActionInstant.m in Sources */,
@@ -2662,6 +2673,7 @@
26622673
files = (
26632674
57202D391970776100238D56 /* CCPlatformTextFieldAndroid.m in Sources */,
26642675
D2FEB6B9194F6C9E00FC0574 /* CCAction.m in Sources */,
2676+
D299CE7D19C2910B00519CBB /* CCEffectDFInnerGlow.m in Sources */,
26652677
D2FEB6BA194F6C9E00FC0574 /* CCNode.m in Sources */,
26662678
D2FEB6BB194F6C9E00FC0574 /* CCDirector.m in Sources */,
26672679
D2FEB6BC194F6C9E00FC0574 /* CCActionInstant.m in Sources */,

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ @implementation CCEffectsTest {
1212
#if CC_EFFECTS_EXPERIMENTAL
1313
CCEffectDistanceField* _distanceFieldEffect;
1414
CCEffectDFOutline* _outlineEffect;
15+
CCEffectDFInnerGlow* _innerGlowEffect;
1516
#endif
1617
}
1718

@@ -29,6 +30,56 @@ -(id)init
2930

3031
#if CC_EFFECTS_EXPERIMENTAL
3132

33+
-(void)setupDFInnerGlowTest
34+
{
35+
self.subTitle = @"Distance Field Inner Glow Test";
36+
37+
CCSprite *environment = [CCSprite spriteWithImageNamed:@"Images/MountainPanorama.jpg"];
38+
environment.positionType = CCPositionTypeNormalized;
39+
environment.anchorPoint = ccp(0.5, 0.5);
40+
environment.position = ccp(0.5f, 0.5f);
41+
42+
CCTexture* texture = [[CCTextureCache sharedTextureCache] addImage:@"Images/output.png"];
43+
44+
CCColor* fillColor = [CCColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:0.5];
45+
_innerGlowEffect = [CCEffectDFInnerGlow effectWithGlowColor:[CCColor redColor] fillColor:fillColor glowWidth:2 fieldScale:32 distanceField:texture];
46+
47+
CCSprite *dfSprite = [CCSprite spriteWithImageNamed:@"Images/df_sprite.png"];
48+
dfSprite.position = ccp(0.5, 0.5);
49+
dfSprite.positionType = CCPositionTypeNormalized;
50+
dfSprite.effect = _innerGlowEffect;
51+
dfSprite.scale = 1.0f;
52+
53+
CCSpriteFrame* background = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-background.png"];
54+
CCSpriteFrame* backgroundHilite = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-background-hilite.png"];
55+
CCSpriteFrame* handle = [CCSpriteFrame frameWithImageNamed:@"Tests/slider-handle.png"];
56+
57+
CCSlider* slider = [[CCSlider alloc] initWithBackground:background andHandleImage:handle];
58+
[slider setBackgroundSpriteFrame:backgroundHilite forState:CCControlStateHighlighted];
59+
slider.positionType = CCPositionTypeNormalized;
60+
slider.position = ccp(0.1f, 0.5f);
61+
slider.sliderValue = 0.3;
62+
slider.preferredSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
63+
slider.preferredSize = CGSizeMake(0.5f, 10);
64+
slider.rotation = 90;
65+
slider.anchorPoint = ccp(0.5f, 0.5f);
66+
slider.scale = 0.8;
67+
68+
[slider setTarget:self selector:@selector(innerGlowWidthChanged:)];
69+
70+
[self.contentNode addChild:environment];
71+
[self.contentNode addChild:slider];
72+
[self.contentNode addChild:dfSprite];
73+
}
74+
75+
- (void)innerGlowWidthChanged:(id)sender
76+
{
77+
const int innerGloWMax = 6;
78+
CCSlider* slider = sender;
79+
_innerGlowEffect.glowWidth = slider.sliderValue * innerGloWMax;
80+
}
81+
82+
3283
-(void)setupDFOutlineEffectTest
3384
{
3485
self.subTitle = @"Distance Field Outline Test";
@@ -40,8 +91,8 @@ -(void)setupDFOutlineEffectTest
4091

4192
CCTexture* texture = [[CCTextureCache sharedTextureCache] addImage:@"Images/output.png"];
4293

43-
CCColor* fillColor = [CCColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
44-
_outlineEffect = [CCEffectDFOutline effectWithOutlineColor:[CCColor redColor] fillColor:fillColor outlineWidth:3 fieldScale:32 distanceField:texture];
94+
CCColor* fillColor = [CCColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.0];
95+
_outlineEffect = [CCEffectDFOutline effectWithOutlineColor:[CCColor redColor] fillColor:fillColor outlineWidth:1 fieldScale:32 distanceField:texture];
4596

4697
CCSprite *dfSprite = [CCSprite spriteWithImageNamed:@"Images/df_sprite.png"];
4798
dfSprite.position = ccp(0.5, 0.5);
@@ -57,12 +108,12 @@ -(void)setupDFOutlineEffectTest
57108
[slider setBackgroundSpriteFrame:backgroundHilite forState:CCControlStateHighlighted];
58109
slider.positionType = CCPositionTypeNormalized;
59110
slider.position = ccp(0.1f, 0.5f);
60-
61111
slider.preferredSizeType = CCSizeTypeMake(CCSizeUnitNormalized, CCSizeUnitUIPoints);
62112
slider.preferredSize = CGSizeMake(0.5f, 10);
63113
slider.rotation = 90;
64114
slider.anchorPoint = ccp(0.5f, 0.5f);
65115
slider.scale = 0.8;
116+
slider.sliderValue = 0.1;
66117

67118
[slider setTarget:self selector:@selector(outlineWidthChagne:)];
68119

cocos2d/CCEffectDFInnerGlow.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// CCEffectDFInnerGlow.h
3+
// cocos2d-ios
4+
//
5+
// Created by Oleg Osin on 9/11/14.
6+
//
7+
//
8+
9+
#import "CCEffect.h"
10+
11+
/**
12+
* CCEffectDFInnerGlow create an inner glow on any sprite (combined with a distance field).
13+
*
14+
*/
15+
#if CC_EFFECTS_EXPERIMENTAL
16+
17+
@interface CCEffectDFInnerGlow : CCEffect
18+
19+
/// -----------------------------------------------------------------------
20+
/// @name Accessing Effect Attributes
21+
/// -----------------------------------------------------------------------
22+
23+
/** Color of the sprite (pass a fillColor with with alpha value of 0.0 to see the actual sprite) */
24+
@property (nonatomic, strong) CCColor* fillColor;
25+
26+
/** Color of glow */
27+
@property (nonatomic, strong) CCColor* glowColor;
28+
29+
/** Outline width pixel width of the glow */
30+
@property (nonatomic) int glowWidth;
31+
32+
// Distance Field, generated by SDFGen (https://github.com/osinoleg/SDFGen)
33+
@property (nonatomic, strong) CCTexture* distanceField;
34+
35+
/// -----------------------------------------------------------------------
36+
/// @name Initializing a CCEffectDFInnerGlow object
37+
/// -----------------------------------------------------------------------
38+
39+
/**
40+
* Initializes a CCEffectDFInnerGlow.
41+
*
42+
* @return The CCEffectDFInnerGlow object.
43+
*/
44+
-(id)init;
45+
46+
/**
47+
* Initializes a CCEffectDFInnerGlow object with the supplied parameters.
48+
*
49+
* @param glowColor Color of the glow, a [CCColor blackColor] will result in an opaque black glow.
50+
* @param fillColor Color of the fillColor, a [CCColor blackColor] will result in an opaque black fillColor.
51+
* @param glowWidth pixel width of the glow.
52+
* @param fieldScale, defined by the distance field generation proccess, for example a distance field that
53+
* was generated with a 4096/4096 input and output a 128/128 texture would have a fieldScale of 32 (
54+
* input size / output size). Note: this parameter could be automatically calculated if we assume that all
55+
* distance fields are generated from a 4096 input.
56+
*
57+
* @return The CCEffectDFInnerGlow object.
58+
*/
59+
-(id)initWithGlowColor:(CCColor*)glowColor fillColor:(CCColor*)fillColor glowWidth:(int)glowWidth fieldScale:(float)fieldScale distanceField:(CCTexture*)distanceField;
60+
61+
62+
/// -----------------------------------------------------------------------
63+
/// @name Creating a CCEffectDFInnerGlow object
64+
/// -----------------------------------------------------------------------
65+
66+
/**
67+
* Initializes a CCEffectDFInnerGlow object with the supplied parameters.
68+
*
69+
* @param glowColor Color of the glow, a [CCColor blackColor] will result in an opaque black glow.
70+
* @param fillColor Color of the fillColor, a [CCColor blackColor] will result in an opaque black fillColor.
71+
* @param glowWidth pixel width of the glow.
72+
* @param fieldScale, defined by the distance field generation proccess, for example a distance field that
73+
* was generated with a 4096/4096 input and output a 128/128 texture would have a fieldScale of 32 (
74+
* input size / output size). Note: this parameter could be automatically calculated if we assume that all
75+
* distance fields are generated from a 4096 input.
76+
*
77+
* @return The CCEffectDFInnerGlow object.
78+
*/
79+
+(id)effectWithGlowColor:(CCColor*)glowColor fillColor:(CCColor*)fillColor glowWidth:(int)glowWidth fieldScale:(float)fieldScale distanceField:(CCTexture*)distanceField;
80+
81+
@end
82+
83+
#endif
84+

0 commit comments

Comments
 (0)