Mesh with verts and colors

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 @stevetranby

If you are not afraid of going low level, try TrianglesCommand. See CCSprite.cpp regarding how to use it.

1 Like

@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?

@UKDeveloper99 yep, you might need to subclass it.
For custom uniforms, you can just add them to the GLProgramState that belongs to TrianglesCommand.

Also, take a look at Material.

@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.

@ricardo correct me if i’m wrong.

@milos1290

It is better to use QuadCommand (Q) and/or TrianglesCommand (T) than CustomCommand ©, since (T)s will be batched whenever possible.

Also, © breaks the “pipe”:
Let’s say that you submit the following commands: (T) + © + (T) + © + (T).
That means that the (T) won’t be batched, even if all the (T)s share the same shader + blending mode, and that is because there is a © between them.

And ©s are not portable… let’s say that in the future we add support for Vulkan or Metal… ©s can’t be ported since they are “custom”… and you (user), have to port them.

Yeah that makes sense,

But how would you normally draw mesh using GL_TRIANGLE_STRIP, and prevent interrupting batching process ?

@milos1290:

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.