I’m looking for a class in Cocos2D-x that can take a vertex array with colors for each vert. I found DrawNode but I noticed that it only creates solid color shapes such as a red triangle. I want to create a mesh with a color for each vert and provide it with a shader so that I can do some light effects. Such as a triangle fan mesh:
I found the cocos2d::Mesh class but it seems to be specifically tailored for 3D needs and lacking some of my requirements. Does anyone know of any such class in Cocos2D-x?
If there isn’t a class that exists could anyone provide some guidance on creating one. Here is my basic understanding, feel free to comment.
Must be wrapped in a custom command if using OpenGL calls
Requires a render texture (not sure the reasoning for this part)
Should it inherit from Node? If not what class.
Probably needs to inherit the Draw function so it can override
What is the best performance solution with Cocos2D-x rendering pipeline?
@Ricardo got it working nicely, any idea how I might supply a custom vertex attribute with TrianglesCommand or is it not supported, do I have to subclass TrianglesCommand or something?
@ricardo thanks, uniforms aren’t the problem though they are very easy to use. There is a line in trianglesCommand.init()
CCASSERT(glProgramState->getVertexAttribsFlags() == 0, "No custom attributes are supported in QuadCommand");
I'm not sure if this is relevant to triangleCommand as well.
I just need to supply a vertex attribute to the glprogram, some guidance would be great. I’m using the float Z component of the vec4 position as a workaround for now.
ah… yeah. That assert is because the Renderer is creating the VBO/VAO for triangle command, and you can’t provide your own VBO/VAO. So, TriangleCommand can’t have custom VBO/VAO… this is bug/limitation. I can fix it for v3.11. Could you open an issue?
Workaround 1:
use Material + mesh command. Take a look at CCMesh.cpp in order to learn how to use it.
Workaround 2 (easier):
Use a CustomCommand and do whatever you want in the draw function.
See LayerColor for a working example.
As @ricardo said, using CustomCommand you can easily start with. But i feel like using a CustomCommand will give you more performance problems. I have like 6-7 meshes with around 300 vertices and i see frame drops on pretty new devices, only on Android though. I’m pretty sure my OpenGL code is good. My bet is because custom commands somehow interrupts batching of other commands that should normally batch.
I’m not sure if I understood the question, but you can create your own node, something similar to DrawNode and implement a custom command that draws all the “queued” GL_TRIANGLE_STRIP.
By doing that, you can batch everything that you want… in your own node.