Demonstrates how assets can be loaded at runtime.
This example shows how to implement a simple asset viewer using the RuntimeLoader .
见 introductory example documentation for an explanation of how to set up a basic scene. Once we have the scene set up we can add the RuntimeLoader item:
RuntimeLoader { id: importNode scale: Qt.vector3d(helper.scaleFactor, helper.scaleFactor, helper.scaleFactor) source: importUrl instancing: instancingButton.checked ? instancing : null onBoundsChanged: helper.updateBounds(bounds) }
Loading an asset is done by setting the
RuntimeLoader
's
source
property. In this example the
source
is bound to the
importUrl
which will be changed when the user selects a file in the file-dialog.
Assuming the asset can be loaded, the content will be created as children of the
RuntimeLoader
importNode
。注意,
RuntimeLoader
是
Node
type, and since it is also the root node for the loaded asset, any transforms applied to the
importNode
will also affect its children.
If an asset fails to load then the
RuntimeLoader
's
status
特性会被设为
Error
。
RuntimeLoader
's
errorString
can then be queried to get a more detailed description of the error.
In this example we will be displaying the error message in a red message box in the middle of the screen, like this:
Rectangle { id: messageBox visible: importNode.status !== RuntimeLoader.Success color: "red" width: parent.width * 0.8 height: parent.height * 0.8 anchors.centerIn: parent radius: Math.min(width, height) / 10 opacity: 0.6 Text { anchors.fill: parent font.pixelSize: 36 text: "Status: " + importNode.errorString + "\nPress \"Import...\" to import a model" color: "white" wrapMode: Text.Wrap horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter } }
To make it possible to change the position of the camera we use the WasdController 从 Helpers module and bind it to our camera, like this:
WasdController { controlledObject: camera } WheelHandler { id: wheelHandler property real factor: 10.0 onWheel: (event)=> { if (event.angleDelta.y > 0) factor *= 1.1 else factor /= 1.1 } } PointHandler { id: rotateHandler acceptedButtons: Qt.MiddleButton onPointChanged: { if (Math.abs(point.velocity.x) >= Math.abs(point.velocity.y)) importNode.eulerRotation.y += point.velocity.x / 2000 else importNode.eulerRotation.x += point.velocity.y / 2000 } }
除了 WasdController , the example uses a WheelHandler 和 PointerHandler to scale and rotate the model.
RuntimeLoader item can also be used in combination with instancing, as shown here:
RandomInstancing { id: instancing instanceCount: 30 position: InstanceRange { property alias boundsDiameter: helper.boundsDiameter from: Qt.vector3d(-3*boundsDiameter, -3*boundsDiameter, -3*boundsDiameter); to: Qt.vector3d(3*boundsDiameter, 3*boundsDiameter, 3*boundsDiameter) } color: InstanceRange { from: "black"; to: "white" } }
RuntimeLoader does not inherit from Model, but it has its own instancing property which allows us to instance a complex imported asset as if it were a simple model. In this case we use the RandomInstancing component from the Helpers module to randomly position items within a fixed area and with a random color.
文件: