智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // @ts-check
  2. import Color from "../Core/Color.js";
  3. import createColorRamp from "../Core/createColorRamp.js";
  4. import CustomShader from "./Model/CustomShader.js";
  5. import defined from "../Core/defined.js";
  6. import MetadataComponentType from "./MetadataComponentType.js";
  7. import MetadataType from "./MetadataType.js";
  8. import TextureUniform from "./Model/TextureUniform.js";
  9. import UniformType from "./Model/UniformType.js";
  10. /** @import VoxelProvider from "./VoxelProvider.js"; */
  11. /**
  12. * Builds a custom shader for a voxel primitive based on information
  13. * from the VoxelProvider about the metadata properties.
  14. *
  15. * Supports scalar float properties, or vec4 properties with float components.
  16. *
  17. * @function
  18. *
  19. * @param {VoxelProvider} provider The VoxelProvider for the primitive.
  20. * @returns {CustomShader|undefined} A custom shader for the primitive, or undefined if a shader could not be constructed.
  21. *
  22. * @private
  23. */
  24. function buildVoxelCustomShader(provider) {
  25. const { names, types, componentTypes, minimumValues, maximumValues } =
  26. provider;
  27. // Loop over metadata properties and build a shader for the first property
  28. // that has a supported type and component type.
  29. for (let i = 0; i < names.length; i++) {
  30. if (!rangeIsValid(minimumValues?.[i], maximumValues?.[i])) {
  31. continue;
  32. }
  33. const shader = constructMetadataShader(
  34. names[i],
  35. types[i],
  36. componentTypes[i],
  37. minimumValues[i],
  38. maximumValues[i],
  39. );
  40. if (defined(shader)) {
  41. return shader;
  42. }
  43. }
  44. }
  45. /**
  46. * Check if a range of values is valid for constructing a meaningful shader.
  47. *
  48. * @param {number[]} minimumValue The minimum value of the range.
  49. * @param {number[]} maximumValue The maximum value of the range.
  50. * @returns {boolean} True if the range is valid, false otherwise.
  51. *
  52. * @private
  53. */
  54. function rangeIsValid(minimumValue, maximumValue) {
  55. if (!Array.isArray(minimumValue) || !Array.isArray(maximumValue)) {
  56. return false;
  57. }
  58. const minimumComponent = Math.min(...minimumValue);
  59. const maximumComponent = Math.max(...maximumValue);
  60. return minimumComponent < maximumComponent;
  61. }
  62. /**
  63. * Construct a metadata shader for a single metadata property.
  64. *
  65. * @param {string} name The name of the metadata property.
  66. * @param {MetadataType} type The type of the metadata property.
  67. * @param {MetadataComponentType} componentType The component type of the metadata property.
  68. * @param {number[]} minimumValue The minimum value of the metadata property.
  69. * @param {number[]} maximumValue The maximum value of the metadata property.
  70. * @returns {CustomShader|undefined} A custom shader for the metadata property, or undefined if a shader could not be constructed.
  71. *
  72. * @private
  73. */
  74. function constructMetadataShader(
  75. name,
  76. type,
  77. componentType,
  78. minimumValue,
  79. maximumValue,
  80. ) {
  81. if (
  82. type === MetadataType.VEC4 &&
  83. componentType === MetadataComponentType.FLOAT32
  84. ) {
  85. return buildColorShader(name, minimumValue, maximumValue);
  86. } else if (
  87. type === MetadataType.SCALAR &&
  88. componentType === MetadataComponentType.FLOAT32
  89. ) {
  90. return buildColorMapShader(name, minimumValue, maximumValue);
  91. }
  92. }
  93. /**
  94. * Build a color shader for a metadata property of type VEC4 and component type FLOAT32.
  95. *
  96. * @param {string} name The name of the metadata property.
  97. * @param {number[]} minimumValue The minimum value of the metadata property.
  98. * @param {number[]} maximumValue The maximum value of the metadata property.
  99. * @returns {CustomShader|undefined} A custom shader for the metadata property, or undefined if a shader could not be constructed.
  100. *
  101. * @private
  102. */
  103. function buildColorShader(name, minimumValue, maximumValue) {
  104. // Check if the values are in a range that would be meaningful to interpret as colors.
  105. const minComponent = Math.min(...minimumValue);
  106. const maxComponent = Math.max(...maximumValue);
  107. if (minComponent < 0 || maxComponent > 1) {
  108. // The data is out of the range expected for colors, so a color shader will be meaningless.
  109. return;
  110. }
  111. return new CustomShader({
  112. fragmentShaderText: `void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material)
  113. {
  114. material.diffuse = fsInput.metadata.${name}.rgb;
  115. material.alpha = fsInput.metadata.${name}.a;
  116. }`,
  117. });
  118. }
  119. /**
  120. * Build a color map shader for a metadata property of type SCALAR and component type FLOAT32.
  121. *
  122. * @param {string} name The name of the metadata property.
  123. * @param {number[]} minimumValue The minimum value of the metadata property.
  124. * @param {number[]} maximumValue The maximum value of the metadata property.
  125. * @returns {CustomShader} A custom shader for the metadata property.
  126. *
  127. * @private
  128. */
  129. function buildColorMapShader(name, minimumValue, maximumValue) {
  130. // Perceptually uniform color map similar to the "viridis" color map used in scientific visualization.
  131. const colorMapWidth = 128;
  132. const colorMap = createColorRamp(
  133. [
  134. new Color(0.267, 0.004, 0.329),
  135. new Color(0.282, 0.14, 0.457),
  136. new Color(0.254, 0.265, 0.53),
  137. new Color(0.207, 0.372, 0.553),
  138. new Color(0.164, 0.471, 0.558),
  139. new Color(0.128, 0.567, 0.551),
  140. new Color(0.135, 0.659, 0.518),
  141. new Color(0.194, 0.741, 0.443),
  142. new Color(0.282, 0.819, 0.369),
  143. new Color(0.396, 0.898, 0.301),
  144. new Color(0.538, 0.965, 0.236),
  145. new Color(0.741, 0.998, 0.149),
  146. new Color(0.993, 1.0, 0.144),
  147. ],
  148. colorMapWidth,
  149. );
  150. const textureUniform = new TextureUniform({
  151. typedArray: colorMap,
  152. width: colorMapWidth,
  153. height: 1,
  154. });
  155. return new CustomShader({
  156. uniforms: {
  157. u_colorMap: {
  158. type: UniformType.SAMPLER_2D,
  159. value: textureUniform,
  160. },
  161. // The starting values are also available in the shader from the metadata statistics.
  162. // But by using uniforms, we enable later dynamic changes to the range.
  163. u_minimumValue: {
  164. type: UniformType.FLOAT,
  165. value: minimumValue[0],
  166. },
  167. u_maximumValue: {
  168. type: UniformType.FLOAT,
  169. value: maximumValue[0],
  170. },
  171. },
  172. fragmentShaderText: `void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material)
  173. {
  174. float value = fsInput.metadata.${name};
  175. vec3 voxelNormal = fsInput.attributes.normalEC;
  176. float diffuse = max(0.0, dot(voxelNormal, czm_lightDirectionEC));
  177. float lighting = 0.5 + 0.5 * diffuse;
  178. if (value >= u_minimumValue && value <= u_maximumValue) {
  179. float lerp = (value - u_minimumValue) / (u_maximumValue - u_minimumValue);
  180. material.diffuse = texture(u_colorMap, vec2(lerp, 0.5)).rgb * lighting;
  181. material.alpha = 1.0;
  182. }
  183. }`,
  184. });
  185. }
  186. export default buildVoxelCustomShader;