/home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/helpers/planeHelper.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/helpers/planeHelper.js

  1. /**
  2.  
  3. Helper that visualizes the position and direction of a plane.
  4.  
  5. @class PlaneHelper
  6. @constructor
  7. @param cfg {*} Configuration
  8. @param [cfg.pos=[0,0,0]] {Float32Array} World-space position.
  9. @param [cfg.dir=[0,0,1]] {Float32Array} World-space direction vector.
  10. @param [cfg.color=[0.4,0.4,0.4]] {Float32Array} Emmissive color
  11. @param [cfg.solid=true] {Boolean} Indicates whether or not this helper is filled with color or just wireframe.
  12. @param [cfg.visible=true] {Boolean} Indicates whether or not this helper is visible.
  13. @param [cfg.planeSize] {Float32Array} The width and height of the PlaneHelper plane indicator.
  14. @param [cfg.autoPlaneSize=false] {Boolean} Indicates whether or not this PlaneHelper's
  15. {{#crossLink "PlaneHelper/planeSize:property"}}{{/crossLink}} is automatically sized to fit within
  16. the {{#crossLink "Scene/aabb:property"}}Scene's boundary{{/crossLink}}.
  17. */
  18.  
  19. {
  20. const arrowPositions = new Float32Array(6);
  21. const zeroVec = new Float32Array([0, 0, -1]);
  22. const quat = new Float32Array(4);
  23.  
  24. xeogl.PlaneHelper = class xeoglPlaneHelper extends xeogl.Component {
  25.  
  26. init(cfg) {
  27.  
  28. super.init(cfg);
  29.  
  30. this._solid = false;
  31. this._visible = false;
  32.  
  33. this._group = new xeogl.Group(this, {
  34. positions: [10, 10, 0],
  35. backfaces: false,
  36. clippable: false
  37. });
  38.  
  39. this._planeWire = this._group.addChild(new xeogl.Mesh(this, {
  40. geometry: new xeogl.Geometry(this, {
  41. primitive: "lines",
  42. positions: [
  43. 0.5, 0.5, 0.0, 0.5, -0.5, 0.0, // 0
  44. -0.5, -0.5, 0.0, -0.5, 0.5, 0.0, // 1
  45. 0.5, 0.5, -0.0, 0.5, -0.5, -0.0, // 2
  46. -0.5, -0.5, -0.0, -0.5, 0.5, -0.0 // 3
  47. ],
  48. indices: [0, 1, 0, 3, 1, 2, 2, 3]
  49. }),
  50. material: new xeogl.PhongMaterial(this, {
  51. emissive: [1, 0, 0],
  52. diffuse: [0, 0, 0],
  53. lineWidth: 2
  54. }),
  55. pickable: false,
  56. collidable: false,
  57. clippable: false
  58. }));
  59.  
  60. this._planeSolid = this._group.addChild(new xeogl.Mesh(this, {
  61. geometry: new xeogl.Geometry(this, {
  62. primitive: "triangles",
  63. positions: [
  64. 0.5, 0.5, 0.0, 0.5, -0.5, 0.0, // 0
  65. -0.5, -0.5, 0.0, -0.5, 0.5, 0.0, // 1
  66. 0.5, 0.5, -0.0, 0.5, -0.5, -0.0, // 2
  67. -0.5, -0.5, -0.0, -0.5, 0.5, -0.0 // 3
  68. ],
  69. indices: [0, 1, 2, 2, 3, 0]
  70. }),
  71. material: new xeogl.PhongMaterial(this, {
  72. emissive: [0, 0, 0],
  73. diffuse: [0, 0, 0],
  74. specular: [1, 1, 1],
  75. shininess: 120,
  76. alpha: 0.3,
  77. alphaMode: "blend",
  78. backfaces: true
  79. }),
  80. pickable: false,
  81. collidable: false,
  82. clippable: false,
  83. visible: false
  84. }));
  85.  
  86. this._label = this._group.addChild(new xeogl.Mesh(this, {
  87. geometry: new xeogl.VectorTextGeometry(this, {
  88. text: this.id,
  89. size: 0.07,
  90. origin: [0.02, 0.02, 0.0]
  91. }),
  92. material: new xeogl.PhongMaterial(this, {
  93. emissive: [0.3, 1, 0.3],
  94. lineWidth: 2
  95. }),
  96. pickable: false,
  97. collidable: false,
  98. clippable: false,
  99. billboard: "spherical"
  100. }));
  101.  
  102. this._arrow = new xeogl.Mesh(this, {
  103. geometry: new xeogl.Geometry(this, {
  104. primitive: "lines",
  105. positions: [
  106. 1.0, 1.0, 1.0, 1.0, -1.0, 1.0
  107. ],
  108. indices: [0, 1]
  109. }),
  110. material: new xeogl.PhongMaterial(this, {
  111. emissive: [1, 0, 0],
  112. diffuse: [0, 0, 0],
  113. lineWidth: 4
  114. }),
  115. pickable: false,
  116. collidable: false,
  117. clippable: false
  118. });
  119.  
  120. this.planeSize = cfg.planeSize;
  121. this.autoPlaneSize = cfg.autoPlaneSize;
  122. this.pos = cfg.pos;
  123. this.dir = cfg.dir;
  124. this.color = cfg.color;
  125. this.solid = cfg.solid;
  126. this.visible = cfg.visible;
  127. }
  128.  
  129. _update() {
  130.  
  131. const pos = this._pos;
  132. const dir = this._dir;
  133.  
  134. // Rebuild arrow geometry
  135.  
  136. arrowPositions[0] = pos[0];
  137. arrowPositions[1] = pos[1];
  138. arrowPositions[2] = pos[2];
  139. arrowPositions[3] = pos[0] + dir[0];
  140. arrowPositions[4] = pos[1] + dir[1];
  141. arrowPositions[5] = pos[2] + dir[2];
  142.  
  143. this._arrow.geometry.positions = arrowPositions;
  144. }
  145.  
  146. /**
  147. * World-space position of this PlaneHelper.
  148. *
  149. * @property worldPos
  150. * @default [0,0,0]
  151. * @type {Float32Array}
  152. */
  153. set pos(value) {
  154. (this._pos = this._pos || xeogl.math.vec3()).set(value || [0, 0, 0]);
  155. this._group.position = this._pos;
  156. this._needUpdate(); // Need to rebuild arrow
  157. }
  158.  
  159. get pos() {
  160. return this._pos;
  161. }
  162.  
  163. /**
  164. * World-space direction of this PlaneHelper.
  165. *
  166. * @property dir
  167. * @default [0,0,1]
  168. * @type {Float32Array}
  169. */
  170. set dir(value) {
  171. (this._dir = this._dir || xeogl.math.vec3()).set(value || [0, 0, 1]);
  172. xeogl.math.vec3PairToQuaternion(zeroVec, this._dir, quat);
  173. this._group.quaternion = quat;
  174. this._needUpdate(); // Need to rebuild arrow
  175. }
  176.  
  177. get dir() {
  178. return this._dir;
  179. }
  180.  
  181. /**
  182. * The width and height of the PlaneHelper plane indicator.
  183. *
  184. * Values assigned to this property will be overridden by an auto-computed value when
  185. * {{#crossLink "PlaneHelper/autoPlaneSize:property"}}{{/crossLink}} is true.
  186. *
  187. * @property planeSize
  188. * @default [1,1]
  189. * @type {Float32Array}
  190. */
  191. set planeSize(value) {
  192. (this._planeSize = this._planeSize || xeogl.math.vec2()).set(value || [1, 1]);
  193. this._group.scale = [this._planeSize[0], this._planeSize[1], 1.0];
  194. }
  195.  
  196. get planeSize() {
  197. return this._planeSize;
  198. }
  199.  
  200. /**
  201. * Indicates whether this PlaneHelper's {{#crossLink "PlaneHelper/planeSize:property"}}{{/crossLink}} is automatically
  202. * generated or not.
  203. *
  204. * When auto-generated, {{#crossLink "PlaneHelper/planeSize:property"}}{{/crossLink}} will automatically size
  205. * to fit within the {{#crossLink "Scene/aabb:property"}}Scene's boundary{{/crossLink}}.
  206. *
  207. * @property autoPlaneSize
  208. * @default false
  209. * @type {Boolean}
  210. */
  211. set autoPlaneSize(value) {
  212.  
  213. value = !!value;
  214.  
  215. if (this._autoPlaneSize === value) {
  216. return;
  217. }
  218.  
  219. this._autoPlaneSize = value;
  220.  
  221. if (this._autoPlaneSize) {
  222. if (!this._onSceneAABB) {
  223. this._onSceneAABB = this.scene.on("boundary", function () {
  224. const aabbDiag = xeogl.math.getAABB3Diag(this.scene.aabb);
  225. const clipSize = (aabbDiag * 0.50);
  226. this.planeSize = [clipSize, clipSize];
  227. }, this);
  228. }
  229. } else {
  230. if (this._onSceneAABB) {
  231. this.scene.off(this._onSceneAABB);
  232. this._onSceneAABB = null;
  233. }
  234. }
  235. }
  236.  
  237. get color() {
  238. return this._autoPlaneSize;
  239. }
  240.  
  241. /**
  242. * Emmissive color of this PlaneHelper.
  243. *
  244. * @property color
  245. * @default [0.4,0.4,0.4]
  246. * @type {Float32Array}
  247. */
  248. set color(value) {
  249. (this._color = this._color || xeogl.math.vec3()).set(value || [0.4, 0.4, 0.4]);
  250. this._planeWire.material.emissive = this._color;
  251. this._arrow.material.emissive = this._color;
  252. }
  253.  
  254. get color() {
  255. return this._color;
  256. }
  257.  
  258. /**
  259. Indicates whether this PlaneHelper is filled with color or just wireframe.
  260.  
  261. @property solid
  262. @default true
  263. @type Boolean
  264. */
  265. set solid(value) {
  266. this._solid = value !== false;
  267. this._planeSolid.visible = this._solid && this._visible;
  268. }
  269.  
  270. get solid() {
  271. return this._solid;
  272. }
  273.  
  274. /**
  275. Indicates whether this PlaneHelper is visible or not.
  276.  
  277. @property visible
  278. @default true
  279. @type Boolean
  280. */
  281. set visible(value) {
  282. this._visible = value !== false;
  283. this._planeWire.visible = this._visible;
  284. this._planeSolid.visible = this._solid && this._visible;
  285. this._arrow.visible = this._visible;
  286. this._label.visible = this._visible;
  287. }
  288.  
  289. get visible() {
  290. return this._visible;
  291. }
  292.  
  293. destroy() {
  294. super.destroy();
  295. if (this._onSceneAABB) {
  296. this.scene.off(this._onSceneAABB);
  297. }
  298. }
  299. };
  300. }