All files / engine/Source/Scene DebugInspector.js

78.46% Statements 51/65
50% Branches 13/26
87.5% Functions 7/8
78.46% Lines 51/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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