-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: AR support #397
Draft
mariobuikhuizen
wants to merge
13
commits into
widgetti:master
Choose a base branch
from
mariobuikhuizen:feat_ar_2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: AR support #397
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dad53f7
WIP: AR support
mariobuikhuizen cac0166
add pythreejs dependecy with updated threejs version
mariobuikhuizen 17c1bd5
fix: no image
mariobuikhuizen 41d684d
fix: don't use render_continuous
mariobuikhuizen 3057275
fix: better directional light defaults
maartenbreddels 29e6dcc
feat: background-opacity in style, used for ar style
maartenbreddels 7bd0d43
use hittest to place box in scene
maartenbreddels 336c623
render to screen in AR mode and ignore update calls
maartenbreddels 1f35631
fix: add lights to rootObject such that they track the AR placement
maartenbreddels 9ea176d
refactor: use template for button and untangle ar code
mariobuikhuizen 0747ec1
fix: set renderloop to null and add missing space
mariobuikhuizen 55aceab
better centering of box
maartenbreddels 5a67b47
add server module
maartenbreddels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,203 +1,106 @@ | ||
import { WebXRManager } from 'three' | ||
|
||
export function createButton( renderer, onStart, onEnd, sessionInit = {} ) { | ||
|
||
const button = document.createElement( 'button' ); | ||
console.log('__renderer:', renderer) | ||
|
||
function showStartAR( /*device*/ ) { | ||
|
||
if ( sessionInit.domOverlay === undefined ) { | ||
|
||
var overlay = document.createElement( 'div' ); | ||
overlay.id = 'aroverlay'; | ||
overlay.style.display = 'none'; | ||
document.body.appendChild( overlay ); | ||
// document.document.getElementById('bliep').appendChild( overlay ); | ||
|
||
var svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ); | ||
svg.setAttribute( 'width', 38 ); | ||
svg.setAttribute( 'height', 38 ); | ||
svg.style.position = 'absolute'; | ||
svg.style.right = '20px'; | ||
svg.style.top = '20px'; | ||
svg.addEventListener( 'click', function () { | ||
onEnd(); | ||
console.log('end'); | ||
currentSession.end(); | ||
|
||
} ); | ||
overlay.appendChild( svg ); | ||
|
||
var path = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' ); | ||
path.setAttribute( 'd', 'M 12,12 L 28,28 M 28,12 12,28' ); | ||
path.setAttribute( 'stroke', '#fff' ); | ||
path.setAttribute( 'stroke-width', 2 ); | ||
svg.appendChild( path ); | ||
|
||
if ( sessionInit.optionalFeatures === undefined ) { | ||
|
||
sessionInit.optionalFeatures = []; | ||
|
||
} | ||
|
||
sessionInit.optionalFeatures.push( 'dom-overlay' ); | ||
sessionInit.domOverlay = { root: overlay }; | ||
|
||
} | ||
|
||
// | ||
|
||
let currentSession = null; | ||
|
||
async function onSessionStarted( session ) { | ||
session.addEventListener( 'end', onSessionEnded ); | ||
console.log('rr', renderer); | ||
console.log('start session', renderer.xr) | ||
renderer.xr.setReferenceSpaceType( 'local' ); | ||
console.log('b4 set session'); | ||
await renderer.xr.setSession( session ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
console.log('after set session'); | ||
button.textContent = 'STOP AR'; | ||
sessionInit.domOverlay.root.style.display = ''; | ||
sessionInit.domOverlay.root.style.display = ''; | ||
|
||
currentSession = session; | ||
|
||
} | ||
|
||
function onSessionEnded( /*event*/ ) { | ||
|
||
currentSession.removeEventListener( 'end', onSessionEnded ); | ||
|
||
button.textContent = 'START AR'; | ||
sessionInit.domOverlay.root.style.display = 'none'; | ||
|
||
currentSession = null; | ||
|
||
} | ||
|
||
// | ||
|
||
button.style.display = ''; | ||
button.id = 'arbtn'; | ||
|
||
button.style.cursor = 'pointer'; | ||
button.style.left = 'calc(50% - 50px)'; | ||
button.style.width = '100px'; | ||
|
||
button.textContent = 'START AR'; | ||
|
||
button.onmouseenter = function () { | ||
|
||
button.style.opacity = '1.0'; | ||
|
||
}; | ||
|
||
button.onmouseleave = function () { | ||
|
||
button.style.opacity = '0.5'; | ||
|
||
}; | ||
|
||
button.onclick = function () { | ||
console.log('current', currentSession); | ||
if ( currentSession === null ) { | ||
console.log('requesting session') | ||
onStart(); | ||
navigator.xr.requestSession( 'immersive-ar', sessionInit ).then( onSessionStarted ); | ||
|
||
} else { | ||
export const closeOverlay = document.createElement("div"); | ||
closeOverlay.style.display = "none"; | ||
document.body.appendChild(closeOverlay); | ||
|
||
let onClose = () => console.log("onClose not set"); | ||
|
||
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | ||
svg.setAttribute("width", 38); | ||
svg.setAttribute("height", 38); | ||
svg.style.position = "absolute"; | ||
svg.style.right = "20px"; | ||
svg.style.top = "20px"; | ||
svg.addEventListener("click", () => onClose()); | ||
closeOverlay.appendChild(svg); | ||
|
||
const path = document.createElementNS("http://www.w3.org/2000/svg", "path"); | ||
path.setAttribute("d", "M 12,12 L 28,28 M 28,12 12,28"); | ||
path.setAttribute("stroke", "#fff"); | ||
path.setAttribute("stroke-width", 2); | ||
svg.appendChild(path); | ||
|
||
function setOnClose(fn) { | ||
onClose = fn; | ||
} | ||
|
||
currentSession.end(); | ||
function showCloseOverlay(value) { | ||
closeOverlay.style.display = value ? "" : "none"; | ||
} | ||
|
||
} | ||
let lastXrTransform = null; | ||
let lastWidth = null; | ||
let lastHeight = null; | ||
|
||
}; | ||
export function createController(renderer, scene, onSelect) { | ||
const arController = renderer.xr.getController(0); | ||
arController.addEventListener("select", () => onSelect(lastXrTransform)); | ||
scene.add(arController); | ||
return arController; | ||
} | ||
|
||
export async function start(renderer, renderFn, onStop) { | ||
if (renderer.xr.getSession() !== null) { | ||
return; | ||
} | ||
renderer.xr.enabled = true; | ||
|
||
function disableButton() { | ||
const sessionInit = { | ||
optionalFeatures: ["dom-overlay", "hit-test"], | ||
domOverlay: { root: closeOverlay } | ||
}; | ||
|
||
button.style.display = ''; | ||
showCloseOverlay(true); | ||
|
||
button.style.cursor = 'auto'; | ||
button.style.left = 'calc(50% - 75px)'; | ||
button.style.width = '150px'; | ||
const session = await navigator.xr.requestSession("immersive-ar", sessionInit); | ||
renderer.xr.setReferenceSpaceType("local"); | ||
renderer.xr.setSession(session); | ||
const { x, y } = renderer.getSize(); | ||
lastWidth = x; | ||
lastHeight = y; | ||
renderer.setSize(window.innerWidth, window.innerHeight); | ||
|
||
button.onmouseenter = null; | ||
button.onmouseleave = null; | ||
lastXrTransform = null; | ||
|
||
button.onclick = null; | ||
renderer.setAnimationLoop( | ||
await extractHitTransform( | ||
renderer, | ||
renderFn, | ||
(xrTransform) => { lastXrTransform = xrTransform; })); | ||
|
||
} | ||
|
||
function showARNotSupported() { | ||
console.log('ar not supported'); | ||
disableButton(); | ||
|
||
button.textContent = 'AR NOT SUPPORTED'; | ||
|
||
} | ||
|
||
function stylizeElement( element ) { | ||
|
||
element.style.position = 'absolute'; | ||
element.style.bottom = '20px'; | ||
element.style.padding = '12px 6px'; | ||
element.style.border = '1px solid #fff'; | ||
element.style.borderRadius = '4px'; | ||
element.style.background = 'rgba(0,0,0,0.1)'; | ||
element.style.color = '#fff'; | ||
element.style.font = 'normal 13px sans-serif'; | ||
element.style.textAlign = 'center'; | ||
element.style.opacity = '0.5'; | ||
element.style.outline = 'none'; | ||
element.style.zIndex = '999'; | ||
setOnClose(onStop); | ||
} | ||
|
||
export function stop(renderer) { | ||
if (renderer.xr.getSession() === null) { | ||
return; | ||
} | ||
console.log('xr:', 'xr' in navigator); | ||
if ( 'xr' in navigator ) { | ||
|
||
button.id = 'ARButton'; | ||
// button.style.display = 'none'; | ||
|
||
stylizeElement( button ); | ||
|
||
navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) { | ||
console.log('supported!:', supported) | ||
console.log('context', renderer.getContext()); | ||
supported ? showStartAR() : showARNotSupported(); | ||
|
||
} ).catch( showARNotSupported ); | ||
|
||
return button; | ||
|
||
} else { | ||
|
||
const message = document.createElement( 'a' ); | ||
|
||
if ( window.isSecureContext === false ) { | ||
|
||
message.href = document.location.href.replace( /^http:/, 'https:' ); | ||
message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message | ||
|
||
} else { | ||
|
||
message.href = 'https://immersiveweb.dev/'; | ||
message.innerHTML = 'WEBXR NOT AVAILABLE'; | ||
renderer.xr.getSession().end(); | ||
renderer.xr.setSession(null); | ||
lastXrTransform = null; | ||
showCloseOverlay(false); | ||
renderer.setAnimationLoop(() => null); | ||
renderer.setSize(lastWidth, lastHeight); | ||
} | ||
|
||
async function extractHitTransform(renderer, renderFn, onXrTransformChange) { | ||
const session = renderer.xr.getSession(); | ||
const referenceSpace = await session.requestReferenceSpace("viewer"); | ||
const hitTestSource = await session.requestHitTestSource({ space: referenceSpace }); | ||
|
||
return (time, frame) => { | ||
if (frame) { | ||
// based on https://threejs.org/examples/webxr_ar_hittest.html | ||
const referenceSpace = renderer.xr.getReferenceSpace(); | ||
|
||
if (hitTestSource) { | ||
const hitTestResults = frame.getHitTestResults(hitTestSource); | ||
if (hitTestResults.length) { | ||
const hit = hitTestResults[0]; | ||
const pose = hit.getPose(referenceSpace); | ||
onXrTransformChange(pose.transform); | ||
} | ||
} | ||
} | ||
|
||
message.style.left = 'calc(50% - 90px)'; | ||
message.style.width = '180px'; | ||
message.style.textDecoration = 'none'; | ||
|
||
stylizeElement( message ); | ||
|
||
return message; | ||
|
||
} | ||
|
||
renderFn(time, frame); | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise we still render continuous after stopping.