GeometryAttribute.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import defaultValue from './defaultValue.js';
  2. import defined from './defined.js';
  3. import DeveloperError from './DeveloperError.js';
  4. /**
  5. * Values and type information for geometry attributes. A {@link Geometry}
  6. * generally contains one or more attributes. All attributes together form
  7. * the geometry's vertices.
  8. *
  9. * @alias GeometryAttribute
  10. * @constructor
  11. *
  12. * @param {Object} [options] Object with the following properties:
  13. * @param {ComponentDatatype} [options.componentDatatype] The datatype of each component in the attribute, e.g., individual elements in values.
  14. * @param {Number} [options.componentsPerAttribute] A number between 1 and 4 that defines the number of components in an attributes.
  15. * @param {Boolean} [options.normalize=false] When <code>true</code> and <code>componentDatatype</code> is an integer format, indicate that the components should be mapped to the range [0, 1] (unsigned) or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  16. * @param {TypedArray} [options.values] The values for the attributes stored in a typed array.
  17. *
  18. * @exception {DeveloperError} options.componentsPerAttribute must be between 1 and 4.
  19. *
  20. *
  21. * @example
  22. * var geometry = new Cesium.Geometry({
  23. * attributes : {
  24. * position : new Cesium.GeometryAttribute({
  25. * componentDatatype : Cesium.ComponentDatatype.FLOAT,
  26. * componentsPerAttribute : 3,
  27. * values : new Float32Array([
  28. * 0.0, 0.0, 0.0,
  29. * 7500000.0, 0.0, 0.0,
  30. * 0.0, 7500000.0, 0.0
  31. * ])
  32. * })
  33. * },
  34. * primitiveType : Cesium.PrimitiveType.LINE_LOOP
  35. * });
  36. *
  37. * @see Geometry
  38. */
  39. function GeometryAttribute(options) {
  40. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  41. //>>includeStart('debug', pragmas.debug);
  42. if (!defined(options.componentDatatype)) {
  43. throw new DeveloperError('options.componentDatatype is required.');
  44. }
  45. if (!defined(options.componentsPerAttribute)) {
  46. throw new DeveloperError('options.componentsPerAttribute is required.');
  47. }
  48. if (options.componentsPerAttribute < 1 || options.componentsPerAttribute > 4) {
  49. throw new DeveloperError('options.componentsPerAttribute must be between 1 and 4.');
  50. }
  51. if (!defined(options.values)) {
  52. throw new DeveloperError('options.values is required.');
  53. }
  54. //>>includeEnd('debug');
  55. /**
  56. * The datatype of each component in the attribute, e.g., individual elements in
  57. * {@link GeometryAttribute#values}.
  58. *
  59. * @type ComponentDatatype
  60. *
  61. * @default undefined
  62. */
  63. this.componentDatatype = options.componentDatatype;
  64. /**
  65. * A number between 1 and 4 that defines the number of components in an attributes.
  66. * For example, a position attribute with x, y, and z components would have 3 as
  67. * shown in the code example.
  68. *
  69. * @type Number
  70. *
  71. * @default undefined
  72. *
  73. * @example
  74. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  75. * attribute.componentsPerAttribute = 3;
  76. * attribute.values = new Float32Array([
  77. * 0.0, 0.0, 0.0,
  78. * 7500000.0, 0.0, 0.0,
  79. * 0.0, 7500000.0, 0.0
  80. * ]);
  81. */
  82. this.componentsPerAttribute = options.componentsPerAttribute;
  83. /**
  84. * When <code>true</code> and <code>componentDatatype</code> is an integer format,
  85. * indicate that the components should be mapped to the range [0, 1] (unsigned)
  86. * or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  87. * <p>
  88. * This is commonly used when storing colors using {@link ComponentDatatype.UNSIGNED_BYTE}.
  89. * </p>
  90. *
  91. * @type Boolean
  92. *
  93. * @default false
  94. *
  95. * @example
  96. * attribute.componentDatatype = Cesium.ComponentDatatype.UNSIGNED_BYTE;
  97. * attribute.componentsPerAttribute = 4;
  98. * attribute.normalize = true;
  99. * attribute.values = new Uint8Array([
  100. * Cesium.Color.floatToByte(color.red),
  101. * Cesium.Color.floatToByte(color.green),
  102. * Cesium.Color.floatToByte(color.blue),
  103. * Cesium.Color.floatToByte(color.alpha)
  104. * ]);
  105. */
  106. this.normalize = defaultValue(options.normalize, false);
  107. /**
  108. * The values for the attributes stored in a typed array. In the code example,
  109. * every three elements in <code>values</code> defines one attributes since
  110. * <code>componentsPerAttribute</code> is 3.
  111. *
  112. * @type TypedArray
  113. *
  114. * @default undefined
  115. *
  116. * @example
  117. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  118. * attribute.componentsPerAttribute = 3;
  119. * attribute.values = new Float32Array([
  120. * 0.0, 0.0, 0.0,
  121. * 7500000.0, 0.0, 0.0,
  122. * 0.0, 7500000.0, 0.0
  123. * ]);
  124. */
  125. this.values = options.values;
  126. }
  127. export default GeometryAttribute;