Question

I am not able to run a shader on a CCSprite comming from a CCTMXTiledMap. After a lot's of research on the internet, I am starting to think that it is may be not possible but I didn't see any documentation or example about it... Has anyone already done this ? If it is possible could you tell me what I am doing wrong ?

Here the code of the creation of the CCTMXTiledMap : (I don't have any problem with it, all the sprites of my CCTMXTiledMap are correctly displaying)

CCTMXTiledMap *tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"FogTMX.tmx"];
[self addChild:tileMap z:2 tag:666];

Here the code where I launch the shader for the CCSprite :

CCTMXLayer *layer = [tileMap layerNamed:@"Layer 0"];
CCSprite *tile = [layer tileAt:ccp(10,10)];
const GLchar * fragmentSource = (GLchar*) [[NSString stringWithContentsOfFile:[[CCFileUtils sharedFileUtils] fullPathFromRelativePath:@"CSEAlpha.fsh"] encoding:NSUTF8StringEncoding error:nil] UTF8String];
tile.shaderProgram = [[CCGLProgram alloc] initWithVertexShaderByteArray:ccPositionTextureA8Color_vert fragmentShaderByteArray:fragmentSource];
[tile.shaderProgram addAttribute:kCCAttributeNamePosition index:kCCVertexAttrib_Position];
[tile.shaderProgram addAttribute:kCCAttributeNameTexCoord index:kCCVertexAttrib_TexCoords];
[tile.shaderProgram link];
[tile.shaderProgram updateUniforms];
[tile.shaderProgram use];
  • I don't have any problem to change the properties of the CCSprite "tile" (coming from the CCTMXLayer. For example with : [tile setColor:ccc3(100,100,100)];)
  • I can launch the shader (CCGLProgram) for a "normal" sprite created with CCSprite *normalSprite = [CCSprite spriteWithFile:@"Fog.png"];
  • I tried it with cocos2d v2.0.0 and cocos2d v2.1.rc2

Thanks for any help.

Was it helpful?

Solution

"CCTMXLayer uses a CCSpriteBatchNode for its rendering, you can change the shader of this node however it will apply to all your tiles as they are using the same texture.

I do not think there is functionality at present to modify the shader on a per CCSprite basis when part of a CCSpriteBatchNode."

Thanks to cocojoe from : http://www.cocos2d-iphone.org

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top