Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/jsm/inspector/Extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Tab } from 'three/addons/inspector/ui/Tab.js';

export class Extension extends Tab {

constructor( name, options = {} ) {

super( name, options );

this.isExtension = true;

}

}
216 changes: 108 additions & 108 deletions examples/jsm/inspector/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,9 @@ import { Parameters } from './tabs/Parameters.js';
import { Settings } from './tabs/Settings.js';
import { Viewer } from './tabs/Viewer.js';
import { Timeline } from './tabs/Timeline.js';
import { setText, splitPath, splitCamelCase } from './ui/utils.js';
import { setText } from './ui/utils.js';

import { QuadMesh, NodeMaterial, CanvasTarget, setConsoleFunction, REVISION, NoToneMapping } from 'three/webgpu';
import { renderOutput, vec2, vec3, vec4, Fn, screenUV, step, OnMaterialUpdate, uniform } from 'three/tsl';

const aspectRatioUV = /*@__PURE__*/ Fn( ( [ uv, textureNode ] ) => {

const aspect = uniform( 0 );

OnMaterialUpdate( () => {

const { width, height } = textureNode.value;

aspect.value = width / height;

} );

const centered = uv.sub( 0.5 );
const corrected = vec2( centered.x.div( aspect ), centered.y );
const finalUV = corrected.add( 0.5 );

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 ) );

return vec3( finalUV, inBounds );

} );
import { setConsoleFunction, REVISION } from 'three/webgpu';

class Inspector extends RendererInspector {

Expand All @@ -43,7 +20,7 @@ class Inspector extends RendererInspector {

// init profiler

const profiler = new Profiler();
const profiler = new Profiler( this );
profiler.addEventListener( 'resize', ( e ) => this.dispatchEvent( e ) );

const parameters = new Parameters( {
Expand Down Expand Up @@ -81,15 +58,16 @@ class Inspector extends RendererInspector {
}

this.statsData = new Map();
this.canvasNodes = new Map();
this.profiler = profiler;
this.performance = performance;
this.memory = memory;
this.console = consoleTab;
this.parameters = parameters;
this.viewer = viewer;
this.timeline = timeline;
this.settings = settings;
this.once = {};
this.extensionsData = new WeakMap();

this.displayCycle = {
text: {
Expand All @@ -112,6 +90,34 @@ class Inspector extends RendererInspector {

}

onExtension( name, callback ) {

const extensionAdded = ( e ) => {

if ( e.name === name ) {

callback( e.tab );

this.settings.removeEventListener( 'extensionadded', extensionAdded );

}

};

if ( this.settings.extensions[ name ] && this.settings.extensions[ name ].loaded ) {

callback( this.settings.extensions[ name ] );

} else {

this.settings.addEventListener( 'extensionadded', extensionAdded );

}

return this;

}

hide() {

this.profiler.hide();
Expand Down Expand Up @@ -154,6 +160,14 @@ class Inspector extends RendererInspector {

}

setActiveExtension( name, value ) {

this.settings.setActiveExtension( name, value );

return this;

}

resolveConsoleOnce( type, message ) {

const key = type + message;
Expand Down Expand Up @@ -351,115 +365,62 @@ class Inspector extends RendererInspector {

}

getCanvasDataByNode( node ) {

let canvasData = this.canvasNodes.get( node );

if ( canvasData === undefined ) {

const renderer = this.getRenderer();

const canvas = document.createElement( 'canvas' );

const canvasTarget = new CanvasTarget( canvas );
canvasTarget.setPixelRatio( window.devicePixelRatio );
canvasTarget.setSize( 140, 140 );

const id = node.id;

const { path, name } = splitPath( splitCamelCase( node.getName() || '(unnamed)' ) );
getNodes() {

const target = node.context( { getUV: ( textureNode ) => {

const uvData = aspectRatioUV( screenUV, textureNode );
const correctedUV = uvData.xy;
const mask = uvData.z;

return correctedUV.mul( mask );

} } );

let output = vec4( vec3( target ), 1 );
output = renderOutput( output, NoToneMapping, renderer.outputColorSpace );
output = output.context( { inspector: true } );

const material = new NodeMaterial();
material.outputNode = output;

const quad = new QuadMesh( material );
quad.name = 'Viewer - ' + name;

canvasData = {
id,
name,
path,
node,
quad,
canvasTarget,
material
};

this.canvasNodes.set( node, canvasData );

}

return canvasData;
return this.currentNodes;

}

resolveViewer() {

const nodes = this.currentNodes;
const renderer = this.getRenderer();
getAverageDeltaTime( statsData, property, frames = this.fps ) {

if ( nodes.length === 0 ) return;
const statsArray = statsData.stats;

if ( ! renderer.backend.isWebGPUBackend ) {
let sum = 0;
let count = 0;

this.resolveConsoleOnce( 'warn', 'Inspector: Viewer is only available with WebGPU.' );
for ( let i = statsArray.length - 1; i >= 0 && count < frames; i -- ) {

return;
const stats = statsArray[ i ];
const value = stats[ property ];

}
if ( value > 0 ) {

//
// ignore invalid values

if ( ! this.viewer.isVisible ) {
sum += value;
count ++;

this.viewer.show();
}

}

const canvasDataList = nodes.map( node => this.getCanvasDataByNode( node ) );

this.viewer.update( renderer, canvasDataList );
return count > 0 ? sum / count : 0;

}

getAverageDeltaTime( statsData, property, frames = this.fps ) {
updateTabs() {

const statsArray = statsData.stats;
// tabs

let sum = 0;
let count = 0;
const tabs = Object.values( this.profiler.tabs );

for ( let i = statsArray.length - 1; i >= 0 && count < frames; i -- ) {
for ( const tab of tabs ) {

const stats = statsArray[ i ];
const value = stats[ property ];
let tabData = this.extensionsData.get( tab );

if ( value > 0 ) {
if ( tabData === undefined ) {

// ignore invalid values
tab.init( this );

sum += value;
count ++;
tabData = {};

this.extensionsData.set( tab, tabData );

}

}
tab.update( this );

return count > 0 ? sum / count : 0;
}

}

Expand Down Expand Up @@ -537,6 +498,45 @@ class Inspector extends RendererInspector {

}

static getItem( id ) {

console.warn( 'Inspector.getItem is deprecated. Use getItem directly instead.' );
return getItem( id );

}

static setItem( id, state ) {

console.warn( 'Inspector.setItem is deprecated. Use setItem directly instead.' );
setItem( id, state );

}

}

function getItem( id ) {

const data = JSON.parse( localStorage.getItem( 'threejs-inspector' ) || '{}' );
return data[ id ] || {};

}

function setItem( id, state ) {

const data = JSON.parse( localStorage.getItem( 'threejs-inspector' ) || '{}' );

if ( state === null ) {

delete data[ id ];

} else {

data[ id ] = state;

}

localStorage.setItem( 'threejs-inspector', JSON.stringify( data ) );

}

export { Inspector };
export { Inspector, getItem, setItem };
4 changes: 2 additions & 2 deletions examples/jsm/inspector/RendererInspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class RendererInspector extends InspectorBase {

}

resolveViewer() { }
updateTabs() { }

resolveFrame( /*frame*/ ) { }

Expand Down Expand Up @@ -321,7 +321,7 @@ export class RendererInspector extends InspectorBase {

if ( this.isAvailable ) {

this.resolveViewer();
this.updateTabs();
this.resolveTimestamp();

}
Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/inspector/extensions/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "TSL Graph",
"url": "./tsl-graph/TSLGraphEditor.js"
}
]
Loading