sphere-collider.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. size = new THREE.Vector3(),
  70. box = new THREE.Box3(),
  71. distanceMap = new Map();
  72. return function () {
  73. var el = this.el,
  74. data = this.data,
  75. mesh = el.getObject3D('mesh'),
  76. collisions = [];
  77. var colliderRadius = void 0;
  78. if (!mesh) {
  79. return;
  80. }
  81. distanceMap.clear();
  82. el.object3D.getWorldPosition(position);
  83. el.object3D.getWorldScale(colliderScale);
  84. colliderRadius = data.radius * scaleFactor(colliderScale);
  85. // Update collision list.
  86. this.els.forEach(intersect);
  87. // Emit events and add collision states, in order of distance.
  88. collisions.sort(function (a, b) {
  89. return distanceMap.get(a) > distanceMap.get(b) ? 1 : -1;
  90. }).forEach(this.handleHit);
  91. // Remove collision state from current element.
  92. if (collisions.length === 0) {
  93. el.emit('hit', { el: null });
  94. }
  95. // Remove collision state from other elements.
  96. this.collisions.filter(function (el) {
  97. return !distanceMap.has(el);
  98. }).forEach(this.handleHitEnd);
  99. // Store new collisions
  100. this.collisions = collisions;
  101. // Bounding sphere collision detection
  102. function intersect(el) {
  103. var radius = void 0,
  104. mesh = void 0,
  105. distance = void 0,
  106. extent = void 0;
  107. if (!el.isEntity) {
  108. return;
  109. }
  110. mesh = el.getObject3D('mesh');
  111. if (!mesh) {
  112. return;
  113. }
  114. box.setFromObject(mesh).getSize(size);
  115. extent = Math.max(size.x, size.y, size.z) / 2;
  116. radius = Math.sqrt(2 * extent * extent);
  117. box.getCenter(meshPosition);
  118. if (!radius) {
  119. return;
  120. }
  121. distance = position.distanceTo(meshPosition);
  122. if (distance < radius + colliderRadius) {
  123. collisions.push(el);
  124. distanceMap.set(el, distance);
  125. }
  126. }
  127. // use max of scale factors to maintain bounding sphere collision
  128. function scaleFactor(scaleVec) {
  129. return Math.max.apply(null, scaleVec.toArray());
  130. }
  131. };
  132. }(),
  133. handleHit: function handleHit(targetEl) {
  134. targetEl.emit('hit');
  135. targetEl.addState(this.data.state);
  136. this.el.emit('hit', { el: targetEl });
  137. },
  138. handleHitEnd: function handleHitEnd(targetEl) {
  139. targetEl.emit('hitend');
  140. targetEl.removeState(this.data.state);
  141. this.el.emit('hitend', { el: targetEl });
  142. }
  143. });
  144. },{}]},{},[1]);