createPlaneGeometry.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./defaultValue-a6eb9f34', './Transforms-c78c4637', './Matrix2-ab676047', './RuntimeError-1088cc64', './ComponentDatatype-e06f4e16', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './VertexFormat-65fd4be5', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './WebGLConstants-d81b330d'], (function (defaultValue, Transforms, Matrix2, RuntimeError, ComponentDatatype, GeometryAttribute, GeometryAttributes, VertexFormat, _commonjsHelpers, combine, WebGLConstants) { 'use strict';
  26. /**
  27. * Describes geometry representing a plane centered at the origin, with a unit width and length.
  28. *
  29. * @alias PlaneGeometry
  30. * @constructor
  31. *
  32. * @param {Object} [options] Object with the following properties:
  33. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  34. *
  35. * @example
  36. * const planeGeometry = new Cesium.PlaneGeometry({
  37. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY
  38. * });
  39. */
  40. function PlaneGeometry(options) {
  41. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  42. const vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  43. this._vertexFormat = vertexFormat;
  44. this._workerName = "createPlaneGeometry";
  45. }
  46. /**
  47. * The number of elements used to pack the object into an array.
  48. * @type {Number}
  49. */
  50. PlaneGeometry.packedLength = VertexFormat.VertexFormat.packedLength;
  51. /**
  52. * Stores the provided instance into the provided array.
  53. *
  54. * @param {PlaneGeometry} value The value to pack.
  55. * @param {Number[]} array The array to pack into.
  56. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  57. *
  58. * @returns {Number[]} The array that was packed into
  59. */
  60. PlaneGeometry.pack = function (value, array, startingIndex) {
  61. //>>includeStart('debug', pragmas.debug);
  62. RuntimeError.Check.typeOf.object("value", value);
  63. RuntimeError.Check.defined("array", array);
  64. //>>includeEnd('debug');
  65. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  66. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  67. return array;
  68. };
  69. const scratchVertexFormat = new VertexFormat.VertexFormat();
  70. const scratchOptions = {
  71. vertexFormat: scratchVertexFormat,
  72. };
  73. /**
  74. * Retrieves an instance from a packed array.
  75. *
  76. * @param {Number[]} array The packed array.
  77. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  78. * @param {PlaneGeometry} [result] The object into which to store the result.
  79. * @returns {PlaneGeometry} The modified result parameter or a new PlaneGeometry instance if one was not provided.
  80. */
  81. PlaneGeometry.unpack = function (array, startingIndex, result) {
  82. //>>includeStart('debug', pragmas.debug);
  83. RuntimeError.Check.defined("array", array);
  84. //>>includeEnd('debug');
  85. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  86. const vertexFormat = VertexFormat.VertexFormat.unpack(
  87. array,
  88. startingIndex,
  89. scratchVertexFormat
  90. );
  91. if (!defaultValue.defined(result)) {
  92. return new PlaneGeometry(scratchOptions);
  93. }
  94. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  95. return result;
  96. };
  97. const min = new Matrix2.Cartesian3(-0.5, -0.5, 0.0);
  98. const max = new Matrix2.Cartesian3(0.5, 0.5, 0.0);
  99. /**
  100. * Computes the geometric representation of a plane, including its vertices, indices, and a bounding sphere.
  101. *
  102. * @param {PlaneGeometry} planeGeometry A description of the plane.
  103. * @returns {Geometry|undefined} The computed vertices and indices.
  104. */
  105. PlaneGeometry.createGeometry = function (planeGeometry) {
  106. const vertexFormat = planeGeometry._vertexFormat;
  107. const attributes = new GeometryAttributes.GeometryAttributes();
  108. let indices;
  109. let positions;
  110. if (vertexFormat.position) {
  111. // 4 corner points. Duplicated 3 times each for each incident edge/face.
  112. positions = new Float64Array(4 * 3);
  113. // +z face
  114. positions[0] = min.x;
  115. positions[1] = min.y;
  116. positions[2] = 0.0;
  117. positions[3] = max.x;
  118. positions[4] = min.y;
  119. positions[5] = 0.0;
  120. positions[6] = max.x;
  121. positions[7] = max.y;
  122. positions[8] = 0.0;
  123. positions[9] = min.x;
  124. positions[10] = max.y;
  125. positions[11] = 0.0;
  126. attributes.position = new GeometryAttribute.GeometryAttribute({
  127. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  128. componentsPerAttribute: 3,
  129. values: positions,
  130. });
  131. if (vertexFormat.normal) {
  132. const normals = new Float32Array(4 * 3);
  133. // +z face
  134. normals[0] = 0.0;
  135. normals[1] = 0.0;
  136. normals[2] = 1.0;
  137. normals[3] = 0.0;
  138. normals[4] = 0.0;
  139. normals[5] = 1.0;
  140. normals[6] = 0.0;
  141. normals[7] = 0.0;
  142. normals[8] = 1.0;
  143. normals[9] = 0.0;
  144. normals[10] = 0.0;
  145. normals[11] = 1.0;
  146. attributes.normal = new GeometryAttribute.GeometryAttribute({
  147. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  148. componentsPerAttribute: 3,
  149. values: normals,
  150. });
  151. }
  152. if (vertexFormat.st) {
  153. const texCoords = new Float32Array(4 * 2);
  154. // +z face
  155. texCoords[0] = 0.0;
  156. texCoords[1] = 0.0;
  157. texCoords[2] = 1.0;
  158. texCoords[3] = 0.0;
  159. texCoords[4] = 1.0;
  160. texCoords[5] = 1.0;
  161. texCoords[6] = 0.0;
  162. texCoords[7] = 1.0;
  163. attributes.st = new GeometryAttribute.GeometryAttribute({
  164. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  165. componentsPerAttribute: 2,
  166. values: texCoords,
  167. });
  168. }
  169. if (vertexFormat.tangent) {
  170. const tangents = new Float32Array(4 * 3);
  171. // +z face
  172. tangents[0] = 1.0;
  173. tangents[1] = 0.0;
  174. tangents[2] = 0.0;
  175. tangents[3] = 1.0;
  176. tangents[4] = 0.0;
  177. tangents[5] = 0.0;
  178. tangents[6] = 1.0;
  179. tangents[7] = 0.0;
  180. tangents[8] = 0.0;
  181. tangents[9] = 1.0;
  182. tangents[10] = 0.0;
  183. tangents[11] = 0.0;
  184. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  185. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  186. componentsPerAttribute: 3,
  187. values: tangents,
  188. });
  189. }
  190. if (vertexFormat.bitangent) {
  191. const bitangents = new Float32Array(4 * 3);
  192. // +z face
  193. bitangents[0] = 0.0;
  194. bitangents[1] = 1.0;
  195. bitangents[2] = 0.0;
  196. bitangents[3] = 0.0;
  197. bitangents[4] = 1.0;
  198. bitangents[5] = 0.0;
  199. bitangents[6] = 0.0;
  200. bitangents[7] = 1.0;
  201. bitangents[8] = 0.0;
  202. bitangents[9] = 0.0;
  203. bitangents[10] = 1.0;
  204. bitangents[11] = 0.0;
  205. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  206. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  207. componentsPerAttribute: 3,
  208. values: bitangents,
  209. });
  210. }
  211. // 2 triangles
  212. indices = new Uint16Array(2 * 3);
  213. // +z face
  214. indices[0] = 0;
  215. indices[1] = 1;
  216. indices[2] = 2;
  217. indices[3] = 0;
  218. indices[4] = 2;
  219. indices[5] = 3;
  220. }
  221. return new GeometryAttribute.Geometry({
  222. attributes: attributes,
  223. indices: indices,
  224. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  225. boundingSphere: new Transforms.BoundingSphere(Matrix2.Cartesian3.ZERO, Math.sqrt(2.0)),
  226. });
  227. };
  228. function createPlaneGeometry(planeGeometry, offset) {
  229. if (defaultValue.defined(offset)) {
  230. planeGeometry = PlaneGeometry.unpack(planeGeometry, offset);
  231. }
  232. return PlaneGeometry.createGeometry(planeGeometry);
  233. }
  234. return createPlaneGeometry;
  235. }));