VertexFormat.js 8.1 KB

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