Как изменить текстуру или цвет объекта в Qt3D с помощью QML?

У меня есть проект, в котором есть один трехмерный объект (файл .obj), и я хочу щелкнуть по нему. Для первого теста я был бы рад изменить текстуру или цвет объекта. Насколько я знаю, это называется сбором. Вы, ребята, знаете, как это сделать в qt3d? Весь мой проект написан на qml, поэтому было бы здорово, если бы я мог делать подборку с помощью qml (без C ++), но если это необходимо, я готов попробовать и так.

Мой проект структурирован следующим образом:

У меня есть Entity как rootEntity и 3D-Entity, куда загружается моя сетка. Эта структура находится в собственном qml-файле под названием View3d.qml. Теперь я устанавливаю Scene3D в моем main.qml и загружаю setup экземпляр View3d.

Я использую бета-версию Qt 5.5 с включенным qt3d в 64-битной системе Windows 8.1, если это необходимо.


person eduard_code    schedule 27.05.2015    source источник


Ответы (2)


Самый простой способ - добавить текстуру в файл .obj с помощью блендера, а затем добавить ее в свой проект. Чтобы сделать это с помощью блендера, существует множество руководств, см. Как добавить текстуру и этот видео.

другой способ - использовать текстуру и Texture2D

посмотрите на этот код как на Пример:

У меня 2 класса qml

в main.qml:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Scene3D 2.12

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

Scene3D
{
    id : scene3d
    anchors.fill: parent
    focus: true
    aspects: ["render", "logic", "input"]
    hoverEnabled: true
    cameraAspectRatioMode: Scene3D.AutomaticAspectRatio


    antialiasing: true

    RootEntity
    {
        id:root
    }

}
}

и в RootEntity.qml:

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

Entity {
id: sceneRoot

readonly property var textureModel: [texture1, texture2, texture3, texture4]

readonly property Texture texture1: TextureLoader {
    source: "qrc:/images/image.png"
}

readonly property Texture texture2: TextureLoader {
    source: "qrc:/images/wood.jpg"
}

readonly property Texture texture3: Texture2D {
    format: Texture.RGBA8_UNorm
    textureImages: TextureImage {
        source:"qrc:/images/image.png"
    }
}

readonly property Texture texture4: Texture2D {
    format: Texture.RGBA8_UNorm
    textureImages: TextureImage {
        source:"qrc:/images/wood.jpg"
    }
}

Camera {
    id: camera
    projectionType: CameraLens.PerspectiveProjection
    fieldOfView: 45
    aspectRatio: 16/9
    nearPlane : 0.1
    farPlane : 1000.0
    position: Qt.vector3d( 0.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: camera
}

components: [
    RenderSettings {
        activeFrameGraph: ForwardRenderer {
            clearColor: "#333339"
            camera: camera
        }
    },
    // Event Source will be set by the Qt3DQuickWindow
    InputSettings { }
]

CuboidMesh { id: mesh }

NodeInstantiator {
    id: instantiator
    model: sceneRoot.textureModel

    Entity {
        readonly property Transform transform: Transform {
            readonly property real angle: model.index / instantiator.count * Math.PI * 2
            translation: Qt.vector3d(Math.cos(angle) * 10, 0, Math.sin(angle) * 10)
            scale: 10
        }

        readonly property DiffuseMapMaterial material: DiffuseMapMaterial {
            diffuse: model.modelData
            ambient: "white"
        }
        components: [ mesh, material, transform ]
    }
}
}

вывод:

autput

person Parisa.H.R    schedule 20.06.2021

Смотрите демо / qt3d / teaservice. Это показывает, как делать выборку (т.е. выбор объекта с помощью мыши). Обратите внимание, что вам нужна демонстрация qt3d, а не тизер QML.

person gbjbaanb    schedule 07.01.2016