Add WebGL compatibility
This commit is contained in:
parent
3725f56c78
commit
1d4980ae28
1 changed files with 567 additions and 212 deletions
34
src/graphics/graphics-library/universal-rendering-context.ts
Normal file
34
src/graphics/graphics-library/universal-rendering-context.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { Insights } from '../rendering/insights';
|
||||
|
||||
export type UniversalRenderingContext = WebGL2RenderingContext &
|
||||
WebGLRenderingContext & { isWebGL2: boolean };
|
||||
|
||||
export const getUniversalRenderingContext = (
|
||||
canvas: HTMLCanvasElement,
|
||||
ignoreWebGL2 = false
|
||||
): UniversalRenderingContext => {
|
||||
let context: WebGL2RenderingContext | WebGLRenderingContext | null = ignoreWebGL2
|
||||
? null
|
||||
: canvas.getContext('webgl2');
|
||||
|
||||
let webgl2Support = true;
|
||||
|
||||
if (!context) {
|
||||
context = canvas.getContext('webgl');
|
||||
webgl2Support = false;
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
context = canvas.getContext('experimental-webgl') as WebGLRenderingContext;
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
throw new Error('Neither WebGL or WebGL2 is supported');
|
||||
}
|
||||
|
||||
Insights.setValue('using WebGL2', webgl2Support);
|
||||
|
||||
const result = context as UniversalRenderingContext;
|
||||
result.isWebGL2 = webgl2Support;
|
||||
return result;
|
||||
};
|
||||
Loading…
Reference in a new issue