/home/lindsay/xeolabs/xeogl-next/xeogl/src/geometry/aabbGeometry.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/src/geometry/aabbGeometry.js

  1. /**
  2. An **AABBGeometry** is a {{#crossLink "Geometry"}}{{/crossLink}} that shows the extents of a World-space axis-aligned bounding box (AABB).
  3.  
  4. <a href="../../examples/#geometry_primitives_AABBGeometry"><img src="http://i.giphy.com/3o6ZsSVy0NKXZ1vDSo.gif"></img></a>
  5.  
  6. ## Overview
  7.  
  8. * A World-space AABB is an axis-aligned box given as a six-element array containing the min/max extents of an axis-aligned volume, ie. ````[xmin,ymin,zmin,xmax,ymax,zmax]````.
  9. * Set a AABBGeometry's {{#crossLink "AABBGeometry/targetAABB:property"}}{{/crossLink}} property to an AABB to fix the AABBGeometry to those extents, or
  10. * set a AABBGeometry's {{#crossLink "AABBGeometry/target:property"}}{{/crossLink}} property to any target {{#crossLink "Component"}}{{/crossLink}}
  11. subtype that has an AABB, to make it dynamically fit itself to changes in the target AABB.
  12.  
  13. ## Examples
  14.  
  15. * [Rendering an AABBGeometry](../../examples/#geometry_primitives_AABBGeometry)
  16.  
  17. ## Usage
  18.  
  19. ````javascript
  20. // First Mesh with a TorusGeometry
  21. var mesh = new xeogl.Mesh({
  22. geometry: new xeogl.TorusGeometry()
  23. });
  24.  
  25. // Second Mesh with an AABBGeometry that shows a wireframe box
  26. // for the World-space axis-aligned boundary of the first Mesh
  27. var boundaryHelper = new xeogl.Mesh({
  28.  
  29. geometry: new xeogl.AABBGeometry({
  30. targetAABB: mesh.aabb
  31. }),
  32.  
  33. material: new xeogl.PhongMaterial({
  34. diffuse: [0.5, 1.0, 0.5],
  35. emissive: [0.5, 1.0, 0.5],
  36. lineWidth:2
  37. })
  38. });
  39. ````
  40.  
  41. Now whenever our mesh {{#crossLink "Mesh"}}{{/crossLink}} changes shape or position, our AABBGeometry will automatically
  42. update to stay fitted to it.
  43.  
  44. We could also directly configure the AABBGeometry with the {{#crossLink "Mesh"}}{{/crossLink}}'s {{#crossLink "Mesh/aabb:property"}}AABB{{/crossLink}}:
  45.  
  46. ````javascript
  47. var boundaryHelper2 = new xeogl.Mesh({
  48.  
  49. geometry: new xeogl.AABBGeometry({
  50. targetAABB: mesh.aabb
  51. }),
  52.  
  53. material: new xeogl.PhongMaterial({
  54. diffuse: [0.5, 1.0, 0.5],
  55. emissive: [0.5, 1.0, 0.5],
  56. lineWidth:2
  57. })
  58. });
  59. ````
  60.  
  61. @class AABBGeometry
  62. @module xeogl
  63. @submodule geometry
  64. @constructor
  65. @param [owner] {Component} Owner component. When destroyed, the owner will destroy this component as well. Creates this component within the default {{#crossLink "Scene"}}{{/crossLink}} when omitted.
  66. @param [cfg] {*} Configs
  67. @param [cfg.id] {String} Optional ID, unique among all components in the parent {{#crossLink "Scene"}}Scene{{/crossLink}},
  68. generated automatically when omitted.
  69. @param [cfg.meta] {String:Object} Optional map of user-defined metadata to attach to this AABBGeometry.
  70. @param [cfg.target] {Component} ID or instance of a {{#crossLink "Component"}}{{/crossLink}} subtype whose AABB we'll show.
  71. @param [cfg.targetAABB] {Float32Array} An axis-aligned box (AABB) in a six-element Float32Array
  72. containing the min/max extents of the axis-aligned volume, ie. ````(xmin,ymin,zmin,xmax,ymax,zmax)````.
  73. @extends Component
  74. */
  75.  
  76. import {utils} from '../utils.js';
  77. import {tasks} from '../tasks.js';
  78. import {Geometry} from './geometry.js';
  79. import {componentClasses} from "./../componentClasses.js";
  80.  
  81. const type = "xeogl.AABBGeometry";
  82.  
  83. class AABBGeometry extends Geometry {
  84.  
  85. /**
  86. JavaScript class name for this Component.
  87.  
  88. For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.
  89.  
  90. @property type
  91. @type String
  92. @final
  93. */
  94. get type() {
  95. return type;
  96. }
  97.  
  98. init(cfg) {
  99.  
  100. super.init(utils.apply(cfg, {
  101. combined: true,
  102. quantized: true, // Quantized geometry is immutable
  103. primitive: cfg.primitive || "lines",
  104. indices: [
  105. 0, 1, 1, 2, 2, 3, 3, 0, 4,
  106. 5, 5, 6, 6, 7, 7, 4, 0, 4,
  107. 1, 5, 2, 6, 3, 7
  108. ],
  109. positions: cfg.positions || [
  110. 1.0, 1.0, 1.0,
  111. 1.0, -1.0, 1.0,
  112. -1.0, -1.0, 1.0,
  113. -1.0, 1.0, 1.0,
  114. 1.0, 1.0, -1.0,
  115. 1.0, -1.0, -1.0,
  116. -1.0, -1.0, -1.0,
  117. -1.0, 1.0, -1.0
  118. ]
  119. }));
  120.  
  121. if (cfg.target) {
  122. this.target = cfg.target;
  123.  
  124. } else if (cfg.targetAABB) {
  125. this.targetAABB = cfg.targetAABB;
  126. }
  127. }
  128.  
  129.  
  130. /**
  131. A component whose AABB we'll dynamically fit this AABBGeometry to.
  132.  
  133. This property effectively replaces the {{#crossLink "AABBGeometry/targetAABB:property"}}{{/crossLink}} property.
  134.  
  135. @property target
  136. @type Component
  137. */
  138. set target(target) {
  139. let geometryDirty = false;
  140. const self = this;
  141. this._attach({
  142. name: "target",
  143. type: "xeogl.Component",
  144. component: target,
  145. sceneDefault: false,
  146. on: {
  147. boundary: function () {
  148. if (geometryDirty) {
  149. return;
  150. }
  151. geometryDirty = true;
  152. tasks.scheduleTask(function () {
  153. self._setPositionsFromAABB(self._attached.target.aabb);
  154. geometryDirty = false;
  155. });
  156. }
  157. },
  158. onAttached: function () {
  159. self._setPositionsFromAABB(self._attached.target.aabb);
  160. }
  161. });
  162. }
  163.  
  164. get target() {
  165. return this._attached.target;
  166. }
  167.  
  168. /**
  169. Sets this AABBGeometry to an axis-aligned box (AABB), given as a six-element Float32Array
  170. containing the min/max extents of the
  171. axis-aligned volume, ie. ````[xmin,ymin,zmin,xmax,ymax,zmax]````.
  172.  
  173. This property overrides the {{#crossLink "AABBGeometry/target:property"}}{{/crossLink}} property, causing it to become null.
  174.  
  175. @property targetAABB
  176. @type Float32Array
  177. */
  178. set targetAABB(aabb) {
  179. if (!aabb) {
  180. return;
  181. }
  182. if (this._attached.target) {
  183. this.target = null;
  184. }
  185. this._setPositionsFromAABB(aabb);
  186. }
  187.  
  188. _setPositionsFromAABB(aabb) {
  189. this.positions = [
  190. aabb[3], aabb[4], aabb[5],
  191. aabb[3], aabb[1], aabb[5],
  192. aabb[0], aabb[1], aabb[5],
  193. aabb[0], aabb[4], aabb[5],
  194. aabb[3], aabb[4], aabb[2],
  195. aabb[3], aabb[1], aabb[2],
  196. aabb[0], aabb[1], aabb[2],
  197. aabb[0], aabb[4], aabb[2]
  198. ];
  199. }
  200. }
  201.  
  202. componentClasses[type] = AABBGeometry;
  203.  
  204. export{AABBGeometry};
  205.