Skip to content

Commit 1bb1822

Browse files
author
Thayer J Andrews
committed
CCEffectReflection - Fix NDC to world space computation
Instead of calculating this within the effect, calculate it in the effect renderer. The math is dependent on whether or not we're rendering directly to the screen or to an intermediate render target and we want to hide that from effects.
1 parent 7a89a62 commit 1bb1822

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

cocos2d/CCEffectReflection.m

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#import "CCTexture.h"
1616

1717
#import "CCEffect_Private.h"
18-
#import "CCRenderer_Private.h"
1918
#import "CCSprite_Private.h"
2019

2120

@@ -196,9 +195,6 @@ -(void)buildRenderPasses
196195

197196
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_envMap"]] = weakSelf.environment.texture ?: [CCTexture none];
198197

199-
CGFloat scale = [CCDirector sharedDirector].contentScaleFactor;
200-
CGAffineTransform screenToWorld = CGAffineTransformMake(1.0f / scale, 0.0f, 0.0f, 1.0f / scale, 0.0f, 0.0f);
201-
202198
// Get the transform from world space to environment texture space. This is
203199
// concatenated with the current transform to move from local node space to
204200
// environment texture space.
@@ -213,12 +209,9 @@ -(void)buildRenderPasses
213209

214210
// Setup the transform from normalized device coordinates (NDC, which is the space our vertex
215211
// positions are in) to environment texture space. We use this to compute environment texture
216-
// coordinates in the vertex shader.
217-
GLKMatrix4 ndcToWorldMat;
218-
[pass.renderer.globalShaderUniforms[CCShaderUniformProjectionInv] getValue:&ndcToWorldMat];
219-
212+
// coordinates in the vertex shader.s
220213
GLKMatrix4 worldToReflectEnvTextureMat = CCEffectUtilsMat4FromAffineTransform(worldToReflectEnvTexture);
221-
GLKMatrix4 ndcToReflectEnvTextureMat = GLKMatrix4Multiply(worldToReflectEnvTextureMat, ndcToWorldMat);
214+
GLKMatrix4 ndcToReflectEnvTextureMat = GLKMatrix4Multiply(worldToReflectEnvTextureMat, pass.ndcToWorld);
222215

223216
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_ndcToEnv"]] = [NSValue valueWithGLKMatrix4:ndcToReflectEnvTextureMat];
224217

cocos2d/CCEffectRenderer.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "ccUtils.h"
1616

1717
#import "CCEffect_Private.h"
18+
#import "CCRenderer_Private.h"
1819
#import "CCSprite_Private.h"
1920
#import "CCTexture_Private.h"
2021

@@ -194,15 +195,33 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect uniforms:(NSM
194195
if (directRendering)
195196
{
196197
renderPass.transform = *transform;
198+
199+
GLKMatrix4 ndcToWorldMat;
200+
[renderer.globalShaderUniforms[CCShaderUniformProjectionInv] getValue:&ndcToWorldMat];
201+
renderPass.ndcToWorld = ndcToWorldMat;
197202

198203
[renderPass begin:previousPassTexture];
199204
[renderPass update];
200205
[renderPass end];
201206
}
202207
else
203208
{
204-
renderPass.transform = GLKMatrix4MakeOrtho(0.0f, _contentSize.width, 0.0f, _contentSize.height, -1024.0f, 1024.0f);
209+
BOOL inverted;
210+
211+
GLKMatrix4 renderTargetProjection = GLKMatrix4MakeOrtho(0.0f, _contentSize.width, 0.0f, _contentSize.height, -1024.0f, 1024.0f);
212+
GLKMatrix4 invRenderTargetProjection = GLKMatrix4Invert(renderTargetProjection, &inverted);
213+
NSAssert(inverted, @"Unable to invert matrix.");
214+
215+
GLKMatrix4 invGlobalProjection;
216+
[renderer.globalShaderUniforms[CCShaderUniformProjectionInv] getValue:&invGlobalProjection];
217+
218+
GLKMatrix4 ndcToNodeMat = invRenderTargetProjection;
219+
GLKMatrix4 nodeToWorldMat = GLKMatrix4Multiply(invGlobalProjection, *transform);
220+
GLKMatrix4 ndcToWorldMat = GLKMatrix4Multiply(nodeToWorldMat, ndcToNodeMat);
205221

222+
renderPass.transform = renderTargetProjection;
223+
renderPass.ndcToWorld = ndcToWorldMat;
224+
206225
CGSize rtSize = CGSizeMake(_contentSize.width * _contentScale, _contentSize.height * _contentScale);
207226
rtSize.width = (rtSize.width <= 1.0f) ? 1.0f : rtSize.width;
208227
rtSize.height = (rtSize.height <= 1.0f) ? 1.0f : rtSize.height;

cocos2d/CCEffect_Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef void (^CCEffectRenderPassEndBlock)(CCEffectRenderPass *pass);
9696
@property (nonatomic) CCRenderer* renderer;
9797
@property (nonatomic) CCSpriteVertexes verts;
9898
@property (nonatomic) GLKMatrix4 transform;
99+
@property (nonatomic) GLKMatrix4 ndcToWorld;
99100
@property (nonatomic) CCBlendMode* blendMode;
100101
@property (nonatomic) CCShader* shader;
101102
@property (nonatomic) NSMutableDictionary* shaderUniforms;

0 commit comments

Comments
 (0)