-
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
Showing
7 changed files
with
239 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<!DOCTYPE html> | ||
<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"> | ||
</head> | ||
<body> | ||
<div id="viewerDiv"></div> | ||
|
||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/" | ||
} | ||
} | ||
</script> | ||
|
||
<!-- Import iTowns source code --> | ||
<script src="../dist/itowns.js"></script> | ||
|
||
<script type="module"> | ||
|
||
import { VRButton } from 'three/addons/webxr/VRButton.js'; | ||
|
||
// ---------- SETUP THE VR VIEW : ---------- | ||
|
||
// 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 }, | ||
}); | ||
|
||
// Instantiate three's VR Button | ||
const vrButton = VRButton.createButton(view.renderer); | ||
viewerDiv.appendChild(vrButton); | ||
|
||
// ---------- 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,69 @@ | ||
/* global XRRigidTransform */ | ||
|
||
import * as THREE from 'three'; | ||
|
||
async function shutdownXR(session) { | ||
if (session) { | ||
await session.end(); | ||
} | ||
} | ||
|
||
const initializeWebXR = (view, options) => { | ||
const scale = options.scale || 1.0; | ||
|
||
const xr = view.mainLoop.gfxEngine.renderer.xr; | ||
|
||
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,37 @@ | ||
import assert from 'assert'; | ||
import View from 'Core/View'; | ||
import Renderer from './bootstrap'; | ||
|
||
describe('WebXR', function () { | ||
let viewer; | ||
before(async function () { | ||
const renderer = new Renderer(); | ||
|
||
viewer = new View('EPSG:4326', renderer.domElement, { | ||
renderer, | ||
webXR: { scale: 0.005 }, | ||
}); | ||
}); | ||
|
||
it('should initialize webXr', function () { | ||
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); | ||
}); | ||
}); |