DebugInspector.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import Color from "../Core/Color.js";
  2. import DrawCommand from "../Renderer/DrawCommand.js";
  3. import ShaderSource from "../Renderer/ShaderSource.js";
  4. import ShaderProgram from "../Renderer/ShaderProgram.js";
  5. import defined from "../Core/defined.js";
  6. /**
  7. * @private
  8. */
  9. function DebugInspector() {
  10. this._cachedShowFrustumsShaders = {};
  11. }
  12. function getAttributeLocations(shaderProgram) {
  13. const attributeLocations = {};
  14. const attributes = shaderProgram.vertexAttributes;
  15. for (const a in attributes) {
  16. if (attributes.hasOwnProperty(a)) {
  17. attributeLocations[a] = attributes[a].index;
  18. }
  19. }
  20. return attributeLocations;
  21. }
  22. function createDebugShowFrustumsShaderProgram(scene, shaderProgram) {
  23. const context = scene.context;
  24. const sp = shaderProgram;
  25. const fs = sp.fragmentShaderSource.clone();
  26. const targets = [];
  27. fs.sources = fs.sources.map(function (source) {
  28. source = ShaderSource.replaceMain(source, "czm_Debug_main");
  29. const re = /gl_FragData\[(\d+)\]/g;
  30. let match;
  31. while ((match = re.exec(source)) !== null) {
  32. if (targets.indexOf(match[1]) === -1) {
  33. targets.push(match[1]);
  34. }
  35. }
  36. return source;
  37. });
  38. const length = targets.length;
  39. let newMain = "";
  40. newMain += "uniform vec3 debugShowCommandsColor;\n";
  41. newMain += "uniform vec3 debugShowFrustumsColor;\n";
  42. newMain += "void main() \n" + "{ \n" + " czm_Debug_main(); \n";
  43. // set debugShowCommandsColor to Color(1.0, 1.0, 1.0, 1.0) to stop rendering scene.debugShowCommands
  44. // set debugShowFrustumsColor to Color(1.0, 1.0, 1.0, 1.0) to stop rendering scene.debugShowFrustums
  45. let i;
  46. if (length > 0) {
  47. for (i = 0; i < length; ++i) {
  48. newMain += ` gl_FragData[${targets[i]}].rgb *= debugShowCommandsColor;\n`;
  49. newMain += ` gl_FragData[${targets[i]}].rgb *= debugShowFrustumsColor;\n`;
  50. }
  51. } else {
  52. newMain += " gl_FragColor.rgb *= debugShowCommandsColor;\n";
  53. newMain += " gl_FragColor.rgb *= debugShowFrustumsColor;\n";
  54. }
  55. newMain += "}";
  56. fs.sources.push(newMain);
  57. const attributeLocations = getAttributeLocations(sp);
  58. return ShaderProgram.fromCache({
  59. context: context,
  60. vertexShaderSource: sp.vertexShaderSource,
  61. fragmentShaderSource: fs,
  62. attributeLocations: attributeLocations,
  63. });
  64. }
  65. const scratchFrustumColor = new Color();
  66. function createDebugShowFrustumsUniformMap(scene, command) {
  67. // setup uniform for the shader
  68. let debugUniformMap;
  69. if (!defined(command.uniformMap)) {
  70. debugUniformMap = {};
  71. } else {
  72. debugUniformMap = command.uniformMap;
  73. }
  74. if (
  75. defined(debugUniformMap.debugShowCommandsColor) ||
  76. defined(debugUniformMap.debugShowFrustumsColor)
  77. ) {
  78. return debugUniformMap;
  79. }
  80. debugUniformMap.debugShowCommandsColor = function () {
  81. if (!scene.debugShowCommands) {
  82. return Color.WHITE;
  83. }
  84. if (!defined(command._debugColor)) {
  85. command._debugColor = Color.fromRandom();
  86. }
  87. return command._debugColor;
  88. };
  89. debugUniformMap.debugShowFrustumsColor = function () {
  90. if (!scene.debugShowFrustums) {
  91. return Color.WHITE;
  92. }
  93. // Support up to three frustums. If a command overlaps all
  94. // three, it's code is not changed.
  95. scratchFrustumColor.red =
  96. command.debugOverlappingFrustums & (1 << 0) ? 1.0 : 0.0;
  97. scratchFrustumColor.green =
  98. command.debugOverlappingFrustums & (1 << 1) ? 1.0 : 0.0;
  99. scratchFrustumColor.blue =
  100. command.debugOverlappingFrustums & (1 << 2) ? 1.0 : 0.0;
  101. scratchFrustumColor.alpha = 1.0;
  102. return scratchFrustumColor;
  103. };
  104. return debugUniformMap;
  105. }
  106. const scratchShowFrustumCommand = new DrawCommand();
  107. DebugInspector.prototype.executeDebugShowFrustumsCommand = function (
  108. scene,
  109. command,
  110. passState
  111. ) {
  112. // create debug command
  113. const shaderProgramId = command.shaderProgram.id;
  114. let debugShaderProgram = this._cachedShowFrustumsShaders[shaderProgramId];
  115. if (!defined(debugShaderProgram)) {
  116. debugShaderProgram = createDebugShowFrustumsShaderProgram(
  117. scene,
  118. command.shaderProgram
  119. );
  120. this._cachedShowFrustumsShaders[shaderProgramId] = debugShaderProgram;
  121. }
  122. const debugCommand = DrawCommand.shallowClone(
  123. command,
  124. scratchShowFrustumCommand
  125. );
  126. debugCommand.shaderProgram = debugShaderProgram;
  127. debugCommand.uniformMap = createDebugShowFrustumsUniformMap(scene, command);
  128. debugCommand.execute(scene.context, passState);
  129. };
  130. export default DebugInspector;