@@ -230,30 +230,53 @@ -(void)buildRenderPasses
230230 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_refractEnvMap" ]] = weakSelf.refractionEnvironment .texture ?: [CCTexture none ];
231231 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_reflectEnvMap" ]] = weakSelf.reflectionEnvironment .texture ?: [CCTexture none ];
232232
233- // Setup the NDC space to refract environment space matrix.
234- CGAffineTransform worldToRefractEnvTexture = CCEffectUtilsWorldToEnvironmentTransform (weakSelf.refractionEnvironment );
235- GLKMatrix4 ndcToRefractEnvTextureMat = GLKMatrix4Multiply (CCEffectUtilsMat4FromAffineTransform (worldToRefractEnvTexture), pass.ndcToWorld );
236- pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_ndcToRefractEnv" ]] = [NSValue valueWithGLKMatrix4: ndcToRefractEnvTextureMat];
237233
238- // Setup the NDC space to reflect environment space matrix.
239- CGAffineTransform worldToReflectEnvTexture = CCEffectUtilsWorldToEnvironmentTransform (weakSelf.reflectionEnvironment );
240- GLKMatrix4 ndcToReflectEnvTextureMat = GLKMatrix4Multiply (CCEffectUtilsMat4FromAffineTransform (worldToReflectEnvTexture), pass.ndcToWorld );
241- pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_ndcToReflectEnv" ]] = [NSValue valueWithGLKMatrix4: ndcToReflectEnvTextureMat];
234+
235+ // Get the transform from the affected node's local coordinates to the environment node.
236+ GLKMatrix4 effectNodeToRefractEnvNode = CCEffectUtilsTransformFromNodeToNode (pass.node , weakSelf.refractionEnvironment , nil );
237+
238+ // Concatenate the node to environment transform with the environment node to environment texture transform.
239+ // The result takes us from the affected node's coordinates to the environment's texture coordinates. We need
240+ // this when computing the tangent and normal vectors below.
241+ GLKMatrix4 effectNodeToRefractEnvTexture = GLKMatrix4Multiply (CCEffectUtilsMat4FromAffineTransform (weakSelf.refractionEnvironment .nodeToTextureTransform ), effectNodeToRefractEnvNode);
242+
243+ // Concatenate the node to environment texture transform together with the transform from NDC to local node
244+ // coordinates. (NDC == normalized device coordinates == render target coordinates that are normalized to the
245+ // range 0..1). The shader uses this to map from NDC directly to environment texture coordinates.
246+ GLKMatrix4 ndcToRefractEnvTexture = GLKMatrix4Multiply (effectNodeToRefractEnvTexture, pass.ndcToNodeLocal );
247+ pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_ndcToRefractEnv" ]] = [NSValue valueWithGLKMatrix4: ndcToRefractEnvTexture];
242248
243249 // Setup the tangent and binormal vectors for the refraction environment
244- GLKVector4 refractTangent = CCEffectUtilsTangentInEnvironmentSpace (pass. transform , CCEffectUtilsMat4FromAffineTransform (worldToRefractEnvTexture ));
250+ GLKVector4 refractTangent = GLKVector4Normalize ( GLKMatrix4MultiplyVector4 (effectNodeToRefractEnvTexture, GLKVector4Make ( 1 . 0f , 0 . 0f , 0 . 0f , 0 . 0f ) ));
245251 GLKVector4 refractNormal = GLKVector4Make (0 .0f , 0 .0f , 1 .0f , 1 .0f );
246252 GLKVector4 refractBinormal = GLKVector4CrossProduct (refractNormal, refractTangent);
247253 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_refractTangent" ]] = [NSValue valueWithGLKVector2: GLKVector2Make (refractTangent.x, refractTangent.y)];
248254 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_refractBinormal" ]] = [NSValue valueWithGLKVector2: GLKVector2Make (refractBinormal.x, refractBinormal.y)];
249255
250- // Setup the tangent and binormal vectors for the reflection environment.
251- GLKVector4 reflectTangent = CCEffectUtilsTangentInEnvironmentSpace (pass.transform , CCEffectUtilsMat4FromAffineTransform (worldToReflectEnvTexture));
256+
257+
258+ // Get the transform from the affected node's local coordinates to the environment node.
259+ GLKMatrix4 effectNodeToReflectEnvNode = CCEffectUtilsTransformFromNodeToNode (pass.node , weakSelf.reflectionEnvironment , nil );
260+
261+ // Concatenate the node to environment transform with the environment node to environment texture transform.
262+ // The result takes us from the affected node's coordinates to the environment's texture coordinates. We need
263+ // this when computing the tangent and normal vectors below.
264+ GLKMatrix4 effectNodeToReflectEnvTexture = GLKMatrix4Multiply (CCEffectUtilsMat4FromAffineTransform (weakSelf.reflectionEnvironment .nodeToTextureTransform ), effectNodeToReflectEnvNode);
265+
266+ // Concatenate the node to environment texture transform together with the transform from NDC to local node
267+ // coordinates. (NDC == normalized device coordinates == render target coordinates that are normalized to the
268+ // range 0..1). The shader uses this to map from NDC directly to environment texture coordinates.
269+ GLKMatrix4 ndcToReflectEnvTexture = GLKMatrix4Multiply (effectNodeToReflectEnvTexture, pass.ndcToNodeLocal );
270+ pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_ndcToReflectEnv" ]] = [NSValue valueWithGLKMatrix4: ndcToReflectEnvTexture];
271+
272+ // Setup the tangent and binormal vectors for the reflection environment
273+ GLKVector4 reflectTangent = GLKVector4Normalize (GLKMatrix4MultiplyVector4 (effectNodeToReflectEnvTexture, GLKVector4Make (1 .0f , 0 .0f , 0 .0f , 0 .0f )));
252274 GLKVector4 reflectNormal = GLKVector4Make (0 .0f , 0 .0f , 1 .0f , 1 .0f );
253275 GLKVector4 reflectBinormal = GLKVector4CrossProduct (reflectNormal, reflectTangent);
254276 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_reflectTangent" ]] = [NSValue valueWithGLKVector2: GLKVector2Make (reflectTangent.x, reflectTangent.y)];
255277 pass.shaderUniforms [weakSelf.uniformTranslationTable[@" u_reflectBinormal" ]] = [NSValue valueWithGLKVector2: GLKVector2Make (reflectBinormal.x, reflectBinormal.y)];
256-
278+
279+
257280 } copy]];
258281
259282 self.renderPasses = @[pass0];
0 commit comments