Commit 948b7125 authored by LAU's avatar LAU
Browse files

Update index.html

parent 0aa50393
Pipeline #7917 passed with stage
in 4 seconds
<!doctype html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <title>Simple WebXR Demo</title>
<meta name="viewport" <meta charset="UTF-8">
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cube Demo</title> <style>
/* Style for the buttons */
<!-- three.js --> button {
<script src="https://unpkg.com/three@0.126.0/build/three.js"></script> background-color: blue;
</head> color: white;
<body> font-size: 24px;
padding: 12px 24px;
<!-- Starting an immersive WebXR session requires user interaction. border: none;
We start this one with a simple button. --> border-radius: 4px;
<button onclick="activateXR()">Start Cube Demo</button> margin: 20px;
<script> cursor: pointer;
async function activateXR() { }
// Add a canvas element and initialize a WebGL context that is compatible with WebXR.
const canvas = document.createElement("canvas"); /* Style for the logo */
document.body.appendChild(canvas); #logo {
const gl = canvas.getContext("webgl", {xrCompatible: true}); float: right;
margin-right: 20px;
// To be continued in upcoming steps. margin-top: 20px;
width: 100px;
const scene = new THREE.Scene(); height: 100px;
}
// The cube will have a different color on each side.
const materials = [ /* Style for the footer */
new THREE.MeshBasicMaterial({color: 0xff0000}), footer {
new THREE.MeshBasicMaterial({color: 0x0000ff}), text-align: center;
new THREE.MeshBasicMaterial({color: 0x00ff00}), margin-top: 50px;
new THREE.MeshBasicMaterial({color: 0xff00ff}), }
new THREE.MeshBasicMaterial({color: 0x00ffff}),
new THREE.MeshBasicMaterial({color: 0xffff00}) /* Style for the email link */
]; footer a {
color: blue;
// Create the cube and add it to the demo scene. text-decoration: none;
const cube = new THREE.Mesh(new THREE.BoxBufferGeometry(0.2, 0.2, 0.2), materials); }
cube.position.set(1, 1, 1); </style>
scene.add(cube); </head>
<body>
// Set up the WebGLRenderer, which handles rendering to the session's base layer. <h1>Welcome to this simple WebXR demo!</h1>
const renderer = new THREE.WebGLRenderer({ <p>Click on one of the buttons below to check the two demos:</p>
alpha: true, <img src="https://www.hft-stuttgart.de/typo3conf/ext/hft_sitepackage/Resources/Public/img/HFT_logo.svg" alt="HfT Stuttgart" id="logo">
preserveDrawingBuffer: true, <button onclick="location.href='1-Cube.html'">Cube demo</button>
canvas: canvas, <button onclick="location.href='2-Hit.html'">Tap to Place Demo</button>
context: gl <p> You can follow this <a href="https://transfer.hft-stuttgart.de/gitlab/coors/visualization/-/wikis/Create-a-simple-web-AR-application">tutorial</a> to create a similar WebXR app.</p>
}); <footer>&copy; 2023 HfT Stuttgart. Contact <a href="mailto:muhammad.alfakhori@hft-stuttgart.de">Muhammad Alfakhori</a>.</footer>
renderer.autoClear = false; </body>
// The API directly updates the camera matrices.
// Disable matrix auto updates so three.js doesn't attempt
// to handle the matrices independently.
const camera = new THREE.PerspectiveCamera();
camera.matrixAutoUpdate = false;
// Initialize a WebXR session using "immersive-ar".
const session = await navigator.xr.requestSession("immersive-ar");
session.updateRenderState({
baseLayer: new XRWebGLLayer(session, gl)
});
// A 'local' reference space has a native origin that is located
// near the viewer's position at the time the session was created.
const referenceSpace = await session.requestReferenceSpace('local');
// Create a render loop that allows us to draw on the AR view.
const onXRFrame = (time, frame) => {
// Queue up the next draw request.
session.requestAnimationFrame(onXRFrame);
// Bind the graphics framebuffer to the baseLayer's framebuffer
gl.bindFramebuffer(gl.FRAMEBUFFER, session.renderState.baseLayer.framebuffer)
// Retrieve the pose of the device.
// XRFrame.getViewerPose can return null while the session attempts to establish tracking.
const pose = frame.getViewerPose(referenceSpace);
if (pose) {
// In mobile AR, we only have one view.
const view = pose.views[0];
const viewport = session.renderState.baseLayer.getViewport(view);
renderer.setSize(viewport.width, viewport.height)
// Use the view's transform matrix and projection matrix to configure the THREE.camera.
camera.matrix.fromArray(view.transform.matrix)
camera.projectionMatrix.fromArray(view.projectionMatrix);
camera.updateMatrixWorld(true);
// Render the scene with THREE.WebGLRenderer.
renderer.render(scene, camera)
}
}
session.requestAnimationFrame(onXRFrame);
}
</script>
</body>
</html> </html>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment