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

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

  1. /**
  2.  
  3. A **PathGeometry** is a {{#crossLink "Geometry"}}{{/crossLink}} that is defined by a {{#crossLink "Curve"}}{{/crossLink}}.
  4.  
  5. ## Usage
  6.  
  7. An {{#crossLink "Mesh"}}{{/crossLink}} with a PathGeometry, a {{#crossLink "Path"}}{{/crossLink}} and
  8. a {{#crossLink "PhongMaterial"}}{{/crossLink}}:
  9.  
  10. ````javascript
  11. new xeogl.Mesh({
  12.  
  13. geometry: new xeogl.PathGeometry({
  14.  
  15. divisions: 10,
  16.  
  17. path: new xeogl.Path({
  18.  
  19. // Subpaths
  20.  
  21. curves: [
  22. new xeogl.CubicBezierCurve({
  23. v0: [-10, 0, 0],
  24. v1: [-5, 15, 0],
  25. v2: [20, 15, 0],
  26. v3: [10, 0, 0]
  27. }),
  28. new xeogl.QuadraticBezierCurve({
  29. v0: [10, 0, 0],
  30. v1: [30, 15, 0],
  31. v2: [20, 0, 0]
  32. }),
  33. new xeogl.SplineCurve({
  34. points: [
  35. [20, 0, 0],
  36. [-5, 15, 0],
  37. [20, 15, 0],
  38. [10, 0, 0]
  39. ]
  40. })
  41. ]
  42. })
  43. }),
  44.  
  45. material: new xeogl.PhongMaterial(
  46. diffuse: [1,0,0]
  47. })
  48. });
  49. ````
  50.  
  51. @class PathGeometry
  52. @module xeogl
  53. @submodule geometry
  54. @extends Geometry
  55. */
  56.  
  57. import {Geometry} from './geometry.js';
  58. import {componentClasses} from "./../componentClasses.js";
  59.  
  60. const type = "xeogl.PathGeometry";
  61.  
  62. const PathGeometry = Geometry.extend({
  63.  
  64. type: type,
  65.  
  66. // Constructor
  67.  
  68. _init: function (cfg) {
  69.  
  70. this._super(cfg);
  71.  
  72. this.path = cfg.path;
  73. this.divisions = cfg.divisions;
  74. },
  75.  
  76. /**
  77. * Implement protected virtual template method {{#crossLink "Geometry/method:_update"}}{{/crossLink}},
  78. * to generate geometry data arrays.
  79. *
  80. * @protected
  81. */
  82. _update: function () {
  83.  
  84. const path = this._attached.path;
  85.  
  86. if (!path) {
  87. return;
  88. }
  89.  
  90. let i;
  91. let len;
  92.  
  93. const points = path.getPoints(this._divisions);
  94.  
  95. const positions = [];
  96. let point;
  97.  
  98. for (i = 0, len = points.length; i < len; i++) {
  99.  
  100. point = points[i];
  101.  
  102. positions.push(point[0]);
  103. positions.push(point[1]);
  104. positions.push(point[2]);
  105. }
  106.  
  107. const indices = [];
  108.  
  109. for (i = 0, len = points.length - 1; i < len; i++) {
  110. indices.push(i);
  111. indices.push(i + 1);
  112. }
  113.  
  114. this.primitive = "lines";
  115. this.positions = positions;
  116. this.indices = indices;
  117. this.normals = null;
  118. this.uv = null;
  119. },
  120.  
  121. _props: {
  122.  
  123. /**
  124. * The Path for this PathGeometry.
  125. *
  126. * @property path
  127. * @type {Path}
  128. */
  129. path: {
  130.  
  131. set: function (value) {
  132.  
  133. this._attach({
  134. name: "path",
  135. type: "xeogl.Curve",
  136. component: value,
  137. sceneDefault: false,
  138. on: {
  139. curves: {
  140. callback: this._needUpdate,
  141. scope: this
  142. }
  143. }
  144. });
  145. },
  146.  
  147. get: function () {
  148. return this._attached.path;
  149. }
  150. },
  151.  
  152. /**
  153. * The number of segments in this PathGeometry.
  154. *
  155. * @property divisions
  156. * @default 6
  157. * @type {Number}
  158. */
  159. divisions: {
  160.  
  161. set: function (value) {
  162.  
  163. value = value || 6;
  164.  
  165. this._divisions = value;
  166.  
  167. this._needUpdate();
  168. },
  169.  
  170. get: function () {
  171. return this._divisions;
  172. }
  173. }
  174. }
  175. });
  176.  
  177. componentClasses[type] = PathGeometry;
  178.  
  179. export{PathGeometry};