PropertyTextureProperty.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import Check from "../Core/Check.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import GltfLoaderUtil from "./GltfLoaderUtil.js";
  5. import MetadataType from "./MetadataType.js";
  6. import MetadataComponentType from "./MetadataComponentType.js";
  7. /**
  8. * A property in a property texture.
  9. *
  10. * <p>
  11. * See the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata|EXT_structural_metadata Extension} as well as the
  12. * previous {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension} for glTF.
  13. * </p>
  14. *
  15. * @param {Object} options Object with the following properties:
  16. * @param {Object} options.property The property JSON object.
  17. * @param {MetadataClassProperty} options.classProperty The class property.
  18. * @param {Object.<Number, Texture>} options.textures An object mapping texture IDs to {@link Texture} objects.
  19. *
  20. * @alias PropertyTextureProperty
  21. * @constructor
  22. *
  23. * @private
  24. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  25. */
  26. function PropertyTextureProperty(options) {
  27. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  28. const property = options.property;
  29. const classProperty = options.classProperty;
  30. const textures = options.textures;
  31. //>>includeStart('debug', pragmas.debug);
  32. Check.typeOf.object("options.property", property);
  33. Check.typeOf.object("options.classProperty", classProperty);
  34. Check.typeOf.object("options.textures", textures);
  35. //>>includeEnd('debug');
  36. // in EXT_structural_metadata, the property is a valid glTF textureInfo
  37. const textureInfo = property;
  38. const textureReader = GltfLoaderUtil.createModelTextureReader({
  39. textureInfo: textureInfo,
  40. channels: reformatChannels(property.channels),
  41. texture: textures[textureInfo.index],
  42. });
  43. this._min = property.min;
  44. this._max = property.max;
  45. let offset = property.offset;
  46. let scale = property.scale;
  47. // This needs to be set before handling default values
  48. const hasValueTransform =
  49. classProperty.hasValueTransform || defined(offset) || defined(scale);
  50. // If the property attribute does not define an offset/scale, it inherits from
  51. // the class property. The class property handles setting the default of
  52. // identity: (offset 0, scale 1) with the same scalar/vector/matrix types.
  53. // array types are disallowed by the spec.
  54. offset = defaultValue(offset, classProperty.offset);
  55. scale = defaultValue(scale, classProperty.scale);
  56. // offset and scale are applied on the GPU, so unpack the values
  57. // as math types we can use in uniform callbacks.
  58. offset = classProperty.unpackVectorAndMatrixTypes(offset);
  59. scale = classProperty.unpackVectorAndMatrixTypes(scale);
  60. this._offset = offset;
  61. this._scale = scale;
  62. this._hasValueTransform = hasValueTransform;
  63. this._textureReader = textureReader;
  64. this._classProperty = classProperty;
  65. this._extras = property.extras;
  66. this._extensions = property.extensions;
  67. }
  68. Object.defineProperties(PropertyTextureProperty.prototype, {
  69. /**
  70. * The texture reader.
  71. *
  72. * @memberof PropertyTextureProperty.prototype
  73. * @type {ModelComponents.TextureReader}
  74. * @readonly
  75. * @private
  76. */
  77. textureReader: {
  78. get: function () {
  79. return this._textureReader;
  80. },
  81. },
  82. /**
  83. * True if offset/scale should be applied. If both offset/scale were
  84. * undefined, they default to identity so this property is set false
  85. *
  86. * @memberof PropertyTextureProperty.prototype
  87. * @type {Boolean}
  88. * @readonly
  89. * @private
  90. */
  91. hasValueTransform: {
  92. get: function () {
  93. return this._hasValueTransform;
  94. },
  95. },
  96. /**
  97. * The offset to be added to property values as part of the value transform.
  98. *
  99. * @memberof PropertyTextureProperty.prototype
  100. * @type {Number|Cartesian2|Cartesian3|Cartesian4|Matrix2|Matrix3|Matrix4}
  101. * @readonly
  102. * @private
  103. */
  104. offset: {
  105. get: function () {
  106. return this._offset;
  107. },
  108. },
  109. /**
  110. * The scale to be multiplied to property values as part of the value transform.
  111. *
  112. * @memberof PropertyTextureProperty.prototype
  113. * @type {Number|Cartesian2|Cartesian3|Cartesian4|Matrix2|Matrix3|Matrix4}
  114. * @readonly
  115. * @private
  116. */
  117. scale: {
  118. get: function () {
  119. return this._scale;
  120. },
  121. },
  122. /**
  123. * Extras in the JSON object.
  124. *
  125. * @memberof PropertyTextureProperty.prototype
  126. * @type {*}
  127. * @readonly
  128. * @private
  129. */
  130. extras: {
  131. get: function () {
  132. return this._extras;
  133. },
  134. },
  135. /**
  136. * Extensions in the JSON object.
  137. *
  138. * @memberof PropertyTextureProperty.prototype
  139. * @type {*}
  140. * @readonly
  141. * @private
  142. */
  143. extensions: {
  144. get: function () {
  145. return this._extensions;
  146. },
  147. },
  148. });
  149. PropertyTextureProperty.prototype.isGpuCompatible = function () {
  150. const classProperty = this._classProperty;
  151. const type = classProperty.type;
  152. const componentType = classProperty.componentType;
  153. if (classProperty.isArray) {
  154. // only support arrays of 1-4 UINT8 scalars (normalized or unnormalized)
  155. return (
  156. !classProperty.isVariableLengthArray &&
  157. classProperty.arrayLength <= 4 &&
  158. type === MetadataType.SCALAR &&
  159. componentType === MetadataComponentType.UINT8
  160. );
  161. }
  162. if (MetadataType.isVectorType(type) || type === MetadataType.SCALAR) {
  163. return componentType === MetadataComponentType.UINT8;
  164. }
  165. // For this initial implementation, only UINT8-based properties
  166. // are supported.
  167. return false;
  168. };
  169. const floatTypesByComponentCount = [undefined, "float", "vec2", "vec3", "vec4"];
  170. const integerTypesByComponentCount = [
  171. undefined,
  172. "int",
  173. "ivec2",
  174. "ivec3",
  175. "ivec4",
  176. ];
  177. PropertyTextureProperty.prototype.getGlslType = function () {
  178. const classProperty = this._classProperty;
  179. let componentCount = MetadataType.getComponentCount(classProperty.type);
  180. if (classProperty.isArray) {
  181. // fixed-sized arrays of length 2-4 UINT8s are represented as vectors as the
  182. // shader since those are more useful in GLSL.
  183. componentCount = classProperty.arrayLength;
  184. }
  185. // Normalized UINT8 properties are float types in the shader
  186. if (classProperty.normalized) {
  187. return floatTypesByComponentCount[componentCount];
  188. }
  189. // other UINT8-based properties are represented as integer types.
  190. return integerTypesByComponentCount[componentCount];
  191. };
  192. PropertyTextureProperty.prototype.unpackInShader = function (packedValueGlsl) {
  193. const classProperty = this._classProperty;
  194. // no unpacking needed if for normalized types
  195. if (classProperty.normalized) {
  196. return packedValueGlsl;
  197. }
  198. // integer types are read from the texture as normalized float values.
  199. // these need to be rescaled to [0, 255] and cast to the appropriate integer
  200. // type.
  201. const glslType = this.getGlslType();
  202. return `${glslType}(255.0 * ${packedValueGlsl})`;
  203. };
  204. /**
  205. * Reformat from an array of channel indices like <code>[0, 1]</code> to a
  206. * string of channels as would be used in GLSL swizzling (e.g. "rg")
  207. *
  208. * @param {Number[]} channels the channel indices
  209. * @return {String} The channels as a string of "r", "g", "b" or "a" characters.
  210. * @private
  211. */
  212. function reformatChannels(channels) {
  213. return channels
  214. .map(function (channelIndex) {
  215. return "rgba".charAt(channelIndex);
  216. })
  217. .join("");
  218. }
  219. export default PropertyTextureProperty;