| 1 |
// Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import Qt3D.Core 2.0 |
| 5 |
import Qt3D.Render 2.11 |
| 6 |
import Qt3D.Input 2.0 |
| 7 |
import Qt3D.Extras 2.0 |
| 8 |
import QtQuick 2.0 |
| 9 |
|
| 10 |
Entity { |
| 11 |
id: sceneRoot |
| 12 |
|
| 13 |
Camera { |
| 14 |
id: camera |
| 15 |
projectionType: CameraLens.PerspectiveProjection |
| 16 |
fieldOfView: 45 |
| 17 |
aspectRatio: 16/9 |
| 18 |
nearPlane : 0.1 |
| 19 |
farPlane : 1000.0 |
| 20 |
position: Qt.vector3d( 0.0, 2.0, 20.0 ) |
| 21 |
upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) |
| 22 |
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) |
| 23 |
} |
| 24 |
|
| 25 |
OrbitCameraController { camera: camera } |
| 26 |
|
| 27 |
RenderSettings { |
| 28 |
id : external_forward_renderer |
| 29 |
activeFrameGraph : ForwardRenderer { |
| 30 |
camera: camera |
| 31 |
clearColor: "black" |
| 32 |
} |
| 33 |
pickingSettings.pickMethod: PickingSettings.TrianglePicking |
| 34 |
} |
| 35 |
|
| 36 |
// Event Source will be set by the Qt3DQuickWindow |
| 37 |
InputSettings { id: inputSettings } |
| 38 |
|
| 39 |
DirectionalLight { |
| 40 |
id: headLight |
| 41 |
worldDirection: camera.viewVector |
| 42 |
} |
| 43 |
|
| 44 |
RayCaster { |
| 45 |
id: raycaster |
| 46 |
origin: Qt.vector3d(0, 0, 4) |
| 47 |
direction: Qt.vector3d(0., 0., -1.) |
| 48 |
length: 5 |
| 49 |
|
| 50 |
onHitsChanged: printHits("Model hits", hits) |
| 51 |
} |
| 52 |
|
| 53 |
ScreenRayCaster { |
| 54 |
id: screenRayCaster |
| 55 |
position: Qt.point(200, 200) |
| 56 |
|
| 57 |
onHitsChanged: printHits("Screen hits", hits) |
| 58 |
} |
| 59 |
|
| 60 |
MouseHandler { |
| 61 |
id: mouseHandler |
| 62 |
sourceDevice: MouseDevice {} |
| 63 |
onReleased: { screenRayCaster.trigger(Qt.point(mouse.x, mouse.y)) } |
| 64 |
} |
| 65 |
|
| 66 |
KeyboardDevice { id: kbDevice } |
| 67 |
KeyboardHandler { |
| 68 |
id: kbHandler |
| 69 |
focus: true |
| 70 |
sourceDevice: kbDevice |
| 71 |
onPressed: { |
| 72 |
if (event.text.toLowerCase() == "a") { raycaster.origin.x -= .1; raycaster.trigger() } |
| 73 |
if (event.text.toLowerCase() == "f") { raycaster.origin.x += .1; raycaster.trigger() } |
| 74 |
if (event.text.toLowerCase() == "s") { raycaster.origin.y += .1; raycaster.trigger() } |
| 75 |
if (event.text.toLowerCase() == "d") { raycaster.origin.y -= .1; raycaster.trigger() } |
| 76 |
} |
| 77 |
onSpacePressed: { |
| 78 |
var syncHits = raycaster.pick(raycaster.origin, raycaster.direction, raycaster.length) |
| 79 |
printHits("Synchronous hits", syncHits) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
components: [ external_forward_renderer, inputSettings, headLight, raycaster, screenRayCaster, mouseHandler, kbHandler ] |
| 84 |
|
| 85 |
function printHits(desc, hits) { |
| 86 |
console.log(desc, hits.length) |
| 87 |
for (var i=0; i<hits.length; i++) { |
| 88 |
console.log(" " + hits[i].entity.objectName, hits[i].distance, |
| 89 |
hits[i].worldIntersection.x, hits[i].worldIntersection.y, hits[i].worldIntersection.z) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
CylinderEntity { |
| 94 |
objectName: "CRed" |
| 95 |
id: leftEntity |
| 96 |
translation: Qt.vector3d(-5, 0, 0) |
| 97 |
color: "red" |
| 98 |
} |
| 99 |
|
| 100 |
CylinderEntity { |
| 101 |
objectName: "CGreen" |
| 102 |
id: mainEntity |
| 103 |
translation: Qt.vector3d(0, 0, 0) |
| 104 |
color: "green" |
| 105 |
} |
| 106 |
|
| 107 |
CylinderEntity { |
| 108 |
objectName: "CBlue" |
| 109 |
id: rightEntity |
| 110 |
translation: Qt.vector3d(5, 0, 0) |
| 111 |
color: "blue" |
| 112 |
} |
| 113 |
|
| 114 |
LineEntity { |
| 115 |
id: raydisplay |
| 116 |
origin: raycaster.origin |
| 117 |
direction: raycaster.direction |
| 118 |
length: raycaster.length |
| 119 |
} |
| 120 |
} |