Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use QMetalRoughMaterial with QExtrudedTextMesh



  • Hi,
    I want to apply a metalic material to a 3D text.

    I have based my example code on this:
    https://code.woboq.org/qt5/qt3d/examples/qt3d/3d-text/main.cpp.html

    The example uses the QPhongMaterial. I naively thought I could simply
    replace that material by QMetalRoughMaterial, but that does not seem
    to work.

    My code is as follows:

    #include <QCoreApplication>
    #include <Qt3DCore/Qt3DCore>
    #include <Qt3DExtras/Qt3DExtras>
    #include <Qt3DExtras/QExtrudedTextMesh>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication a(argc, argv);
    
        Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
        view->setTitle(QStringLiteral("3D Text CPP"));
        view->defaultFrameGraph()->setClearColor(QColor("white"));
    
        auto *root = new Qt3DCore::QEntity();
    
        //Light
        Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(root);
        Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
        light->setColor("white");
        light->setIntensity(0.5);
        lightEntity->addComponent(light);
    
        //auto *textMaterial = new Qt3DExtras::QPhongMaterial(root);
        auto *textMaterial = new Qt3DExtras::QMetalRoughMaterial(root);
    
        auto *text = new Qt3DCore::QEntity(root);
        auto *textMesh = new Qt3DExtras::QExtrudedTextMesh();
        auto *textTransform = new Qt3DCore::QTransform();
    
        QFont font("Arial", 32, -1, false);
        textTransform->setTranslation(QVector3D(-2.45f, 2.0 * .5f, 0));
        textTransform->setScale(.8f);
    
        textMesh->setDepth(.25f);
        textMesh->setFont(font);
        textMesh->setText("TEST");
    
        //FOR PHONG MATERIAL
        //textMaterial->setDiffuse(QColor("white"));
        //textMaterial->setShininess(1.0);
        //textMaterial->setSpecular(QColor("white"));
    
        //FOR METAL
        textMaterial->setBaseColor(QColor("blue"));
        textMaterial->setMetalness(1.0);
        textMaterial->setRoughness(0.2);
        //textMaterial->setNormal(...)); //<-- what needs to come here?
    
        text->addComponent(textMaterial);
        text->addComponent(textMesh);
        text->addComponent(textTransform);
    
        // Camera
        float aspect = static_cast<float>(view->screen()->size().width()) / view->screen()->size().height();
    
        Qt3DRender::QCamera *camera = view->camera();
        camera->lens()->setPerspectiveProjection(65.f, aspect, 0.1f, 100.f);
        camera->setPosition(QVector3D(0, 1, 3));
        camera->setViewCenter(QVector3D(0, 1, 0));
        auto *cameraController = new Qt3DExtras::QOrbitCameraController(root);
        cameraController->setCamera(camera);
    
        view->setRootEntity(root);
        view->show();
        return a.exec();
    }
    

    How can I make get the metal material to work?

    Thanks!



  • Solved on Windows by changing environment variable QT_OPENGL from the default angle to desktop.



  • @diracsbracket said in How to use QMetalRoughMaterial with QExtrudedTextMesh:

    //textMaterial->setNormal(...)); //<-- what needs to come here?

    the normal must be a texture. You can set it with a TextureLoader:

           normal: TextureLoader {
                source: "qrc:/assets/textures/pbr-default/pbr-default-normal.png"
    
                minificationFilter: Texture.LinearMipMapLinear
                magnificationFilter: Texture.Linear
                wrapMode {
                    x: WrapMode.ClampToEdge
                    y: WrapMode.ClampToEdge
                }
                generateMipMaps: true
            }
    

    Edit: this is QMl code of course. Just noticed you write C++. I don't seem to see the difference anymore. But you get the idea...


Log in to reply