Qt 3D教程(四)結合Qt Quick來實現動畫的效果

Qt 3D教程(四)結合Qt Quick來實現動畫的效果

       上篇教程已經給大家帶來較爲實用的效果了,這回我們嘗試載入兩個模型,並且讓PhongMaterial成爲它們的共享材質,此外,我們通過藉助Qt Quick的動畫框架來讓我們的三維場景動起來!

蔣彩陽原創文章,首發地址:http://blog.csdn.net/gamesdev/article/details/47132099。歡迎同行前來探討。

       這次的改動沒有上次那麼多,只需要修改main.qml即可滿足效果。main.qml文件如下所示:

import Qt3D 2.0
import Qt3D.Renderer 2.0
import QtQuick 2.5 as Quick

Entity
{
    id: root

    Camera
    {
        id: camera
        position: Qt.vector3d( 0.0, 20.0, 100.0 )
        projectionType: CameraLens.PerspectiveProjection
        fieldOfView: 45
        aspectRatio: 16.0 / 9.0
        nearPlane : 0.1
        farPlane : 1000.0
        upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
        viewCenter: Qt.vector3d( 0.0, 20.0, 0.0 )
    }

    components: FrameGraph
    {
        ForwardRenderer
        {
            clearColor: Qt.rgba( 0.2, 0, 0, 1 )
            camera: camera
        }
    }

    PhongMaterial
    {
        id: phongMaterial
        ambient: _settings.ambient
        diffuse: _settings.diffuse
        specular: _settings.specular
        shininess: _settings.shininess
    }

    Entity
    {
        Mesh
        {
            id: chestMesh
            source: "qrc:/assets/Chest.obj"
            enabled: _settings.showModel
        }

        components: [ chestMesh, phongMaterial ]
    }

    Entity
    {
        Mesh
        {
            id: trefoilMesh
            source: "qrc:/assets/trefoil.obj"
            enabled: _settings.showModel
        }

        Transform
        {
            id: transform

            Translate
            {
                translation: Qt.vector3d( 0, 30, 50 )
            }

            Rotate
            {
                axis: Qt.vector3d( 0, 1, 0 )
                Quick.NumberAnimation on angle
                {
                    from: 0
                    to: 360
                    loops: Quick.Animation.Infinite
                    duration: 2000
                    running: true
                }
            }
        }

        components: [ trefoilMesh, phongMaterial, transform ]
    }

    Configuration
    {
        controlledCamera: camera
    }
}

       這裏我們將PhongMaterial單獨提出來,讓它成爲一個共享的材質,另外,我們再添加一個模型——三葉草環狀模型,並且我們給它做了一系列轉換。我想要是讓這個模型繞着寶箱模型旋轉該多好啊。於是我們將Qt Quick引入,我們將旋轉的部分做了動畫,從0度到360度,並且做了無限循環。就是這麼簡單,我已經迫不及待地想要看到動態的效果了:

       我們看到,這個環狀模型繞着寶箱模型旋轉了,這個效果非常帶感!

       本次教程的代碼均在我的github中,感興趣的同行們可以通過git clone或者是直接下載我的git項目來獲取到本套教程的所有源代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章