Skip to content

Commit

Permalink
Add n-gltf loaded event, bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvergenz committed Feb 9, 2018
1 parent 7262c83 commit 53df5eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
29 changes: 28 additions & 1 deletion examples/aframe/native-gltf.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@
<title>Native glTF</title>
<script src="https://aframe.io/releases/0.7.0/aframe.js"></script>
<script src='../../dist/altspace.js'></script>
<script>
AFRAME.registerComponent('draw-bounds', {
dependencies: ['n-gltf'],
init: function()
{
var gltf = this.el.components['n-gltf'];
var generateBox = this.generateBox.bind(this);
this.el.addEventListener('n-gltf-loaded', function(){
gltf.getBoundingBox().then(generateBox);
});
},
generateBox: function(bounds)
{
console.log(bounds);
var size = bounds.getSize();
var box = new AFRAME.THREE.Mesh(
new AFRAME.THREE.BoxBufferGeometry(size.x, size.y, size.z),
new AFRAME.THREE.MeshBasicMaterial({transparent: true, opacity: 0.3})
);
bounds.getCenter(box.position);
this.el.object3D.add(box);
this.el.setObject3D('bounds', box);
}
});
</script>
</head>
<body>
<a-scene altspace>
<a-entity id='native' n-gltf='url: https://rawgit.com/KhronosGroup/glTF-Sample-Models/master/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf'></a-entity>
<a-entity id='native'
n-gltf='url: https://rawgit.com/KhronosGroup/glTF-Sample-Models/master/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf'
draw-bounds></a-entity>
</a-scene>
</body>
</html>
23 changes: 23 additions & 0 deletions src/components/NGLTF.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ export default class NGLTF extends NativeComponent {
altspace.updateNativeComponent(mesh, this.name, data);
}
}

/**
* Returns a promise that resolves with a [THREE.Box3](https://threejs.org/docs/index.html#api/math/Box3)
* @instance
* @method getBoundingBox
* @memberof module:altspace/components.n-gltf
* @returns {Promise}
*/
getBoundingBox()
{
return this.callComponent('GetBoundingBox').then(data => {
const V3 = AFRAME.THREE.Vector3;
return new AFRAME.THREE.Box3(
new V3().subVectors(data.center, data.extents),
new V3().addVectors(data.center, data.extents)
);
})
}
}

/**
* Emitted when the glTF model is finished loading
* @event module:altspace/components.n-gltf#n-gltf-loaded
*/
2 changes: 1 addition & 1 deletion src/components/NativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NativeComponent extends AFrameComponent

callComponent(name, ...args){
let mesh = this.mesh || this.el.object3DMap.mesh;
altspace.callNativeComponent(mesh, this.name, name, args);
return altspace.callNativeComponent(mesh, this.name, name, args);
}
}

Expand Down

0 comments on commit 53df5eb

Please sign in to comment.