createPlaneGeometry.js 11 KB

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