1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
import QtQuick
import QtQuick.Window
import QtQuick3D
import QtQuick.Controls
import QtQuick.Layouts
import QtQml
import QtQuick3D.Helpers
import io.qt.tests.manual.dynamic3DTest
Window {
width: 1280
height: 720
visible: true
title: qsTr("Dynamic 3D Object Tester")
Node {
id: sceneRoot
PerspectiveCamera {
id: mainCamera
z: 600
}
Model {
id: mainSphere
source: "#Sphere"
materials: PrincipledMaterial {
id: sphereMaterial
baseColor: "blue"
}
function updateTexture() {
if (objectSpawner.textures.length > 0)
sphereMaterial.baseColorMap = objectSpawner.textures[objectSpawner.textures.length - 1];
else
sphereMaterial.baseColorMap = null
if (objectSpawner.dynamicTextures.length > 0) {
sphereMaterial.emissiveFactor = Qt.vector3d(1, 1, 1)
sphereMaterial.emissiveMap = objectSpawner.dynamicTextures[objectSpawner.dynamicTextures.length - 1]
} else {
sphereMaterial.emissiveMap = null
sphereMaterial.emissiveFactor = Qt.vector3d(0, 0, 0)
}
}
NumberAnimation on y {
from: 0
to: 50
duration: 10000
running: enableAnimationCheckbox.checked
loops: -1
}
}
Node {
id: objectSpawner
property real range: 300
property var directionLights: []
property var pointLights: []
property var spotLights: []
property var cameras: []
property var models: []
property var riggedModels: []
property var dynamicModels: []
property var dynamicGeometries: []
property var textures: []
property var dynamicTextures: []
property var qmlTextures: []
property var qmlSharedTextures: []
property var item2Ds: []
property int directionLightsCount: 0
property int pointLightsCount: 0
property int spotLightsCount: 0
property int camerasCount: 0
property int modelsCount: 0
property int riggedModelsCount: 0
property int dynamicModelsCount: 0
property int texturesCount: 0
property int dynamicTexturesCount: 0
property int qmlTexturesCount: 0
property int qmlSharedTexturesCount: 0
property int item2DsCount: 0
Component {
id: directionalLight
DirectionalLight {
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
}
}
Component {
id: pointLight
PointLight {
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
}
}
Component {
id: spotLight
SpotLight {
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
}
}
Component {
id: camera
PerspectiveCamera {
}
}
Component {
id: model
Model {
property alias color: material.diffuseColor
//source: "#Cube"
materials: DefaultMaterial {
id: material
diffuseColor: "red"
}
}
}
Component {
id: riggedModel
RiggedFigure {
}
}
Component {
id: dynamicModel
Model {
property alias color: material.baseColor
materials: PrincipledMaterial {
lighting: PrincipledMaterial.NoLighting
id: material
baseColor: "red"
}
}
}
Component {
id: dynamicGeometry
GridGeometry {
property alias lines: gridGeometry.horizontalLines
property alias step: gridGeometry.horizontalStep
id: gridGeometry
verticalLines: horizontalLines
verticalStep: horizontalStep
}
}
Component {
id: texture
Texture {
}
}
Component {
id: dynamicTexture
Texture {
property alias height: gradientTexture.height
property alias width: gradientTexture.width
property alias startColor: gradientTexture.startColor
property alias endColor: gradientTexture.endColor
textureData: GradientTexture {
id: gradientTexture
}
}
}
Component {
id: qmlTexture
Model {
property alias color: sourceItemRect.color
property alias text: textLabel.text
source: "#Rectangle"
materials: PrincipledMaterial {
lighting: PrincipledMaterial.NoLighting
baseColorMap: Texture {
sourceItem: Rectangle {
id: sourceItemRect
width: 256
height: 256
color: "black"
Text {
id: textLabel
anchors.centerIn: parent
color: "white"
font.pointSize: 64
}
}
}
}
}
}
Component {
id: qmlTextureShared
Model {
property alias sharedItem: texture.sourceItem
source: "#Rectangle"
materials: PrincipledMaterial {
lighting: PrincipledMaterial.NoLighting
baseColorMap: Texture {
id: texture
}
}
}
}
Component {
id: item2D
Node {
property alias text: textLabel.text
Text {
id: textLabel
anchors.centerIn: parent
font.pointSize: 64
color: "black"
}
}
}
function getRandomVector3d(range) {
return Qt.vector3d((2 * Math.random() * range) - range,
(2 * Math.random() * range) - range,
(2 * Math.random() * range) - range)
}
function getRandomColor() {
return Qt.rgba(Math.random(),
Math.random(),
Math.random(),
1.0)
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * max) + min;
}
function getMeshSource() {
let sources = [
"random1.mesh",
"random2.mesh",
"random3.mesh",
"random4.mesh",
"#Cube",
"#Sphere",
"#Cone",
"#Cylinder"
]
let index = Math.floor(Math.random() * sources.length);
return sources[index]
}
function getRiggedMeshSource() {
let sources = [
"riggedfigure.mesh",
"riggedsimple.mesh"
]
let index = Math.floor(Math.random() * sources.length);
return sources[index]
}
function getImageSource() {
let sources = [
"noise1.jpg",
"noise2.jpg",
"noise3.jpg",
"noise4.jpg",
"noise5.jpg"
]
let index = Math.floor(Math.random() * sources.length);
return sources[index]
}
function addDirectionalLight() {
let position = getRandomVector3d(objectSpawner.range)
let rotation = getRandomVector3d(360)
let instance = directionalLight.createObject(objectSpawner, { "position": position, "eulerRotation": rotation})
directionLights.push(instance);
directionLightsCount++
}
function removeDirectionalLight() {
if (directionLights.length > 0) {
let instance = directionLights.pop();
instance.destroy();
directionLightsCount--
}
}
function addPointLight() {
let position = getRandomVector3d(objectSpawner.range)
let instance = pointLight.createObject(objectSpawner, { "position": position})
pointLights.push(instance);
pointLightsCount++
}
function removePointLight() {
if (pointLights.length > 0) {
let instance = pointLights.pop();
instance.destroy();
pointLightsCount--
}
}
function addSpotLight() {
let position = getRandomVector3d(objectSpawner.range)
let rotation = getRandomVector3d(360)
let instance = spotLight.createObject(objectSpawner, { "position": position, "eulerRotation": rotation})
spotLights.push(instance);
spotLightsCount++
}
function removeSpotLight() {
if (spotLights.length > 0) {
let instance = spotLights.pop();
instance.destroy();
spotLightsCount--
}
}
function addCamera() {
let position = getRandomVector3d(objectSpawner.range * 2)
let rotation = getRandomVector3d(360)
let instance = camera.createObject(objectSpawner, { "position": position, "eulerRotation": rotation})
cameras.push(instance);
instance.lookAt(Qt.vector3d(0, 0, 0))
view2.updateCamera();
camerasCount++
}
function removeCamera() {
if (cameras.length > 0) {
let instance = cameras.pop();
instance.destroy();
camerasCount--
}
view2.updateCamera();
}
function addModel() {
let position = getRandomVector3d(objectSpawner.range)
let rotation = getRandomVector3d(360)
let source = getMeshSource();
let color = getRandomColor();
let instance = model.createObject(objectSpawner, { "position": position, "eulerRotation": rotation, "color": color, "source": source})
models.push(instance);
modelsCount++
}
function removeModel() {
if (models.length > 0) {
let instance = models.pop();
instance.destroy();
modelsCount--
}
}
function addRiggedModel() {
let position = getRandomVector3d(objectSpawner.range)
let rotation = getRandomVector3d(360)
let instance = riggedModel.createObject(objectSpawner, { "position": position, "eulerRotation": rotation})
riggedModels.push(instance);
riggedModelsCount++
}
function removeRiggedModel() {
if (riggedModels.length > 0) {
let instance = riggedModels.pop();
instance.destroy();
riggedModelsCount--
}
}
function addDynamicModel() {
let position = getRandomVector3d(objectSpawner.range)
let rotation = getRandomVector3d(360)
let color = getRandomColor();
let lines = getRandomInt(100, 1000);
let steps = getRandomInt(1, 25);
let geometry = dynamicGeometry.createObject(objectSpawner, {"lines": lines, "step": steps})
dynamicGeometries.push(geometry)
let instance = dynamicModel.createObject(objectSpawner, {"position": position, "eulerRotation": rotation, "color": color, "geometry": geometry})
dynamicModels.push(instance)
dynamicModelsCount++
}
function removeDynamicModel() {
if (dynamicModels.length > 0) {
let instance = dynamicModels.pop();
instance.destroy();
// Reset the dynamicGeometries
for (var i = 0; i < dynamicModels.length; i++)
dynamicModels[i].geometry = dynamicGeometries[i]
let geometryInstance = dynamicGeometries.pop();
geometryInstance.destroy()
dynamicModelsCount--
}
}
function addTexture() {
let source = getImageSource()
let instance = texture.createObject(objectSpawner, {"source": source})
textures.push(instance);
texturesCount++
mainSphere.updateTexture()
}
function removeTexture() {
if (textures.length > 0) {
let instance = textures.pop();
instance.destroy();
texturesCount--
mainSphere.updateTexture()
}
}
function changeTextures() {
// reset the texture sources
textures.forEach(texture => texture.source = getImageSource())
}
function addDynamicTexture() {
let startColor = getRandomColor()
let endColor = getRandomColor()
let width = 2048
let height = 2048
let instance = dynamicTexture.createObject(objectSpawner, {"startColor": startColor, "endColor": endColor, "height": height, "width": width})
dynamicTextures.push(instance);
dynamicTexturesCount++
mainSphere.updateTexture()
}
function removeDynamicTexture() {
if (dynamicTextures.length > 0) {
let instance = dynamicTextures.pop();
instance.destroy();
dynamicTexturesCount--
mainSphere.updateTexture()
}
}
function addQmlTexture() {
let rectColor = getRandomColor()
let position = getRandomVector3d(objectSpawner.range * 2)
let labelText = qmlTexturesCount + 1
let instance = qmlTexture.createObject(objectSpawner, {"position": position, "color": rectColor, "text": labelText})
qmlTextures.push(instance)
qmlTexturesCount++
}
function removeQmlTexture() {
if (qmlTextures.length > 0) {
let instance = qmlTextures.pop()
instance.destroy()
qmlTexturesCount--
}
}
function addQmlSharedTexture() {
let position = getRandomVector3d(objectSpawner.range * 2)
let instance = qmlTextureShared.createObject(objectSpawner, {"sharedItem": sharedItem, "position": position})
qmlSharedTextures.push(instance)
qmlSharedTexturesCount++
}
function removeQmlSharedTexture() {
if (qmlSharedTextures.length > 0) {
let instance = qmlSharedTextures.pop()
instance.destroy()
qmlSharedTexturesCount--
}
}
function addItem2D() {
let position = getRandomVector3d(objectSpawner.range * 2)
let labelText = item2DsCount + 1
let instance = item2D.createObject(objectSpawner, {"position": position, "text":labelText})
item2Ds.push(instance);
item2DsCount++
}
function removeItem2D() {
if (item2Ds.length > 0) {
let instance = item2Ds.pop()
instance.destroy()
item2DsCount--
}
}
function changeModels() {
// reset the model sources
models.forEach(model => model.source = getMeshSource())
}
function changeDynamicModels() {
// reset the dynamic model sources
if (dynamicModels.length == 0)
return
var shuffleIndexs = Array.from({length: dynamicModels.length}, (x, i) => i);
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
shuffleIndexs = shuffle(shuffleIndexs);
for (var i = 0; i < dynamicModels.length; i++)
dynamicModels[i].geometry = dynamicGeometries[shuffleIndexs[i]]
}
}
}
Item {
id: viewsContainer
anchors.top: parent.top
anchors.right: controlsContainer.left
anchors.bottom: parent.bottom
anchors.left: parent.left
property var windowView: undefined
Image {
id: sharedItem
source: "noise1.jpg"
width: 256
height: 256
}
View3D {
id: view1
anchors.top: parent.top
anchors.left: parent.left
anchors.bottom: parent.bottom
width: parent.width * 0.5
importScene: sceneRoot
camera: mainCamera
environment: SceneEnvironment {
clearColor: "green"
backgroundMode: SceneEnvironment.Color
}
}
View3D {
id: view2
anchors.top: parent.top
anchors.left: view1.right
anchors.bottom: parent.bottom
anchors.right: parent.right
importScene: sceneRoot
environment: SceneEnvironment {
clearColor: "pink"
backgroundMode: SceneEnvironment.Color
}
function updateCamera() {
if (objectSpawner.cameras.length > 0) {
var dynamicCamera = objectSpawner.cameras[objectSpawner.cameras.length - 1]
view2.camera = dynamicCamera
} else {
view2.camera = mainCamera
}
}
//camera: objectSpawner.cameras.length > 0 ? objectSpawner.cameras[objectSpawner.cameras.length - 1] : mainCamera
}
}
Component {
id: windowComponent
Window {
id: subWindow
visible: true
height: 400
width: 400
View3D {
id: view3
anchors.fill: parent
importScene: sceneRoot
environment: SceneEnvironment {
clearColor: "orange"
backgroundMode: SceneEnvironment.Color
}
}
}
}
Rectangle {
id: controlsContainer
width: 300
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
color: "grey"
ColumnLayout {
CheckBox {
id: enableAnimationCheckbox
checked: false
text: "Enable Animation"
}
CheckBox {
id: enableWindowCheckbox
checked: false
text: "Enable Window View"
onCheckedChanged: {
if (checked) {
// Create Window component
viewsContainer.windowView = windowComponent.createObject(viewsContainer)
} else {
// Delete Window component
viewsContainer.windowView.destroy();
viewsContainer.windowView = undefined
}
}
}
RowLayout {
Label {
text: "Directional Light"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.directionLightsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addDirectionalLight()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeDirectionalLight()
}
}
}
RowLayout {
Label {
text: "Point Light"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.pointLightsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addPointLight()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removePointLight()
}
}
}
RowLayout {
Label {
text: "Spot Light"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.spotLightsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addSpotLight()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeSpotLight()
}
}
}
RowLayout {
Label {
text: "Camera"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.camerasCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addCamera()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeCamera()
}
}
}
RowLayout {
Label {
text: "Model"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.modelsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addModel()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeModel()
}
}
ToolButton {
text: "<->"
onClicked: {
objectSpawner.changeModels()
}
}
}
RowLayout {
Label {
text: "Rigged Model"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.riggedModelsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addRiggedModel()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeRiggedModel()
}
}
}
RowLayout {
Label {
text: "Dynamic Model"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.dynamicModelsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addDynamicModel()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeDynamicModel()
}
}
ToolButton {
text: "<->"
onClicked: {
objectSpawner.changeDynamicModels()
}
}
}
RowLayout {
Label {
text: "Texture"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.texturesCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addTexture()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeTexture()
}
}
ToolButton {
text: "<->"
onClicked: {
objectSpawner.changeTextures()
}
}
}
RowLayout {
Label {
text: "Dynamic Texture"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.dynamicTexturesCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addDynamicTexture()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeDynamicTexture()
}
}
}
RowLayout {
Label {
text: "QML Texture"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.qmlTexturesCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addQmlTexture()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeQmlTexture()
}
}
}
RowLayout {
Label {
text: "QML Texture (Shared)"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.qmlSharedTexturesCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addQmlSharedTexture()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeQmlSharedTexture()
}
}
}
RowLayout {
Label {
text: "Item 2D"
color: "white"
Layout.fillWidth: true
}
Label {
text: objectSpawner.item2DsCount
color: "white"
Layout.fillWidth: true
}
ToolButton {
text: "+"
onClicked: {
objectSpawner.addItem2D()
}
}
ToolButton {
text: "-"
onClicked: {
objectSpawner.removeItem2D()
}
}
}
}
}
}
|