Skip to content

Commit 29a2372

Browse files
authored
SFE: Add option to force WebGL1 (#131)
1 parent 425c0a1 commit 29a2372

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

packages/editor/src/components/propertyTab/propertyTabComponent.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BlockTools } from "../../blockTools.js";
2020
import type { Nullable } from "@babylonjs/core/types";
2121
import type { FrameNodePort } from "@babylonjs/shared-ui-components/nodeGraphSystem/frameNodePort";
2222
import type { LockObject } from "@babylonjs/shared-ui-components/tabs/propertyGrids/lockObject";
23-
import type { GlobalState } from "../../globalState";
23+
import { ForceWebGL1StorageKey, type GlobalState } from "../../globalState.js";
2424
import type { ISelectionChangedOptions } from "@babylonjs/shared-ui-components/nodeGraphSystem/interfaces/selectionChangedOptions";
2525
import { SmartFilterCoreVersion, type AnyInputBlock } from "@babylonjs/smart-filters";
2626
import type { Observer } from "@babylonjs/core/Misc/observable.js";
@@ -345,6 +345,21 @@ export class PropertyTabComponent extends react.Component<IPropertyTabComponentP
345345
}}
346346
/>
347347
)}
348+
{this.props.globalState.onNewEngine && ( // NOTE: only display this option if the Editor controls creating the Engine
349+
<CheckBoxLineComponent
350+
label="Force WebGL v1"
351+
isSelected={() => this.props.globalState.forceWebGL1}
352+
onSelect={(value: boolean) => {
353+
if (window.confirm("Any unsaved changes will be lost. Do you want to continue?")) {
354+
localStorage.setItem(ForceWebGL1StorageKey, value ? "true" : "");
355+
window.location.reload();
356+
} else {
357+
// Re-apply the original value (this.props.globalState.forceWebGL1)
358+
this.forceUpdate();
359+
}
360+
}}
361+
/>
362+
)}
348363
</LineContainerComponent>
349364
{(this.props.globalState.loadSmartFilter ||
350365
this.props.globalState.downloadSmartFilter ||

packages/editor/src/globalState.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const PreviewBackgroundStorageKey = "PreviewBackground";
2424
export const DefaultPreviewAspectRatio = "1.33333";
2525
export const PreviewAspectRatioKey = "PreviewAspectRatio";
2626
export const PreviewFillContainerKey = "PreviewFillContainer";
27+
export const ForceWebGL1StorageKey = "ForceWebGL1";
2728

2829
export class GlobalState {
2930
private _previewBackground: string;
@@ -44,6 +45,8 @@ export class GlobalState {
4445
!!localStorage.getItem(PreviewFillContainerKey)
4546
);
4647

48+
forceWebGL1: boolean = !!localStorage.getItem(ForceWebGL1StorageKey);
49+
4750
smartFilter: SmartFilter;
4851

4952
blockEditorRegistration: BlockEditorRegistration;

packages/editor/src/graphEditor.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Portal } from "./portal.js";
88

99
import { MessageDialog } from "@babylonjs/shared-ui-components/components/MessageDialog.js";
1010
import { GraphCanvasComponent } from "@babylonjs/shared-ui-components/nodeGraphSystem/graphCanvas.js";
11-
import { LogComponent } from "./components/log/logComponent.js";
11+
import { LogComponent, LogEntry } from "./components/log/logComponent.js";
1212
import { TypeLedger } from "@babylonjs/shared-ui-components/nodeGraphSystem/typeLedger.js";
1313
import { BlockTools } from "./blockTools.js";
1414
import { PropertyTabComponent } from "./components/propertyTab/propertyTabComponent.js";
@@ -33,6 +33,7 @@ import { OutputBlockName } from "./configuration/constants.js";
3333
import type { BlockNodeData } from "./graphSystem/blockNodeData";
3434
import { DataStorage } from "@babylonjs/core/Misc/dataStorage.js";
3535
import { OnlyShowCustomBlocksDefaultValue } from "./constants.js";
36+
import { ThinEngine } from "@babylonjs/core/Engines/thinEngine.js";
3637

3738
interface IGraphEditorProps {
3839
globalState: GlobalState;
@@ -116,7 +117,9 @@ export class GraphEditor extends react.Component<IGraphEditorProps, IGraphEditor
116117
"sfe-preview-canvas"
117118
) as HTMLCanvasElement;
118119
if (canvas && this.props.globalState.onNewEngine) {
119-
const engine = initializePreview(canvas);
120+
const engine = initializePreview(canvas, this.props.globalState.forceWebGL1);
121+
const versionToLog = `Babylon.js v${ThinEngine.Version} - WebGL${engine.webGLVersion}`;
122+
this.props.globalState.onLogRequiredObservable.notifyObservers(new LogEntry(versionToLog, false));
120123
this.props.globalState.engine = engine;
121124
this.props.globalState.onNewEngine(engine);
122125
this._canvasResizeObserver.observe(canvas);
@@ -447,7 +450,7 @@ export class GraphEditor extends react.Component<IGraphEditorProps, IGraphEditor
447450
) as HTMLCanvasElement
448451
) => {
449452
if (canvas && this.props.globalState.onNewEngine) {
450-
const engine = initializePreview(canvas);
453+
const engine = initializePreview(canvas, this.props.globalState.forceWebGL1);
451454
this.props.globalState.engine = engine;
452455
this.props.globalState.onNewEngine(engine);
453456
}

packages/editor/src/initializePreview.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ThinEngine } from "@babylonjs/core/Engines/thinEngine.js";
22

3-
export function initializePreview(canvas: HTMLCanvasElement): ThinEngine {
3+
export function initializePreview(canvas: HTMLCanvasElement, forceWebGL1: boolean): ThinEngine {
44
const antialias = false;
55
const engine = new ThinEngine(
66
canvas,
@@ -12,9 +12,8 @@ export function initializePreview(canvas: HTMLCanvasElement): ThinEngine {
1212
audioEngine: false,
1313
// Important to allow skip frame and tiled optimizations
1414
preserveDrawingBuffer: false,
15-
// Useful during debug to simulate WebGL1 devices (Safari)
16-
// disableWebGL2Support: true,
1715
premultipliedAlpha: false,
16+
disableWebGL2Support: forceWebGL1,
1817
},
1918
false
2019
);

0 commit comments

Comments
 (0)