UNPKG

aframe-babia-components

Version:

A data visualization set of components for A-Frame.

1 lines 10.5 kB
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var i=e();for(var o in i)("object"==typeof exports?exports:t)[o]=i[o]}}(this,(function(){return function(t){var e={};function i(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,o){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(o,n,function(e){return t[e]}.bind(null,n));return o},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerComponent("lounge-collider",{schema:{interval:{type:"number",default:80},objects:{type:"selectorAll",default:""}},init:function(){this.boundingBox=new THREE.Box3,this.boxCenter=new THREE.Vector3,this.objectEls=[],this.intersectedEls=[],this.previousIntersectedEls=[],this.newIntersectedEls=[],this.clearedIntersectedEls=[],this.prevCheckTime=void 0,this.observer=new MutationObserver(this.setDirty),this.setDirty=this.setDirty.bind(this),this.boxMax=new THREE.Vector3,this.boxMin=new THREE.Vector3},play:function(){this.observer.observe(this.el.sceneEl,{childList:!0,attributes:!0,subtree:!0}),this.el.sceneEl.addEventListener("object3dset",this.setDirty),this.el.sceneEl.addEventListener("object3dremove",this.setDirty)},remove:function(){this.observer.disconnect(),this.el.sceneEl.removeEventListener("object3dset",this.setDirty),this.el.sceneEl.removeEventListener("object3dremove",this.setDirty)},tick:function(t){const e=this.boundingBox,o=this.el,n=this.objectEls,l=this.intersectedEls,r=this.previousIntersectedEls,s=this.newIntersectedEls,a=this.clearedIntersectedEls,h=this.prevCheckTime;if(!(h&&t-h<this.data.interval)){this.prevCheckTime=t,this.dirty&&this.refreshObjects(),e.setFromObject(o.object3D),this.boxMin.copy(e.min),this.boxMax.copy(e.max),e.getCenter(this.boxCenter),r.length=0;for(let t=0;t<l.length;t++)r[t]=l[t];for(l.length=0,i=0;i<n.length;i++)n[i]!==this.el&&this.isIntersecting(n[i])&&l.push(n[i]);for(s.length=0,i=0;i<l.length;i++)-1===r.indexOf(l[i])&&s.push(l[i]);for(a.length=0,i=0;i<r.length;i++)-1===l.indexOf(r[i])&&(r[i].emit("hitend"),a.push(r[i]));for(i=0;i<s.length;i++)s[i]!==this.el&&s[i].emit("hitstart")}},isIntersecting:function(t){let e;if(!t.object3D)return!1;t.object3D.aabbBox||(t.object3D.aabbBox=(new THREE.Box3).setFromObject(t.object3D),t.object3D.boundingBoxCenter=new THREE.Vector3,t.object3D.aabbBox.getCenter(t.object3D.boundingBoxCenter)),e=t.object3D.aabbBox;const i=e.min,o=e.max;return this.boxMin.x<=o.x&&this.boxMax.x>=i.x&&this.boxMin.y<=o.y&&this.boxMax.y>=i.y&&this.boxMin.z<=o.z&&this.boxMax.z>=i.z},setDirty:function(){this.dirty=!0},refreshObjects:function(){const t=this.data;t.objects?this.objectEls=this.el.sceneEl.querySelectorAll(t.objects):this.objectEls=this.el.sceneEl.children,this.dirty=!1}}),AFRAME.registerComponent("lounge-plinth",{schema:{width:{type:"number",default:1},depth:{type:"number",default:1},height:{type:"number",default:.5},color:{type:"color",default:"#404040"}},multiple:!1,init:function(){let t=this.el,e=this.data;console.log("lounge-plinth (init)"),this.el.setAttribute("geometry",{primitive:"box",width:this.data.width,depth:this.data.depth,height:this.data.height}),this.el.setAttribute("material",{color:this.data.color}),this.el.addEventListener("staydown",(function(i){let o=t.object3D.worldToLocal(i.detail.worldPosition);t.object3D.position.y=o.y+e.height/2}))},update:function(t){},remove:function(){}}),AFRAME.registerComponent("lounge-staydown",{schema:{},multiple:!1,floor_level:function(t){localPosition=new THREE.Vector3(t.x,t.y,t.z),this.el.object3D.updateMatrixWorld(),this.el.emit("staydown",{worldPosition:this.el.object3D.localToWorld(localPosition)},!1)},init:function(){console.log("lounge-staydown component (init)");let t=this.floor_level.bind(this),e=this.el;for(;(e=e.parentNode)&&!("lounge"in e.attributes););let i=e;i.addEventListener("loaded",(function(){let e=i.querySelector("a-entity[lounge-floor]"),o=e.components["lounge-floor"];"data"in o?t(o.data.position):e.addEventListener("componentinitialized",(function(e){"lounge-floor"==e.detail.name&&t(o.data.position)}))}))},update:function(t){},remove:function(){}}),AFRAME.registerComponent("lounge-entry-point",{schema:{loungeId:{type:"string",default:"lounge"}},multiple:!1,init:function(){let t=this.el;console.log("lounge-entry-point component (init)");let e=document.getElementById(this.data.loungeId);null==e&&(e=document.querySelector("a-entity[lounge]")),e.addEventListener("componentinitialized",(function(i){if("lounge"==i.detail.name){let i=e.components.lounge.entry_point(),o=t.object3D.worldToLocal(i);t.object3D.position.copy(o)}}))},update:function(t){},remove:function(){}}),AFRAME.registerComponent("lounge-floor",{schema:{width:{type:"number",default:10},depth:{type:"number",default:7},color:{type:"color",default:""},texture:{type:"asset",default:""},position:{type:"vec3",default:{x:0,y:0,z:0}}},multiple:!1,init:function(){console.log("lounge-floor component (init)"),this.floor=document.createElement("a-plane"),this.floor.setAttribute("class","lounge-floor"),""==this.data.color&&""==this.data.texture&&(this.data.color="#808080"),this.floor.setAttribute("color",this.data.color),this.floor.setAttribute("src",this.data.texture),this.floor.setAttribute("width",this.data.width),this.floor.setAttribute("height",this.data.depth),this.floor.setAttribute("position",this.data.position),this.floor.setAttribute("rotation","270 0 0"),this.floor.setAttribute("side","double"),this.el.appendChild(this.floor)},update:function(t){},remove:function(){}}),AFRAME.registerComponent("lounge-ceiling",{schema:{width:{type:"number",default:10},depth:{type:"number",default:7},color:{type:"color",default:"#808080"},position:{type:"vec3",default:{x:0,y:0,z:0}}},multiple:!1,init:function(){console.log("lounge-ceiling component (init)"),this.floor=document.createElement("a-plane"),this.floor.setAttribute("class","lounge-ceiling"),this.floor.setAttribute("color",this.data.color),this.floor.setAttribute("width",this.data.width),this.floor.setAttribute("height",this.data.depth),this.floor.setAttribute("position",this.data.position),this.floor.setAttribute("rotation","90 0 0"),this.floor.setAttribute("side","double"),this.el.appendChild(this.floor)},update:function(t){},remove:function(){}}),AFRAME.registerComponent("lounge-wall",{schema:{width:{type:"number",default:10},height:{type:"number",default:4},depth:{type:"number",default:.3},color:{type:"color",default:"#aaa4a4"},position:{type:"vec3",default:{x:0,y:0,z:0}},opacity:{type:"number",default:1},wireframe:{type:"boolean",default:!1}},multiple:!0,init:function(){data=this.data,console.log("lounge-wall component (init)"),this.wall=document.createElement("a-box"),this.wall.setAttribute("class","lounge-wall"),"north"==this.id?this.wall.setAttribute("rotation","0 0 0"):"east"==this.id?this.wall.setAttribute("rotation","0 90 0"):"south"==this.id?this.wall.setAttribute("rotation","0 180 0"):"west"==this.id&&this.wall.setAttribute("rotation","0 270 0"),this.el.appendChild(this.wall)},update:function(t){data=this.data,console.log("lounge-wall component (update)"),this.wall.setAttribute("color",data.color),this.wall.setAttribute("width",data.width),this.wall.setAttribute("depth",data.depth),this.wall.setAttribute("height",data.height),this.wall.setAttribute("position",data.position),data.opacity<1&&this.wall.setAttribute("material",{transparent:!0,opacity:data.opacity}),this.wall.setAttribute("wireframe",data.wireframe)},remove:function(){}}),AFRAME.registerComponent("lounge",{schema:{width:{type:"number",default:10},height:{type:"number",default:4},depth:{type:"number",default:7},floorColor:{type:"color",default:""},floorTexture:{type:"asset",default:""},north:{type:"string",default:"wall"},east:{type:"string",default:"wall"},south:{type:"string",default:"wall"},west:{type:"string",default:"wall"},wallColor:{type:"color",default:"#aaa4a4"},glassOpacity:{type:"number",default:.4},barrierHeight:{type:"number",default:1.4},ceiling:{type:"boolean",default:!0},entryPoint:{type:"vec3",default:{}}},multiple:!1,init:function(){let t=this.data;console.log("lounge component (init)"),this.lounge=document.createElement("a-entity"),this.lounge.setAttribute("lounge-floor",{color:t.floorColor,texture:t.floorTexture,width:t.width,depth:t.depth,position:{x:0,y:-t.height/2,z:0}});let e={};const i={north:{x:0,z:-t.depth/2,width:t.width},east:{x:t.width/2,z:0,width:t.depth},south:{x:0,z:t.depth/2,width:t.width},west:{x:-t.width/2,z:0,width:t.depth}};for(direction in i)wall={},["wall","barrier","glass"].includes(t[direction])&&(wall.x=i[direction].x,wall.z=i[direction].z,wall.width=i[direction].width,["wall","glass"].includes(t[direction])?(wall.height=t.height,wall.y=0):"barrier"==t[direction]&&(wall.height=t.barrierHeight,wall.y=(wall.height-t.height)/2),["glass","barrier"].includes(t[direction])?wall.opacity=t.glassOpacity:wall.opacity=1,e[direction]=wall);for(const t in e){const i=e[t];this.lounge.setAttribute("lounge-wall__"+t,{color:this.data.wallColor,width:i.width,height:i.height,position:{x:i.x,y:i.y,z:i.z},opacity:i.opacity})}this.data.ceiling&&this.lounge.setAttribute("lounge-ceiling",{color:this.data.ceilingColor,width:this.data.width,depth:this.data.depth,position:{x:0,y:this.data.height/2,z:0}}),this.el.appendChild(this.lounge),console.log(this.lounge)},update:function(t){},remove:function(){},pause:function(){},play:function(){},events:{},entry_point(){var t;return t=0==Object.keys(this.data.entryPoint).length?new THREE.Vector3(0,-this.data.height/2,this.data.depth/4):new THREE.Vector3(this.data.entryPoint.x,this.data.entryPoint.y,this.data.entryPoint.z),this.el.object3D.updateMatrixWorld(),this.el.object3D.localToWorld(t)}})}])}));