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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Transpiles a [GLSL 3.00]{@link https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf}
  3. * shader to a [GLSL 1.00]{@link https://registry.khronos.org/OpenGL/specs/es/2.0/GLSL_ES_Specification_1.00.pdf} shader.
  4. *
  5. * This function does not aim to provide a comprehensive transpilation from GLSL 3.00 to GLSL 1.00; only the functionality
  6. * used within the CesiumJS shaders is supported.
  7. *
  8. * @private
  9. *
  10. * @param {string} input The GLSL 3.00 shader.
  11. * @param {boolean} isFragmentShader True if the shader is a fragment shader.
  12. *
  13. * @return {string}
  14. */
  15. function demodernizeShader(input, isFragmentShader) {
  16. let output = input;
  17. // Remove version string got GLSL 3.00.
  18. output = output.replaceAll(`version 300 es`, ``);
  19. // Replace all texture calls with texture2D
  20. output = output.replaceAll(
  21. /(texture\()/g,
  22. `texture2D(`, // Trailing ')' is included in the match group.
  23. );
  24. if (isFragmentShader) {
  25. // Replace the in with varying.
  26. output = output.replaceAll(
  27. /\n\s*(in)\s+(vec\d|mat\d|float)/g,
  28. `\nvarying $2`,
  29. );
  30. if (/out_FragData_(\d+)/.test(output)) {
  31. output = `#extension GL_EXT_draw_buffers : enable\n${output}`;
  32. // Remove all layout declarations for out_FragData.
  33. output = output.replaceAll(
  34. /layout\s+\(location\s*=\s*\d+\)\s*out\s+vec4\s+out_FragData_\d+;/g,
  35. ``,
  36. );
  37. // Replace out_FragData with gl_FragData.
  38. output = output.replaceAll(/out_FragData_(\d+)/g, `gl_FragData[$1]`);
  39. }
  40. // Remove all layout declarations for out_FragColor.
  41. output = output.replaceAll(
  42. /layout\s+\(location\s*=\s*0\)\s*out\s+vec4\s+out_FragColor;/g,
  43. ``,
  44. );
  45. // Replace out_FragColor with gl_FragColor.
  46. output = output.replaceAll(/out_FragColor/g, `gl_FragColor`);
  47. output = output.replaceAll(/out_FragColor\[(\d+)\]/g, `gl_FragColor[$1]`);
  48. if (/gl_FragDepth/.test(output)) {
  49. output = `#extension GL_EXT_frag_depth : enable\n${output}`;
  50. // Replace gl_FragDepth with gl_FragDepthEXT.
  51. output = output.replaceAll(/gl_FragDepth/g, `gl_FragDepthEXT`);
  52. }
  53. // Enable the EXT_shader_texture_lod extension
  54. output = `#ifdef GL_EXT_shader_texture_lod\n#extension GL_EXT_shader_texture_lod : enable\n#endif\n${output}`;
  55. // Enable the OES_standard_derivatives extension
  56. output = `#ifdef GL_OES_standard_derivatives\n#extension GL_OES_standard_derivatives : enable\n#endif\n${output}`;
  57. } else {
  58. // Replace the in with attribute.
  59. output = output.replaceAll(/(in)\s+(vec\d|mat\d|float)/g, `attribute $2`);
  60. // Replace the out with varying.
  61. output = output.replaceAll(
  62. /(out)\s+(vec\d|mat\d|float)\s+([\w]+);/g,
  63. `varying $2 $3;`,
  64. );
  65. }
  66. // Add version string for GLSL 1.00.
  67. output = `#version 100\n${output}`;
  68. return output;
  69. }
  70. export default demodernizeShader;