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

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

  1. /**
  2. An **OBBGeometry** is a {{#crossLink "Geometry"}}{{/crossLink}} that shows the extents of an oriented bounding box (OBB).
  3.  
  4. <a href="../../examples/#geometry_primitives_OBBGeometry"><img src="http://i.giphy.com/3o6ZsSVy0NKXZ1vDSo.gif"></img></a>
  5.  
  6. ## Overview
  7.  
  8. * A World-space OBB a bounding box that's oriented to its contents, given as a 32-element array containing the homogeneous coordinates for the eight corner vertices, ie. each having elements [x,y,z,w].
  9. * Set an OBBGeometry's {{#crossLink "OBBGeometry/targetOBB:property"}}{{/crossLink}} property to an OBB to fix it to those extents, or
  10. * Set an OBBGeometry's {{#crossLink "OBBGeometry/target:property"}}{{/crossLink}} property to any {{#crossLink "Component"}}{{/crossLink}} subtype that has an OBB.
  11.  
  12. ## Examples
  13.  
  14. * [Rendering an OBBGeometry](../../examples/#geometry_primitives_OBBGeometry)
  15.  
  16. ## Usage
  17.  
  18. ````javascript
  19. // First Mesh with a TorusGeometry
  20. var mesh = new xeogl.Mesh({
  21. geometry: new xeogl.TorusGeometry()
  22. });
  23.  
  24. // Second Mesh with an OBBGeometry that shows a wireframe box
  25. // for the World-space boundary of the first Mesh
  26.  
  27. var boundaryHelper = new xeogl.Mesh({
  28.  
  29. geometry: new xeogl.OBBGeometry({
  30. target: mesh
  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 OBBGeometry will automatically
  42. update to stay fitted to it.
  43.  
  44. We could also directly configure the OBBGeometry with the {{#crossLink "Mesh"}}{{/crossLink}}'s {{#crossLink "Mesh/obb:property"}}OBB{{/crossLink}}:
  45.  
  46. ````javascript
  47. var boundaryHelper2 = new xeogl.Mesh({
  48.  
  49. geometry: new xeogl.OBBGeometry({
  50. targetOBB: mesh.obb
  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 OBBGeometry
  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 OBBGeometry.
  70. @param [cfg.target] {Component} ID or instance of a {{#crossLink "Component"}}{{/crossLink}} whose OBB we'll show.
  71. @param [cfg.targetOBB] {Float32Array} An mesh-oriented box (OBB) in a 32-element Float32Array
  72. containing homogeneous coordinates for the eight corner vertices, ie. each having elements (x,y,z,w).
  73. @extends Component
  74. */
  75. import {utils} from '../utils.js';
  76. import {tasks} from '../tasks.js';
  77. import {Geometry} from './geometry.js';
  78. import {componentClasses} from "./../componentClasses.js";
  79.  
  80. const type = "xeogl.OBBGeometry";
  81.  
  82. class OBBGeometry extends Geometry {
  83.  
  84. /**
  85. JavaScript class name for this Component.
  86.  
  87. For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.
  88.  
  89. @property type
  90. @type String
  91. @final
  92. */
  93. get type() {
  94. return type;
  95. }
  96.  
  97. init(cfg) {
  98. super.init(utils.apply(cfg, {
  99. combined: true,
  100. quantized: false, // Quantized geometry is immutable
  101. primitive: cfg.primitive || "lines",
  102. positions: cfg.positions || [1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0,
  103. 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0],
  104. indices: [0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7]
  105. }));
  106. if (cfg.target) {
  107. this.target = cfg.target;
  108. } else if (cfg.targetOBB) {
  109. this.targetOBB = cfg.targetOBB;
  110. }
  111. }
  112.  
  113. /**
  114. A component whose OBB we'll dynamically fit this AABBGeometry to.
  115.  
  116. This property effectively replaces the {{#crossLink "OBBGeometry/targetOBB:property"}}{{/crossLink}} property.
  117.  
  118. @property target
  119. @type Component
  120. */
  121. set target(value) {
  122. let geometryDirty = false;
  123. const self = this;
  124. this._attach({
  125. name: "target",
  126. type: "xeogl.Component",
  127. component: value,
  128. sceneDefault: false,
  129. on: {
  130. boundary: function () {
  131. if (geometryDirty) {
  132. return;
  133. }
  134. geometryDirty = true;
  135. tasks.scheduleTask(function () {
  136. self._setPositionsFromOBB(self._attached.target.obb);
  137. geometryDirty = false;
  138. });
  139. }
  140. },
  141. onAttached: function () {
  142. self._setPositionsFromOBB(self._attached.target.obb);
  143. }
  144. });
  145. }
  146.  
  147. get target() {
  148. return this._attached.target;
  149. }
  150.  
  151. /**
  152. Sets this OBBGeometry to an mesh-oriented bounding box (OBB), given as a 32-element Float32Array
  153. containing homogeneous coordinates for the eight corner vertices, ie. each having elements [x,y,z,w].
  154.  
  155. This property effectively replaces the {{#crossLink "OBBGeometry/boundary:property"}}{{/crossLink}} property, causing it to become null.
  156.  
  157. @property targetOBB
  158. @type Float32Array
  159. */
  160. set targetOBB(value) {
  161. if (!value) {
  162. return;
  163. }
  164. if (this._attached.target) {
  165. this.target = null;
  166. }
  167. this._setPositionsFromOBB(value);
  168. }
  169.  
  170. _setPositionsFromOBB(obb) {
  171. this.positions = [
  172. obb[0], obb[1], obb[2],
  173. obb[4], obb[5], obb[6],
  174. obb[8], obb[9], obb[10],
  175. obb[12], obb[13], obb[14],
  176. obb[16], obb[17], obb[18],
  177. obb[20], obb[21], obb[22],
  178. obb[24], obb[25], obb[26],
  179. obb[28], obb[29], obb[30]
  180. ];
  181. }
  182. }
  183.  
  184. componentClasses[type] = OBBGeometry;
  185.  
  186. export{OBBGeometry};
  187.