Cocos2d 3d/sprite3D and mipmapping

Greetings!

Long time lurker but i just made an account so i can seek help in the forums.
I like how things are going with the 3D development of the framework and i need to say i’m quite excited. Keep up the good work!

So my problem. I want to achieve texture mipmapping for a sprite3D “plane” as a background i’m trying to display.
What i’m trying to achieve is actually this:

Instead of this:

Those pictures represent my problem quite well.
I’m looking for guidance on how to tackle this problem under the cocos2d framework.

Thanks!

I think what you want is to set the filter mode on your texture to GL_LINEAR_MIPMAP_LINEAR.
A.K.A trilinear filtering.

You should be able to use Texture2D::generateMipmap() and Texture2D::setTexParameters() to achieve this.

I’ve stumbled upon something like this but i was not sure if sprited3D can be used like this.
It has the setTexture method, which takes an object of Texture2D. But is that changing anything?
I’ll also give it a try.

Okay, basically this is what i tried and i see no difference at all. I also experimented with textParams a little bit but still no difference. Maybe i’m doing something wrong?

auto aText = cocos2d::Director::getInstance()->getTextureCache()->addImage("bg.png");

aText->generateMipmap();

cocos2d::Texture2D::TexParams tp = { 
	GL_LINEAR_MIPMAP_LINEAR, 
	GL_LINEAR, 
	GL_CLAMP_TO_EDGE, 
	GL_CLAMP_TO_EDGE 
};

aText->setTexParameters(&tp);

auto bgSprite = cocos2d::Sprite3D::create("bg.obj");

bgSprite->setTexture(aText);
bgSprite->setScale(5.0);

    float dis = bgSprite->getContentSize().width * bgSprite->getScale();
    
bgSprite->setPosition3D(cocos2d::Vec3(0.0, 0.3, (i * dis)));
bgSprite->setRotation3D(cocos2d::Vec3(0.0, 90.0, 0.0));

this->addChild(bgSprite);

Quick update:
The above code actually works. And produces the desirable effect. Need to do some more research because it only worked after changing my image to something different.

As far as I’m aware mipmapping only works for Power-of-Two textures on most mobile devices, maybe this is the problem?

http://aras-p.info/blog/2012/10/17/non-power-of-two-textures/

And I can’t see GL_ARB_texture_non_power_of_two or GL_OES_texture_npot on Apple’s device information page
https://developer.apple.com/library/ios/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/OpenGLESPlatforms/OpenGLESPlatforms.html

I’ve made sure i am using POT textures.
Everything seems to be working great now. Your suggestion worked as expected. The issue i am facing now is that if i load up the same texture my .obj model has and try to replace them with mipmaps generated nothing changes.
If i simply copy & rename the same image (ex: cube_text.png -> cube_text2.png) and try to load it up and replace the sprite3d texture with it, everything is working great.

Try doing it after you load your model. It’s a shame you can’t iterate over all meshes in sprite3d. Or perhaps you could try extending your local version of it’s implementation to expose the meshes. That way you can load the model, iterate over all of it’s textures and generate mipmaps for them.

Hi, I tried this on Cocos 3.14 (I only have one texture in my c3t)

    Texture2D *aText = mySprite3D->getMesh()->getTexture();
    if (aText!=NULL)
    {
        if (!aText->hasMipmaps())
        {
            aText->generateMipmap();
        }
        cocos2d::Texture2D::TexParams tp = {
                GL_LINEAR_MIPMAP_LINEAR,
                GL_LINEAR,
                GL_CLAMP_TO_EDGE,
                GL_CLAMP_TO_EDGE
        };
        aText->setTexParameters(&tp);
    }

but if I create another Sprite3D from the same file, the textures of the already created sprite3D become non-mipmaped. It seems that when you create the Sprite3D the texture parameters are set to a default again? I’m reporting this because I’m not sure if it is a bug or the defult behaivor, or if I’m doing something wrong by creating multiple Sprite3Ds from the same file. Thanks!

Sounds like a bug to me, definitely unexpected behaviour.

1 Like

I’m also facing this issue. Stepping through the code and it looks like the mip maps are already in the PVR texture but when it loads up I get horrible texture aliasing.