Qt Quick 3D - RuntimeLoaderの例

アセットをランタイムにロードする方法を示します。

この例では、RuntimeLoader を使用して簡単なアセットビューアを実装する方法を示します。

基本的なシーンのセットアップ方法については、導入例のドキュメントを参照してください。シーンをセットアップしたら、RuntimeLoader アイテムを追加します:

RuntimeLoader {
    id: importNode
    source: windowRoot.importUrl
    instancing: instancingButton.checked ? instancing : null
    onBoundsChanged: helper.updateBounds(bounds)
}

アセットをロードするには、RuntimeLoadersource プロパティを設定します。この例では、source は、importUrl にバインドされており、ユーザーがファイルダイアログでファイルを選択すると変更されます。

アセットが読み込まれると、コンテンツはRuntimeLoader importNode の子として作成されます。RuntimeLoaderNode タイプであり、読み込まれたアセットのルートノードでもあるため、importNode に適用されたトランスフォームはその子にも影響することに注意してください。

エラー処理

アセットの読み込みに失敗した場合、RuntimeLoaderstatus プロパティはError に設定されます。RuntimeLoadererrorString をクエリして、エラーの詳細を取得することができます。

この例では、エラーメッセージを画面中央の赤いメッセージボックスに表示します:

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
    }
}
カメラの移動

カメラの位置を変更できるようにするために、Helpers モジュールのWasdController を使用し、次のようにカメラにバインドします:

OrbitCameraController {
    id: orbitController
    origin: orbitCameraNode
    camera: orbitCamera
    enabled: helper.orbitControllerEnabled
}
WasdController {
    id: wasdController
    controlledObject: wasdCamera
    enabled: !helper.orbitControllerEnabled
}

この例では、WasdController に加えて、WheelHandlerPointerHandler を使って、モデルの拡大縮小と回転を行っています。

インスタンス化

RuntimeLoader アイテムは、インスタンス化と組み合わせて使うこともできます:

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 は Model を継承していませんが、独自の プロパティを持っており、複雑なインポート アセットを単純なモデルのようにインスタンス化することができます。この場合、 モジュールの コンポーネントを使用して、アイテムを一定の領域内にランダムな色で配置します。instancing Helpers RandomInstancing

マテリアルのオーバーライド

アセットを読み込むとき、期待通りに見えないことがあります。materialOverride プロパティは、シーン内の各マテリアルのレンダリング方法を変更し、レンダリング全体に対する特定の寄与を表示します。これは、必要に応じて元のアセットを調整できるように、アセットについて正確には何が正しくないのかを判断するのに便利です。

プロジェクト例 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.