In questa pagina

Qt Quick 3D - Esempio di morphing

Mostra come controllare le animazioni di morphing in Qt Quick 3D.

Modello 3D della testa con cursori di morphing per bocca, orecchie e cubify

Questo esempio mostra come controllare l'animazione di morphing su un modello importato da uno strumento esterno.

Creazione di una mesh

La mesh del modello è stata creata utilizzando lo strumento di modellazione 3D Blender. Oltre alla forma di base, la mesh contiene tre target di morphing (chiamati shape key in Blender).

Interfaccia di Blender con modello di testa di scimmia e controlli dei shape key

È stata esportata da Blender come file glTF e importata utilizzando balsam.

Controllo dei target di morphing

Normalmente, il morphing viene controllato da un'animazione sulla timeline o da un'animazione delle proprietà. In questo esempio, utilizziamo invece dei cursori, per consentire di sperimentare liberamente la combinazione di pesi diversi:

            Label { text: "Mouth:"; }
            RealSlider {
                id: mouthSlider
                from: 0.0
                to: 1.0
            }
            Label { text: "Ears and eyebrows:" }
            RealSlider {
                id: earSlider
                from: 0.0
                to: 1.0
            }
            Label { text: "Cubify:" }
            RealSlider {
                id: cubeSlider
                from: 0.0
                to: 1.0
            }

(RealSlider è un semplice cursore che fornisce un valore in virgola mobile. È stato preso in prestito dall’esempio degli effetti.)

Definiamo un MorphTarget per ciascuno dei target presenti nella mesh. I target di morphing sono gli oggetti a cui ci colleghiamo per controllare il morphing. In genere, vengono animati, ma in questo esempio li colleghiamo ai valori dei cursori:

        MorphTarget {
            id: morphtarget0
            weight: mouthSlider.value
            attributes: MorphTarget.Position | MorphTarget.Normal
        }
        MorphTarget {
            id: morphtarget1
            weight: earSlider.value
            attributes: MorphTarget.Position | MorphTarget.Normal
        }
        MorphTarget {
            id: morphtarget2
            weight: cubeSlider.value
            attributes: MorphTarget.Position | MorphTarget.Normal
        }

Infine creiamo un modello e associamo i target di morphing ai target presenti nella mesh:

        Model {
            source: "suzanne.mesh"
            morphTargets: [
                morphtarget0,
                morphtarget1,
                morphtarget2
            ]
            materials: PrincipledMaterial {
                baseColor: "#41cd52"
                roughness: 0.1
            }
            SequentialAnimation on eulerRotation.y {
                NumberAnimation { from: -45; to: 45; duration: 10000 }
                NumberAnimation { from: 45; to: -45; duration: 10000 }
                loops: Animation.Infinite
            }
        }

Si noti che i target vengono abbinati in base alla loro posizione nell’elenco: i nomi non sono rilevanti.

Progetto di esempio su code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.