File: dynamic.qml

package info (click to toggle)
qt6-quick3d 6.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 143,588 kB
  • sloc: cpp: 395,989; ansic: 41,469; xml: 288; sh: 242; makefile: 32
file content (560 lines) | stat: -rw-r--r-- 20,004 bytes parent folder | download | duplicates (3)
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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtQuick
import QtQuick3D
import QtQml
import QtQuick3D.Helpers
import io.qt.tests.auto.BufferManager

Item {
    id: root
    width: 1280
    height: 720
    visible: true

    property alias controller: objectSpawner

    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)
                }
            }
        }

        GridGeometry {
            id: unusedStaticGeometry
        }

        Node {
            id: objectSpawner
            property real range: 300
            property var directionLights: []
            property var pointLights: []
            property var spotLights: []
            property var cameras: []
            property var models: []
            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 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: dynamicModel
                Model {
                    property alias color: material.baseColor
                    property alias lines: gridGeometry.horizontalLines
                    property alias step: gridGeometry.horizontalStep
                    materials: PrincipledMaterial {
                        lighting: PrincipledMaterial.NoLighting
                        id: material
                        baseColor: "red"
                    }
                    geometry: GridGeometry {
                        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"
                    }
                }
            }

            Component {
                id: resourceLoader
                ResourceLoader {
                    meshSources: [
                        "#Cube",
                        "#Sphere",
                        "#Cone",
                        "random1.mesh",
                        "random2.mesh"
                    ]
                    textures: [
                        Texture {
                            source: "noise1.jpg"
                        },
                        Texture {
                            source: "noise2.jpg"
                        },
                        Texture {
                            source: "noise1.jpg"
                            generateMipmaps: true
                        },
                        Texture {
                            sourceItem: Rectangle {
                                width: 200
                                height: 200
                                color: "pink"
                            }
                        },
                        Texture {
                            textureData: GradientTexture {

                            }
                        }
                    ]
                    geometries: [
                        unusedStaticGeometry
                    ]
                }
            }

            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 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(source : string) : Model {
                let position = getRandomVector3d(objectSpawner.range)
                let rotation = getRandomVector3d(360)
                let color = getRandomColor();
                let instance = model.createObject(objectSpawner, { "position": position, "eulerRotation": rotation, "color": color, "source": source})
                models.push(instance);
                modelsCount++
                return instance;
            }
            function removeModel() {
                if (models.length > 0) {
                    let instance = models.pop();
                    //instance.destroy();
                    modelsCount--
                }
            }

            function addDynamicModel() : Model {
                let position = getRandomVector3d(objectSpawner.range)
                let rotation = getRandomVector3d(360)
                let color = getRandomColor();
                let lines = getRandomInt(100, 1000);
                let steps = getRandomInt(1, 25);
                let instance = dynamicModel.createObject(objectSpawner, {"position": position, "eulerRotation": rotation, "color": color, "lines": lines, "step": steps})
                dynamicModels.push(instance)
                dynamicModelsCount++
                return instance
            }

            function removeDynamicModel() {
                if (dynamicModels.length > 0) {
                    let instance = dynamicModels.pop();
                    //instance.destroy();
                    dynamicModelsCount--
                }
            }

            function addTexture(source : string) : Texture {
                let instance = texture.createObject(objectSpawner, {"source": source})
                textures.push(instance);
                texturesCount++
                mainSphere.updateTexture();
                return instance;
            }
            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() : Texture {
                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()
                return instance;
            }

            function removeDynamicTexture() {
                if (dynamicTextures.length > 0) {
                    let instance = dynamicTextures.pop();
                    //instance.destroy();
                    dynamicTexturesCount--
                    mainSphere.updateTexture()
                }
            }

            function addQmlTexture() : Model {
                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++
                return instance
            }

            function removeQmlTexture() {
                if (qmlTextures.length > 0) {
                    let instance = qmlTextures.pop()
                    //instance.destroy()
                    qmlTexturesCount--
                }
            }

            function addQmlSharedTexture() : Model {
                let position = getRandomVector3d(objectSpawner.range * 2)
                let instance = qmlTextureShared.createObject(objectSpawner, {"sharedItem": sharedItem, "position": position})
                qmlSharedTextures.push(instance)
                qmlSharedTexturesCount++
                return instance
            }

            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 addResourceLoader(): ResourceLoader {
                return resourceLoader.createObject(objectSpawner);
            }

            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.fill: parent

        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
                }
            }
        }
    }
}