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

來源:互聯網
上載者:User

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

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

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

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

import Qt3D 2.0import Qt3D.Renderer 2.0import QtQuick 2.5 as QuickEntity{    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項目來擷取到本套教程的所有原始碼。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.