xrfragment-haxe/src/3rd/js/three/xrf/src.js

135 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-06-07 17:42:21 +02:00
// *TODO* use webgl instancing
xrf.frag.src = function(v, opts){
opts.embedded = v // indicate embedded XR fragment
2023-10-12 17:04:46 +02:00
let { mesh, model, camera, scene, renderer, THREE, hashbus, frag} = opts
2023-07-04 15:23:10 +02:00
2023-11-14 18:08:19 +01:00
const hasMaterialName = mesh.material && mesh.material.name.length > 0
const hasTexture = mesh.material && mesh.material.map
const isPlane = mesh.geometry && mesh.geometry.attributes.uv && mesh.geometry.attributes.uv.count == 4
const hasLocalSRC = mesh.userData.src != undefined && mesh.userData.src[0] == '#'
2023-09-21 13:05:30 +02:00
let src;
let url = v.string
2023-10-12 17:04:46 +02:00
let vfrag = xrfragment.URI.parse(url)
2023-11-14 18:08:19 +01:00
// handle non-euclidian planes
if( mesh.geometry && !hasMaterialName && !hasTexture && hasLocalSRC && isPlane ){
return xrf.portalNonEuclidian(opts)
}
2023-09-21 13:05:30 +02:00
const addModel = (model,url,frag) => {
let scene = model.scene
2023-11-16 14:50:57 +01:00
xrf.frag.src.filterScene(scene,{...opts,frag})
xrf.frag.src.scale( scene, opts, url )
//enableSourcePortation(scene)
mesh.add(model.scene)
mesh.traverse( (n) => n.isSRC = n.isXRF = true ) // mark everything SRC
2023-11-16 14:50:57 +01:00
xrf.emit('parseModel', {...opts, scene, model})
if( mesh.material ) mesh.material.visible = false // hide placeholder object
2023-06-07 17:42:21 +02:00
}
2023-07-04 15:23:10 +02:00
2023-10-12 17:04:46 +02:00
const enableSourcePortation = (src) => {
// show sourceportation clickable plane
2023-10-12 17:04:46 +02:00
if( vfrag.href || v.string[0] == '#' ) return
let scale = new THREE.Vector3()
let size = new THREE.Vector3()
mesh.getWorldScale(scale)
new THREE.Box3().setFromObject(src).getSize(size)
const geo = new THREE.SphereGeometry( Math.max(size.x, size.y, size.z) / scale.x, 10, 10 )
const mat = new THREE.MeshBasicMaterial()
mat.transparent = true
mat.roughness = 0.05
mat.metalness = 1
mat.opacity = 0
const cube = new THREE.Mesh( geo, mat )
console.log("todo: sourceportate")
}
2023-09-21 13:05:30 +02:00
const externalSRC = (url,frag,src) => {
fetch(url, { method: 'HEAD' })
2023-07-05 16:43:07 +02:00
.then( (res) => {
2023-09-21 13:05:30 +02:00
console.log(`loading src ${url}`)
2023-07-05 16:43:07 +02:00
let mimetype = res.headers.get('Content-type')
2023-09-21 13:05:30 +02:00
if( url.replace(/#.*/,'').match(/\.(gltf|glb)$/) ) mimetype = 'gltf'
//if( url.match(/\.(fbx|stl|obj)$/) ) mimetype =
opts = { ...opts, src, frag, mimetype }
2023-09-21 13:05:30 +02:00
return xrf.frag.src.type[ mimetype ] ? xrf.frag.src.type[ mimetype ](url,opts) : xrf.frag.src.type.unknown(url,opts)
2023-07-05 16:43:07 +02:00
})
2023-09-21 13:05:30 +02:00
.then( (model) => {
if( model && model.scene ) addModel(model, url, frag )
2023-07-05 16:43:07 +02:00
})
2023-09-21 13:05:30 +02:00
.finally( () => { })
2023-07-05 16:43:07 +02:00
.catch( console.error )
2023-07-04 15:23:10 +02:00
}
if( url[0] == "#" ){
2023-11-16 14:50:57 +01:00
let _model = {
animations: model.animations,
scene: scene.clone()
}
_model.scenes = [_model.scene]
addModel(_model,url,vfrag) // current file
}else externalSRC(url,vfrag) // external file
2023-07-05 16:43:07 +02:00
}
// scale embedded XR fragments https://xrfragment.org/#scaling%20of%20instanced%20objects
xrf.frag.src.scale = function(scene, opts, url){
let { mesh, model, camera, renderer, THREE} = opts
2023-09-21 13:05:30 +02:00
// remove invisible objects (hidden by selectors) which might corrupt boundingbox size-detection
let cleanScene = scene.clone()
if( !cleanScene ) debugger
let remove = []
const notVisible = (n) => !n.visible || (n.material && !n.material.visible)
cleanScene.traverse( (n) => notVisible(n) && n.children.length == 0 && (remove.push(n)) )
remove.map( (n) => n.removeFromParent() )
2023-09-21 13:05:30 +02:00
let restrictTo3DBoundingBox = mesh.geometry
if( restrictTo3DBoundingBox ){
2023-08-04 09:11:26 +02:00
// spec 3 of https://xrfragment.org/#src
// spec 1 of https://xrfragment.org/#scaling%20of%20instanced%20objects
// normalize instanced objectsize to boundingbox
2023-09-21 13:05:30 +02:00
let sizeFrom = new THREE.Vector3()
let sizeTo = new THREE.Vector3()
let empty = new THREE.Object3D()
new THREE.Box3().setFromObject(mesh).getSize(sizeTo)
new THREE.Box3().setFromObject(cleanScene).getSize(sizeFrom)
2023-09-21 13:05:30 +02:00
let ratio = sizeFrom.divide(sizeTo)
scene.scale.multiplyScalar( 1.0 / Math.max(ratio.x, ratio.y, ratio.z));
2023-08-04 09:11:26 +02:00
}else{
// spec 4 of https://xrfragment.org/#src
2023-08-08 12:40:38 +02:00
// spec 2 of https://xrfragment.org/#scaling%20of%20instanced%20objects
2023-08-04 09:11:26 +02:00
scene.scale.multiply( mesh.scale )
2023-07-05 16:43:07 +02:00
}
2023-08-04 09:11:26 +02:00
scene.isXRF = model.scene.isSRC = true
2023-09-21 13:05:30 +02:00
}
xrf.frag.src.filterScene = (scene,opts) => {
let { mesh, model, camera, renderer, THREE, hashbus, frag} = opts
2023-11-16 14:50:57 +01:00
xrf.filter.scene({scene,frag,reparent:true})
2023-11-16 14:50:57 +01:00
scene.traverse( (m) => {
2023-09-21 13:05:30 +02:00
if( m.userData && (m.userData.src || m.userData.href) ) return ; // prevent infinite recursion
hashbus.pub.mesh(m,{scene,recursive:true}) // cool idea: recursion-depth based distance between face & src
})
2023-11-16 14:50:57 +01:00
return scene
2023-07-05 16:43:07 +02:00
}
2023-07-06 16:54:12 +02:00
/*
* replace the src-mesh with the contents of the src
*/
2023-07-05 16:43:07 +02:00
xrf.frag.src.type = {}
/*
* mimetype: unknown
*/
xrf.frag.src.type['unknown'] = function( url, opts ){
return new Promise( (resolve,reject) => {
reject(`${url} mimetype '${opts.mimetype}' not found or supported (yet)`)
2023-07-05 16:43:07 +02:00
})
}