我有一个项目,其中有一个 3d 对象(.obj 文件),我想单击该对象。对于第一次测试,我很乐意更改对象的纹理或颜色。据我所知,这叫做采摘。你们知道如何在 qt3d 中管理这个吗?我的整个项目都是用 qml 编写的,所以如果我可以用 qml(没有 c++)进行选择,那就太好了,但如果有必要,我也准备尝试那样做。
我的项目结构如下:
我有一个实体作为 rootEntity 和 3D-Entity,我的网格加载到那里。这个结构在一个名为 View3d.qml 的自己的 qml 文件中。现在我在我的 main.qml 中设置了一个 Scene3D 并加载了一个 View3d 实例。
如果需要,我在 Windows 8.1 64 位系统上使用 Qt 5.5 beta 和包含的 qt3d。
最佳答案
最简单的方法是使用 blender 将 Texture 添加到您的 .obj 文件中,然后将其添加到您的项目中。通过使用 blender 执行此操作有很多教程,请参阅此 How to Add Texture 还有这个video .
以这段代码为例:
我有 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 ]
}
}
}
输出是:
关于qt - 如何使用 QML 更改 Qt3D 中对象的纹理或颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483274/