-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2428d56
commit ec64d2b
Showing
8 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<html> | ||
<head> | ||
<title>Itowns - WebXR Example</title> | ||
|
||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
|
||
<link rel="stylesheet" type="text/css" href="css/example.css"> | ||
<link rel="stylesheet" type="text/css" href="css/LoadingScreen.css"> | ||
<link rel="stylesheet" type="text/css" href="css/widgets.css"> | ||
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script> | ||
</head> | ||
<body> | ||
<div id="viewerDiv"></div> | ||
|
||
<!-- Import iTowns source code --> | ||
<script src="../dist/itowns.js"></script> | ||
<script src="../dist/debug.js"></script> | ||
<!-- Import iTowns Widgets plugin --> | ||
<script src="../dist/itowns_widgets.js"></script> | ||
<!-- Import iTowns LoadingScreen and GuiTools plugins --> | ||
<script src="js/GUI/GuiTools.js"></script> | ||
<script src="js/GUI/LoadingScreen.js"></script> | ||
|
||
<script type="text/javascript"> | ||
// ---------- CREATE A GlobeView FOR SUPPORTING DATA VISUALIZATION : ---------- | ||
|
||
// Define camera initial position | ||
const placement = { | ||
coord: new itowns.Coordinates('EPSG:4326', 6.227, 45.167), | ||
range: 15000, | ||
tilt: 5, | ||
heading: 62, | ||
} | ||
|
||
// `viewerDiv` will contain iTowns' rendering area (`<canvas>`) | ||
const viewerDiv = document.getElementById('viewerDiv'); | ||
|
||
// Create a GlobeView | ||
const view = new itowns.GlobeView(viewerDiv, placement, { | ||
webXR: { scale: 0.005 }, | ||
}); | ||
|
||
// Setup loading screen and debug menu | ||
setupLoadingScreen(viewerDiv, view); | ||
|
||
// ---------- DISPLAY ORTHO-IMAGES : ---------- | ||
|
||
// Add one imagery layer to the scene. This layer's properties are | ||
// defined in a json file, but it could be defined as a plain js | ||
// object. See `Layer` documentation for more info. | ||
itowns.Fetcher.json('./layers/JSONLayers/Ortho.json').then((config) => { | ||
config.source = new itowns.WMTSSource(config.source); | ||
view.addLayer(new itowns.ColorLayer('Ortho', config), | ||
); | ||
}); | ||
|
||
// ---------- DISPLAY A DIGITAL ELEVATION MODEL : ---------- | ||
|
||
// Add two elevation layers, each with a different level of detail. | ||
// Here again, each layer's properties are defined in a json file. | ||
function addElevationLayerFromConfig(config) { | ||
config.source = new itowns.WMTSSource(config.source); | ||
view.addLayer( | ||
new itowns.ElevationLayer(config.id, config), | ||
); | ||
} | ||
itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json') | ||
.then(addElevationLayerFromConfig); | ||
itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json') | ||
.then(addElevationLayerFromConfig); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* global XRRigidTransform */ | ||
|
||
import * as THREE from 'three'; | ||
import { VRButton } from 'ThreeExtended/webxr/VRButton'; | ||
|
||
async function shutdownXR(session) { | ||
if (session) { | ||
await session.end(); | ||
} | ||
} | ||
|
||
const initializeWebXR = (view, options) => { | ||
const scale = options.scale || 1.0; | ||
|
||
const renderer = view.mainLoop.gfxEngine.renderer; | ||
const xr = renderer.xr; | ||
|
||
view.domElement.appendChild(VRButton.createButton(renderer)); | ||
|
||
xr.addEventListener('sessionstart', () => { | ||
const camera = view.camera.camera3D; | ||
|
||
const exitXRSession = (event) => { | ||
if (event.key === 'Escape') { | ||
document.removeEventListener('keydown', exitXRSession); | ||
xr.enabled = false; | ||
view.camera.camera3D = camera; | ||
|
||
view.scene.scale.multiplyScalar(1 / scale); | ||
view.scene.updateMatrixWorld(); | ||
|
||
shutdownXR(xr.getSession()); | ||
view.notifyChange(view.camera.camera3D, true); | ||
} | ||
}; | ||
view.scene.scale.multiplyScalar(scale); | ||
view.scene.updateMatrixWorld(); | ||
xr.enabled = true; | ||
xr.getReferenceSpace('local'); | ||
|
||
const position = view.camera.position(); | ||
const geodesicNormal = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 0, 1), position.geodesicNormal).invert(); | ||
|
||
const quat = new THREE.Quaternion(-1, 0, 0, 1).normalize().multiply(geodesicNormal); | ||
const trans = camera.position.clone().multiplyScalar(-scale).applyQuaternion(quat); | ||
const transform = new XRRigidTransform(trans, quat); | ||
|
||
const baseReferenceSpace = xr.getReferenceSpace(); | ||
const teleportSpaceOffset = baseReferenceSpace.getOffsetReferenceSpace(transform); | ||
xr.setReferenceSpace(teleportSpaceOffset); | ||
|
||
view.camera.camera3D = xr.getCamera(); | ||
view.camera.resize(view.camera.width, view.camera.height); | ||
|
||
document.addEventListener('keydown', exitXRSession, false); | ||
|
||
// TODO Fix asynchronization between xr and MainLoop render loops. | ||
// (see MainLoop#scheduleViewUpdate). | ||
xr.setAnimationLoop((timestamp) => { | ||
if (xr.isPresenting && view.camera.camera3D.cameras[0]) { | ||
view.camera.camera3D.updateMatrix(); | ||
view.camera.camera3D.updateMatrixWorld(true); | ||
view.notifyChange(view.camera.camera3D, true); | ||
} | ||
|
||
view.mainLoop.step(view, timestamp); | ||
}); | ||
}); | ||
}; | ||
|
||
export default initializeWebXR; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import assert from 'assert'; | ||
import View from 'Core/View'; | ||
import Renderer from './bootstrap'; | ||
|
||
describe('WebXR', function () { | ||
let viewer; | ||
before(function () { | ||
const renderer = new Renderer(); | ||
|
||
viewer = new View('EPSG:4326', renderer.domElement, { | ||
renderer, | ||
webXR: { scale: 0.005 }, | ||
}); | ||
}); | ||
|
||
it('should initialize webXr', function () { | ||
// VR button added to viewer div | ||
// Children shall be the canvas and the VR button | ||
assert.ok(viewer.domElement.children.length === 2); | ||
const webXRManager = viewer.mainLoop.gfxEngine.renderer.xr; | ||
|
||
const sessionEvent = webXRManager.events.get('sessionstart'); | ||
|
||
assert.ok(typeof sessionEvent === 'function'); | ||
}); | ||
|
||
it('should initialize webXr session', function () { | ||
const webXRManager = viewer.mainLoop.gfxEngine.renderer.xr; | ||
assert.ok(webXRManager.enabled === undefined); | ||
webXRManager.dispatchEvent({ type: 'sessionstart' }); | ||
assert.ok(webXRManager.enabled); | ||
}); | ||
|
||
it('should close webXr session', function () { | ||
const webXRManager = viewer.mainLoop.gfxEngine.renderer.xr; | ||
assert.ok(webXRManager.enabled); | ||
document.emitEvent('keydown', { key: 'Escape' }); | ||
assert.ok(webXRManager.enabled === false); | ||
assert.ok(viewer); | ||
}); | ||
}); |