VertexFormat.js 9.4 KB

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