Transform texture for QML Qt3D material

75 views Asked by At

In my project I have to set some textures for models and configure their orientation. I'm using Qt 5.15 for now. I managed to put some texture for DiffuseSpecularMaterial setting its diffuse: property with Texture2D { TextureImage { source: "file:res1.png" }} component.

But I can't find a way to rotate or scale this texture. There is a fine Texture component for QtQuick3D, that seems to have needed properties, but I can't move to it and have to stay with Qt3D.Extras, Qt3D.Core, etc...

Here's full code for example (res1.png could be any image, just put it in folder):

.pro file

QT += quick 3dcore 3drender 3dinput 3dquick 3dlogic

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

QML_IMPORT_PATH =

QML_DESIGNER_IMPORT_PATH =

qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp file

#include <QGuiApplication>
#include <QQmlApplicationEngine>


int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
  QGuiApplication app(argc, argv);

  qputenv("QSG_RHI_BACKEND", "opengl");

  QQmlApplicationEngine engine;
  const QUrl url(QStringLiteral("qrc:/main.qml"));
  QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                   &app, [url](QObject *obj, const QUrl &objUrl) {
    if (!obj && url == objUrl)
      QCoreApplication::exit(-1);
  }, Qt::QueuedConnection);
  engine.load(url);

  return app.exec();
}

main.qml file

import QtQuick
import QtQuick.Window

import Qt3D.Core
import Qt3D.Render
import Qt3D.Input
import Qt3D.Extras
import QtQuick.Scene3D

Window {
  width: 640
  height: 480
  visible: true
  title: qsTr("Hello World")

  Scene3D
  {
    id: id_Scene3d
    anchors.fill: parent
    anchors.margins: 10
    focus: true
    aspects: ["input", "logic"]
    cameraAspectRatioMode: Scene3D.AutomaticAspectRatio

    Entity
    {
      id: id_SceneRootEntity

      Camera
      {
        id: id_Camera
        projectionType: CameraLens.PerspectiveProjection
        fieldOfView: 45
        nearPlane : 0.1
        farPlane : 1000.0
        position: Qt.vector3d( -20.0, 20.0, 40.0 )
        upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
        viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
      }

      OrbitCameraController
      {
        camera: id_Camera
        lookSpeed: 150
        linearSpeed: 200
      }

      components:
      [
        RenderSettings
        {
          id: id_RenderSettings
          activeFrameGraph:
            ForwardRenderer
            {
              camera: id_Camera
              clearColor: "transparent"
              showDebugOverlay: false
            }
        },
        InputSettings { }
      ]

      Entity
      {
        components:
        [
          PlaneMesh { height: 5; width: 7 }
          ,
          DiffuseSpecularMaterial
          {
            ambient: "white"
            diffuse: Texture2D { TextureImage { source: "file:res1.png" }} // HOW TO ROTATE/SCALE THIS?
          }
        ]
      }
    }
  }
}
0

There are 0 answers