diff --git a/src/components/UtilComponents.js b/src/components/UtilComponents.js new file mode 100644 index 00000000..87fbebfc --- /dev/null +++ b/src/components/UtilComponents.js @@ -0,0 +1,22 @@ +import {AFrameComponent} from './AFrameComponent'; + +class CollapseModel extends AFrameComponent +{ + init() + { + function getFirstMesh(obj){ + if(obj.isMesh) + return obj; + else if(obj.children.length === 0) + return null; + else + return obj.children.map(c => getFirstMesh(c)).find(o => !!o); + } + + this.el.addEventListener('model-loaded', () => { + this.el.setObject3D('mesh', getFirstMesh(this.el.object3DMap.mesh)); + }); + } +} + +export {CollapseModel} \ No newline at end of file diff --git a/src/components/index.js b/src/components/index.js index 190864b9..84391789 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -44,6 +44,7 @@ import NContainer from './NContainer'; import NPortal from './NPortal'; import NSound from './NSound'; import NLayoutBrowser from './NLayoutBrowser'; +import {CollapseModel} from './UtilComponents'; if (window.AFRAME) { @@ -73,6 +74,7 @@ if (window.AFRAME) registerComponentClass('n-capsule-collider', NCapsuleCollider); registerComponentClass('n-mesh-collider', NMeshCollider); registerComponentClass('n-layout-browser', NLayoutBrowser); + registerComponentClass('collapse-model', CollapseModel); } export { @@ -80,5 +82,5 @@ export { SyncComponent, SyncColor, SyncTransform, SyncNSound, Wire, OnePerUser, Instantiator, SyncNSkeletonParent, NObject, NPortal, NSpawner, NText, NBillboard, NSkeletonParent, NCockpitParent, NContainer, NSound, NSphereCollider, NBoxCollider, NCapsuleCollider, - NMeshCollider, NLayoutBrowser + NMeshCollider, NLayoutBrowser, CollapseModel };