createPlaneGeometry.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. define(['./defaultValue-fe22d8c0', './Transforms-bc45e707', './Matrix3-41c58dde', './Check-6ede7e26', './ComponentDatatype-cf1fa08e', './GeometryAttribute-a466e9c7', './GeometryAttributes-ad136444', './VertexFormat-030f11ff', './Math-0a2ac845', './Matrix2-e1298525', './RuntimeError-ef395448', './combine-d9581036', './WebGLConstants-0b1ce7ba'], (function (defaultValue, Transforms, Matrix3, Check, ComponentDatatype, GeometryAttribute, GeometryAttributes, VertexFormat, Math$1, Matrix2, RuntimeError, combine, WebGLConstants) { 'use strict';
  2. /**
  3. * Describes geometry representing a plane centered at the origin, with a unit width and length.
  4. *
  5. * @alias PlaneGeometry
  6. * @constructor
  7. *
  8. * @param {object} [options] Object with the following properties:
  9. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  10. *
  11. * @example
  12. * const planeGeometry = new Cesium.PlaneGeometry({
  13. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY
  14. * });
  15. */
  16. function PlaneGeometry(options) {
  17. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  18. const vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  19. this._vertexFormat = vertexFormat;
  20. this._workerName = "createPlaneGeometry";
  21. }
  22. /**
  23. * The number of elements used to pack the object into an array.
  24. * @type {number}
  25. */
  26. PlaneGeometry.packedLength = VertexFormat.VertexFormat.packedLength;
  27. /**
  28. * Stores the provided instance into the provided array.
  29. *
  30. * @param {PlaneGeometry} value The value to pack.
  31. * @param {number[]} array The array to pack into.
  32. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  33. *
  34. * @returns {number[]} The array that was packed into
  35. */
  36. PlaneGeometry.pack = function (value, array, startingIndex) {
  37. //>>includeStart('debug', pragmas.debug);
  38. Check.Check.typeOf.object("value", value);
  39. Check.Check.defined("array", array);
  40. //>>includeEnd('debug');
  41. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  42. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  43. return array;
  44. };
  45. const scratchVertexFormat = new VertexFormat.VertexFormat();
  46. const scratchOptions = {
  47. vertexFormat: scratchVertexFormat,
  48. };
  49. /**
  50. * Retrieves an instance from a packed array.
  51. *
  52. * @param {number[]} array The packed array.
  53. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  54. * @param {PlaneGeometry} [result] The object into which to store the result.
  55. * @returns {PlaneGeometry} The modified result parameter or a new PlaneGeometry instance if one was not provided.
  56. */
  57. PlaneGeometry.unpack = function (array, startingIndex, result) {
  58. //>>includeStart('debug', pragmas.debug);
  59. Check.Check.defined("array", array);
  60. //>>includeEnd('debug');
  61. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  62. const vertexFormat = VertexFormat.VertexFormat.unpack(
  63. array,
  64. startingIndex,
  65. scratchVertexFormat
  66. );
  67. if (!defaultValue.defined(result)) {
  68. return new PlaneGeometry(scratchOptions);
  69. }
  70. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  71. return result;
  72. };
  73. const min = new Matrix3.Cartesian3(-0.5, -0.5, 0.0);
  74. const max = new Matrix3.Cartesian3(0.5, 0.5, 0.0);
  75. /**
  76. * Computes the geometric representation of a plane, including its vertices, indices, and a bounding sphere.
  77. *
  78. * @param {PlaneGeometry} planeGeometry A description of the plane.
  79. * @returns {Geometry|undefined} The computed vertices and indices.
  80. */
  81. PlaneGeometry.createGeometry = function (planeGeometry) {
  82. const vertexFormat = planeGeometry._vertexFormat;
  83. const attributes = new GeometryAttributes.GeometryAttributes();
  84. let indices;
  85. let positions;
  86. if (vertexFormat.position) {
  87. // 4 corner points. Duplicated 3 times each for each incident edge/face.
  88. positions = new Float64Array(4 * 3);
  89. // +z face
  90. positions[0] = min.x;
  91. positions[1] = min.y;
  92. positions[2] = 0.0;
  93. positions[3] = max.x;
  94. positions[4] = min.y;
  95. positions[5] = 0.0;
  96. positions[6] = max.x;
  97. positions[7] = max.y;
  98. positions[8] = 0.0;
  99. positions[9] = min.x;
  100. positions[10] = max.y;
  101. positions[11] = 0.0;
  102. attributes.position = new GeometryAttribute.GeometryAttribute({
  103. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  104. componentsPerAttribute: 3,
  105. values: positions,
  106. });
  107. if (vertexFormat.normal) {
  108. const normals = new Float32Array(4 * 3);
  109. // +z face
  110. normals[0] = 0.0;
  111. normals[1] = 0.0;
  112. normals[2] = 1.0;
  113. normals[3] = 0.0;
  114. normals[4] = 0.0;
  115. normals[5] = 1.0;
  116. normals[6] = 0.0;
  117. normals[7] = 0.0;
  118. normals[8] = 1.0;
  119. normals[9] = 0.0;
  120. normals[10] = 0.0;
  121. normals[11] = 1.0;
  122. attributes.normal = new GeometryAttribute.GeometryAttribute({
  123. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  124. componentsPerAttribute: 3,
  125. values: normals,
  126. });
  127. }
  128. if (vertexFormat.st) {
  129. const texCoords = new Float32Array(4 * 2);
  130. // +z face
  131. texCoords[0] = 0.0;
  132. texCoords[1] = 0.0;
  133. texCoords[2] = 1.0;
  134. texCoords[3] = 0.0;
  135. texCoords[4] = 1.0;
  136. texCoords[5] = 1.0;
  137. texCoords[6] = 0.0;
  138. texCoords[7] = 1.0;
  139. attributes.st = new GeometryAttribute.GeometryAttribute({
  140. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  141. componentsPerAttribute: 2,
  142. values: texCoords,
  143. });
  144. }
  145. if (vertexFormat.tangent) {
  146. const tangents = new Float32Array(4 * 3);
  147. // +z face
  148. tangents[0] = 1.0;
  149. tangents[1] = 0.0;
  150. tangents[2] = 0.0;
  151. tangents[3] = 1.0;
  152. tangents[4] = 0.0;
  153. tangents[5] = 0.0;
  154. tangents[6] = 1.0;
  155. tangents[7] = 0.0;
  156. tangents[8] = 0.0;
  157. tangents[9] = 1.0;
  158. tangents[10] = 0.0;
  159. tangents[11] = 0.0;
  160. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  161. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  162. componentsPerAttribute: 3,
  163. values: tangents,
  164. });
  165. }
  166. if (vertexFormat.bitangent) {
  167. const bitangents = new Float32Array(4 * 3);
  168. // +z face
  169. bitangents[0] = 0.0;
  170. bitangents[1] = 1.0;
  171. bitangents[2] = 0.0;
  172. bitangents[3] = 0.0;
  173. bitangents[4] = 1.0;
  174. bitangents[5] = 0.0;
  175. bitangents[6] = 0.0;
  176. bitangents[7] = 1.0;
  177. bitangents[8] = 0.0;
  178. bitangents[9] = 0.0;
  179. bitangents[10] = 1.0;
  180. bitangents[11] = 0.0;
  181. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  182. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  183. componentsPerAttribute: 3,
  184. values: bitangents,
  185. });
  186. }
  187. // 2 triangles
  188. indices = new Uint16Array(2 * 3);
  189. // +z face
  190. indices[0] = 0;
  191. indices[1] = 1;
  192. indices[2] = 2;
  193. indices[3] = 0;
  194. indices[4] = 2;
  195. indices[5] = 3;
  196. }
  197. return new GeometryAttribute.Geometry({
  198. attributes: attributes,
  199. indices: indices,
  200. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  201. boundingSphere: new Transforms.BoundingSphere(Matrix3.Cartesian3.ZERO, Math.sqrt(2.0)),
  202. });
  203. };
  204. function createPlaneGeometry(planeGeometry, offset) {
  205. if (defaultValue.defined(offset)) {
  206. planeGeometry = PlaneGeometry.unpack(planeGeometry, offset);
  207. }
  208. return PlaneGeometry.createGeometry(planeGeometry);
  209. }
  210. return createPlaneGeometry;
  211. }));