Skip to content

Commit 0186edc

Browse files
author
Avaer Kazmer
committed
Hack support for WebGL1 proxy context
1 parent 0604a33 commit 0186edc

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/Graphics.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ self.WebGL2RenderingContext = undefined; */
1010

1111
const {/*WebGLRenderingContext, WebGL2RenderingContext,*/ CanvasRenderingContext2D} = self;
1212

13+
const hasWebGL2 = !!window.WebGL2RenderingContext;
14+
1315
const _makeState = () => {
1416
const gl = GlobalContext.proxyContext;
1517

@@ -93,7 +95,7 @@ const _makeState = () => {
9395
};
9496
HTMLCanvasElement.prototype.getContext = (oldGetContext => function getContext(type, init = {}) {
9597
const match = type.match(/^(?:experimental-)?(webgl2?)$/);
96-
if (match) {
98+
if (match && (hasWebGL2 || match[1] !== 'webgl2')) {
9799
window[symbols.ensureProxyContext]();
98100

99101
const canvas = this;
@@ -136,7 +138,11 @@ HTMLCanvasElement.prototype.getBoundingClientRect = function getBoundingClientRe
136138
return new DOMRect(canvasViewport[0], canvasViewport[1], canvasViewport[2], canvasViewport[3]);
137139
};
138140

139-
const [WebGLRenderingContext, WebGL2RenderingContext] = [self.WebGLRenderingContext, self.WebGL2RenderingContext].map(WebGLRenderingContext => {
141+
const [WebGLRenderingContext, WebGL2RenderingContext] = [window.WebGLRenderingContext, window.WebGL2RenderingContext].map(WebGLRenderingContext => {
142+
143+
if (!WebGLRenderingContext) {
144+
return WebGLRenderingContext;
145+
}
140146

141147
function ProxiedWebGLRenderingContext(canvas) {
142148
Object.defineProperty(this, 'canvas', { // Object.defineProperty to avoid proxying
@@ -151,13 +157,15 @@ function ProxiedWebGLRenderingContext(canvas) {
151157
clear: true,
152158
};
153159

154-
if (this.createVertexArray) {
155-
const vao = this.createVertexArray();
156-
this.bindVertexArray(vao);
157-
} else {
158-
const extension = this.getExtension('OES_vertex_array_object');
159-
const vao = extension.createVertexArrayOES();
160-
extension.bindVertexArrayOES(vao);
160+
if (hasWebGL2) {
161+
if (this.createVertexArray) {
162+
const vao = this.createVertexArray();
163+
this.bindVertexArray(vao);
164+
} else {
165+
const extension = this.getExtension('OES_vertex_array_object');
166+
const vao = extension.createVertexArrayOES();
167+
extension.bindVertexArrayOES(vao);
168+
}
161169
}
162170

163171
GlobalContext.contexts.push(this);
@@ -228,7 +236,11 @@ class OES_vertex_array_object {
228236
}
229237
ProxiedWebGLRenderingContext.prototype.getExtension = (_getExtension => function getExtension(name) {
230238
if (name === 'OES_vertex_array_object') {
231-
return new OES_vertex_array_object(this);
239+
if (hasWebGL2) {
240+
return new OES_vertex_array_object(this);
241+
} else {
242+
return GlobalContext.proxyContext.getExtension(name);
243+
}
232244
} else if ([
233245
'EXT_texture_filter_anisotropic',
234246
'WEBGL_debug_renderer_info',
@@ -259,7 +271,9 @@ ProxiedWebGLRenderingContext.prototype.setProxyState = function setProxyState()
259271
const {state} = this;
260272
const gl = GlobalContext.proxyContext;
261273

262-
gl.bindVertexArray(state.vao);
274+
if (hasWebGL2) {
275+
gl.bindVertexArray(state.vao);
276+
}
263277

264278
gl.bindBuffer(gl.ARRAY_BUFFER, state.arrayBuffer);
265279
for (const k in state.renderbuffer) {
@@ -570,7 +584,7 @@ ProxiedWebGLRenderingContext.prototype.deleteTexture = (_deleteTexture => functi
570584
})(ProxiedWebGLRenderingContext.prototype.deleteTexture);
571585

572586
// WebGL1 -> WebGL2 translations
573-
if (WebGLRenderingContext.name === 'WebGLRenderingContext') {
587+
if (hasWebGL2 && WebGLRenderingContext.name === 'WebGLRenderingContext') {
574588
const glslVersion = '300 es';
575589
ProxiedWebGLRenderingContext.prototype.createShader = (_createShader => function createShader(type) {
576590
const result = _createShader.call(this, type);

src/xr-scene.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class XRScene extends HTMLElement {
6666
GlobalContext.xrState.canvasViewport[2] = width;
6767
GlobalContext.xrState.canvasViewport[3] = height;
6868
});
69-
win.ctx = win.canvas.getContext('webgl2', {
69+
win.ctx = win.canvas.getContext(window.WebGL2RenderingContext ? 'webgl2' : 'webgl', {
7070
antialias: true,
7171
alpha: true,
7272
xrCompatible: true,

0 commit comments

Comments
 (0)