panel.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // Store the state of our inspector
  2. const state = {
  3. revision: null,
  4. scenes: new Map(),
  5. renderers: new Map(),
  6. objects: new Map()
  7. };
  8. // console.log('Panel script loaded');
  9. // Create a connection to the background page
  10. const backgroundPageConnection = chrome.runtime.connect( {
  11. name: 'three-devtools'
  12. } );
  13. // Initialize the connection with the inspected tab ID
  14. backgroundPageConnection.postMessage( {
  15. name: 'init',
  16. tabId: chrome.devtools.inspectedWindow.tabId
  17. } );
  18. // Request the initial state from the bridge script
  19. backgroundPageConnection.postMessage( {
  20. name: 'request-state',
  21. tabId: chrome.devtools.inspectedWindow.tabId
  22. } );
  23. const intervalId = setInterval( () => {
  24. backgroundPageConnection.postMessage( {
  25. name: 'request-state',
  26. tabId: chrome.devtools.inspectedWindow.tabId
  27. } );
  28. }, 1000 );
  29. backgroundPageConnection.onDisconnect.addListener( () => {
  30. console.log( 'Panel: Connection to background page lost' );
  31. clearInterval( intervalId );
  32. clearState();
  33. } );
  34. // console.log('Connected to background page with tab ID:', chrome.devtools.inspectedWindow.tabId);
  35. // Store renderer collapse states
  36. const rendererCollapsedState = new Map();
  37. // Clear state when panel is reloaded
  38. function clearState() {
  39. state.revision = null;
  40. state.scenes.clear();
  41. state.renderers.clear();
  42. state.objects.clear();
  43. const container = document.getElementById( 'scene-tree' );
  44. if ( container ) {
  45. container.innerHTML = '';
  46. }
  47. }
  48. // Listen for messages from the background page
  49. backgroundPageConnection.onMessage.addListener( function ( message ) {
  50. if ( message.id === 'three-devtools' ) {
  51. handleThreeEvent( message );
  52. }
  53. } );
  54. function handleThreeEvent( message ) {
  55. switch ( message.name ) {
  56. case 'register':
  57. state.revision = message.detail.revision;
  58. updateUI();
  59. break;
  60. // Handle individual renderer observation
  61. case 'renderer':
  62. const detail = message.detail;
  63. // Only store each unique object once
  64. if ( ! state.objects.has( detail.uuid ) ) {
  65. state.objects.set( detail.uuid, detail );
  66. state.renderers.set( detail.uuid, detail );
  67. }
  68. // Update or add the renderer in the state map
  69. state.renderers.set( detail.uuid, detail ); // Ensure the latest detail is always stored
  70. // Also update the generic objects map if renderers are stored there too
  71. state.objects.set( detail.uuid, detail );
  72. // The DOM update logic previously here is redundant because updateUI()
  73. // rebuilds the entire renderer element anyway, using the updated data
  74. // from state.renderers and the persisted open/closed state.
  75. updateUI(); // Call updateUI to rebuild based on the new state
  76. break;
  77. // Handle a batch of objects for a specific scene
  78. case 'scene':
  79. const { sceneUuid, objects: batchObjects } = message.detail;
  80. console.log( 'Panel: Received scene batch for', sceneUuid, 'with', batchObjects.length, 'objects' );
  81. // 1. Identify UUIDs in the new batch
  82. const newObjectUuids = new Set( batchObjects.map( obj => obj.uuid ) );
  83. // 2. Identify current object UUIDs associated with this scene that are NOT renderers
  84. const currentSceneObjectUuids = new Set();
  85. state.objects.forEach( ( obj, uuid ) => {
  86. // Use the _sceneUuid property we'll add below, or check if it's the scene root itself
  87. if ( obj._sceneUuid === sceneUuid || uuid === sceneUuid ) {
  88. currentSceneObjectUuids.add( uuid );
  89. }
  90. } );
  91. // 3. Find UUIDs to remove (in current state for this scene, but not in the new batch)
  92. const uuidsToRemove = new Set();
  93. currentSceneObjectUuids.forEach( uuid => {
  94. if ( ! newObjectUuids.has( uuid ) ) {
  95. uuidsToRemove.add( uuid );
  96. }
  97. } );
  98. // 4. Remove stale objects from state
  99. uuidsToRemove.forEach( uuid => {
  100. state.objects.delete( uuid );
  101. // If a scene object itself was somehow removed (unlikely for root), clean up scenes map too
  102. if ( state.scenes.has( uuid ) ) {
  103. state.scenes.delete( uuid );
  104. }
  105. } );
  106. // 5. Process the new batch: Add/Update objects and mark their scene association
  107. batchObjects.forEach( objData => {
  108. // Add a private property to track which scene this object belongs to
  109. objData._sceneUuid = sceneUuid;
  110. state.objects.set( objData.uuid, objData );
  111. // Ensure the scene root is in the scenes map
  112. if ( objData.isScene && objData.uuid === sceneUuid ) {
  113. state.scenes.set( objData.uuid, objData );
  114. }
  115. // Note: Renderers are handled separately by 'renderer' events and shouldn't appear in scene batches.
  116. } );
  117. // Update UI once after processing the entire batch
  118. updateUI();
  119. break;
  120. case 'committed':
  121. // Page was reloaded, clear state
  122. clearState();
  123. break;
  124. }
  125. }
  126. // Function to get an object icon based on its type
  127. function getObjectIcon( obj ) {
  128. if ( obj.isScene ) return '🌍';
  129. if ( obj.isCamera ) return '📷';
  130. if ( obj.isLight ) return '💡';
  131. if ( obj.isInstancedMesh ) return '🔸';
  132. if ( obj.isMesh ) return '🔷';
  133. if ( obj.type === 'Group' ) return '📁';
  134. return '📦';
  135. }
  136. function renderRenderer( obj, container ) {
  137. // Create <details> element as the main container
  138. const detailsElement = document.createElement( 'details' );
  139. detailsElement.className = 'renderer-container';
  140. detailsElement.setAttribute( 'data-uuid', obj.uuid );
  141. // Set initial state
  142. detailsElement.open = false;
  143. if ( rendererCollapsedState.has( obj.uuid ) ) {
  144. detailsElement.open = rendererCollapsedState.get( obj.uuid );
  145. }
  146. // Add toggle listener to save state
  147. detailsElement.addEventListener( 'toggle', () => {
  148. rendererCollapsedState.set( obj.uuid, detailsElement.open );
  149. } );
  150. // Create the summary element (clickable header) - THIS IS THE FIRST CHILD
  151. const summaryElem = document.createElement( 'summary' ); // USE <summary> tag
  152. summaryElem.className = 'tree-item renderer-summary'; // Acts as summary
  153. // Update display name in the summary line
  154. const props = obj.properties;
  155. const details = [ `${props.width}x${props.height}` ];
  156. if ( props.info ) {
  157. details.push( `${props.info.render.calls} draws` );
  158. details.push( `${props.info.render.triangles.toLocaleString()} triangles` );
  159. }
  160. const displayName = `${obj.type} <span class="object-details">${details.join( ' ・ ' )}</span>`;
  161. // Use toggle icon instead of paint icon
  162. summaryElem.innerHTML = `<span class="icon toggle-icon"></span>
  163. <span class="label">${displayName}</span>
  164. <span class="type">${obj.type}</span>`;
  165. detailsElement.appendChild( summaryElem );
  166. const propsContainer = document.createElement( 'div' );
  167. propsContainer.className = 'properties-list';
  168. // Adjust padding calculation if needed, ensure it's a number before adding
  169. const summaryPaddingLeft = parseFloat( summaryElem.style.paddingLeft ) || 0;
  170. propsContainer.style.paddingLeft = `${summaryPaddingLeft + 20}px`; // Indent further
  171. propsContainer.innerHTML = ''; // Clear placeholder
  172. if ( obj.properties ) {
  173. const props = obj.properties;
  174. const info = props.info || { render: {}, memory: {} }; // Default empty objects if info is missing
  175. const gridContainer = document.createElement( 'div' );
  176. gridContainer.style.display = 'grid';
  177. gridContainer.style.gridTemplateColumns = 'repeat(auto-fit, minmax(200px, 1fr))'; // Responsive columns
  178. gridContainer.style.gap = '10px 20px'; // Row and column gap
  179. // --- Column 1: Properties ---
  180. const propsCol = document.createElement( 'div' );
  181. propsCol.className = 'properties-column';
  182. const propsTitle = document.createElement( 'h4' );
  183. propsTitle.textContent = 'Properties';
  184. propsCol.appendChild( propsTitle );
  185. propsCol.appendChild( createPropertyRow( 'Size', `${props.width}x${props.height}` ) );
  186. propsCol.appendChild( createPropertyRow( 'Alpha', props.alpha ) );
  187. propsCol.appendChild( createPropertyRow( 'Antialias', props.antialias ) );
  188. propsCol.appendChild( createPropertyRow( 'Output Color Space', props.outputColorSpace ) );
  189. propsCol.appendChild( createPropertyRow( 'Tone Mapping', props.toneMapping ) );
  190. propsCol.appendChild( createPropertyRow( 'Tone Mapping Exposure', props.toneMappingExposure ) );
  191. propsCol.appendChild( createPropertyRow( 'Shadows', props.shadows ? 'enabled' : 'disabled' ) ); // Display string
  192. propsCol.appendChild( createPropertyRow( 'Auto Clear', props.autoClear ) );
  193. propsCol.appendChild( createPropertyRow( 'Auto Clear Color', props.autoClearColor ) );
  194. propsCol.appendChild( createPropertyRow( 'Auto Clear Depth', props.autoClearDepth ) );
  195. propsCol.appendChild( createPropertyRow( 'Auto Clear Stencil', props.autoClearStencil ) );
  196. propsCol.appendChild( createPropertyRow( 'Local Clipping', props.localClipping ) );
  197. propsCol.appendChild( createPropertyRow( 'Physically Correct Lights', props.physicallyCorrectLights ) );
  198. gridContainer.appendChild( propsCol );
  199. // --- Column 2: Render Stats & Memory ---
  200. const statsCol = document.createElement( 'div' );
  201. statsCol.className = 'stats-column';
  202. // Render Stats
  203. const renderTitle = document.createElement( 'h4' );
  204. renderTitle.textContent = 'Render Stats';
  205. statsCol.appendChild( renderTitle );
  206. statsCol.appendChild( createPropertyRow( 'Frame', info.render.frame ) );
  207. statsCol.appendChild( createPropertyRow( 'Draw Calls', info.render.calls ) );
  208. statsCol.appendChild( createPropertyRow( 'Triangles', info.render.triangles ) );
  209. statsCol.appendChild( createPropertyRow( 'Points', info.render.points ) );
  210. statsCol.appendChild( createPropertyRow( 'Lines', info.render.lines ) );
  211. // Memory
  212. const memoryTitle = document.createElement( 'h4' );
  213. memoryTitle.textContent = 'Memory';
  214. memoryTitle.style.marginTop = '10px'; // Add space before Memory section
  215. statsCol.appendChild( memoryTitle );
  216. statsCol.appendChild( createPropertyRow( 'Geometries', info.memory.geometries ) ); // Memory Geometries
  217. statsCol.appendChild( createPropertyRow( 'Textures', info.memory.textures ) );
  218. statsCol.appendChild( createPropertyRow( 'Shader Programs', info.memory.programs ) );
  219. gridContainer.appendChild( statsCol );
  220. propsContainer.appendChild( gridContainer );
  221. } else {
  222. propsContainer.textContent = 'No properties available.';
  223. }
  224. detailsElement.appendChild( propsContainer );
  225. container.appendChild( detailsElement ); // Append details to the main container
  226. }
  227. // Function to render an object and its children
  228. function renderObject( obj, container, level = 0 ) {
  229. const icon = getObjectIcon( obj );
  230. let displayName = obj.name || obj.type;
  231. // Default rendering for other object types
  232. const elem = document.createElement( 'div' );
  233. elem.className = 'tree-item';
  234. elem.style.paddingLeft = `${level * 20}px`;
  235. elem.setAttribute( 'data-uuid', obj.uuid );
  236. let labelContent = `<span class="icon">${icon}</span>
  237. <span class="label">${displayName}</span>
  238. <span class="type">${obj.type}</span>`;
  239. if ( obj.isScene ) {
  240. // Add object count for scenes
  241. let objectCount = - 1;
  242. function countObjects( uuid ) {
  243. const object = state.objects.get( uuid );
  244. if ( object ) {
  245. objectCount ++; // Increment count for the object itself
  246. if ( object.children ) {
  247. object.children.forEach( childId => countObjects( childId ) );
  248. }
  249. }
  250. }
  251. countObjects( obj.uuid );
  252. displayName = `${obj.name || obj.type} <span class="object-details">${objectCount} objects</span>`;
  253. labelContent = `<span class="icon">${icon}</span>
  254. <span class="label">${displayName}</span>
  255. <span class="type">${obj.type}</span>`;
  256. }
  257. elem.innerHTML = labelContent;
  258. container.appendChild( elem );
  259. // Handle children (excluding children of renderers, as properties are shown in details)
  260. if ( ! obj.isRenderer && obj.children && obj.children.length > 0 ) {
  261. // Create a container for children
  262. const childContainer = document.createElement( 'div' );
  263. childContainer.className = 'children';
  264. container.appendChild( childContainer );
  265. // Get all children and sort them by type for better organization
  266. const children = obj.children
  267. .map( childId => state.objects.get( childId ) )
  268. .filter( child => child !== undefined )
  269. .sort( ( a, b ) => {
  270. // Sort order: Cameras, Lights, Groups, Meshes, Others
  271. const typeOrder = {
  272. isCamera: 1,
  273. isLight: 2,
  274. isGroup: 3,
  275. isMesh: 4
  276. };
  277. // Refactored to avoid optional chaining parser error
  278. const findOrder = ( obj ) => {
  279. const entry = Object.entries( typeOrder ).find( ( [ key ] ) => obj[ key ] );
  280. return entry ? entry[ 1 ] : 5; // Check if entry exists, then access index 1 (value)
  281. };
  282. const aOrder = findOrder( a );
  283. const bOrder = findOrder( b );
  284. return aOrder - bOrder;
  285. } );
  286. // Render each child
  287. children.forEach( child => {
  288. renderObject( child, childContainer, level + 1 );
  289. } );
  290. }
  291. }
  292. // Function to update the UI
  293. function updateUI() {
  294. const container = document.getElementById( 'scene-tree' );
  295. container.innerHTML = '';
  296. const versionInfo = document.createElement( 'div' );
  297. versionInfo.className = 'info-item';
  298. versionInfo.style.display = 'flex'; // Use flexbox
  299. versionInfo.style.justifyContent = 'space-between'; // Align items left and right
  300. const threeVersionSpan = document.createElement( 'span' );
  301. // TODO: Why it's not available?
  302. if ( state.revision ) {
  303. threeVersionSpan.textContent = `Three.js r${state.revision}`;
  304. }
  305. const manifest = chrome.runtime.getManifest();
  306. const manifestVersionSpan = document.createElement( 'span' );
  307. manifestVersionSpan.textContent = `${manifest.version}`;
  308. manifestVersionSpan.style.opacity = '0.5'; // Make it less prominent
  309. versionInfo.appendChild( threeVersionSpan );
  310. versionInfo.appendChild( manifestVersionSpan );
  311. container.appendChild( versionInfo );
  312. // Add renderers section
  313. if ( state.renderers.size > 0 ) {
  314. const renderersSection = document.createElement( 'div' );
  315. renderersSection.className = 'section';
  316. renderersSection.innerHTML = '<h3>Renderers</h3>';
  317. state.renderers.forEach( renderer => {
  318. renderRenderer( renderer, renderersSection );
  319. } );
  320. container.appendChild( renderersSection );
  321. }
  322. // Add scenes section
  323. if ( state.scenes.size > 0 ) {
  324. const scenesSection = document.createElement( 'div' );
  325. scenesSection.className = 'section';
  326. scenesSection.innerHTML = '<h3>Scenes</h3>';
  327. state.scenes.forEach( scene => {
  328. renderObject( scene, scenesSection );
  329. } );
  330. container.appendChild( scenesSection );
  331. }
  332. }
  333. // Initial UI update
  334. clearState();
  335. updateUI();
  336. // Helper function to create a property row (Label: Value)
  337. function createPropertyRow( label, value ) {
  338. const row = document.createElement( 'div' );
  339. row.className = 'property-row'; // Add class for potential styling
  340. row.style.display = 'flex';
  341. row.style.justifyContent = 'space-between'; // Align label left, value right
  342. row.style.marginBottom = '2px'; // Small gap between rows
  343. const labelSpan = document.createElement( 'span' );
  344. labelSpan.className = 'property-label';
  345. labelSpan.textContent = `${label}:`;
  346. labelSpan.style.marginRight = '10px'; // Space between label and value
  347. labelSpan.style.whiteSpace = 'nowrap'; // Prevent label wrapping
  348. const valueSpan = document.createElement( 'span' );
  349. valueSpan.className = 'property-value';
  350. // Format numbers nicely, handle undefined/null with '–'
  351. const displayValue = ( value === undefined || value === null )
  352. ? '–'
  353. : ( typeof value === 'number' ? value.toLocaleString() : value );
  354. valueSpan.textContent = displayValue;
  355. valueSpan.style.textAlign = 'right'; // Align value text to the right
  356. row.appendChild( labelSpan );
  357. row.appendChild( valueSpan );
  358. return row;
  359. }
粤ICP备19079148号