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