eduard_code
eduard_code

Reputation: 49

How to change the texture or colour of an object in Qt3D with QML?

I have a project, where i have one 3d object (.obj file) and i want to click on this object. For the first test i would be happy to change the texture or colour of the object. As far as i know it's called picking. Do you guys know how to manage this in qt3d? My whole project is written in qml, so it would be great if i could do the picking with qml (without c++), but if it's necessary im ready to try it that way, too.

My project is structured as followed:

I have an Entity as rootEntity and 3D-Entity, where my mesh is loaded. This structure is in an own qml file called View3d.qml. Now I set a Scene3D in my main.qml and load setup an instance of View3d.

I am using the Qt 5.5 beta with included qt3d on a windows 8.1 64Bit system, if its necessary.

Upvotes: 3

Views: 2075

Answers (2)

Parisa.H.R
Parisa.H.R

Reputation: 3883

The easiest way is that you add Texture to your .obj file with blender and then add it to your project .for doing this by using blender there is a lot of tutorials, see this How to Add Texture and this video.

another way is to use Texture and Texture2D

look at this code as an Example :

I have 2 qml class

in 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
    }

}
}

and in 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 ]
    }
}
}

the output is :

the autput

Upvotes: 1

gbjbaanb
gbjbaanb

Reputation: 52659

See the demos/qt3d/teaservice. This shows how to do picking (ie selection of an object using the mouse). Note you need the qt3d demo, not the QML teaservice.

Upvotes: 0

Related Questions