PlaneGeometry.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * var planeGeometry = new Cesium.PlaneGeometry({
  23. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY
  24. * });
  25. */
  26. function PlaneGeometry(options) {
  27. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  28. var 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. var scratchVertexFormat = new VertexFormat();
  56. var 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. var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  73. if (!defined(result)) {
  74. return new PlaneGeometry(scratchOptions);
  75. }
  76. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  77. return result;
  78. };
  79. var min = new Cartesian3(-0.5, -0.5, 0.0);
  80. var max = new Cartesian3( 0.5, 0.5, 0.0);
  81. /**
  82. * Computes the geometric representation of a plane, including its vertices, indices, and a bounding sphere.
  83. *
  84. * @param {PlaneGeometry} planeGeometry A description of the plane.
  85. * @returns {Geometry|undefined} The computed vertices and indices.
  86. */
  87. PlaneGeometry.createGeometry = function(planeGeometry) {
  88. var vertexFormat = planeGeometry._vertexFormat;
  89. var attributes = new GeometryAttributes();
  90. var indices;
  91. var positions;
  92. if (vertexFormat.position) {
  93. // 4 corner points. Duplicated 3 times each for each incident edge/face.
  94. positions = new Float64Array(4 * 3);
  95. // +z face
  96. positions[0] = min.x;
  97. positions[1] = min.y;
  98. positions[2] = 0.0;
  99. positions[3] = max.x;
  100. positions[4] = min.y;
  101. positions[5] = 0.0;
  102. positions[6] = max.x;
  103. positions[7] = max.y;
  104. positions[8] = 0.0;
  105. positions[9] = min.x;
  106. positions[10] = max.y;
  107. positions[11] = 0.0;
  108. attributes.position = new GeometryAttribute({
  109. componentDatatype : ComponentDatatype.DOUBLE,
  110. componentsPerAttribute : 3,
  111. values : positions
  112. });
  113. if (vertexFormat.normal) {
  114. var normals = new Float32Array(4 * 3);
  115. // +z face
  116. normals[0] = 0.0;
  117. normals[1] = 0.0;
  118. normals[2] = 1.0;
  119. normals[3] = 0.0;
  120. normals[4] = 0.0;
  121. normals[5] = 1.0;
  122. normals[6] = 0.0;
  123. normals[7] = 0.0;
  124. normals[8] = 1.0;
  125. normals[9] = 0.0;
  126. normals[10] = 0.0;
  127. normals[11] = 1.0;
  128. attributes.normal = new GeometryAttribute({
  129. componentDatatype : ComponentDatatype.FLOAT,
  130. componentsPerAttribute : 3,
  131. values : normals
  132. });
  133. }
  134. if (vertexFormat.st) {
  135. var texCoords = new Float32Array(4 * 2);
  136. // +z face
  137. texCoords[0] = 0.0;
  138. texCoords[1] = 0.0;
  139. texCoords[2] = 1.0;
  140. texCoords[3] = 0.0;
  141. texCoords[4] = 1.0;
  142. texCoords[5] = 1.0;
  143. texCoords[6] = 0.0;
  144. texCoords[7] = 1.0;
  145. attributes.st = new GeometryAttribute({
  146. componentDatatype : ComponentDatatype.FLOAT,
  147. componentsPerAttribute : 2,
  148. values : texCoords
  149. });
  150. }
  151. if (vertexFormat.tangent) {
  152. var tangents = new Float32Array(4 * 3);
  153. // +z face
  154. tangents[0] = 1.0;
  155. tangents[1] = 0.0;
  156. tangents[2] = 0.0;
  157. tangents[3] = 1.0;
  158. tangents[4] = 0.0;
  159. tangents[5] = 0.0;
  160. tangents[6] = 1.0;
  161. tangents[7] = 0.0;
  162. tangents[8] = 0.0;
  163. tangents[9] = 1.0;
  164. tangents[10] = 0.0;
  165. tangents[11] = 0.0;
  166. attributes.tangent = new GeometryAttribute({
  167. componentDatatype : ComponentDatatype.FLOAT,
  168. componentsPerAttribute : 3,
  169. values : tangents
  170. });
  171. }
  172. if (vertexFormat.bitangent) {
  173. var bitangents = new Float32Array(4 * 3);
  174. // +z face
  175. bitangents[0] = 0.0;
  176. bitangents[1] = 1.0;
  177. bitangents[2] = 0.0;
  178. bitangents[3] = 0.0;
  179. bitangents[4] = 1.0;
  180. bitangents[5] = 0.0;
  181. bitangents[6] = 0.0;
  182. bitangents[7] = 1.0;
  183. bitangents[8] = 0.0;
  184. bitangents[9] = 0.0;
  185. bitangents[10] = 1.0;
  186. bitangents[11] = 0.0;
  187. attributes.bitangent = new GeometryAttribute({
  188. componentDatatype : ComponentDatatype.FLOAT,
  189. componentsPerAttribute : 3,
  190. values : bitangents
  191. });
  192. }
  193. // 2 triangles
  194. indices = new Uint16Array(2 * 3);
  195. // +z face
  196. indices[0] = 0;
  197. indices[1] = 1;
  198. indices[2] = 2;
  199. indices[3] = 0;
  200. indices[4] = 2;
  201. indices[5] = 3;
  202. }
  203. return new Geometry({
  204. attributes : attributes,
  205. indices : indices,
  206. primitiveType : PrimitiveType.TRIANGLES,
  207. boundingSphere : new BoundingSphere(Cartesian3.ZERO, Math.sqrt(2.0))
  208. });
  209. };
  210. export default PlaneGeometry;