Colored sprite without texture?

Hey,

Whats the recommended to have a simple colored rectangle (or maybe even other geometries)?
As far as i know there exist some Draw rect functions or DrawPrimitive, but does there exist something like a sprite without texture, which has a color, opacity etc, and can also be added as child to the Scene?

Something like a custom geometry sprite.

I don’t mind if i have to create the Vertices my self and Color them if that is possible.

I definitly want to avoid adding a 1x1 Image and use that a dummy texture…

Have you tried LayerColor?

no, but i tried it quickly, but not sure whats the best way to use them for this case.

even if this somehow works.
it would be in general interesting if it is possible to have a geometry with like 4 vertices to created a custom geometry which still can be added as child etc. I’ve read mutliple times about TriangleCommands which is used in the sprites, but since those articles where really old not sure if those still are the recommended way of doing this.

Never used TriangleCommands, so not sure.

You could use a DrawNode. Pretty sure that allows for geometry of any number of vertices and shape, and can be added as a child. Doesn’t automatically calculate its own bounding box size, though.

You could also create the data in code for a 1x1 white texture if you want to use a Sprite but just don’t want to have to include a 1x1 image file.

The last idea I can think of is that you can create an empty sprite and then use a shader to fill it it with whichever colour you want.

template <class T>
static T* createBatchedLayerColor(cocos2d::Color4B c, float w, float h)
{
    // defaults to 2x2 white texture 
    // otherwise use a solid white image of your own
    // or use spriteframe from a spritesheet/textureatlas 
    auto node = T::create();
    if(w <= 0 || h <= 0) {
        auto ws = cocos2d::Director::getInstance()->getWinSize();
        w = ws.width;
        h = ws.height;
    }
    node->setTextureRect(cocos2d::Rect(0, 0, w, h));
    node->setColor(cocos2d::Color3B(c));
    node->setOpacity(c.a);
    node->setAnchorPoint(cocos2d::Vec2::ANCHOR_BOTTOM_LEFT);
    node->setIgnoreAnchorPointForPosition(false);
    return node;
}

template <class T>
static void setBatchedLayerColorContentSize(T* node, cocos2d::Size s)
{
    node->setTextureRect(cocos2d::Rect(0,0,s.width,s.height));
}

hmm this doesn’t work for me, can’t see anything.
Not sure if I’m using it wrong.

I guess T should be a Sprite? since only sprite Support setTextureRect

nvm it works, used 1 to 0 valies for the opacity :slight_smile: gonna use this

would be still interesting to see if it is possible somehow to Create my own geometry with Vertices .

Sprite allows for:

  • Quad
  • Polygon (3+ verts you define, but I think requires concave? maybe not)
  • 9-patch sprite (SLICE9) that defines 9 quads sharing 9 verts

So if your needs are met by these you could for example create a PolyInfo struct instance and set that in your sprite to get one that has N verts.
(see: static Sprite* create(const PolygonInfo& info))

// Also see: 
// SpriteFrameCache::initializePolygonInfo(...)
//
// TODO: fill these with your vert, texture UVs, and mesh indices
std::vector<int> vertices;
std::vector<int> verticesUV;
std::vector<int> indices;

PolygonInfo info;
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);
spriteFrame->setPolygonInfo(info);

If you need more advanced capabilities you’ll probably find yourself using PrimitiveCommand or CustomCommand rendering commands.

PrimitiveCommand: allows for setting up custom vertex/index buffers.
(see CCFastTMXLayer)

CustomCommand: allows for defining draw callbacks to execute OpenGL commands directly.
(see DrawNode, MotionStreak, ProgressTimer)

1 Like