Skip to content

Commit 2677e6e

Browse files
author
Thayer J Andrews
committed
CCEffectRenderer - Fix texture coordinates for intermediate render passes from sprite sheets
We had been using the sprite's original texture coordinates for all render passes which will not work with sprite sheets. When reading from an intermediate render target we need the texture coordinates to be lower left (0,0) and upper right (1,1) and this is not going to be the case when a sprite draws its image from a sprite sheet. Fix this problem by overwriting the sprite texture coordinates with the required values when reading from a sprite sheet.
1 parent 7e1c72a commit 2677e6e

File tree

1 file changed

+67
-21
lines changed

1 file changed

+67
-21
lines changed

cocos2d/CCEffectRenderer.m

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@
2020
#import "CCTexture_Private.h"
2121

2222

23-
static CCSpriteVertexes padVertices(const CCSpriteVertexes *input, CGSize padding, BOOL padVertices);
24-
static CCVertex padVertex(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoord1Offset, GLKVector2 texCoord2Offset);
23+
typedef NS_ENUM(NSUInteger, CCEffectTexCoordTransform)
24+
{
25+
CCEffectTexCoordNoChange = 0,
26+
CCEffectTexCoordOverwrite = 1,
27+
CCEffectTexCoordPad = 2,
28+
};
29+
30+
31+
static CCSpriteVertexes padVertices(const CCSpriteVertexes *input, CGSize padding, CCEffectTexCoordTransform texCoordTransform);
32+
static CCVertex padVertex(CCVertex input, GLKVector2 positionOffset);
33+
static CCVertex padVertexAndTexCoords(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoord1Offset, GLKVector2 texCoord2Offset);
34+
static CCVertex padVertexAndOverwriteTexCoords(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoord1, GLKVector2 texCoord2);
35+
2536

2637
@interface CCEffectRenderTarget : NSObject
2738

@@ -229,8 +240,9 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect uniforms:(NSM
229240
// - Later pass into FB : Pad vertices but not texture coordiates
230241
// - Later pass into intermediate RT : Pad vertices but not texture coordinates, add padding to RT, adjust ortho matrix
231242
//
243+
CCEffectTexCoordTransform texCoordTransform = (fromIntermediate) ? CCEffectTexCoordOverwrite : CCEffectTexCoordPad;
232244

233-
renderPass.verts = padVertices(sprite.vertexes, effect.padding, !fromIntermediate);
245+
renderPass.verts = padVertices(sprite.vertexes, effect.padding, texCoordTransform);
234246
renderPass.texCoord1Center = GLKVector2Make((sprite.vertexes->tr.texCoord1.s + sprite.vertexes->bl.texCoord1.s) * 0.5f, (sprite.vertexes->tr.texCoord1.t + sprite.vertexes->bl.texCoord1.t) * 0.5f);
235247
renderPass.texCoord1Extents = GLKVector2Make((sprite.vertexes->tr.texCoord1.s - sprite.vertexes->bl.texCoord1.s) * 0.5f, (sprite.vertexes->tr.texCoord1.t - sprite.vertexes->bl.texCoord1.t) * 0.5f);
236248
renderPass.texCoord2Center = GLKVector2Make((sprite.vertexes->tr.texCoord2.s + sprite.vertexes->bl.texCoord2.s) * 0.5f, (sprite.vertexes->tr.texCoord2.t + sprite.vertexes->bl.texCoord2.t) * 0.5f);
@@ -364,34 +376,53 @@ - (void)freeAllRenderTargets
364376

365377
@end
366378

367-
CCSpriteVertexes padVertices(const CCSpriteVertexes *input, CGSize padding, BOOL padTexCoords)
379+
CCSpriteVertexes padVertices(const CCSpriteVertexes *input, CGSize padding, CCEffectTexCoordTransform texCoordTransform)
368380
{
369381
CCSpriteVertexes output;
370-
if (CGSizeEqualToSize(CGSizeZero, padding))
382+
if (texCoordTransform == CCEffectTexCoordNoChange)
371383
{
372-
output = *input;
384+
output.bl = padVertex(input->bl, GLKVector2Make(-padding.width, -padding.height));
385+
output.br = padVertex(input->br, GLKVector2Make( padding.width, -padding.height));
386+
output.tr = padVertex(input->tr, GLKVector2Make( padding.width, padding.height));
387+
output.tl = padVertex(input->tl, GLKVector2Make(-padding.width, padding.height));
373388
}
374-
else
389+
else if (texCoordTransform == CCEffectTexCoordOverwrite)
375390
{
376-
GLKVector2 texCoord1Step = GLKVector2Make(0.0f, 0.0f);
377-
GLKVector2 texCoord2Step = GLKVector2Make(0.0f, 0.0f);
378-
if (padTexCoords)
379-
{
380-
texCoord1Step = GLKVector2Make(padding.width * (input->br.texCoord1.s - input->bl.texCoord1.s) / (input->br.position.x - input->bl.position.x),
381-
padding.height * (input->tl.texCoord1.t - input->bl.texCoord1.t) / (input->tl.position.y - input->bl.position.y));
382-
texCoord2Step = GLKVector2Make(padding.width * (input->br.texCoord2.s - input->bl.texCoord2.s) / (input->br.position.x - input->bl.position.x),
383-
padding.height * (input->tl.texCoord2.t - input->bl.texCoord2.t) / (input->tl.position.y - input->bl.position.y));
384-
}
391+
output.bl = padVertexAndOverwriteTexCoords(input->bl, GLKVector2Make(-padding.width, -padding.height), GLKVector2Make(0.0f, 0.0f), GLKVector2Make(0.0f, 0.0f));
392+
output.br = padVertexAndOverwriteTexCoords(input->br, GLKVector2Make( padding.width, -padding.height), GLKVector2Make(1.0f, 0.0f), GLKVector2Make(1.0f, 0.0f));
393+
output.tr = padVertexAndOverwriteTexCoords(input->tr, GLKVector2Make( padding.width, padding.height), GLKVector2Make(1.0f, 1.0f), GLKVector2Make(1.0f, 1.0f));
394+
output.tl = padVertexAndOverwriteTexCoords(input->tl, GLKVector2Make(-padding.width, padding.height), GLKVector2Make(0.0f, 1.0f), GLKVector2Make(0.0f, 1.0f));
395+
}
396+
else if (texCoordTransform == CCEffectTexCoordPad)
397+
{
398+
GLKVector2 texCoord1Step = GLKVector2Make(padding.width * (input->br.texCoord1.s - input->bl.texCoord1.s) / (input->br.position.x - input->bl.position.x),
399+
padding.height * (input->tl.texCoord1.t - input->bl.texCoord1.t) / (input->tl.position.y - input->bl.position.y));
400+
GLKVector2 texCoord2Step = GLKVector2Make(padding.width * (input->br.texCoord2.s - input->bl.texCoord2.s) / (input->br.position.x - input->bl.position.x),
401+
padding.height * (input->tl.texCoord2.t - input->bl.texCoord2.t) / (input->tl.position.y - input->bl.position.y));
385402

386-
output.bl = padVertex(input->bl, GLKVector2Make(-padding.width, -padding.height), GLKVector2Make(-texCoord1Step.x, -texCoord1Step.y), GLKVector2Make(-texCoord2Step.x, -texCoord2Step.y));
387-
output.br = padVertex(input->br, GLKVector2Make( padding.width, -padding.height), GLKVector2Make( texCoord1Step.x, -texCoord1Step.y), GLKVector2Make( texCoord2Step.x, -texCoord2Step.y));
388-
output.tr = padVertex(input->tr, GLKVector2Make( padding.width, padding.height), GLKVector2Make( texCoord1Step.x, texCoord1Step.y), GLKVector2Make( texCoord2Step.x, texCoord2Step.y));
389-
output.tl = padVertex(input->tl, GLKVector2Make(-padding.width, padding.height), GLKVector2Make(-texCoord1Step.x, texCoord1Step.y), GLKVector2Make(-texCoord2Step.x, texCoord2Step.y));
403+
output.bl = padVertexAndTexCoords(input->bl, GLKVector2Make(-padding.width, -padding.height), GLKVector2Make(-texCoord1Step.x, -texCoord1Step.y), GLKVector2Make(-texCoord2Step.x, -texCoord2Step.y));
404+
output.br = padVertexAndTexCoords(input->br, GLKVector2Make( padding.width, -padding.height), GLKVector2Make( texCoord1Step.x, -texCoord1Step.y), GLKVector2Make( texCoord2Step.x, -texCoord2Step.y));
405+
output.tr = padVertexAndTexCoords(input->tr, GLKVector2Make( padding.width, padding.height), GLKVector2Make( texCoord1Step.x, texCoord1Step.y), GLKVector2Make( texCoord2Step.x, texCoord2Step.y));
406+
output.tl = padVertexAndTexCoords(input->tl, GLKVector2Make(-padding.width, padding.height), GLKVector2Make(-texCoord1Step.x, texCoord1Step.y), GLKVector2Make(-texCoord2Step.x, texCoord2Step.y));
390407
}
391408
return output;
392409
}
393410

394-
CCVertex padVertex(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoord1Offset, GLKVector2 texCoord2Offset)
411+
CCVertex padVertex(CCVertex input, GLKVector2 positionOffset)
412+
{
413+
CCVertex output;
414+
output.position.x = input.position.x + positionOffset.x;
415+
output.position.y = input.position.y + positionOffset.y;
416+
output.position.z = input.position.z;
417+
output.position.w = input.position.w;
418+
output.texCoord1 = input.texCoord1;
419+
output.texCoord2 = input.texCoord2;
420+
output.color = input.color;
421+
422+
return output;
423+
}
424+
425+
CCVertex padVertexAndTexCoords(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoord1Offset, GLKVector2 texCoord2Offset)
395426
{
396427
CCVertex output;
397428
output.position.x = input.position.x + positionOffset.x;
@@ -407,3 +438,18 @@ CCVertex padVertex(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoor
407438
return output;
408439
}
409440

441+
CCVertex padVertexAndOverwriteTexCoords(CCVertex input, GLKVector2 positionOffset, GLKVector2 texCoord1, GLKVector2 texCoord2)
442+
{
443+
CCVertex output;
444+
output.position.x = input.position.x + positionOffset.x;
445+
output.position.y = input.position.y + positionOffset.y;
446+
output.position.z = input.position.z;
447+
output.position.w = input.position.w;
448+
output.texCoord1 = texCoord1;
449+
output.texCoord2 = texCoord2;
450+
output.color = input.color;
451+
452+
return output;
453+
}
454+
455+

0 commit comments

Comments
 (0)