DebugInspector.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. var attributeLocations = {};
  14. var attributes = shaderProgram.vertexAttributes;
  15. for (var 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. var context = scene.context;
  24. var sp = shaderProgram;
  25. var fs = sp.fragmentShaderSource.clone();
  26. var targets = [];
  27. fs.sources = fs.sources.map(function (source) {
  28. source = ShaderSource.replaceMain(source, "czm_Debug_main");
  29. var re = /gl_FragData\[(\d+)\]/g;
  30. var 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. var length = targets.length;
  39. var 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. var i;
  46. if (length > 0) {
  47. for (i = 0; i < length; ++i) {
  48. newMain +=
  49. " gl_FragData[" + targets[i] + "].rgb *= debugShowCommandsColor;\n";
  50. newMain +=
  51. " gl_FragData[" + targets[i] + "].rgb *= debugShowFrustumsColor;\n";
  52. }
  53. } else {
  54. newMain += " gl_FragColor.rgb *= debugShowCommandsColor;\n";
  55. newMain += " gl_FragColor.rgb *= debugShowFrustumsColor;\n";
  56. }
  57. newMain += "}";
  58. fs.sources.push(newMain);
  59. var attributeLocations = getAttributeLocations(sp);
  60. return ShaderProgram.fromCache({
  61. context: context,
  62. vertexShaderSource: sp.vertexShaderSource,
  63. fragmentShaderSource: fs,
  64. attributeLocations: attributeLocations,
  65. });
  66. }
  67. var scratchFrustumColor = new Color();
  68. function createDebugShowFrustumsUniformMap(scene, command) {
  69. // setup uniform for the shader
  70. var debugUniformMap;
  71. if (!defined(command.uniformMap)) {
  72. debugUniformMap = {};
  73. } else {
  74. debugUniformMap = command.uniformMap;
  75. }
  76. if (
  77. defined(debugUniformMap.debugShowCommandsColor) ||
  78. defined(debugUniformMap.debugShowFrustumsColor)
  79. ) {
  80. return debugUniformMap;
  81. }
  82. debugUniformMap.debugShowCommandsColor = function () {
  83. if (!scene.debugShowCommands) {
  84. return Color.WHITE;
  85. }
  86. if (!defined(command._debugColor)) {
  87. command._debugColor = Color.fromRandom();
  88. }
  89. return command._debugColor;
  90. };
  91. debugUniformMap.debugShowFrustumsColor = function () {
  92. if (!scene.debugShowFrustums) {
  93. return Color.WHITE;
  94. }
  95. // Support up to three frustums. If a command overlaps all
  96. // three, it's code is not changed.
  97. scratchFrustumColor.red =
  98. command.debugOverlappingFrustums & (1 << 0) ? 1.0 : 0.0;
  99. scratchFrustumColor.green =
  100. command.debugOverlappingFrustums & (1 << 1) ? 1.0 : 0.0;
  101. scratchFrustumColor.blue =
  102. command.debugOverlappingFrustums & (1 << 2) ? 1.0 : 0.0;
  103. scratchFrustumColor.alpha = 1.0;
  104. return scratchFrustumColor;
  105. };
  106. return debugUniformMap;
  107. }
  108. var scratchShowFrustumCommand = new DrawCommand();
  109. DebugInspector.prototype.executeDebugShowFrustumsCommand = function (
  110. scene,
  111. command,
  112. passState
  113. ) {
  114. // create debug command
  115. var shaderProgramId = command.shaderProgram.id;
  116. var debugShaderProgram = this._cachedShowFrustumsShaders[shaderProgramId];
  117. if (!defined(debugShaderProgram)) {
  118. debugShaderProgram = createDebugShowFrustumsShaderProgram(
  119. scene,
  120. command.shaderProgram
  121. );
  122. this._cachedShowFrustumsShaders[shaderProgramId] = debugShaderProgram;
  123. }
  124. var debugCommand = DrawCommand.shallowClone(
  125. command,
  126. scratchShowFrustumCommand
  127. );
  128. debugCommand.shaderProgram = debugShaderProgram;
  129. debugCommand.uniformMap = createDebugShowFrustumsUniformMap(scene, command);
  130. debugCommand.execute(scene.context, passState);
  131. };
  132. export default DebugInspector;