xrfragment/src/3rd/js/three/xrf/dynamic/URIvars.js

78 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-02-08 19:40:43 +01:00
// this holds all the URI Template variables (https://www.rfc-editor.org/rfc/rfc6570)
xrf.addEventListener('parseModel', (opts) => {
let {model,url,file} = opts
if( model.isSRC || opts.isSRC ) return // ignore SRC models
2024-02-13 17:10:24 +00:00
2024-02-08 19:40:43 +01:00
xrf.URI.vars = new Proxy({},{
2024-02-13 17:10:24 +00:00
set(me,k,v){
if( k.match(/^(name)$/) ) return
me[k] = v
},
2024-02-08 19:40:43 +01:00
get(me,k ){
if( k == '__object' ){
let obj = {}
for( let i in xrf.URI.vars ) obj[i] = xrf.URI.vars[i]()
return obj
}
return me[k]
},
})
model.scene.traverse( (n) => {
2024-02-15 11:43:15 +00:00
const variables = /{([a-zA-Z0-9-]+)}/g
2024-02-08 19:40:43 +01:00
if( n.userData ){
for( let i in n.userData ){
2024-02-15 11:43:15 +00:00
if( i[0] == '#' || i.match(/^(href|tag)$/) ) continue // ignore XR Fragment aliases
if( i == 'src' ){
// lets declare empty variables found in src-values ('https://foo.com/video.mp4#{somevar}') e.g.
if( n.userData[i].match(variables) ){
let vars = [].concat( n.userData[i].match(variables) )
const strip = (v) => v.replace(/[{}]/g,'')
vars.map( (v) => xrf.URI.vars[ strip(v) ] = () => '' )
}
}else xrf.URI.vars[i] = () => n.userData[i] // declare variables with values
2024-02-08 19:40:43 +01:00
}
}
})
})
xrf.addEventListener('dynamicKeyValue', (opts) => {
// select active camera if any
let {id,match,v} = opts
if( !v.is( xrf.XRF.CUSTOMFRAG) ) return // only process custom frags from here
2024-02-13 17:10:24 +00:00
if( v.string.match(/(<|>)/) ) return // ignore filter values
2024-02-15 11:43:15 +00:00
2024-02-08 19:40:43 +01:00
if( match.length > 0 ){
2024-02-14 15:47:34 +00:00
xrf.frag.dynamic.material(v,opts) // check if fragment is an objectname
2024-02-08 19:40:43 +01:00
}
2024-02-13 17:10:24 +00:00
2024-02-14 15:47:34 +00:00
if( !xrf.URI.vars[ v.string ] ) return console.error(`'${v.string}' metadata-key not found in scene`)
2024-02-15 11:43:15 +00:00
//if( xrf.URI.vars[ id ] && !match.length ) return console.error(`'${id}' object/tag/metadata-key not found in scene`)
2024-02-13 17:10:24 +00:00
if( xrf.debug ) console.log(`URI.vars[${id}]='${v.string}'`)
2024-02-14 15:47:34 +00:00
if( xrf.URI.vars[id] ){
xrf.URI.vars[ id ] = xrf.URI.vars[ v.string ] // update var
xrf.scene.traverse( (n) => {
// re-expand src-values which use the updated URI Template var
if( n.userData && n.userData.src && n.userData.srcTemplate && n.userData.srcTemplate.match(`{${id}}`) ){
let srcNewFragments = xrf.frag.src.expandURI( n ).replace(/.*#/,'')
console.log(`URI.vars[${id}] => updating ${n.name} => ${srcNewFragments}`)
let frag = xrf.hashbus.pub( srcNewFragments, n )
}
})
}else{
xrf.XRWG.deepApplyMatch(match, v, (match,v,node,type) => {
console.log(v.string)
if( node.geometry ) xrf.hashbus.pub( xrf.URI.vars[ v.string ](), node) // apply fragment mesh(es)
})
}
2024-02-08 19:40:43 +01:00
})