-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
debugLayer.ts
405 lines (369 loc) · 12.5 KB
/
debugLayer.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { Tools } from "../Misc/tools";
import { Observable } from "../Misc/observable";
import { Scene } from "../scene";
import { Engine } from "../Engines/engine";
import { EngineStore } from "../Engines/engineStore";
import type { IInspectable } from "../Misc/iInspectable";
import type { Camera } from "../Cameras/camera";
// declare INSPECTOR namespace for compilation issue
declare let INSPECTOR: any;
declare let BABYLON: any;
// load the inspector using require, if not present in the global namespace.
/**
* Interface used to define scene explorer extensibility option
*/
export interface IExplorerExtensibilityOption {
/**
* Define the option label
*/
label: string;
/**
* Defines the action to execute on click
*/
action: (entity: any) => void;
/**
* Keep popup open after click
*/
keepOpenAfterClick?: boolean;
}
/**
* Defines a group of actions associated with a predicate to use when extending the Inspector scene explorer
*/
export interface IExplorerExtensibilityGroup {
/**
* Defines a predicate to test if a given type mut be extended
*/
predicate: (entity: any) => boolean;
/**
* Gets the list of options added to a type
*/
entries: IExplorerExtensibilityOption[];
}
/**
* Defines a new node that will be displayed as top level node in the explorer
*/
export interface IExplorerAdditionalChild {
/**
* Gets the name of the additional node
*/
name: string;
/**
* Function used to return the class name of the child node
*/
getClassName(): string;
/**
* List of inspectable custom properties (used by the Inspector)
* @see https://doc.babylonjs.com/toolsAndResources/inspector#extensibility
*/
inspectableCustomProperties: IInspectable[];
}
/**
* Defines a new node that will be displayed as top level node in the explorer
*/
export interface IExplorerAdditionalNode {
/**
* Gets the name of the additional node
*/
name: string;
/**
* Function used to return the list of child entries
*/
getContent(): IExplorerAdditionalChild[];
}
export type IInspectorContextMenuType = "pipeline" | "node" | "materials" | "spriteManagers" | "particleSystems";
/**
* Context menu item
*/
export interface IInspectorContextMenuItem {
/**
* Display label - menu item
*/
label: string;
/**
* Callback function that will be called when the menu item is selected
* @param entity the entity that is currently selected in the scene explorer
*/
action: (entity?: unknown) => void;
}
/**
* Interface used to define the options to use to create the Inspector
*/
export interface IInspectorOptions {
/**
* Display in overlay mode (default: false)
*/
overlay?: boolean;
/**
* HTML element to use as root (the parent of the rendering canvas will be used as default value)
*/
globalRoot?: HTMLElement;
/**
* Display the Scene explorer
*/
showExplorer?: boolean;
/**
* Display the property inspector
*/
showInspector?: boolean;
/**
* Display in embed mode (both panes on the right)
*/
embedMode?: boolean;
/**
* let the Inspector handles resize of the canvas when panes are resized (default to true)
*/
handleResize?: boolean;
/**
* Allow the panes to popup (default: true)
*/
enablePopup?: boolean;
/**
* Allow the panes to be closed by users (default: true)
*/
enableClose?: boolean;
/**
* Optional list of extensibility entries
*/
explorerExtensibility?: IExplorerExtensibilityGroup[];
/**
* Optional list of additional top level nodes
*/
additionalNodes?: IExplorerAdditionalNode[];
/**
* Optional URL to get the inspector script from (by default it uses the babylonjs CDN).
*/
inspectorURL?: string;
/**
* Optional initial tab (default to DebugLayerTab.Properties)
*/
initialTab?: DebugLayerTab;
/**
* Optional camera to use to render the gizmos from the inspector (default to the scene.activeCamera or the latest from scene.activeCameras)
*/
gizmoCamera?: Camera;
/**
* Context menu for inspector tools such as "Post Process", "Nodes", "Materials", etc.
*/
contextMenu?: Partial<Record<IInspectorContextMenuType, IInspectorContextMenuItem[]>>;
/**
* List of context menu items that should be completely overridden by custom items from the contextMenu property.
*/
contextMenuOverride?: IInspectorContextMenuType[];
}
declare module "../scene" {
export interface Scene {
/**
* @internal
* Backing field
*/
_debugLayer: DebugLayer;
/**
* Gets the debug layer (aka Inspector) associated with the scene
* @see https://doc.babylonjs.com/toolsAndResources/inspector
*/
debugLayer: DebugLayer;
}
}
Object.defineProperty(Scene.prototype, "debugLayer", {
get: function (this: Scene) {
if (!this._debugLayer) {
this._debugLayer = new DebugLayer(this);
}
return this._debugLayer;
},
enumerable: true,
configurable: true,
});
/**
* Enum of inspector action tab
*/
export enum DebugLayerTab {
/**
* Properties tag (default)
*/
Properties = 0,
/**
* Debug tab
*/
Debug = 1,
/**
* Statistics tab
*/
Statistics = 2,
/**
* Tools tab
*/
Tools = 3,
/**
* Settings tab
*/
Settings = 4,
}
/**
* The debug layer (aka Inspector) is the go to tool in order to better understand
* what is happening in your scene
* @see https://doc.babylonjs.com/toolsAndResources/inspector
*/
export class DebugLayer {
/**
* Define the url to get the inspector script from.
* By default it uses the babylonjs CDN.
* @ignoreNaming
*/
public static InspectorURL = `v${Engine.Version}/inspector/babylon.inspector.bundle.js`;
private _scene: Scene;
// eslint-disable-next-line @typescript-eslint/naming-convention
private BJSINSPECTOR = this._getGlobalInspector();
private _onPropertyChangedObservable?: Observable<{ object: any; property: string; value: any; initialValue: any }>;
/**
* Observable triggered when a property is changed through the inspector.
*/
public get onPropertyChangedObservable() {
if (this.BJSINSPECTOR && this.BJSINSPECTOR.Inspector) {
return this.BJSINSPECTOR.Inspector.OnPropertyChangedObservable;
}
if (!this._onPropertyChangedObservable) {
this._onPropertyChangedObservable = new Observable<{ object: any; property: string; value: any; initialValue: any }>();
}
return this._onPropertyChangedObservable;
}
private _onSelectionChangedObservable?: Observable<any>;
/**
* Observable triggered when the selection is changed through the inspector.
*/
public get onSelectionChangedObservable() {
if (this.BJSINSPECTOR && this.BJSINSPECTOR.Inspector) {
return this.BJSINSPECTOR.Inspector.OnSelectionChangeObservable;
}
if (!this._onSelectionChangedObservable) {
this._onSelectionChangedObservable = new Observable<any>();
}
return this._onSelectionChangedObservable;
}
/**
* Instantiates a new debug layer.
* The debug layer (aka Inspector) is the go to tool in order to better understand
* what is happening in your scene
* @see https://doc.babylonjs.com/toolsAndResources/inspector
* @param scene Defines the scene to inspect
*/
constructor(scene?: Scene) {
this._scene = scene || <Scene>EngineStore.LastCreatedScene;
if (!this._scene) {
return;
}
this._scene.onDisposeObservable.add(() => {
// Debug layer
if (this._scene._debugLayer) {
this._scene._debugLayer.hide();
}
});
}
/**
* Creates the inspector window.
* @param config
*/
private _createInspector(config?: Partial<IInspectorOptions>) {
if (this.isVisible()) {
return;
}
if (this._onPropertyChangedObservable) {
for (const observer of this._onPropertyChangedObservable!.observers) {
this.BJSINSPECTOR.Inspector.OnPropertyChangedObservable.add(observer);
}
this._onPropertyChangedObservable.clear();
this._onPropertyChangedObservable = undefined;
}
if (this._onSelectionChangedObservable) {
for (const observer of this._onSelectionChangedObservable!.observers) {
this.BJSINSPECTOR.Inspector.OnSelectionChangedObservable.add(observer);
}
this._onSelectionChangedObservable.clear();
this._onSelectionChangedObservable = undefined;
}
const userOptions: IInspectorOptions = {
overlay: false,
showExplorer: true,
showInspector: true,
embedMode: false,
handleResize: true,
enablePopup: true,
...config,
};
this.BJSINSPECTOR = this.BJSINSPECTOR || this._getGlobalInspector();
this.BJSINSPECTOR.Inspector.Show(this._scene, userOptions);
}
/**
* Select a specific entity in the scene explorer and highlight a specific block in that entity property grid
* @param entity defines the entity to select
* @param lineContainerTitles defines the specific blocks to highlight (could be a string or an array of strings)
*/
public select(entity: any, lineContainerTitles?: string | string[]) {
if (this.BJSINSPECTOR) {
if (lineContainerTitles) {
if (Object.prototype.toString.call(lineContainerTitles) == "[object String]") {
this.BJSINSPECTOR.Inspector.MarkLineContainerTitleForHighlighting(lineContainerTitles);
} else {
this.BJSINSPECTOR.Inspector.MarkMultipleLineContainerTitlesForHighlighting(lineContainerTitles);
}
}
this.BJSINSPECTOR.Inspector.OnSelectionChangeObservable.notifyObservers(entity);
}
}
/** Get the inspector from bundle or global */
private _getGlobalInspector(): any {
// UMD Global name detection from Webpack Bundle UMD Name.
if (typeof INSPECTOR !== "undefined") {
return INSPECTOR;
}
// In case of module let s check the global emitted from the Inspector entry point.
if (typeof BABYLON !== "undefined" && typeof BABYLON.Inspector !== "undefined") {
return BABYLON;
}
return undefined;
}
/**
* Get if the inspector is visible or not.
* @returns true if visible otherwise, false
*/
public isVisible(): boolean {
return this.BJSINSPECTOR && this.BJSINSPECTOR.Inspector.IsVisible;
}
/**
* Hide the inspector and close its window.
*/
public hide() {
if (this.BJSINSPECTOR) {
this.BJSINSPECTOR.Inspector.Hide();
}
}
/**
* Update the scene in the inspector
*/
public setAsActiveScene() {
if (this.BJSINSPECTOR) {
this.BJSINSPECTOR.Inspector._SetNewScene(this._scene);
}
}
/**
* Launch the debugLayer.
* @param config Define the configuration of the inspector
* @returns a promise fulfilled when the debug layer is visible
*/
public show(config?: IInspectorOptions): Promise<DebugLayer> {
return new Promise((resolve) => {
if (typeof this.BJSINSPECTOR == "undefined") {
const inspectorUrl = config && config.inspectorURL ? config.inspectorURL : DebugLayer.InspectorURL;
// Load inspector and add it to the DOM
Tools.LoadBabylonScript(inspectorUrl, () => {
this._createInspector(config);
resolve(this);
});
} else {
// Otherwise creates the inspector
this._createInspector(config);
resolve(this);
}
});
}
}