Files
junisaber/src/components/cursor-laser.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-09-25 05:40:08 -07:00
/**
* Laser beam.
* Automatically set length on intersection.
*/
AFRAME.registerComponent('cursor-laser', {
2018-10-16 16:20:57 -07:00
dependencies: ['geometry'],
2018-09-25 05:40:08 -07:00
init: function () {
const el = this.el;
const box = new THREE.Box3();
const size = new THREE.Vector3();
this.currentLength = undefined;
this.originalSize = undefined;
// Calculate size to position beam at tip of controller.
2018-10-16 16:20:57 -07:00
box.setFromObject(el.getObject3D('mesh'));
box.getSize(size);
el.object3D.position.z = -0.3;
this.originalSize = size.y;
this.currentLength = size.y;
2018-09-25 05:40:08 -07:00
},
tick: function () {
const el = this.el;
const cursor = el.parentNode.components.cursor;
2018-09-25 05:40:08 -07:00
if (!cursor) { return; }
// Toggle beam.
const intersectedEl = cursor.intersectedEl;
if (!intersectedEl) {
// Retract the beam if not intersecting.
2018-10-16 16:20:57 -07:00
el.object3D.position.z = -25;
el.object3D.scale.x = 0.75;
el.getObject3D('mesh').scale.y = 50;
this.currentLength = 1;
2018-09-25 05:40:08 -07:00
return;
}
// Set appropriate length of beam on intersection.
const intersection = el.parentNode.components.raycaster.intersections[0];
2018-09-25 05:40:08 -07:00
el.object3D.scale.x = 1;
2018-10-16 16:20:57 -07:00
el.object3D.position.z = (-intersection.distance / 2);
el.getObject3D('mesh').scale.y = this.currentLength = intersection.distance;
2018-09-25 05:40:08 -07:00
}
});