From 22baf4e1d9093ba2d9e1c259088262437c1faee8 Mon Sep 17 00:00:00 2001 From: mathis Date: Tue, 24 Oct 2023 23:37:25 +0200 Subject: [PATCH] fallback to webgl1 --- client/src/gl/global.ts | 2 +- client/src/gl/renderer.ts | 4 ++-- client/src/gl/shader.ts | 12 +++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/client/src/gl/global.ts b/client/src/gl/global.ts index b94dde5..c4846ef 100644 --- a/client/src/gl/global.ts +++ b/client/src/gl/global.ts @@ -7,7 +7,7 @@ export interface Context { viewport: Viewport } -export let gl: WebGL2RenderingContext +export let gl: WebGL2RenderingContext | WebGLRenderingContext export let shader: Shader export let renderer: Renderer export let viewport: Viewport diff --git a/client/src/gl/renderer.ts b/client/src/gl/renderer.ts index 4db42cc..955dd39 100644 --- a/client/src/gl/renderer.ts +++ b/client/src/gl/renderer.ts @@ -5,12 +5,12 @@ import { TW_VERT, TW_FRAG } from './shaders' import { mat4 } from 'gl-matrix' export class Renderer { - gl: WebGL2RenderingContext + gl: WebGL2RenderingContext | WebGLRenderingContext shader: Shader proj: mat4 constructor(canvas: HTMLCanvasElement) { - this.gl = canvas.getContext('webgl2', { antialias: false })! + this.gl = canvas.getContext('webgl', { antialias: false })! this.proj = mat4.create() diff --git a/client/src/gl/shader.ts b/client/src/gl/shader.ts index 92933a3..5eb65aa 100644 --- a/client/src/gl/shader.ts +++ b/client/src/gl/shader.ts @@ -14,13 +14,15 @@ type ProgLocations = { unifs: { [k in typeof UniformLocations[number]]: WebGLUniformLocation | null } } +type WebGLCtx = WebGL2RenderingContext | WebGLRenderingContext + export class Shader { prog: WebGLProgram vert: WebGLShader frag: WebGLShader locs: ProgLocations - constructor(gl: WebGL2RenderingContext, vertSrc: string, fragSrc: string) { + constructor(gl: WebGLCtx, vertSrc: string, fragSrc: string) { this.prog = gl.createProgram()! this.vert = Shader.makeShader(gl, vertSrc, gl.VERTEX_SHADER) this.frag = Shader.makeShader(gl, fragSrc, gl.FRAGMENT_SHADER) @@ -34,7 +36,7 @@ export class Shader { this.initUniforms(gl) } - private initLocations(gl: WebGL2RenderingContext) { + private initLocations(gl: WebGLCtx) { const locs: ProgLocations = { attrs: { aPosition: gl.getAttribLocation(this.prog, 'aPosition'), @@ -53,17 +55,17 @@ export class Shader { return locs } - private initAttributes(gl: WebGL2RenderingContext) { + private initAttributes(gl: WebGLCtx) { gl.enableVertexAttribArray(this.locs.attrs.aPosition) } - private initUniforms(gl: WebGL2RenderingContext) { + private initUniforms(gl: WebGLCtx) { gl.uniform4fv(this.locs.unifs.uColorMask, [1.0, 1.0, 1.0, 1.0]) gl.uniform1i(this.locs.unifs.uTexCoord, 0) gl.uniform1i(this.locs.unifs.uVertexColor, 0) } - private static makeShader(gl: WebGL2RenderingContext, src: string, typ: number) { + private static makeShader(gl: WebGLCtx, src: string, typ: number) { const shader = gl.createShader(typ)! gl.shaderSource(shader, src) gl.compileShader(shader)