PlaneGeometry.js 6.7 KB

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