Loading src/gl-program.ts +10 −93 Changes for src/gl-program.ts: 10 added lines, 93 removed lines. Original line number Diff line number Diff line Loading @@ -3,9 +3,7 @@ import rlghtFrgSource from './shaders/relight.frag'; import lghtbllVtxSource from './shaders/lightball.vert'; import lghtbllFrgSource from './shaders/lightball.frag'; import exportRectVtxSource from './shaders/exportRect.vert'; import exportCornerVtxSource from './shaders/exportCorner.vert'; import exportRectFrgSource from './shaders/exportRect.frag'; import exportCornerFrgSource from './shaders/exportCorner.frag'; import { mat4, vec2, vec3 } from 'gl-matrix'; import { Controls, CONTROLS_SET_UNIFORMS, State } from './control.ts'; import { DEBUG, Exported, GL } from './utils.ts'; Loading Loading @@ -404,21 +402,13 @@ export class ExportRectProgram extends GLProgramBase< static attributes = { vertexPosition: GLProgramBase.newAttribute('aVertexPosition', 2, WebGLRenderingContext.FLOAT), }; static commonUniformNames = { static uniformNames = { color: 'uColor', exportRect: 'uExportRect', lineWidth: 'uLineWidth', projectionMatrix: 'uProjectionMatrix', }; static uniformNames = { ...ExportRectProgram.commonUniformNames, renderRect: 'uRenderRect', }; static cornerUniformNames = { ...ExportRectProgram.commonUniformNames, cornerCenter: 'uCornerCenter', cornerRadius: 'uCornerRadius', }; constructor(gl: WebGLRenderingContext) { super( Loading @@ -437,33 +427,6 @@ export class ExportRectProgram extends GLProgramBase< gl.clearColor(0.0, 0.0, 0.0, 0.0); // Clear to transparent } private static setCommonParams( gl: WebGLRenderingContext, data: RenderData, uniforms: { [key: string]: WebGLUniformLocation }, ): typeof ExportRectProgram.PARAMS { let { cornerRadius, color, lineSize } = ExportRectProgram.PARAMS; cornerRadius /= Math.min(1, data.viewport.zoom); lineSize /= Math.min(1, data.viewport.zoom); gl.uniformMatrix4fv(uniforms.projectionMatrix, false, data.viewport.projectionMat); for (const [name, value] of [ ['exportRect', [...data.exportParams!.offset, ...data.exportParams!.size]], ['renderRect', [0, 0, ...data.renderArea]], ['lineWidth', [lineSize]], ['cornerRadius', [cornerRadius]], ['color', color], ] as [string, number[]][]) { const location = uniforms[name]; if (location) { const func = [gl.uniform1fv, gl.uniform2fv, gl.uniform3fv, gl.uniform4fv][value.length - 1]; func.call(gl, location, value); } } return { cornerRadius, color, lineSize }; } public render(data: RenderData): string { if (!data.exportParams) throw new Error('ExportRectProgram called with no export params'); const gl = this.gl; Loading @@ -472,7 +435,15 @@ export class ExportRectProgram extends GLProgramBase< gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); gl.useProgram(this.program); let { color, lineSize } = ExportRectProgram.setCommonParams(gl, data, this.uniformLocations); let { color, lineSize } = ExportRectProgram.PARAMS; lineSize /= Math.min(1, data.viewport.zoom); gl.uniformMatrix4fv(this.uniformLocations.projectionMatrix, false, data.viewport.projectionMat); gl.uniform4f(this.uniformLocations.exportRect, ...data.exportParams!.offset, ...data.exportParams!.size); gl.uniform4f(this.uniformLocations.renderRect, 0, 0, ...data.renderArea); gl.uniform1f(this.uniformLocations.lineWidth, lineSize); gl.uniform4fv(this.uniformLocations.color, color); const area = new Rect(0, 0, ...data.renderArea).grow(lineSize); this.setAttribute('vertexPosition'); Loading @@ -484,64 +455,10 @@ export class ExportRectProgram extends GLProgramBase< ` exportRect: ${new Rect(...data.exportParams.offset, ...data.exportParams.size)}\n` + ` renderRect: ${new Rect(0, 0, ...data.renderArea)}\n`; output += this.cornerProgram.render(data); return output; } private cornerProgram = new (class extends GLProgramBase< [keyof typeof ExportRectProgram.attributes], [keyof typeof ExportRectProgram.cornerUniformNames] > { static attributes = ExportRectProgram.attributes; constructor(rect: ExportRectProgram) { super( rect.gl, exportCornerVtxSource, exportCornerFrgSource, ExportRectProgram.attributes, ExportRectProgram.cornerUniformNames, ); this.buffers = { vertexPosition: super.initF32Buffer(this.gl, [0, 0, 1, 0, 0, 1, 1, 1]), }; } public render(data: RenderData): string { if (!data.exportParams) throw new Error('ExportCornerProgram called with no export params'); const gl = this.gl; let output = '== ExportCornerProgram ==\n'; gl.useProgram(this.program); let { cornerRadius, lineSize } = ExportRectProgram.setCommonParams(gl, data, this.uniformLocations); const minX = data.exportParams.offset[0] - lineSize / 2, minY = data.exportParams.offset[1] - lineSize / 2, width = data.exportParams.size[0] + lineSize, height = data.exportParams.size[1] + lineSize, cornerArea = (cornerRadius + lineSize) * 2 + 1; this.setAttribute('vertexPosition'); for (const corner of [ [0, 0], [1, 0], [0, 1], [1, 1], ]) { const cornerX = minX + width * corner[0], cornerY = minY + height * corner[1]; const area = new Rect(cornerX - cornerArea / 2, cornerY - cornerArea / 2, cornerArea, cornerArea); gl.uniform2f(this.uniformLocations.cornerCenter, cornerX, cornerY); this.gl.bufferData(GL.ARRAY_BUFFER, area.vertexPositions(), GL.DYNAMIC_DRAW); gl.drawArrays(GL.TRIANGLE_STRIP, 0, 4); } output += `Corner radius: ${cornerRadius}\n`; return output; } })(this); } export function resizeCanvasToDisplaySize(gl: WebGLRenderingContext, canvasSize?: [number, number]) { if (gl.canvas instanceof OffscreenCanvas) return false; // Lookup the size the browser is displaying the canvas in CSS pixels. Loading src/shaders/exportCorner.fragdeleted 100644 → 0 +0 −28 Changes for src/shaders/exportCorner.frag: 0 added lines, 28 removed lines. Original line number Diff line number Diff line precision mediump float; uniform mediump vec4 uColor; uniform mediump vec2 uCornerCenter; uniform mediump float uCornerRadius; uniform mediump vec4 uExportRect; uniform mediump float uLineWidth; varying mediump vec2 vVertexCoord; bool inRect(vec2 pos, vec4 rect) { return pos.x > rect.x && pos.y > rect.y && pos.x < rect.x + rect.z && pos.y < rect.y + rect.w; } void main(void) { float cornerDistance = distance(vVertexCoord, uCornerCenter); bool inCorner = bool(cornerDistance <= uCornerRadius);// Redundant constructor because of an IDE issue bool onBorder = inCorner && (uCornerRadius - cornerDistance <= uLineWidth); if (inCorner && !inRect(vVertexCoord, uExportRect)) { vec4 col = uColor; if (!onBorder) { col.a *= .5; } gl_FragColor = col; } else { discard; } } No newline at end of file src/shaders/exportCorner.vertdeleted 100644 → 0 +0 −10 Changes for src/shaders/exportCorner.vert: 0 added lines, 10 removed lines. Original line number Diff line number Diff line uniform mat4 uProjectionMatrix; attribute vec4 aVertexPosition; varying mediump vec2 vVertexCoord; void main(void) { gl_Position = uProjectionMatrix * aVertexPosition; vVertexCoord = aVertexPosition.xy; } No newline at end of file Loading
src/gl-program.ts +10 −93 Changes for src/gl-program.ts: 10 added lines, 93 removed lines. Original line number Diff line number Diff line Loading @@ -3,9 +3,7 @@ import rlghtFrgSource from './shaders/relight.frag'; import lghtbllVtxSource from './shaders/lightball.vert'; import lghtbllFrgSource from './shaders/lightball.frag'; import exportRectVtxSource from './shaders/exportRect.vert'; import exportCornerVtxSource from './shaders/exportCorner.vert'; import exportRectFrgSource from './shaders/exportRect.frag'; import exportCornerFrgSource from './shaders/exportCorner.frag'; import { mat4, vec2, vec3 } from 'gl-matrix'; import { Controls, CONTROLS_SET_UNIFORMS, State } from './control.ts'; import { DEBUG, Exported, GL } from './utils.ts'; Loading Loading @@ -404,21 +402,13 @@ export class ExportRectProgram extends GLProgramBase< static attributes = { vertexPosition: GLProgramBase.newAttribute('aVertexPosition', 2, WebGLRenderingContext.FLOAT), }; static commonUniformNames = { static uniformNames = { color: 'uColor', exportRect: 'uExportRect', lineWidth: 'uLineWidth', projectionMatrix: 'uProjectionMatrix', }; static uniformNames = { ...ExportRectProgram.commonUniformNames, renderRect: 'uRenderRect', }; static cornerUniformNames = { ...ExportRectProgram.commonUniformNames, cornerCenter: 'uCornerCenter', cornerRadius: 'uCornerRadius', }; constructor(gl: WebGLRenderingContext) { super( Loading @@ -437,33 +427,6 @@ export class ExportRectProgram extends GLProgramBase< gl.clearColor(0.0, 0.0, 0.0, 0.0); // Clear to transparent } private static setCommonParams( gl: WebGLRenderingContext, data: RenderData, uniforms: { [key: string]: WebGLUniformLocation }, ): typeof ExportRectProgram.PARAMS { let { cornerRadius, color, lineSize } = ExportRectProgram.PARAMS; cornerRadius /= Math.min(1, data.viewport.zoom); lineSize /= Math.min(1, data.viewport.zoom); gl.uniformMatrix4fv(uniforms.projectionMatrix, false, data.viewport.projectionMat); for (const [name, value] of [ ['exportRect', [...data.exportParams!.offset, ...data.exportParams!.size]], ['renderRect', [0, 0, ...data.renderArea]], ['lineWidth', [lineSize]], ['cornerRadius', [cornerRadius]], ['color', color], ] as [string, number[]][]) { const location = uniforms[name]; if (location) { const func = [gl.uniform1fv, gl.uniform2fv, gl.uniform3fv, gl.uniform4fv][value.length - 1]; func.call(gl, location, value); } } return { cornerRadius, color, lineSize }; } public render(data: RenderData): string { if (!data.exportParams) throw new Error('ExportRectProgram called with no export params'); const gl = this.gl; Loading @@ -472,7 +435,15 @@ export class ExportRectProgram extends GLProgramBase< gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); gl.useProgram(this.program); let { color, lineSize } = ExportRectProgram.setCommonParams(gl, data, this.uniformLocations); let { color, lineSize } = ExportRectProgram.PARAMS; lineSize /= Math.min(1, data.viewport.zoom); gl.uniformMatrix4fv(this.uniformLocations.projectionMatrix, false, data.viewport.projectionMat); gl.uniform4f(this.uniformLocations.exportRect, ...data.exportParams!.offset, ...data.exportParams!.size); gl.uniform4f(this.uniformLocations.renderRect, 0, 0, ...data.renderArea); gl.uniform1f(this.uniformLocations.lineWidth, lineSize); gl.uniform4fv(this.uniformLocations.color, color); const area = new Rect(0, 0, ...data.renderArea).grow(lineSize); this.setAttribute('vertexPosition'); Loading @@ -484,64 +455,10 @@ export class ExportRectProgram extends GLProgramBase< ` exportRect: ${new Rect(...data.exportParams.offset, ...data.exportParams.size)}\n` + ` renderRect: ${new Rect(0, 0, ...data.renderArea)}\n`; output += this.cornerProgram.render(data); return output; } private cornerProgram = new (class extends GLProgramBase< [keyof typeof ExportRectProgram.attributes], [keyof typeof ExportRectProgram.cornerUniformNames] > { static attributes = ExportRectProgram.attributes; constructor(rect: ExportRectProgram) { super( rect.gl, exportCornerVtxSource, exportCornerFrgSource, ExportRectProgram.attributes, ExportRectProgram.cornerUniformNames, ); this.buffers = { vertexPosition: super.initF32Buffer(this.gl, [0, 0, 1, 0, 0, 1, 1, 1]), }; } public render(data: RenderData): string { if (!data.exportParams) throw new Error('ExportCornerProgram called with no export params'); const gl = this.gl; let output = '== ExportCornerProgram ==\n'; gl.useProgram(this.program); let { cornerRadius, lineSize } = ExportRectProgram.setCommonParams(gl, data, this.uniformLocations); const minX = data.exportParams.offset[0] - lineSize / 2, minY = data.exportParams.offset[1] - lineSize / 2, width = data.exportParams.size[0] + lineSize, height = data.exportParams.size[1] + lineSize, cornerArea = (cornerRadius + lineSize) * 2 + 1; this.setAttribute('vertexPosition'); for (const corner of [ [0, 0], [1, 0], [0, 1], [1, 1], ]) { const cornerX = minX + width * corner[0], cornerY = minY + height * corner[1]; const area = new Rect(cornerX - cornerArea / 2, cornerY - cornerArea / 2, cornerArea, cornerArea); gl.uniform2f(this.uniformLocations.cornerCenter, cornerX, cornerY); this.gl.bufferData(GL.ARRAY_BUFFER, area.vertexPositions(), GL.DYNAMIC_DRAW); gl.drawArrays(GL.TRIANGLE_STRIP, 0, 4); } output += `Corner radius: ${cornerRadius}\n`; return output; } })(this); } export function resizeCanvasToDisplaySize(gl: WebGLRenderingContext, canvasSize?: [number, number]) { if (gl.canvas instanceof OffscreenCanvas) return false; // Lookup the size the browser is displaying the canvas in CSS pixels. Loading
src/shaders/exportCorner.fragdeleted 100644 → 0 +0 −28 Changes for src/shaders/exportCorner.frag: 0 added lines, 28 removed lines. Original line number Diff line number Diff line precision mediump float; uniform mediump vec4 uColor; uniform mediump vec2 uCornerCenter; uniform mediump float uCornerRadius; uniform mediump vec4 uExportRect; uniform mediump float uLineWidth; varying mediump vec2 vVertexCoord; bool inRect(vec2 pos, vec4 rect) { return pos.x > rect.x && pos.y > rect.y && pos.x < rect.x + rect.z && pos.y < rect.y + rect.w; } void main(void) { float cornerDistance = distance(vVertexCoord, uCornerCenter); bool inCorner = bool(cornerDistance <= uCornerRadius);// Redundant constructor because of an IDE issue bool onBorder = inCorner && (uCornerRadius - cornerDistance <= uLineWidth); if (inCorner && !inRect(vVertexCoord, uExportRect)) { vec4 col = uColor; if (!onBorder) { col.a *= .5; } gl_FragColor = col; } else { discard; } } No newline at end of file
src/shaders/exportCorner.vertdeleted 100644 → 0 +0 −10 Changes for src/shaders/exportCorner.vert: 0 added lines, 10 removed lines. Original line number Diff line number Diff line uniform mat4 uProjectionMatrix; attribute vec4 aVertexPosition; varying mediump vec2 vVertexCoord; void main(void) { gl_Position = uProjectionMatrix * aVertexPosition; vVertexCoord = aVertexPosition.xy; } No newline at end of file