Hi. Shader can do it easy.
But shader and stuff sometimes hard to use. but in my example it is very simple. Just do this step by step:
1- Create one dummy sprite
auto s = Sprite::create("notImportantImage.png");
addChild(s);
2- write this lines for shader attaching to this sprite.
auto fileUtiles = cocos2d::FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename("gradient.fsh");
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
cocos2d::GLProgramState* glState=GLProgramState::getOrCreateWithGLProgram(cocos2d::GLProgram::createWithByteArrays(cocos2d::ccPositionTextureColor_noMVP_vert, fragSource.c_str()));
s->setGLProgramState(glState);
3- create fragment shader file and name it gradient.fsh and put this file in resource folder of your project.
4- In gradient.fsh write this code:
Only one moment. Now I am using a white image (540x960) to create the sprite. I am wondering if I can create the image by the code instead? I looked in the API reference, but did not find anything.
Glad to help you.
My code is linear distribution between two colour. you can use non linear for some cool effects.
Try to use colours as uniform in shader and then you can animate your colours and that’s the power of shaders.
I already used uniform in my code in the previous post and it seems that it works. I just thought that it is better to have a set colors than a set of shaders. But it is a cool idea to try to animate it.
Another newbie question.
If I create a sprite using a 10x10 image, then how many times will the fragment shader be called? 100?
I do not see how I can get this gradient with LayerGradient.
Or do you suggest using two gradients one by one?
And I looked in the source code of CCLayer.cpp and it is interesting that LayerColor (and LayerGradient) does not use the image. Although I am still too newbie to fully understand the code.
Apparently I need to study the manual on the opengl.org carefully and for a long time, because now I kind of fell in love with shaders.
p.s. @slackmoehrle We still do not have the 3.16 api reference on the site.