问题描述
我正在尝试使用以下QML展示简单的3D汽车:
import QtQuick 2.2 as QQ2
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0
Entity {
id: sceneRoot
Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 45
aspectRatio: 16/9
nearPlane : 0.1
farPlane : 500.0
position: Qt.vector3d( 0.0,0.0,-40.0 )
upVector: Qt.vector3d( 0.0,1.0,0.0 )
viewCenter: Qt.vector3d( 0.0,0.0 )
}
OrbitCameraController {
camera: camera
}
components: [
RenderSettings {
activeFrameGraph: ForwardRenderer {
clearColor: Qt.rgba(0,0.5,1,1)
camera: camera
}
},// Event Source will be set by the Qt3DQuickWindow
InputSettings { }
]
Mesh{
id: sphereMesh
source: "qrc:/../../../../../Desktop/Qt-3d/assets/55z27frcahz4P911GT/Porsche_911_GT2.obj"
}
phongMaterial {
id : spherematerial
shininess: 1
}
Transform {
id: sphereTransform
property real userAngle: 0.0
scale: sceneRoot.scale
rotation: fromAxisAndAngle(Qt.vector3d(0,0),45)
matrix: {
var m = Qt.matrix4x4();
m.rotate(userAngle,Qt.vector3d(0,0));
// m.translate(Qt.vector3d(0,20));
return m;
}
}
QQ2.NumberAnimation {
target: sphereTransform
property: "userAngle"
duration: 10000
from: 0
to: 360
loops: QQ2.Animation.Infinite
running: true
}
Entity {
id: sphereEntity
components: [ sphereMesh,spherematerial,sphereTransform ]
}
}
但是,执行此代码时,我可以看到3D汽车正在旋转。但是它不包含3D对象的原始颜色。它显示了3D汽车的默认颜色。如何获得原始颜色?
- 我需要在材质上使用纹理吗?如果是,该怎么做?
- 我是否需要在QML中使用mtl文件?如果是,该怎么做
请注意
- 出于某些原因,我不想使用Qt Quick 3D模块。
- 此外,我不想使用Qt3DStudio
解决方法
使用using namespace std;
#include <iostream>
int main()
{
cout << "something";
return 0;
}
加载网格仅加载顶点数据(顶点,法线,纹理坐标),但不加载纹理,并且不添加任何Qt3D::QMesh
。您要添加一个简单的Qt3DRender::QMaterial
,该颜色仅将一种颜色应用于整个对象。
如果您的对象提供每个顶点颜色信息,则可以尝试Qt3DExtras::QPhongMaterial
。
如果不起作用,请尝试Qt3DExtras::QPerVertexColorMaterial
:在其中设置模型的URL,它会自动加载所有内容并尝试应用正确的材料。创建它来代替Qt3DRender::QSceneLoader
,并将其添加为组件。