Skip to content

Commit 8b12673

Browse files
committed
Finally got outerglow working with a SDF (still needs some clean up).
1 parent 6540687 commit 8b12673

File tree

4 files changed

+20
-65
lines changed

4 files changed

+20
-65
lines changed

Resources/Images/DistanceFieldX.png

2.26 KB
Loading

Resources/Images/dist4.tga

-16 KB
Binary file not shown.

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ -(void)setupOuterGlowEffectTest
2020
{
2121
self.subTitle = @"OuterGlow Effect Test";
2222

23-
CCNodeColor* environment = [CCNodeColor nodeWithColor:[CCColor whiteColor]];
23+
// CCNodeColor* environment = [CCNodeColor nodeWithColor:[CCColor whiteColor]];
24+
CCSprite *environment = [CCSprite spriteWithImageNamed:@"Images/MountainPanorama.jpg"];
2425
environment.positionType = CCPositionTypeNormalized;
2526
environment.anchorPoint = ccp(0.5, 0.5);
2627
environment.position = ccp(0.5f, 0.5f);
@@ -30,7 +31,7 @@ -(void)setupOuterGlowEffectTest
3031
CCColor *glowColor = [CCColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5];
3132
CCEffectOuterGlow* effect = [CCEffectOuterGlow effectWithGlowColor:glowColor];
3233

33-
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/dist4.tga"];
34+
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/DistanceFieldX.png"];
3435
sampleSprite.position = ccp(0.5, 0.5);
3536
sampleSprite.positionType = CCPositionTypeNormalized;
3637
sampleSprite.effect = effect;

cocos2d/CCEffectOuterGlow.m

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -44,79 +44,33 @@ -(void)buildFragmentFunctions
4444
self.fragmentFunctions = [[NSMutableArray alloc] init];
4545

4646
NSString* effectBody = CC_GLSL(
47+
// works with DistanceFieldX.png
4748

48-
vec4 outputColor = vec4(0.0);
49-
// Make the color values to go from [-1, 1].
50-
vec4 distanceField = 2.0 * texture2D(cc_MainTexture, cc_FragTexCoord1) - 1.0;
51-
vec4 fw = fwidth(distanceField);
52-
vec4 mask = smoothstep(-fw, fw, distanceField);
49+
vec4 outputColor = vec4(0.0, 0.0, 0.0, 1.0);
50+
float distAlphaMask = texture2D(cc_MainTexture, cc_FragTexCoord1).r;
5351

54-
float distAlphaMask = distanceField.r;
55-
// outline
56-
const float min0 = 0.0;
57-
const float min1 = 0.0;
58-
const float max0 = 0.4;
59-
const float max1 = 0.8;
52+
float center = 0.46;
53+
float transition = fwidth(distAlphaMask) * 1.0;
6054

61-
bool less = distAlphaMask >= min0;
62-
bool more = distAlphaMask <= max1;
63-
64-
if(less && more && false) // outline
65-
{
66-
float oFactor = 1.0;
67-
if(distAlphaMask <= min1)
68-
{
69-
oFactor = smoothstep(min0, min1, distAlphaMask);
70-
}
71-
else
72-
{
73-
oFactor = smoothstep(max1, max0, distAlphaMask);
74-
}
75-
76-
mask = mix(mask, vec4(1.0, 0.0, 0.0, 1.0), oFactor);
77-
78-
}
79-
80-
if(false) // soft edges
81-
{
82-
const float min = 0.2;
83-
const float max = 0.8;
84-
distanceField.r *= smoothstep(min, max, distAlphaMask);
85-
distanceField.g *= smoothstep(min, max, distAlphaMask);
86-
distanceField.b *= smoothstep(min, max, distAlphaMask);
87-
}
88-
else
89-
{
90-
if(distAlphaMask >= 0.5)
91-
{
92-
// distanceField.a = 1.0;
93-
distanceField.rgb = vec3(1.0);
94-
}
95-
else
96-
{
97-
//distanceField.a = 0.0;
98-
distanceField.rgb = vec3(0.0);
99-
}
100-
}
55+
float min = center - transition;
56+
float max = center + transition;
10157

58+
// soft edges
59+
outputColor.a *= smoothstep(min, max, distAlphaMask);
10260

103-
104-
if(true) {
61+
// glow
62+
if(true)
63+
{
10564
vec4 glowTexel = texture2D(cc_MainTexture, cc_FragTexCoord1);
106-
vec4 glowc = vec4(1.0, 0.0, 0.0, 1.0) * smoothstep(0.2, 0.4, glowTexel.r);
65+
min -= 0.09;
66+
max -= 0.05;
67+
vec4 glowc = u_glowColor * smoothstep(min, max, glowTexel.r);
10768

108-
distanceField = mix(glowc, distanceField, mask);
109-
}
110-
//else
111-
{
112-
outputColor.r = distanceField.r + (1.0 - distanceField.r) * mask.r;
113-
outputColor.g = distanceField.g + (1.0 - distanceField.g) * mask.g;
114-
outputColor.b = distanceField.b + (1.0 - distanceField.b) * mask.b;
115-
outputColor.a = distanceField.a;
116-
//outputColor = distanceField + mask;
69+
outputColor = mix(glowc, outputColor, outputColor.a);
11770
}
11871

11972
return outputColor;
73+
12074
);
12175

12276
CCEffectFunction* fragmentFunction = [[CCEffectFunction alloc] initWithName:@"outerGlowEffect"

0 commit comments

Comments
 (0)