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

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

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