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

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

  1. /**
  2. A **TorusGeometry** is a parameterized {{#crossLink "Geometry"}}{{/crossLink}} that defines a torus-shaped mesh for attached {{#crossLink "Mesh"}}Meshes{{/crossLink}}.
  3.  
  4. <a href="../../examples/#geometry_primitives_torus"><img src="../../assets/images/screenshots/TorusGeometry.png"></img></a>
  5.  
  6. ## Overview
  7.  
  8. * Dynamically modify a TorusGeometry's shape at any time by updating its {{#crossLink "TorusGeometry/center:property"}}{{/crossLink}}, {{#crossLink "TorusGeometry/radius:property"}}{{/crossLink}}, {{#crossLink "TorusGeometry/tube:property"}}{{/crossLink}},
  9. {{#crossLink "TorusGeometry/radialSegments:property"}}{{/crossLink}}, {{#crossLink "TorusGeometry/tubeSegments:property"}}{{/crossLink}}, and
  10. {{#crossLink "TorusGeometry/arc:property"}}{{/crossLink}} properties.
  11. * Dynamically switch its primitive type between ````"points"````, ````"lines"```` and ````"triangles"```` at any time by
  12. updating its {{#crossLink "Geometry/primitive:property"}}{{/crossLink}} property.
  13.  
  14. ## Examples
  15.  
  16.  
  17. * [Textured TorusGeometry](../../examples/#geometry_primitives_torus)
  18.  
  19.  
  20. ## Usage
  21.  
  22. An {{#crossLink "Mesh"}}{{/crossLink}} with a TorusGeometry and a {{#crossLink "PhongMaterial"}}{{/crossLink}} with
  23. diffuse {{#crossLink "Texture"}}{{/crossLink}}:
  24.  
  25. ````javascript
  26. new xeogl.Mesh({
  27.  
  28. geometry: new xeogl.TorusGeometry({
  29. center: [0,0,0],
  30. radius: 1.0,
  31. tube: 0.5,
  32. radialSegments: 32,
  33. tubeSegments: 24,
  34. arc: Math.PI * 2.0
  35. }),
  36.  
  37. material: new xeogl.PhongMaterial({
  38. diffuseMap: new xeogl.Texture({
  39. src: "textures/diffuse/uvGrid2.jpg"
  40. })
  41. })
  42. });
  43. ````
  44.  
  45. @class TorusGeometry
  46. @module xeogl
  47. @submodule geometry
  48. @constructor
  49. @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.
  50. @param [cfg] {*} Configs
  51. @param [cfg.id] {String} Optional ID, unique among all components in the parent {{#crossLink "Scene"}}Scene{{/crossLink}},
  52. generated automatically when omitted.
  53. @param [cfg.meta] {String:Object} Optional map of user-defined metadata to attach to this TorusGeometry.
  54. @param [cfg.primitive="triangles"] {String} The primitive type. Accepted values for a TorusGeometry are 'points', 'lines' and 'triangles'.
  55. @param [cfg.center] {Float32Array} 3D point indicating the center position of the TorusGeometry.
  56. @param [cfg.radius=1] {Number} The overall radius of the TorusGeometry.
  57. @param [cfg.tube=0.3] {Number} The tube radius of the TorusGeometry.
  58. @param [cfg.radialSegments=32] {Number} The number of radial segments that make up the TorusGeometry.
  59. @param [cfg.tubeSegments=24] {Number} The number of tubular segments that make up the TorusGeometry.
  60. @param [cfg.arc=Math.PI / 2.0] {Number} The length of the TorusGeometry's arc in radians, where Math.PI*2 is a closed torus.
  61. @extends Geometry
  62. */
  63. import {core} from "./../core.js";
  64. import {utils} from "./../utils.js";
  65. import {Geometry} from './geometry.js';
  66. import {math} from '../math/math.js';
  67. import {componentClasses} from "./../componentClasses.js";
  68.  
  69. const type = "xeogl.TorusGeometry";
  70.  
  71. class TorusGeometry extends Geometry {
  72.  
  73. /**
  74. JavaScript class name for this Component.
  75.  
  76. For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.
  77.  
  78. @property type
  79. @type String
  80. @final
  81. */
  82. get type() {
  83. return type;
  84. }
  85.  
  86. init(cfg) {
  87.  
  88. let radius = cfg.radius || 1;
  89. if (radius < 0) {
  90. this.error("negative radius not allowed - will invert");
  91. radius *= -1;
  92. }
  93. radius *= 0.5;
  94.  
  95. let tube = cfg.tube || 0.3;
  96. if (tube < 0) {
  97. this.error("negative tube not allowed - will invert");
  98. tube *= -1;
  99. }
  100.  
  101. let radialSegments = cfg.radialSegments || 32;
  102. if (radialSegments < 0) {
  103. this.error("negative radialSegments not allowed - will invert");
  104. radialSegments *= -1;
  105. }
  106. if (radialSegments < 4) {
  107. radialSegments = 4;
  108. }
  109.  
  110. let tubeSegments = cfg.tubeSegments || 24;
  111. if (tubeSegments < 0) {
  112. this.error("negative tubeSegments not allowed - will invert");
  113. tubeSegments *= -1;
  114. }
  115. if (tubeSegments < 4) {
  116. tubeSegments = 4;
  117. }
  118.  
  119. let arc = cfg.arc || Math.PI * 2;
  120. if (arc < 0) {
  121. this.warn("negative arc not allowed - will invert");
  122. arc *= -1;
  123. }
  124. if (arc > 360) {
  125. arc = 360;
  126. }
  127.  
  128. const center = cfg.center;
  129. let centerX = center ? center[0] : 0;
  130. let centerY = center ? center[1] : 0;
  131. const centerZ = center ? center[2] : 0;
  132.  
  133. const positions = [];
  134. const normals = [];
  135. const uvs = [];
  136. const indices = [];
  137.  
  138. let u;
  139. let v;
  140. let x;
  141. let y;
  142. let z;
  143. let vec;
  144.  
  145. let i;
  146. let j;
  147.  
  148. for (j = 0; j <= tubeSegments; j++) {
  149. for (i = 0; i <= radialSegments; i++) {
  150.  
  151. u = i / radialSegments * arc;
  152. v = 0.785398 + (j / tubeSegments * Math.PI * 2);
  153.  
  154. centerX = radius * Math.cos(u);
  155. centerY = radius * Math.sin(u);
  156.  
  157. x = (radius + tube * Math.cos(v) ) * Math.cos(u);
  158. y = (radius + tube * Math.cos(v) ) * Math.sin(u);
  159. z = tube * Math.sin(v);
  160.  
  161. positions.push(x + centerX);
  162. positions.push(y + centerY);
  163. positions.push(z + centerZ);
  164.  
  165. uvs.push(1 - (i / radialSegments));
  166. uvs.push((j / tubeSegments));
  167.  
  168. vec = math.normalizeVec3(math.subVec3([x, y, z], [centerX, centerY, centerZ], []), []);
  169.  
  170. normals.push(vec[0]);
  171. normals.push(vec[1]);
  172. normals.push(vec[2]);
  173. }
  174. }
  175.  
  176. let a;
  177. let b;
  178. let c;
  179. let d;
  180.  
  181. for (j = 1; j <= tubeSegments; j++) {
  182. for (i = 1; i <= radialSegments; i++) {
  183.  
  184. a = ( radialSegments + 1 ) * j + i - 1;
  185. b = ( radialSegments + 1 ) * ( j - 1 ) + i - 1;
  186. c = ( radialSegments + 1 ) * ( j - 1 ) + i;
  187. d = ( radialSegments + 1 ) * j + i;
  188.  
  189. indices.push(a);
  190. indices.push(b);
  191. indices.push(c);
  192.  
  193. indices.push(c);
  194. indices.push(d);
  195. indices.push(a);
  196. }
  197. }
  198.  
  199. super.init(utils.apply(cfg, {
  200. positions: positions,
  201. normals: normals,
  202. uv: uvs,
  203. indices: indices
  204. }));
  205. }
  206. }
  207.  
  208. componentClasses[type] = TorusGeometry;
  209.  
  210. export {TorusGeometry};
  211.