VertexFormat-fe4db402.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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(['exports', './when-8d13db60', './Check-70bec281'], function (exports, when, Check) { 'use strict';
  24. /**
  25. * A vertex format defines what attributes make up a vertex. A VertexFormat can be provided
  26. * to a {@link Geometry} to request that certain properties be computed, e.g., just position,
  27. * position and normal, etc.
  28. *
  29. * @param {Object} [options] An object with boolean properties corresponding to VertexFormat properties as shown in the code example.
  30. *
  31. * @alias VertexFormat
  32. * @constructor
  33. *
  34. * @example
  35. * // Create a vertex format with position and 2D texture coordinate attributes.
  36. * var format = new Cesium.VertexFormat({
  37. * position : true,
  38. * st : true
  39. * });
  40. *
  41. * @see Geometry#attributes
  42. * @see Packable
  43. */
  44. function VertexFormat(options) {
  45. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  46. /**
  47. * When <code>true</code>, the vertex has a 3D position attribute.
  48. * <p>
  49. * 64-bit floating-point (for precision). 3 components per attribute.
  50. * </p>
  51. *
  52. * @type Boolean
  53. *
  54. * @default false
  55. */
  56. this.position = when.defaultValue(options.position, false);
  57. /**
  58. * When <code>true</code>, the vertex has a normal attribute (normalized), which is commonly used for lighting.
  59. * <p>
  60. * 32-bit floating-point. 3 components per attribute.
  61. * </p>
  62. *
  63. * @type Boolean
  64. *
  65. * @default false
  66. */
  67. this.normal = when.defaultValue(options.normal, false);
  68. /**
  69. * When <code>true</code>, the vertex has a 2D texture coordinate attribute.
  70. * <p>
  71. * 32-bit floating-point. 2 components per attribute
  72. * </p>
  73. *
  74. * @type Boolean
  75. *
  76. * @default false
  77. */
  78. this.st = when.defaultValue(options.st, false);
  79. /**
  80. * When <code>true</code>, the vertex has a bitangent attribute (normalized), which is used for tangent-space effects like bump mapping.
  81. * <p>
  82. * 32-bit floating-point. 3 components per attribute.
  83. * </p>
  84. *
  85. * @type Boolean
  86. *
  87. * @default false
  88. */
  89. this.bitangent = when.defaultValue(options.bitangent, false);
  90. /**
  91. * When <code>true</code>, the vertex has a tangent attribute (normalized), which is used for tangent-space effects like bump mapping.
  92. * <p>
  93. * 32-bit floating-point. 3 components per attribute.
  94. * </p>
  95. *
  96. * @type Boolean
  97. *
  98. * @default false
  99. */
  100. this.tangent = when.defaultValue(options.tangent, false);
  101. /**
  102. * When <code>true</code>, the vertex has an RGB color attribute.
  103. * <p>
  104. * 8-bit unsigned byte. 3 components per attribute.
  105. * </p>
  106. *
  107. * @type Boolean
  108. *
  109. * @default false
  110. */
  111. this.color = when.defaultValue(options.color, false);
  112. }
  113. /**
  114. * An immutable vertex format with only a position attribute.
  115. *
  116. * @type {VertexFormat}
  117. * @constant
  118. *
  119. * @see VertexFormat#position
  120. */
  121. VertexFormat.POSITION_ONLY = Object.freeze(new VertexFormat({
  122. position : true
  123. }));
  124. /**
  125. * An immutable vertex format with position and normal attributes.
  126. * This is compatible with per-instance color appearances like {@link PerInstanceColorAppearance}.
  127. *
  128. * @type {VertexFormat}
  129. * @constant
  130. *
  131. * @see VertexFormat#position
  132. * @see VertexFormat#normal
  133. */
  134. VertexFormat.POSITION_AND_NORMAL = Object.freeze(new VertexFormat({
  135. position : true,
  136. normal : true
  137. }));
  138. /**
  139. * An immutable vertex format with position, normal, and st attributes.
  140. * This is compatible with {@link MaterialAppearance} when {@link MaterialAppearance#materialSupport}
  141. * is <code>TEXTURED/code>.
  142. *
  143. * @type {VertexFormat}
  144. * @constant
  145. *
  146. * @see VertexFormat#position
  147. * @see VertexFormat#normal
  148. * @see VertexFormat#st
  149. */
  150. VertexFormat.POSITION_NORMAL_AND_ST = Object.freeze(new VertexFormat({
  151. position : true,
  152. normal : true,
  153. st : true
  154. }));
  155. /**
  156. * An immutable vertex format with position and st attributes.
  157. * This is compatible with {@link EllipsoidSurfaceAppearance}.
  158. *
  159. * @type {VertexFormat}
  160. * @constant
  161. *
  162. * @see VertexFormat#position
  163. * @see VertexFormat#st
  164. */
  165. VertexFormat.POSITION_AND_ST = Object.freeze(new VertexFormat({
  166. position : true,
  167. st : true
  168. }));
  169. /**
  170. * An immutable vertex format with position and color attributes.
  171. *
  172. * @type {VertexFormat}
  173. * @constant
  174. *
  175. * @see VertexFormat#position
  176. * @see VertexFormat#color
  177. */
  178. VertexFormat.POSITION_AND_COLOR = Object.freeze(new VertexFormat({
  179. position : true,
  180. color : true
  181. }));
  182. /**
  183. * An immutable vertex format with well-known attributes: position, normal, st, tangent, and bitangent.
  184. *
  185. * @type {VertexFormat}
  186. * @constant
  187. *
  188. * @see VertexFormat#position
  189. * @see VertexFormat#normal
  190. * @see VertexFormat#st
  191. * @see VertexFormat#tangent
  192. * @see VertexFormat#bitangent
  193. */
  194. VertexFormat.ALL = Object.freeze(new VertexFormat({
  195. position : true,
  196. normal : true,
  197. st : true,
  198. tangent : true,
  199. bitangent : true
  200. }));
  201. /**
  202. * An immutable vertex format with position, normal, and st attributes.
  203. * This is compatible with most appearances and materials; however
  204. * normal and st attributes are not always required. When this is
  205. * known in advance, another <code>VertexFormat</code> should be used.
  206. *
  207. * @type {VertexFormat}
  208. * @constant
  209. *
  210. * @see VertexFormat#position
  211. * @see VertexFormat#normal
  212. */
  213. VertexFormat.DEFAULT = VertexFormat.POSITION_NORMAL_AND_ST;
  214. /**
  215. * The number of elements used to pack the object into an array.
  216. * @type {Number}
  217. */
  218. VertexFormat.packedLength = 6;
  219. /**
  220. * Stores the provided instance into the provided array.
  221. *
  222. * @param {VertexFormat} value The value to pack.
  223. * @param {Number[]} array The array to pack into.
  224. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  225. *
  226. * @returns {Number[]} The array that was packed into
  227. */
  228. VertexFormat.pack = function(value, array, startingIndex) {
  229. //>>includeStart('debug', pragmas.debug);
  230. if (!when.defined(value)) {
  231. throw new Check.DeveloperError('value is required');
  232. }
  233. if (!when.defined(array)) {
  234. throw new Check.DeveloperError('array is required');
  235. }
  236. //>>includeEnd('debug');
  237. startingIndex = when.defaultValue(startingIndex, 0);
  238. array[startingIndex++] = value.position ? 1.0 : 0.0;
  239. array[startingIndex++] = value.normal ? 1.0 : 0.0;
  240. array[startingIndex++] = value.st ? 1.0 : 0.0;
  241. array[startingIndex++] = value.tangent ? 1.0 : 0.0;
  242. array[startingIndex++] = value.bitangent ? 1.0 : 0.0;
  243. array[startingIndex] = value.color ? 1.0 : 0.0;
  244. return array;
  245. };
  246. /**
  247. * Retrieves an instance from a packed array.
  248. *
  249. * @param {Number[]} array The packed array.
  250. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  251. * @param {VertexFormat} [result] The object into which to store the result.
  252. * @returns {VertexFormat} The modified result parameter or a new VertexFormat instance if one was not provided.
  253. */
  254. VertexFormat.unpack = function(array, startingIndex, result) {
  255. //>>includeStart('debug', pragmas.debug);
  256. if (!when.defined(array)) {
  257. throw new Check.DeveloperError('array is required');
  258. }
  259. //>>includeEnd('debug');
  260. startingIndex = when.defaultValue(startingIndex, 0);
  261. if (!when.defined(result)) {
  262. result = new VertexFormat();
  263. }
  264. result.position = array[startingIndex++] === 1.0;
  265. result.normal = array[startingIndex++] === 1.0;
  266. result.st = array[startingIndex++] === 1.0;
  267. result.tangent = array[startingIndex++] === 1.0;
  268. result.bitangent = array[startingIndex++] === 1.0;
  269. result.color = array[startingIndex] === 1.0;
  270. return result;
  271. };
  272. /**
  273. * Duplicates a VertexFormat instance.
  274. *
  275. * @param {VertexFormat} vertexFormat The vertex format to duplicate.
  276. * @param {VertexFormat} [result] The object onto which to store the result.
  277. * @returns {VertexFormat} The modified result parameter or a new VertexFormat instance if one was not provided. (Returns undefined if vertexFormat is undefined)
  278. */
  279. VertexFormat.clone = function(vertexFormat, result) {
  280. if (!when.defined(vertexFormat)) {
  281. return undefined;
  282. }
  283. if (!when.defined(result)) {
  284. result = new VertexFormat();
  285. }
  286. result.position = vertexFormat.position;
  287. result.normal = vertexFormat.normal;
  288. result.st = vertexFormat.st;
  289. result.tangent = vertexFormat.tangent;
  290. result.bitangent = vertexFormat.bitangent;
  291. result.color = vertexFormat.color;
  292. return result;
  293. };
  294. exports.VertexFormat = VertexFormat;
  295. });