Skip to content

Commit 6540687

Browse files
committed
sdf - WIP
1 parent b91ef30 commit 6540687

File tree

3 files changed

+84
-27
lines changed

3 files changed

+84
-27
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ -(void)setupDropShadowEffectTest
4848

4949
[self.contentNode addChild:environment];
5050

51-
CCColor *shadowColor = [CCColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5];
51+
CCColor *shadowColor = [CCColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:0.5];
5252
CCEffectDropShadow* effect = [CCEffectDropShadow effectWithShadowOffset:GLKVector2Make(2.0, -2.0) shadowColor:shadowColor];
5353

5454
CCSprite *sampleSprite = [CCSprite spriteWithImageNamed:@"Images/Ohm.png"];

cocos2d/CCEffectDropShadow.m

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,52 @@ -(void)buildFragmentFunctions
5151
// shadowColor should be added at all.
5252
float shadowOffsetAlpha = texture2D(cc_MainTexture, cc_FragTexCoord1 - u_shadowOffset).a;
5353

54-
vec4 shadowColor = vec4(u_shadowColor.rgb, shadowOffsetAlpha * u_shadowColor.a);
54+
vec4 shadowColor = u_shadowColor*shadowOffsetAlpha;
55+
outputColor = outputColor + (1.0 - outputColor.a) * shadowColor;
5556

56-
// Since we use premultiplied alpha, we need to be careful and avoid changing the
57-
// output color of every fragment. If we add a non-zero shadowColor to the output, then
58-
// we will end up tinting the whole quad with a shadowColor.
59-
const float alphaThreshold = 0.2; // Maybe make this a uniform? it's kind of hacky..
60-
if(shadowOffsetAlpha < alphaThreshold)
61-
return outputColor; //shadowColor = vec4(0.0);
57+
// vec4 shadowColor = vec4(u_shadowColor.rgb, shadowOffsetAlpha * u_shadowColor.a);
6258

63-
// Ensures that the cc_MainTexture color does not get over written by the shadowcolor
64-
outputColor = outputColor + (1.0 - outputColor.a) * shadowColor;
59+
// // Since we use premultiplied alpha, we need to be careful and avoid changing the
60+
// // output color of every fragment. If we add a non-zero shadowColor to the output, then
61+
// // we will end up tinting the whole quad with a shadowColor.
62+
// const float alphaThreshold = 0.2; // Maybe make this a uniform? it's kind of hacky..
63+
// if(shadowOffsetAlpha < alphaThreshold)
64+
// return outputColor; //shadowColor = vec4(0.0);
65+
//
66+
// // Ensures that the cc_MainTexture color does not get over written by the shadowcolor
67+
// outputColor = outputColor + (1.0 - outputColor.a) * shadowColor;
68+
69+
70+
const float sampleDist = 1.0;
71+
const float sampleStrength = 2.2;
72+
73+
float samples[10];
74+
samples[0] = -0.08;
75+
samples[1] = -0.05;
76+
samples[2] = -0.03;
77+
samples[3] = -0.02;
78+
samples[4] = -0.01;
79+
samples[5] = 0.01;
80+
samples[6] = 0.02;
81+
samples[7] = 0.03;
82+
samples[8] = 0.05;
83+
samples[9] = 0.08;
84+
85+
vec2 dir = 0.5 - cc_FragTexCoord1;
86+
float dist = sqrt(dir.x*dir.x + dir.y*dir.y);
87+
dir = dir/dist;
88+
89+
vec4 sum = outputColor;
90+
91+
for (int i = 0; i < 10; i++)
92+
sum += texture2D( cc_MainTexture, cc_FragTexCoord1 + dir * samples[i] * sampleDist );
93+
94+
sum *= 1.0/11.0;
95+
float t = dist * sampleStrength;
96+
t = clamp( t ,0.0,1.0);
6597

98+
outputColor = mix( outputColor, sum, t );
99+
66100
return outputColor;
67101
);
68102

cocos2d/CCEffectOuterGlow.m

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ -(void)buildFragmentFunctions
4545

4646
NSString* effectBody = CC_GLSL(
4747

48+
vec4 outputColor = vec4(0.0);
4849
// Make the color values to go from [-1, 1].
4950
vec4 distanceField = 2.0 * texture2D(cc_MainTexture, cc_FragTexCoord1) - 1.0;
5051
vec4 fw = fwidth(distanceField);
5152
vec4 mask = smoothstep(-fw, fw, distanceField);
5253

53-
float distAlphaMask = distanceField.a;
54+
float distAlphaMask = distanceField.r;
5455
// outline
5556
const float min0 = 0.0;
56-
const float min1 = 0.4;
57+
const float min1 = 0.0;
5758
const float max0 = 0.4;
58-
const float max1 = 0.9;
59+
const float max1 = 0.8;
5960

6061
bool less = distAlphaMask >= min0;
6162
bool more = distAlphaMask <= max1;
6263

63-
if(less && more)
64+
if(less && more && false) // outline
6465
{
6566
float oFactor = 1.0;
6667
if(distAlphaMask <= min1)
@@ -72,28 +73,50 @@ -(void)buildFragmentFunctions
7273
oFactor = smoothstep(max1, max0, distAlphaMask);
7374
}
7475

75-
mask = vec4(1.0, 0.0, 0.0, 1.0);//mix(mask, vec4(1.0, 1.0, 0.0, 1.0), oFactor);
76+
mask = mix(mask, vec4(1.0, 0.0, 0.0, 1.0), oFactor);
7677

7778
}
7879

79-
if(distAlphaMask >= 0.5)
80-
mask.a = 1.0;
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+
}
8188
else
82-
mask.a = 0.0;
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+
}
83101

84102

85103

86-
if(false) {
87-
vec4 glowTexel = texture2D(cc_MainTexture, cc_FragTexCoord1);
88-
vec4 glowc = vec4(1.0, 0.0, 0.0, 1.0) * smoothstep(0.0, 0.0, glowTexel.a);
89-
90-
distanceField = mix(glowc, distanceField, mask);
91-
92-
vec4 outputColor = distanceField;
104+
if(true) {
105+
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);
107+
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;
93117
}
94118

95-
96-
return mask;
119+
return outputColor;
97120
);
98121

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

0 commit comments

Comments
 (0)