ElevationBandMaterial.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform sampler2D heights;\n\
  3. uniform sampler2D colors;\n\
  4. \n\
  5. // This material expects heights to be sorted from lowest to highest.\n\
  6. \n\
  7. float getHeight(int idx, float invTexSize)\n\
  8. {\n\
  9. vec2 uv = vec2((float(idx) + 0.5) * invTexSize, 0.5);\n\
  10. #ifdef OES_texture_float\n\
  11. return texture2D(heights, uv).x;\n\
  12. #else\n\
  13. return czm_unpackFloat(texture2D(heights, uv));\n\
  14. #endif\n\
  15. }\n\
  16. \n\
  17. czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  18. {\n\
  19. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  20. \n\
  21. float height = materialInput.height;\n\
  22. float invTexSize = 1.0 / float(heightsDimensions.x);\n\
  23. \n\
  24. float minHeight = getHeight(0, invTexSize);\n\
  25. float maxHeight = getHeight(heightsDimensions.x - 1, invTexSize);\n\
  26. \n\
  27. // early-out when outside the height range\n\
  28. if (height < minHeight || height > maxHeight) {\n\
  29. material.diffuse = vec3(0.0);\n\
  30. material.alpha = 0.0;\n\
  31. return material;\n\
  32. }\n\
  33. \n\
  34. // Binary search to find heights above and below.\n\
  35. int idxBelow = 0;\n\
  36. int idxAbove = heightsDimensions.x;\n\
  37. float heightBelow = minHeight;\n\
  38. float heightAbove = maxHeight;\n\
  39. \n\
  40. // while loop not allowed, so use for loop with max iterations.\n\
  41. // maxIterations of 16 supports a texture size up to 65536 (2^16).\n\
  42. const int maxIterations = 16;\n\
  43. for (int i = 0; i < maxIterations; i++) {\n\
  44. if (idxBelow >= idxAbove - 1) {\n\
  45. break;\n\
  46. }\n\
  47. \n\
  48. int idxMid = (idxBelow + idxAbove) / 2;\n\
  49. float heightTex = getHeight(idxMid, invTexSize);\n\
  50. \n\
  51. if (height > heightTex) {\n\
  52. idxBelow = idxMid;\n\
  53. heightBelow = heightTex;\n\
  54. } else {\n\
  55. idxAbove = idxMid;\n\
  56. heightAbove = heightTex;\n\
  57. }\n\
  58. }\n\
  59. \n\
  60. float lerper = heightBelow == heightAbove ? 1.0 : (height - heightBelow) / (heightAbove - heightBelow);\n\
  61. vec2 colorUv = vec2(invTexSize * (float(idxBelow) + 0.5 + lerper), 0.5);\n\
  62. vec4 color = texture2D(colors, colorUv);\n\
  63. \n\
  64. // undo preumultiplied alpha\n\
  65. if (color.a > 0.0) \n\
  66. {\n\
  67. color.rgb /= color.a;\n\
  68. }\n\
  69. \n\
  70. color.rgb = czm_gammaCorrect(color.rgb);\n\
  71. \n\
  72. material.diffuse = color.rgb;\n\
  73. material.alpha = color.a;\n\
  74. return material;\n\
  75. }\n\
  76. ";