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