1

How can I insert the same entity to be displayed into more than one Scene3Ds in QML?

(it'll probably be abundantly clear that I'm pretty new to QML)

I would like to create something similar to the Multi Viewport QML Example, except I would like to have the viewports in differet layouts (e.g. a SplitView).

Ultimately I would like my entities (Mesh or SceneLoader entities) to be created completely outside of my views (my Scene3D), and be able to show the same data in multiple views without having copies.

I haven't had much luck putting a Layout into a Scene3D, and even so, that would constrain me against having the same data show up elsewhere. I simply can't figure out how to define my data outside the views, especially since I can't figure out how to append to the components/data/children properties.

For example, in the (long) example below, I figured out how to define torusMesh2 outside of the Scene3D by setting it's .parent to inject it into scene3DRightEntity, but I cannot figure out how to inject it into both Scene3Ds

import QtQuick 2.0
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.3
import QtQuick.Scene3D 2.0

import Qt3D.Extras 2.0
Item {

    Button {
        text: 'Button'
        onClicked: {
            console.log('Button clicked');
        }
    }


    SphereMesh {
        id: torusMesh2; radius: 5
        parent: scene3DRightEntity
    }
    PhongMaterial { id: material2; parent: scene3DRightEntity }
    Transform { id: torusTransform2; scale3D: Qt.vector3d(1.5, 1, 0.5); rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45); parent: scene3DRightEntity }


    Rectangle {
        id: topRect
        anchors.fill: parent; anchors.margins: 50

        color: 'green'

        SplitView {
            anchors.fill: parent; orientation: Qt.Horizontal

            Rectangle {
                id: scene
                anchors.margins: 50; width: 200; Layout.minimumWidth: 100; Layout.maximumWidth: 500
                color: "darkRed"

                Text { text: "View 1"; anchors.centerIn: parent }

                Scene3D {
                    id: scene3dLeft
                    anchors.fill: parent; anchors.margins: 10; focus: true
                    aspects: ["input", "logic"]
                    cameraAspectRatioMode: Scene3D.AutomaticAspectRatio

                    Entity {

                        SimpleCamera {
                            id: camera1; fieldOfView: 45; position: Qt.vector3d( 0.0, 0.0, 40.0 )
                        }

                        components: [
                            RenderSettings {
                                activeFrameGraph: ForwardRenderer {
                                    camera: camera1.camera
                                    clearColor: "transparent"
                                }
                            }
                            , InputSettings { }
                        ]

                        TorusMesh {
                            id: torusMesh1; radius: 5; minorRadius: 1; rings: 100; slices: 20
                        }
                        PhongMaterial { id: material1 }
                        Transform { id: torusTransform1; scale3D: Qt.vector3d(1.5, 1, 0.5); rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45) }

                        Entity {
                            id: torusEntity1
                            components: [ torusMesh1, material1, torusTransform1 ]
                        }
                    }

                }
            }


            Rectangle {
                id: scene2
                Layout.fillWidth: true; Layout.minimumWidth: 50; Layout.maximumWidth: 400; height: 300
                color: "darkBlue"

                Scene3D {
                    id: scene3dRight
                    anchors.fill: parent; anchors.margins: 50; focus: true;
                    aspects: ["input", "logic"]
                    cameraAspectRatioMode: Scene3D.AutomaticAspectRatio

                    Entity {
                        id: scene3DRightEntity
                        SimpleCamera {
                            id: camera2
                            position: Qt.vector3d( 0.0, 0.0, 40.0 )
                        }

                        components: [
                            RenderSettings {
                                activeFrameGraph: ForwardRenderer {
                                    camera: camera2.camera
                                    clearColor: "transparent"
                                }
                            }
                            , InputSettings { }
                        ]

                        Entity {
                            id: torusEntity2
                            components: [ torusMesh2, material2, torusTransform2 ]
                        }
                    }

                }
            }
        }
    }

} // Item

UPDATE

My first post was creating torusMesh2 as a child of the SplitView, this edit moves it to the top of the tree and sets its .parent.

Matt
  • 1,928
  • 24
  • 44

0 Answers0