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

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

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