Use CCGrid to deform CCSprite

Hi. I’d to have a way to deform arbitrarily a sprite (e.g., suppose I have a road texture and I want to deform the sprite to follow a spline). I have the suspicion that I can use CCGrid3D for this objective, because I’ve seen it in action for the effects (Waves, Ripples, etc.)

I’m observing that the CCGrid, however, tries to use the window coordinates, and as such is oriented to be used only for window effects.

I’ve tried to google for this to no avail, can anyone point me in the right direction to have this effect in my sprites?

Thanks.

This is going to be a bit tricky. You need to deform your vertices for the sprite, either in vertex shader or on CPU. Now, a sprite only has 4 vertices, which limits the ways you can deform it. You need to create your own mesh for this object with sufficient resolution of vertices for your effect. I’m not aware of any built-in way in cocos2d-x for this.

Thanks KJS, it’s just the answer I needed :slight_smile: I hope I can come up with a way to do it and share it here.

I could manage to do a deformable sprite class by copying some stuff from CCGrid to a sublcass of CCSprite :slight_smile:

I’d like to share the class, but I have not tested it throughly, I have to test in case the texture comes from an atlas non rotated, and see how it would interact with CCSpriteBatchNode (as this is a terrain it will be repeated many times and I’d like to have just one OpenGL call to draw them all.

SpriteBatchNode assumes a quad to be rendered, i doubt it will work for you. Have you considered trying out Sprite3D for this? Cocos2d-x probably also batches those meshes for you.

Yes, I know SpriteBatchNode does its own drawing routine and skips the Sprite draw function.

I’m checking Sprite3D out, thanks. I’m still working in 2.x, sadly. I don’t know if it’s worth doing the big jump :stuck_out_tongue: