sphere-collider.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. 'use strict';
  3. require('./src/misc/sphere-collider');
  4. },{"./src/misc/sphere-collider":2}],2:[function(require,module,exports){
  5. 'use strict';
  6. /**
  7. * Based on aframe/examples/showcase/tracked-controls.
  8. *
  9. * Implement bounding sphere collision detection for entities with a mesh.
  10. * Sets the specified state on the intersected entities.
  11. *
  12. * @property {string} objects - Selector of the entities to test for collision.
  13. * @property {string} state - State to set on collided entities.
  14. *
  15. */
  16. module.exports = AFRAME.registerComponent('sphere-collider', {
  17. schema: {
  18. objects: { default: '' },
  19. state: { default: 'collided' },
  20. radius: { default: 0.05 },
  21. watch: { default: true }
  22. },
  23. init: function init() {
  24. /** @type {MutationObserver} */
  25. this.observer = null;
  26. /** @type {Array<Element>} Elements to watch for collisions. */
  27. this.els = [];
  28. /** @type {Array<Element>} Elements currently in collision state. */
  29. this.collisions = [];
  30. this.handleHit = this.handleHit.bind(this);
  31. this.handleHitEnd = this.handleHitEnd.bind(this);
  32. },
  33. remove: function remove() {
  34. this.pause();
  35. },
  36. play: function play() {
  37. var sceneEl = this.el.sceneEl;
  38. if (this.data.watch) {
  39. this.observer = new MutationObserver(this.update.bind(this, null));
  40. this.observer.observe(sceneEl, { childList: true, subtree: true });
  41. }
  42. },
  43. pause: function pause() {
  44. if (this.observer) {
  45. this.observer.disconnect();
  46. this.observer = null;
  47. }
  48. },
  49. /**
  50. * Update list of entities to test for collision.
  51. */
  52. update: function update() {
  53. var data = this.data;
  54. var objectEls = void 0;
  55. // Push entities into list of els to intersect.
  56. if (data.objects) {
  57. objectEls = this.el.sceneEl.querySelectorAll(data.objects);
  58. } else {
  59. // If objects not defined, intersect with everything.
  60. objectEls = this.el.sceneEl.children;
  61. }
  62. // Convert from NodeList to Array
  63. this.els = Array.prototype.slice.call(objectEls);
  64. },
  65. tick: function () {
  66. var position = new THREE.Vector3(),
  67. meshPosition = new THREE.Vector3(),
  68. colliderScale = new THREE.Vector3(),
  69. distanceMap = new Map();
  70. return function () {
  71. var el = this.el,
  72. data = this.data,
  73. mesh = el.getObject3D('mesh'),
  74. collisions = [];
  75. var colliderRadius = void 0;
  76. if (!mesh) {
  77. return;
  78. }
  79. distanceMap.clear();
  80. position.copy(el.object3D.getWorldPosition());
  81. el.object3D.getWorldScale(colliderScale);
  82. colliderRadius = data.radius * scaleFactor(colliderScale);
  83. // Update collision list.
  84. this.els.forEach(intersect);
  85. // Emit events and add collision states, in order of distance.
  86. collisions.sort(function (a, b) {
  87. return distanceMap.get(a) > distanceMap.get(b) ? 1 : -1;
  88. }).forEach(this.handleHit);
  89. // Remove collision state from current element.
  90. if (collisions.length === 0) {
  91. el.emit('hit', { el: null });
  92. }
  93. // Remove collision state from other elements.
  94. this.collisions.filter(function (el) {
  95. return !distanceMap.has(el);
  96. }).forEach(this.handleHitEnd);
  97. // Store new collisions
  98. this.collisions = collisions;
  99. // Bounding sphere collision detection
  100. function intersect(el) {
  101. var radius = void 0,
  102. mesh = void 0,
  103. distance = void 0,
  104. box = void 0,
  105. extent = void 0,
  106. size = void 0;
  107. if (!el.isEntity) {
  108. return;
  109. }
  110. mesh = el.getObject3D('mesh');
  111. if (!mesh) {
  112. return;
  113. }
  114. box = new THREE.Box3().setFromObject(mesh);
  115. size = box.getSize();
  116. extent = Math.max(size.x, size.y, size.z) / 2;
  117. radius = Math.sqrt(2 * extent * extent);
  118. box.getCenter(meshPosition);
  119. if (!radius) {
  120. return;
  121. }
  122. distance = position.distanceTo(meshPosition);
  123. if (distance < radius + colliderRadius) {
  124. collisions.push(el);
  125. distanceMap.set(el, distance);
  126. }
  127. }
  128. // use max of scale factors to maintain bounding sphere collision
  129. function scaleFactor(scaleVec) {
  130. return Math.max.apply(null, scaleVec.toArray());
  131. }
  132. };
  133. }(),
  134. handleHit: function handleHit(targetEl) {
  135. targetEl.emit('hit');
  136. targetEl.addState(this.data.state);
  137. this.el.emit('hit', { el: targetEl });
  138. },
  139. handleHitEnd: function handleHitEnd(targetEl) {
  140. targetEl.emit('hitend');
  141. targetEl.removeState(this.data.state);
  142. this.el.emit('hitend', { el: targetEl });
  143. }
  144. });
  145. },{}]},{},[1]);