GeometryAttribute.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 {number[]|Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} [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. * const 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 (
  49. options.componentsPerAttribute < 1 ||
  50. options.componentsPerAttribute > 4
  51. ) {
  52. throw new DeveloperError(
  53. "options.componentsPerAttribute must be between 1 and 4."
  54. );
  55. }
  56. if (!defined(options.values)) {
  57. throw new DeveloperError("options.values is required.");
  58. }
  59. //>>includeEnd('debug');
  60. /**
  61. * The datatype of each component in the attribute, e.g., individual elements in
  62. * {@link GeometryAttribute#values}.
  63. *
  64. * @type ComponentDatatype
  65. *
  66. * @default undefined
  67. */
  68. this.componentDatatype = options.componentDatatype;
  69. /**
  70. * A number between 1 and 4 that defines the number of components in an attributes.
  71. * For example, a position attribute with x, y, and z components would have 3 as
  72. * shown in the code example.
  73. *
  74. * @type Number
  75. *
  76. * @default undefined
  77. *
  78. * @example
  79. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  80. * attribute.componentsPerAttribute = 3;
  81. * attribute.values = new Float32Array([
  82. * 0.0, 0.0, 0.0,
  83. * 7500000.0, 0.0, 0.0,
  84. * 0.0, 7500000.0, 0.0
  85. * ]);
  86. */
  87. this.componentsPerAttribute = options.componentsPerAttribute;
  88. /**
  89. * When <code>true</code> and <code>componentDatatype</code> is an integer format,
  90. * indicate that the components should be mapped to the range [0, 1] (unsigned)
  91. * or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  92. * <p>
  93. * This is commonly used when storing colors using {@link ComponentDatatype.UNSIGNED_BYTE}.
  94. * </p>
  95. *
  96. * @type Boolean
  97. *
  98. * @default false
  99. *
  100. * @example
  101. * attribute.componentDatatype = Cesium.ComponentDatatype.UNSIGNED_BYTE;
  102. * attribute.componentsPerAttribute = 4;
  103. * attribute.normalize = true;
  104. * attribute.values = new Uint8Array([
  105. * Cesium.Color.floatToByte(color.red),
  106. * Cesium.Color.floatToByte(color.green),
  107. * Cesium.Color.floatToByte(color.blue),
  108. * Cesium.Color.floatToByte(color.alpha)
  109. * ]);
  110. */
  111. this.normalize = defaultValue(options.normalize, false);
  112. /**
  113. * The values for the attributes stored in a typed array. In the code example,
  114. * every three elements in <code>values</code> defines one attributes since
  115. * <code>componentsPerAttribute</code> is 3.
  116. *
  117. * @type {number[]|Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array}
  118. *
  119. * @default undefined
  120. *
  121. * @example
  122. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  123. * attribute.componentsPerAttribute = 3;
  124. * attribute.values = new Float32Array([
  125. * 0.0, 0.0, 0.0,
  126. * 7500000.0, 0.0, 0.0,
  127. * 0.0, 7500000.0, 0.0
  128. * ]);
  129. */
  130. this.values = options.values;
  131. }
  132. export default GeometryAttribute;