Skip to content

Commit 9571498

Browse files
author
Thayer J Andrews
committed
CCEffectRefraction - Disable the refraction effect according to the normal map's alpha channel
If alpha == 0, then you just get the base color and texture but no contribution from the environment. This is implemented as a multiply not a conditional so alpha values between 0 and 1 will modulate the environment color.
1 parent 0b423c8 commit 9571498

File tree

6 files changed

+12
-6
lines changed

6 files changed

+12
-6
lines changed

Resources/Images/ShinyBallColor.png

54.9 KB
Loading

Resources/Images/ShinyBallNormals.png

42.3 KB
Loading

Resources/Images/ShinyTorusColor.png

48.9 KB
Loading
55.1 KB
Loading

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ -(void)setupRefractionEffectTest
5757

5858
[self.contentNode addChild:renderTexture];
5959

60-
NSString *sphereTextureFile = @"Images/sphere-256.png";
60+
NSString *sphereTextureFile = @"Images/ShinyBallColor.png";
6161
CCTexture *sphereTexture = [CCTexture textureWithFile:sphereTextureFile];
62-
CCSpriteFrame *sphereNormalMap = [CCSpriteFrame frameWithImageNamed:@"Images/sphere-normal-256.png"];
62+
CCSpriteFrame *sphereNormalMap = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyBallNormals.png"];
6363
CCEffectRefraction *sphereRefraction = [[CCEffectRefraction alloc] initWithRefraction:0.1f environment:renderTexture.sprite normalMap:nil];
6464

6565
p1 = CGPointMake(0.1f, 0.8f);
@@ -76,9 +76,9 @@ -(void)setupRefractionEffectTest
7676
]]];
7777
[self.contentNode addChild:sprite1];
7878

79-
NSString *torusTextureFile = @"Images/torus-256.png";
79+
NSString *torusTextureFile = @"Images/ShinyTorusColor.png";
8080
CCTexture *torusTexture = [CCTexture textureWithFile:torusTextureFile];
81-
CCSpriteFrame *torusNormalMap = [CCSpriteFrame frameWithImageNamed:@"Images/torus-normal-256.png"];
81+
CCSpriteFrame *torusNormalMap = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyTorusNormals.png"];
8282
CCEffectRefraction *torusRefraction = [[CCEffectRefraction alloc] initWithRefraction:0.1f environment:renderTexture.sprite normalMap:torusNormalMap];
8383

8484
p1 = CGPointMake(0.65f, 0.2f);

cocos2d/CCEffectRefraction.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ -(void)buildFragmentFunctions
6464
vec4 envSpaceTexCoords = u_screenToEnv * gl_FragCoord;
6565

6666
// Index the normal map and expand the color value from [0..1] to [-1..1]
67-
vec4 tangentSpaceNormal = texture2D(cc_NormalMapTexture, cc_FragTexCoord2) * 2.0 - 1.0;
67+
vec4 normalMap = texture2D(cc_NormalMapTexture, cc_FragTexCoord2);
68+
vec4 tangentSpaceNormal = normalMap * 2.0 - 1.0;
6869

6970
// Convert the normal vector from tangent space to environment space
7071
vec2 normal = u_tangent * tangentSpaceNormal.x + u_binormal * tangentSpaceNormal.y;
@@ -79,8 +80,13 @@ -(void)buildFragmentFunctions
7980
// This is 1.0 if both refracted texture coords are in bounds and 0.0 otherwise.
8081
float inBounds = step(0.0, min(compare.x, compare.y));
8182

83+
// Compute the combination of the sprite's color and texture.
8284
vec4 primaryColor = cc_FragColor * texture2D(cc_MainTexture, cc_FragTexCoord1);
83-
primaryColor += inBounds * texture2D(u_envMap, refractTexCoords) * (1.0 - primaryColor.a);
85+
86+
// If the refracted texture coordinates are within the bounds of the environment map
87+
// blend the primary color with the refracted environment. Multiplying by the normal
88+
// map alpha also allows the effect to be disabled for specific pixels.
89+
primaryColor += inBounds * normalMap.a * texture2D(u_envMap, refractTexCoords) * (1.0 - primaryColor.a);
8490

8591
return primaryColor;
8692
);

0 commit comments

Comments
 (0)