2023-06-07 17:42:21 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
// Copyright (c) 2023 Leon van Kammen/NLNET
|
|
|
|
|
|
|
|
|
|
var xrf = {}
|
|
|
|
|
|
|
|
|
|
xrf.init = function(opts){
|
2023-11-08 18:28:18 +01:00
|
|
|
opts = opts || {}
|
2024-02-01 09:04:01 +00:00
|
|
|
|
2024-02-01 19:10:41 +00:00
|
|
|
xrf.debug = document.location.hostname.match(/^(localhost|[0-9])/) ? 0 : false
|
2024-02-01 09:04:01 +00:00
|
|
|
if( !xrf.debug ){
|
|
|
|
|
console.log("add #debug=[0-9] to URL to see XR Fragment debuglog")
|
|
|
|
|
xrf.debug = parseInt( ( document.location.hash.match(/debug=([0-9])/) || [0,'0'] )[1] )
|
|
|
|
|
}
|
2024-02-13 17:10:24 +00:00
|
|
|
if( xrf.debug != undefined ) xrf.stats()
|
2024-02-01 09:04:01 +00:00
|
|
|
|
2023-06-07 17:42:21 +02:00
|
|
|
xrf.Parser.debug = xrf.debug
|
2023-10-24 18:18:29 +02:00
|
|
|
xrf.detectCameraRig(opts)
|
2023-06-07 17:42:21 +02:00
|
|
|
for ( let i in opts ) xrf[i] = opts[i]
|
|
|
|
|
xrf.emit('init',opts)
|
2023-10-13 11:45:17 +02:00
|
|
|
return xrf
|
2023-06-07 17:42:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xrf.query = function(){
|
|
|
|
|
// framework implementations can override this function, see src/3rd/js/three/index.sj
|
|
|
|
|
alert("queries are not implemented (yet) for this particular framework")
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 18:18:29 +02:00
|
|
|
xrf.detectCameraRig = function(opts){
|
|
|
|
|
if( opts.camera ){ // detect rig (if any)
|
|
|
|
|
let getCam = ((cam) => () => cam)(opts.camera)
|
|
|
|
|
let offsetY = 0
|
|
|
|
|
while( opts.camera.parent.type != "Scene" ){
|
|
|
|
|
offsetY += opts.camera.position.y
|
|
|
|
|
opts.camera = opts.camera.parent
|
|
|
|
|
opts.camera.getCam = getCam
|
|
|
|
|
opts.camera.updateProjectionMatrix = () => opts.camera.getCam().updateProjectionMatrix()
|
|
|
|
|
}
|
|
|
|
|
opts.camera.offsetY = offsetY
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 16:40:08 +02:00
|
|
|
xrf.roundrobin = (frag, store) => {
|
|
|
|
|
if( !frag.args || frag.args.length == 0 ) return 0
|
|
|
|
|
if( !store.rr ) store.rr = {}
|
|
|
|
|
let label = frag.fragment
|
|
|
|
|
if( store.rr[label] ) return store.rr[label].next()
|
|
|
|
|
store.rr[label] = frag.args
|
|
|
|
|
store.rr[label].next = () => {
|
|
|
|
|
store.rr[label].index = (store.rr[label].index + 1) % store.rr[label].length
|
|
|
|
|
return store.rr[label].index
|
|
|
|
|
}
|
|
|
|
|
return store.rr[label].index = 0
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 17:10:24 +00:00
|
|
|
xrf.stats = () => {
|
|
|
|
|
// bookmarklet from https://github.com/zlgenuine/threejs_stats
|
|
|
|
|
(function(){
|
|
|
|
|
for( let i = 0; i < 4; i++ ){
|
|
|
|
|
var script=document.createElement('script');script.onload=function(){var stats=new Stats();stats.showPanel( i );
|
|
|
|
|
stats.dom.style.marginTop = `${i*48}px`; document.body.appendChild(stats.dom);requestAnimationFrame(function loop(){stats.update();requestAnimationFrame(loop)});};script.src='//rawgit.com/mrdoob/stats.js/master/build/stats.min.js';document.head.appendChild(script);
|
|
|
|
|
}
|
|
|
|
|
})()
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
xrf.hasTag = (tag,tags) => String(tags).match( new RegExp(`(^| )${tag}( |$)`,`g`) )
|
2023-06-09 16:40:08 +02:00
|
|
|
|
2023-06-07 17:42:21 +02:00
|
|
|
// map library functions to xrf
|
|
|
|
|
for ( let i in xrfragment ) xrf[i] = xrfragment[i]
|