| 1 |
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import QtQuick 2.0 as Quick |
| 5 |
import Qt3D.Core 2.0 |
| 6 |
import Qt3D.Render 2.0 |
| 7 |
import Qt3D.Examples 2.0 |
| 8 |
import Qt3D.Input 2.0 |
| 9 |
import Qt3D.Extras 2.0 |
| 10 |
|
| 11 |
Entity |
| 12 |
{ |
| 13 |
components: [ |
| 14 |
RenderSettings { |
| 15 |
activeFrameGraph: ForwardRenderer { |
| 16 |
clearColor: Qt.rgba(0, 0.5, 1, 1) |
| 17 |
camera: camera |
| 18 |
} |
| 19 |
}, |
| 20 |
// Event Source will be set by the Qt3DQuickWindow |
| 21 |
InputSettings { } |
| 22 |
] |
| 23 |
|
| 24 |
Camera { |
| 25 |
id: camera |
| 26 |
projectionType: CameraLens.PerspectiveProjection |
| 27 |
fieldOfView: 45 |
| 28 |
aspectRatio: 16/9 |
| 29 |
nearPlane : 0.1 |
| 30 |
farPlane : 1000.0 |
| 31 |
position: Qt.vector3d( 0.0, 0.0, 40.0 ) |
| 32 |
upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) |
| 33 |
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) |
| 34 |
} |
| 35 |
|
| 36 |
FirstPersonCameraController { |
| 37 |
camera: camera |
| 38 |
} |
| 39 |
|
| 40 |
// test_scene.dae contains three named nodes. Once the asynchronous loading of the |
| 41 |
// scene completes, we look up some of them. |
| 42 |
SceneHelper { |
| 43 |
id: sceneHelper |
| 44 |
} |
| 45 |
|
| 46 |
Quick.Component { |
| 47 |
id: animParamComp |
| 48 |
Quick.SequentialAnimation { |
| 49 |
id: seqAnim |
| 50 |
loops: Quick.Animation.Infinite |
| 51 |
property variant target: null |
| 52 |
Quick.ColorAnimation { |
| 53 |
target: seqAnim.target |
| 54 |
property: "value" |
| 55 |
from: Qt.rgba(0, 0, 0, 1) |
| 56 |
to: Qt.rgba(1, 1, 1, 1) |
| 57 |
duration: 2000 |
| 58 |
} |
| 59 |
Quick.ColorAnimation { |
| 60 |
target: seqAnim.target |
| 61 |
property: "value" |
| 62 |
from: Qt.rgba(1, 1, 1, 1) |
| 63 |
to: Qt.rgba(0, 0, 0, 1) |
| 64 |
duration: 1000 |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
Quick.Component { |
| 70 |
id: animRotComp |
| 71 |
Quick.QtObject { |
| 72 |
id: rotationData |
| 73 |
property real userAngle: 0.0 |
| 74 |
property vector3d axis: Qt.vector3d(0, 0, 1) |
| 75 |
Quick.NumberAnimation on userAngle { |
| 76 |
from: 0 |
| 77 |
to: 360 |
| 78 |
duration: 5000 |
| 79 |
loops: Quick.Animation.Infinite |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
Entity { |
| 85 |
components: [ |
| 86 |
Transform { |
| 87 |
matrix: { |
| 88 |
var m = Qt.matrix4x4(); |
| 89 |
m.rotate(90, Qt.vector3d(0, 1, 0)); |
| 90 |
return m; |
| 91 |
} |
| 92 |
}, |
| 93 |
SceneLoader { |
| 94 |
id: sceneLoader |
| 95 |
source: "qrc:/assets/test_scene.dae" |
| 96 |
onStatusChanged: (status) => { |
| 97 |
console.log("SceneLoader status: " + status); |
| 98 |
if (status == SceneLoader.Ready) { |
| 99 |
console.log("Scene is ready"); |
| 100 |
|
| 101 |
// Now find the torus and animate one of the material effect's parameters. |
| 102 |
var e = sceneHelper.findEntity(sceneLoader, "Torus"); |
| 103 |
console.log("Found entity: " + e + " its components are:"); |
| 104 |
for (var i = 0; i < e.components.length; ++i) { |
| 105 |
console.log(" " + e.components[i]); |
| 106 |
if (e.components[i].effect !== undefined) { |
| 107 |
var p = e.components[i].effect.parameters; |
| 108 |
if (p !== undefined) { |
| 109 |
for (var j = 0; j < p.length; ++j) { |
| 110 |
if (p[j].name === "kd") { |
| 111 |
var anim = animParamComp.createObject(p[j]); |
| 112 |
anim.target = p[j]; |
| 113 |
anim.running = true; |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// Add an animated rotation transform to make Suzanne orbit around. |
| 122 |
e = sceneHelper.findEntity(sceneLoader, "Suzanne"); |
| 123 |
var t = sceneHelper.findComponent(e, "Qt3DCore::QTransform"); |
| 124 |
if (!t) |
| 125 |
return; |
| 126 |
|
| 127 |
// Add object with animation data as a child |
| 128 |
var suzanneAnim = animRotComp.createObject(t); |
| 129 |
|
| 130 |
// Query the existing transformation. We will use parts of this in |
| 131 |
// the replacement animation |
| 132 |
var origTranslation = t.translation; |
| 133 |
var origRotation = t.rotation; |
| 134 |
var origScale = t.scale; |
| 135 |
console.log("origTranslation = " + origTranslation); |
| 136 |
console.log("origRotation = " + origRotation); |
| 137 |
console.log("origScale = " + origScale); |
| 138 |
|
| 139 |
// Make a deep copy of the original translation. This will not be updated when the |
| 140 |
// transformation's translation property gets updated due to the animation. |
| 141 |
var constantTranslation = Qt.vector3d(origTranslation.x, origTranslation.y, origTranslation.z); |
| 142 |
|
| 143 |
t.matrix = Qt.binding(function() { |
| 144 |
var m = Qt.matrix4x4(); |
| 145 |
m.rotate(suzanneAnim.userAngle, suzanneAnim.axis); |
| 146 |
m.translate(constantTranslation); |
| 147 |
m.rotate(90, Qt.vector3d(1, 0, 0)); |
| 148 |
m.scale(origScale); |
| 149 |
return m; |
| 150 |
}) |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
] |
| 155 |
} |
| 156 |
|
| 157 |
Entity { |
| 158 |
components: [ |
| 159 |
Transform { |
| 160 |
scale: 0.2 |
| 161 |
translation: Qt.vector3d(0.0, -15.0, 0.0) |
| 162 |
}, |
| 163 |
SceneLoader { |
| 164 |
source: "qrc:/assets/obj/material-sphere.obj" |
| 165 |
} |
| 166 |
] |
| 167 |
} |
| 168 |
} |