Skip to content

Commit 0ef1245

Browse files
committed
Merge pull request #876 from thayerandrews/develop
CCEffectRefraction - Fix various issues uncovered by Viktor during testing
2 parents dc1c7b2 + ce7c59e commit 0ef1245

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

cocos2d/CCEffectRefraction.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,17 @@ -(void)buildFragmentFunctions
7272
// Perturb the screen space texture coordinate by the scaled normal
7373
// vector.
7474
vec2 refractTexCoords = envSpaceTexCoords.xy + normal.xy * u_refraction;
75+
76+
// This is positive if refractTexCoords is in [0..1] and negative otherwise.
77+
vec2 compare = 0.5 - abs(refractTexCoords - 0.5);
78+
79+
// This is 1.0 if both refracted texture coords are in bounds and 0.0 otherwise.
80+
float inBounds = step(0.0, min(compare.x, compare.y));
7581

7682
vec4 primaryColor = cc_FragColor * texture2D(cc_MainTexture, cc_FragTexCoord1);
77-
return primaryColor * texture2D(u_envMap, refractTexCoords);
83+
primaryColor += inBounds * texture2D(u_envMap, refractTexCoords) * (1.0 - primaryColor.a);
84+
85+
return primaryColor;
7886
);
7987

8088
CCEffectFunction* fragmentFunction = [[CCEffectFunction alloc] initWithName:@"refractionEffect" body:effectBody inputs:@[input] returnType:@"vec4"];

cocos2d/CCEffectRenderer.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect uniforms:(NSM
159159
CCTexture *previousPassTexture = nil;
160160
if (previousPassRT)
161161
{
162+
NSAssert(previousPassRT.texture, @"Texture for render target unexpectedly nil.");
162163
previousPassTexture = previousPassRT.texture;
163164
}
164165
else
165166
{
166-
previousPassTexture = sprite.texture;
167+
previousPassTexture = sprite.texture ?: [CCTexture none];
167168
}
168169

169170
CCEffectRenderPass* renderPass = [effect renderPassAtIndex:i];

cocos2d/CCSprite.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,22 @@ -(void) setSpriteFrame:(CCSpriteFrame*)frame
529529

530530
-(void) setNormalMapSpriteFrame:(CCSpriteFrame*)frame
531531
{
532+
if (!self.texture)
533+
{
534+
// If there is no texture set on the sprite, set the sprite's texture rect from the
535+
// normal map's sprite frame. Note that setting the main texture, then the normal map,
536+
// and then removing the main texture will leave the texture rect from the main texture.
537+
[self setTextureRect:frame.rect rotated:frame.rotated untrimmedSize:frame.originalSize];
538+
}
539+
540+
// Set the second texture coordinate set from the normal map's sprite frame.
532541
CCSpriteTexCoordSet texCoords = [CCSprite textureCoordsForTexture:frame.texture withRect:frame.rect rotated:frame.rotated xFlipped:_flipX yFlipped:_flipY];
533542
_verts.bl.texCoord2 = texCoords.bl;
534543
_verts.br.texCoord2 = texCoords.br;
535544
_verts.tr.texCoord2 = texCoords.tr;
536545
_verts.tl.texCoord2 = texCoords.tl;
537546

538-
// Set the main texture in the uniforms dictionary (if the dictionary exists).
547+
// Set the normal map texture in the uniforms dictionary (if the dictionary exists).
539548
self.shaderUniforms[CCShaderUniformNormalMapTexture] = (frame.texture ?: [CCTexture none]);
540549
_renderState = nil;
541550

0 commit comments

Comments
 (0)