How to dynamically texture sprite3d?

Hi All,

I’d like to do something that seems like it should be pretty simple; have tried a few variations from other topics; but nothing seems to work. I have an un-textured 3D plane that I’ve exported from blender as a wavefront obj; and I want to (based off of user interaction) dynamically add a 2D texture to this plane using code rather than texturing it in blender (so that I can switch other textures in later based off of user interaction). Its a square plane 1x1 gl units; and the texture I’m trying is 512x512 px png.

spr = Sprite3D::create("plane.obj");

Some of the variations I’ve tried; all run without error; the plane shows with the default blender grey texture; but never shows my programmatically added texture image:

A)

cocos2d::Image *im = new Image();
    im->initWithImageFile("cubetext.png");
    
    Texture2D *t2 = new Texture2D();
    t2->initWithImage(im);
    t2->retain();
    spr->setTexture(t2);

B)

spr->getMesh()->setTexture("cubetext.png");

C)

spr->setTexture(Sprite::create("cubetext.png")->getTexture());

D)

//deprecated but worth a shot right?
spr->setTexture(CCTextureCache::sharedTextureCache()->addImage("cubetext.png"));

E)

auto c = new Texture2D();
    auto i = new Image();
    i->Image::initWithImageFile("cubetext.png");
    Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGB888);
    c->initWithImage(i,Texture2D::PixelFormat::RGB888);
    spr->setTexture(c);

Any help would be greatly appreciated - thanks!

Is what I’m trying to do even feasible with cocos2dx? Have done something similar before with c++/opengl & I’ve seen examples of how to override ondraw to call open GL from within cocos2dx - but would rather avoid that for simplicity.

OK - I got this to work now I think. I went back into blender and set an actual texture for my plane (rather than using default solid colour) and saved the UV’s. Then loaded it again in my app; this time the plane showed with the red place holder texture; as it was looking for the absolute path on my desktop for the texture file. So I then used this code to programmatically load the texture:

cocos2d::Image *im = new Image();
    im->initWithImageFile("cubetext.png");
    
    Texture2D *t2 = new Texture2D();
    t2->initWithImage(im);
    t2->retain();
    spr->setTexture(t2);

@grire974
All the solutions should work.