xrfragment-haxe/src/3rd/js/index.js

56 lines
1.9 KiB
JavaScript
Raw Normal View History

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 || {}
xrf.debug = document.location.hostname.match(/^(localhost|[0-9]\.[0-9])/) || document.location.port == '8080' ? 0 : false
if( xrf.debug === false ){
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] )
}else{
xrf.stats()
}
2023-06-07 17:42:21 +02:00
xrf.Parser.debug = xrf.debug
xrf.detectCameraRig(opts)
for ( let i in opts ) xrf[i] = opts[i]
2023-06-07 17:42:21 +02:00
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")
}
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
}
}
2024-02-13 17:10:24 +00:00
xrf.stats = () => {
// bookmarklet from https://github.com/zlgenuine/threejs_stats
(function(){
let i = 0;
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);
2024-02-13 17:10:24 +00: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]