Inspector.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. import { RendererInspector } from './RendererInspector.js';
  2. import { Profiler } from './ui/Profiler.js';
  3. import { Performance } from './tabs/Performance.js';
  4. import { Console } from './tabs/Console.js';
  5. import { Parameters } from './tabs/Parameters.js';
  6. import { Viewer } from './tabs/Viewer.js';
  7. import { setText, splitPath, splitCamelCase } from './ui/utils.js';
  8. import { QuadMesh, NodeMaterial, CanvasTarget, setConsoleFunction, REVISION, NoToneMapping } from 'three/webgpu';
  9. import { renderOutput, vec2, vec3, vec4, Fn, screenUV, step, OnMaterialUpdate, uniform } from 'three/tsl';
  10. const aspectRatioUV = /*@__PURE__*/ Fn( ( [ uv, textureNode ] ) => {
  11. const aspect = uniform( 0 );
  12. OnMaterialUpdate( () => {
  13. const { width, height } = textureNode.value;
  14. aspect.value = width / height;
  15. } );
  16. const centered = uv.sub( 0.5 );
  17. const corrected = vec2( centered.x.div( aspect ), centered.y );
  18. const finalUV = corrected.add( 0.5 );
  19. const inBounds = step( 0.0, finalUV.x ).mul( step( finalUV.x, 1.0 ) ).mul( step( 0.0, finalUV.y ) ).mul( step( finalUV.y, 1.0 ) );
  20. return vec3( finalUV, inBounds );
  21. } );
  22. class Inspector extends RendererInspector {
  23. constructor() {
  24. super();
  25. // init profiler
  26. const profiler = new Profiler();
  27. const parameters = new Parameters( {
  28. builtin: true,
  29. icon: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M14 6m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" /><path d="M4 6l8 0" /><path d="M16 6l4 0" /><path d="M8 12m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" /><path d="M4 12l2 0" /><path d="M10 12l10 0" /><path d="M17 18m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" /><path d="M4 18l11 0" /><path d="M19 18l1 0" /></svg>'
  30. } );
  31. parameters.hide();
  32. profiler.addTab( parameters );
  33. const viewer = new Viewer();
  34. viewer.hide();
  35. profiler.addTab( viewer );
  36. const performance = new Performance();
  37. profiler.addTab( performance );
  38. const consoleTab = new Console();
  39. profiler.addTab( consoleTab );
  40. profiler.loadLayout();
  41. if ( ! profiler.activeTabId ) {
  42. profiler.setActiveTab( performance.id );
  43. }
  44. this.statsData = new Map();
  45. this.canvasNodes = new Map();
  46. this.profiler = profiler;
  47. this.performance = performance;
  48. this.console = consoleTab;
  49. this.parameters = parameters;
  50. this.viewer = viewer;
  51. this.once = {};
  52. this.displayCycle = {
  53. text: {
  54. needsUpdate: false,
  55. duration: .25,
  56. time: 0
  57. },
  58. graph: {
  59. needsUpdate: false,
  60. duration: .02,
  61. time: 0
  62. }
  63. };
  64. }
  65. get domElement() {
  66. return this.profiler.domElement;
  67. }
  68. resolveConsoleOnce( type, message ) {
  69. const key = type + message;
  70. if ( this.once[ key ] !== true ) {
  71. this.resolveConsole( type, message );
  72. this.once[ key ] = true;
  73. }
  74. }
  75. resolveConsole( type, message ) {
  76. switch ( type ) {
  77. case 'log':
  78. this.console.addMessage( 'info', message );
  79. console.log( message );
  80. break;
  81. case 'warn':
  82. this.console.addMessage( 'warn', message );
  83. console.warn( message );
  84. break;
  85. case 'error':
  86. this.console.addMessage( 'error', message );
  87. console.error( message );
  88. break;
  89. }
  90. }
  91. init() {
  92. const renderer = this.getRenderer();
  93. let sign = `THREE.WebGPURenderer: ${ REVISION } [ "`;
  94. if ( renderer.backend.isWebGPUBackend ) {
  95. sign += 'WebGPU';
  96. } else if ( renderer.backend.isWebGLBackend ) {
  97. sign += 'WebGL2';
  98. }
  99. sign += '" ]';
  100. this.console.addMessage( 'info', sign );
  101. //
  102. if ( renderer.inspector.domElement.parentElement === null && renderer.domElement.parentElement !== null ) {
  103. renderer.domElement.parentElement.appendChild( renderer.inspector.domElement );
  104. }
  105. }
  106. setRenderer( renderer ) {
  107. super.setRenderer( renderer );
  108. if ( renderer !== null ) {
  109. setConsoleFunction( this.resolveConsole.bind( this ) );
  110. if ( this.isAvailable ) {
  111. renderer.backend.trackTimestamp = true;
  112. renderer.init().then( () => {
  113. if ( renderer.hasFeature( 'timestamp-query' ) !== true ) {
  114. this.console.addMessage( 'error', 'THREE.Inspector: GPU Timestamp Queries not available.' );
  115. }
  116. } );
  117. }
  118. }
  119. return this;
  120. }
  121. createParameters( name ) {
  122. if ( this.parameters.isVisible === false ) {
  123. this.parameters.show();
  124. if ( this.parameters.isDetached === false ) {
  125. this.profiler.setActiveTab( this.parameters.id );
  126. }
  127. }
  128. return this.parameters.createGroup( name );
  129. }
  130. getStatsData( cid ) {
  131. let data = this.statsData.get( cid );
  132. if ( data === undefined ) {
  133. data = {};
  134. this.statsData.set( cid, data );
  135. }
  136. return data;
  137. }
  138. resolveStats( stats ) {
  139. const data = this.getStatsData( stats.cid );
  140. if ( data.initialized !== true ) {
  141. data.cpu = stats.cpu;
  142. data.gpu = stats.gpu;
  143. data.stats = [];
  144. data.initialized = true;
  145. }
  146. // store stats
  147. if ( data.stats.length > this.maxFrames ) {
  148. data.stats.shift();
  149. }
  150. data.stats.push( stats );
  151. // compute averages
  152. data.cpu = this.getAverageDeltaTime( data, 'cpu' );
  153. data.gpu = this.getAverageDeltaTime( data, 'gpu' );
  154. data.total = data.cpu + data.gpu;
  155. // children
  156. for ( const child of stats.children ) {
  157. this.resolveStats( child );
  158. const childData = this.getStatsData( child.cid );
  159. data.cpu += childData.cpu;
  160. data.gpu += childData.gpu;
  161. data.total += childData.total;
  162. }
  163. }
  164. getCanvasDataByNode( node ) {
  165. let canvasData = this.canvasNodes.get( node );
  166. if ( canvasData === undefined ) {
  167. const renderer = this.getRenderer();
  168. const canvas = document.createElement( 'canvas' );
  169. const canvasTarget = new CanvasTarget( canvas );
  170. canvasTarget.setPixelRatio( window.devicePixelRatio );
  171. canvasTarget.setSize( 140, 140 );
  172. const id = node.id;
  173. const { path, name } = splitPath( splitCamelCase( node.getName() || '(unnamed)' ) );
  174. const target = node.context( { getUV: ( textureNode ) => {
  175. const uvData = aspectRatioUV( screenUV, textureNode );
  176. const correctedUV = uvData.xy;
  177. const mask = uvData.z;
  178. return correctedUV.mul( mask );
  179. } } );
  180. let output = vec4( vec3( target ), 1 );
  181. output = renderOutput( output, NoToneMapping, renderer.outputColorSpace );
  182. output = output.context( { inspector: true } );
  183. const material = new NodeMaterial();
  184. material.outputNode = output;
  185. const quad = new QuadMesh( material );
  186. quad.name = 'Viewer - ' + name;
  187. canvasData = {
  188. id,
  189. name,
  190. path,
  191. node,
  192. quad,
  193. canvasTarget,
  194. material
  195. };
  196. this.canvasNodes.set( node, canvasData );
  197. }
  198. return canvasData;
  199. }
  200. resolveViewer() {
  201. const nodes = this.currentNodes;
  202. const renderer = this.getRenderer();
  203. if ( nodes.length === 0 ) return;
  204. if ( ! renderer.backend.isWebGPUBackend ) {
  205. this.resolveConsoleOnce( 'warn', 'Inspector: Viewer is only available with WebGPU.' );
  206. return;
  207. }
  208. //
  209. if ( ! this.viewer.isVisible ) {
  210. this.viewer.show();
  211. }
  212. const canvasDataList = nodes.map( node => this.getCanvasDataByNode( node ) );
  213. this.viewer.update( renderer, canvasDataList );
  214. }
  215. getAverageDeltaTime( statsData, property, frames = this.fps ) {
  216. const statsArray = statsData.stats;
  217. let sum = 0;
  218. let count = 0;
  219. for ( let i = statsArray.length - 1; i >= 0 && count < frames; i -- ) {
  220. const stats = statsArray[ i ];
  221. const value = stats[ property ];
  222. if ( value > 0 ) {
  223. // ignore invalid values
  224. sum += value;
  225. count ++;
  226. }
  227. }
  228. return count > 0 ? sum / count : 0;
  229. }
  230. resolveFrame( frame ) {
  231. const nextFrame = this.getFrameById( frame.frameId + 1 );
  232. if ( ! nextFrame ) return;
  233. frame.cpu = 0;
  234. frame.gpu = 0;
  235. frame.total = 0;
  236. for ( const stats of frame.children ) {
  237. this.resolveStats( stats );
  238. const data = this.getStatsData( stats.cid );
  239. frame.cpu += data.cpu;
  240. frame.gpu += data.gpu;
  241. frame.total += data.total;
  242. }
  243. // improve stats using next frame
  244. frame.deltaTime = nextFrame.startTime - frame.startTime;
  245. frame.miscellaneous = frame.deltaTime - frame.total;
  246. if ( frame.miscellaneous < 0 ) {
  247. // Frame desync, probably due to async GPU timing.
  248. frame.miscellaneous = 0;
  249. }
  250. //
  251. this.updateCycle( this.displayCycle.text );
  252. this.updateCycle( this.displayCycle.graph );
  253. if ( this.displayCycle.text.needsUpdate ) {
  254. setText( 'fps-counter', this.fps.toFixed() );
  255. this.performance.updateText( this, frame );
  256. }
  257. if ( this.displayCycle.graph.needsUpdate ) {
  258. this.performance.updateGraph( this, frame );
  259. }
  260. this.displayCycle.text.needsUpdate = false;
  261. this.displayCycle.graph.needsUpdate = false;
  262. }
  263. updateCycle( cycle ) {
  264. cycle.time += this.nodeFrame.deltaTime;
  265. if ( cycle.time >= cycle.duration ) {
  266. cycle.needsUpdate = true;
  267. cycle.time = 0;
  268. }
  269. }
  270. }
  271. export { Inspector };
粤ICP备19079148号