Skip to content

Commit

Permalink
Merge branch 'feature/native-gltf-component'
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvergenz committed Feb 15, 2018
2 parents 23dbbb1 + 9ad8b7f commit 654429d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
32 changes: 31 additions & 1 deletion examples/aframe/native-gltf.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@
<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='helmet' position='-1 0 0'
n-gltf='url: https://rawgit.com/KhronosGroup/glTF-Sample-Models/master/2.0/DamagedHelmet/glTF-Binary/DamagedHelmet.glb'
draw-bounds></a-entity>
<a-entity id='lantern' position='1 -1.5 0' scale='.1 .1 .1'
n-gltf='url: https://rawgit.com/KhronosGroup/glTF-Sample-Models/master/2.0/Lantern/glTF-pbrSpecularGlossiness/Lantern.gltf'
></a-entity>
</a-scene>
</body>
</html>
37 changes: 36 additions & 1 deletion src/components/NGLTF.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,48 @@ export default class NGLTF extends NativeComponent {
sceneIndex: { type: 'int' }
};
}
update(){
init(){
NativeComponent.prototype.init.call(this);
this.boundsCache = null;
}
update(oldData)
{
let mesh = this.mesh || this.el.object3DMap.mesh;
var data = Object.assign({}, this.data);
data.url = new Url(data.url).toString();

if(this.sendUpdates){
altspace.updateNativeComponent(mesh, this.name, data);
}

if(this.data.url && oldData && this.data.url !== oldData.url){
this.boundsCache = null;
}
}

/**
* 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()
{
if(!this.boundsCache){
this.boundsCache = this.callComponentFunc('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)
);
});
}
return this.boundsCache;
}
}

/**
* Emitted when the glTF model is finished loading
* @event module:altspace/components.n-gltf#n-gltf-loaded
*/
6 changes: 3 additions & 3 deletions src/components/NSound.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class NSound extends NativeComponent
* @fires module:altspace/components.n-sound#sound-paused
*/
pauseSound() {
this.callComponent('pause');
this.callComponentAction('pause');

this.el.emit('sound-paused');
}
Expand All @@ -187,7 +187,7 @@ class NSound extends NativeComponent
* @fires module:altspace/components.n-sound#sound-played
*/
playSound() {
this.callComponent('play');
this.callComponentAction('play');

this.el.emit('sound-played');
}
Expand All @@ -197,7 +197,7 @@ class NSound extends NativeComponent
* @param {number} time - The time in milliseconds to jump to.
*/
seek(time) {
this.callComponent('seek', {time: time});
this.callComponentAction('seek', {time: time});
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/components/NativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ class NativeComponent extends AFrameComponent
altspace.removeNativeComponent(mesh, this.name);
}

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

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

Expand Down

0 comments on commit 654429d

Please sign in to comment.