Something went wrong. Try again.
Minecraft text rendering inside the browser! www.npmjs.com/package/libmojangles
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749import type { Renderer, RenderContext, TextMeshGroup, TextMesh, RenderState, ShaderProgram, Texture, Mat4, ComponentUniforms,} from "../types";import { WebGLShaderManager, getPickingProgramId, getTFProgramId } from "./webgl-shader";import { WebGLTextureManager } from "./webgl-texture";
const FLOATS_PER_VERTEX = 12;const PICKING_FLOATS_PER_VERTEX = 16;
function createOrthographicMatrix( left: number, right: number, bottom: number, top: number, near: number, far: number): Mat4 { const out = new Float32Array(16); const lr = 1 / (left - right); const bt = 1 / (bottom - top); const nf = 1 / (near - far);
out[0] = -2 * lr; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = -2 * bt; out[6] = 0; out[7] = 0; out[8] = 0; out[9] = 0; out[10] = 2 * nf; out[11] = 0; out[12] = (left + right) * lr; out[13] = (top + bottom) * bt; out[14] = (far + near) * nf; out[15] = 1;
return out;}
function createIdentityMatrix(): Mat4 { const out = new Float32Array(16); out[0] = 1; out[5] = 1; out[10] = 1; out[15] = 1; return out;}
class WebGLRenderContext implements RenderContext { private currentProgramId: string | null = null; private currentProgram: WebGLProgram | null = null; private currentUniforms: Map<string, WebGLUniformLocation> | null = null;
constructor( private gl: WebGL2RenderingContext, private renderer: WebGLRenderer, private vao: WebGLVertexArrayObject, private vbo: WebGLBuffer, private ebo: WebGLBuffer ) {}
private get shaders(): WebGLShaderManager | null { return this.renderer.shaders ?? null; }
private get textures(): WebGLTextureManager | null { return this.renderer.textures ?? null; }
get width(): number { return this.gl.drawingBufferWidth; }
get height(): number { return this.gl.drawingBufferHeight; }
setProgram(program: ShaderProgram): void { const compiled = this.shaders?.getCompiledProgram(program.id); if (!compiled) return;
this.gl.useProgram(compiled.program); this.currentProgramId = program.id; this.currentProgram = compiled.program; this.currentUniforms = compiled.uniforms; }
setTexture(slot: number, texture: Texture): void { this.gl.activeTexture(this.gl.TEXTURE0 + slot); const glTexture = (texture as unknown as { getGLTexture(): WebGLTexture }).getGLTexture(); this.gl.bindTexture(this.gl.TEXTURE_2D, glTexture); }
setUniform(name: string, value: unknown): void { if (!this.currentUniforms) return;
const aliases: string[] = [name]; if (name.startsWith("u_")) aliases.push(name.slice(2)); if (name === "u_projection") aliases.push("ProjMat"); if (name === "u_modelView") aliases.push("ModelViewMat"); if (name === "u_colorModulator") aliases.push("ColorModulator"); if (name === "u_texture") aliases.push("Sampler0");
let loc: WebGLUniformLocation | undefined; for (const alias of aliases) { loc = this.currentUniforms.get(alias); if (loc) break; } if (!loc) return;
if (typeof value === "number") { this.gl.uniform1f(loc, value); } else if (Array.isArray(value)) { switch (value.length) { case 2: this.gl.uniform2f(loc, value[0] ?? 0, value[1] ?? 0); break; case 3: this.gl.uniform3f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0); break; case 4: this.gl.uniform4f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0); break; } } else if (value instanceof Float32Array) { if (value.length === 2) { this.gl.uniform2f(loc, value[0] ?? 0, value[1] ?? 0); } else if (value.length === 3) { this.gl.uniform3f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0); } else if (value.length === 4) { this.gl.uniform4f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0); } else if (value.length === 16) { this.gl.uniformMatrix4fv(loc, false, value); } } }
draw(mesh: TextMeshGroup, componentUniforms?: Record<string, ComponentUniforms>): void { this.gl.bindVertexArray(this.vao);
for (const submesh of mesh.meshes) { if (componentUniforms && submesh.componentId) { const uniforms = componentUniforms[submesh.componentId]; if (uniforms) { for (const [name, value] of Object.entries(uniforms)) { this.setUniform(name, value); } } }
if (this.textures) { const texture = this.textures.getTextureByKey(submesh.textureId); if (texture) { this.gl.activeTexture(this.gl.TEXTURE0); this.gl.bindTexture( this.gl.TEXTURE_2D, (texture as unknown as { getGLTexture(): WebGLTexture }).getGLTexture() ); const texLoc = this.currentUniforms?.get("Sampler0"); if (texLoc) this.gl.uniform1i(texLoc, 0); } }
const sampler2Loc = this.currentUniforms?.get("Sampler2"); if (sampler2Loc && this.textures) { const whiteTexture = this.textures.getTextureByKey("__white__"); if (whiteTexture) { this.gl.activeTexture(this.gl.TEXTURE2); this.gl.bindTexture( this.gl.TEXTURE_2D, (whiteTexture as unknown as { getGLTexture(): WebGLTexture }).getGLTexture() ); this.gl.uniform1i(sampler2Loc, 2); } }
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vbo); this.gl.bufferData(this.gl.ARRAY_BUFFER, submesh.vertices, this.gl.DYNAMIC_DRAW);
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.ebo); this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, submesh.indices, this.gl.DYNAMIC_DRAW);
this.gl.drawElements(this.gl.TRIANGLES, submesh.indexCount, this.gl.UNSIGNED_SHORT, 0); }
this.gl.bindVertexArray(null); }
flush(): void { this.currentProgramId = null; this.currentProgram = null; this.currentUniforms = null; }}
export class WebGLRenderer implements Renderer { readonly context: RenderContext; private gl: WebGL2RenderingContext; shaders: WebGLShaderManager | null = null; textures: WebGLTextureManager | null = null;
private vao: WebGLVertexArrayObject | null = null; private pickingVao: WebGLVertexArrayObject | null = null; private vbo: WebGLBuffer | null = null; private ebo: WebGLBuffer | null = null;
private pickingFramebuffer: WebGLFramebuffer | null = null; private pickingTexture: WebGLTexture | null = null; private pickingRenderbuffer: WebGLRenderbuffer | null = null; private pickingWidth = 0; private pickingHeight = 0;
private pickingCache: Uint8Array | null = null; private enablePickingCache = false;
private tfFeedback: WebGLTransformFeedback | null = null; private tfBuffer: WebGLBuffer | null = null; private tfBBoxResult: Map<number, { minX: number; minY: number; maxX: number; maxY: number }> | null = null;
private projectionMatrix: Mat4 = createIdentityMatrix(); private modelViewMatrix: Mat4 = createIdentityMatrix();
constructor(canvas: HTMLCanvasElement) { const gl = canvas.getContext("webgl2", { alpha: true, premultipliedAlpha: false, antialias: false, depth: true, stencil: false, preserveDrawingBuffer: true, }); if (!gl) { throw new Error("WebGL2 not supported"); } this.gl = gl;
this.initBuffers(); this.shaders = new WebGLShaderManager(gl); this.textures = new WebGLTextureManager(gl, null!);
this.context = new WebGLRenderContext( gl, this, this.vao!, this.vbo!, this.ebo! ); }
setManagers(shaders: WebGLShaderManager, textures: WebGLTextureManager): void { this.shaders = shaders; this.textures = textures; }
getShaderManager(): WebGLShaderManager | null { return this.shaders; }
getTextureManager(): WebGLTextureManager | null { return this.textures; }
private initBuffers(): void { const gl = this.gl;
this.vao = gl.createVertexArray(); this.pickingVao = gl.createVertexArray(); this.vbo = gl.createBuffer(); this.ebo = gl.createBuffer();
gl.bindVertexArray(this.vao); gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.ebo);
const stride = FLOATS_PER_VERTEX * 4;
gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 3, gl.FLOAT, false, stride, 0);
gl.enableVertexAttribArray(1); gl.vertexAttribPointer(1, 4, gl.FLOAT, false, stride, 12);
gl.enableVertexAttribArray(2); gl.vertexAttribPointer(2, 2, gl.FLOAT, false, stride, 28);
gl.enableVertexAttribArray(3); gl.vertexAttribPointer(3, 2, gl.FLOAT, false, stride, 36);
gl.bindVertexArray(this.pickingVao); gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.ebo);
const pickingStride = PICKING_FLOATS_PER_VERTEX * 4;
gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 3, gl.FLOAT, false, pickingStride, 0);
gl.enableVertexAttribArray(1); gl.vertexAttribPointer(1, 4, gl.FLOAT, false, pickingStride, 12);
gl.enableVertexAttribArray(2); gl.vertexAttribPointer(2, 2, gl.FLOAT, false, pickingStride, 28);
gl.enableVertexAttribArray(3); gl.vertexAttribPointer(3, 2, gl.FLOAT, false, pickingStride, 36);
gl.enableVertexAttribArray(4); gl.vertexAttribPointer(4, 4, gl.FLOAT, false, pickingStride, 48);
gl.bindVertexArray(null);
this.tfFeedback = gl.createTransformFeedback(); this.tfBuffer = gl.createBuffer();
}
private initPickingFramebuffer(width: number, height: number): void { if (this.pickingWidth === width && this.pickingHeight === height) { return; }
const gl = this.gl;
if (this.pickingFramebuffer) { gl.deleteFramebuffer(this.pickingFramebuffer); gl.deleteTexture(this.pickingTexture); gl.deleteRenderbuffer(this.pickingRenderbuffer); }
this.pickingFramebuffer = gl.createFramebuffer(); this.pickingTexture = gl.createTexture(); this.pickingRenderbuffer = gl.createRenderbuffer();
gl.bindTexture(gl.TEXTURE_2D, this.pickingTexture); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.bindRenderbuffer(gl.RENDERBUFFER, this.pickingRenderbuffer); gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.pickingFramebuffer); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.pickingTexture, 0); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.pickingRenderbuffer);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
this.pickingWidth = width; this.pickingHeight = height; }
beginFrame(): void { const gl = this.gl; gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.DEPTH_TEST); gl.depthFunc(gl.LEQUAL); }
endFrame(): void {}
drawText(mesh: TextMeshGroup, state: Partial<RenderState>): void { const ctx = this.context as WebGLRenderContext; ctx.flush();
const programId = state.programId ?? "text"; ctx.setProgram({ id: programId } as ShaderProgram);
const projection = state.projectionMatrix ?? this.projectionMatrix; const modelView = state.modelViewMatrix ?? this.modelViewMatrix; const colorMod = state.colorModulator ?? { r: 1, g: 1, b: 1, a: 1 };
ctx.setUniform("u_projection", projection); ctx.setUniform("u_modelView", modelView); ctx.setUniform("u_colorModulator", new Float32Array([colorMod.r, colorMod.g, colorMod.b, colorMod.a]));
this.setGlobalsUniforms(ctx, state); this.setFogUniforms(ctx, state);
ctx.draw(mesh, state.componentUniforms); }
private setGlobalsUniforms(ctx: RenderContext, state: Partial<RenderState>): void { const screenSize = state.screenSize ?? [this.gl.drawingBufferWidth, this.gl.drawingBufferHeight]; ctx.setUniform("ScreenSize", new Float32Array(screenSize)); }
private setFogUniforms(ctx: RenderContext, state: Partial<RenderState>): void { const fogStart = state.fogStart ?? 1e9; const fogEnd = state.fogEnd ?? 2e9; const fogColor = state.fogColor ?? { r: 0, g: 0, b: 0, a: 0 };
ctx.setUniform("FogEnvironmentalStart", fogStart); ctx.setUniform("FogEnvironmentalEnd", fogEnd); ctx.setUniform("FogRenderDistanceStart", fogStart); ctx.setUniform("FogRenderDistanceEnd", fogEnd); ctx.setUniform("FogSkyEnd", fogEnd); ctx.setUniform("FogCloudsEnd", fogEnd); ctx.setUniform("FogColor", new Float32Array([fogColor.r, fogColor.g, fogColor.b, fogColor.a])); }
drawTextWithPicking(mesh: TextMeshGroup, state: Partial<RenderState>): void { this.drawText(mesh, state);
if (!mesh.pickingMeshes || mesh.pickingMeshes.length === 0 || !this.shaders) return;
if (state.transformFeedback) { this.tfBBoxResult = null; this.computeBBoxesWithTF(mesh, state); }
const needsPickingFramebuffer = !state.transformFeedback || state.cachePicking; if (!needsPickingFramebuffer) return;
const gl = this.gl;
// Use program-specific picking shader if available, otherwise fall back to default const programId = state.programId ?? "text"; const pickingProgramId = getPickingProgramId(programId); let compiled = this.shaders.getCompiledProgram(pickingProgramId); if (!compiled) { compiled = this.shaders.getCompiledProgram("picking"); } if (!compiled) return;
const shouldCache = (state.cachePicking ?? false) && !state.transformFeedback; if (shouldCache) { this.enablePickingCache = true; this.pickingCache = new Uint8Array(this.context.width * this.context.height * 4); } else { this.enablePickingCache = false; this.pickingCache = null; }
this.initPickingFramebuffer(this.context.width, this.context.height);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.pickingFramebuffer); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(compiled.program);
const projection = state.projectionMatrix ?? this.projectionMatrix; const modelView = state.modelViewMatrix ?? this.modelViewMatrix;
const projLoc = compiled.uniforms.get("u_projection") ?? compiled.uniforms.get("ProjMat"); const mvLoc = compiled.uniforms.get("u_modelView") ?? compiled.uniforms.get("ModelViewMat"); if (projLoc) gl.uniformMatrix4fv(projLoc, false, projection); if (mvLoc) gl.uniformMatrix4fv(mvLoc, false, modelView);
const screenSizeLoc = compiled.uniforms.get("ScreenSize"); if (screenSizeLoc) { const screenSize = state.screenSize ?? [this.context.width, this.context.height]; gl.uniform2f(screenSizeLoc, screenSize[0]!, screenSize[1]!); }
gl.bindVertexArray(this.pickingVao); for (const pickingMesh of mesh.pickingMeshes) { this.drawPickingMesh(pickingMesh, compiled, state.componentUniforms); } gl.bindVertexArray(null);
if (this.enablePickingCache && this.pickingCache) { gl.readPixels( 0, 0, this.context.width, this.context.height, gl.RGBA, gl.UNSIGNED_BYTE, this.pickingCache ); }
gl.bindFramebuffer(gl.FRAMEBUFFER, null); }
private drawPickingMesh(mesh: TextMesh, compiled: { program: WebGLProgram; uniforms: Map<string, WebGLUniformLocation> }, componentUniforms?: Record<string, ComponentUniforms>): void { const gl = this.gl; const isCustomPickingMesh = (mesh.floatsPerVertex ?? FLOATS_PER_VERTEX) === PICKING_FLOATS_PER_VERTEX;
// Bind white texture to Sampler2 for lightmap — required by Minecraft vertex shader const sampler2Loc = compiled.uniforms.get("Sampler2"); if (sampler2Loc) { const whiteTexture = this.textures?.getTextureByKey("__white__"); if (whiteTexture) { gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, (whiteTexture as unknown as { getGLTexture(): WebGLTexture }).getGLTexture()); gl.uniform1i(sampler2Loc, 2); } }
if (componentUniforms && mesh.componentId) { const uniforms = componentUniforms[mesh.componentId]; if (uniforms) { for (const [name, value] of Object.entries(uniforms)) { const loc = compiled.uniforms.get(name); if (!loc) continue; if (typeof value === "number") { gl.uniform1f(loc, value); } else if (Array.isArray(value)) { if (value.length === 2) gl.uniform2f(loc, value[0] ?? 0, value[1] ?? 0); else if (value.length === 3) gl.uniform3f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0); else if (value.length === 4) gl.uniform4f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0); } else if (value instanceof Float32Array) { if (value.length === 2) gl.uniform2f(loc, value[0] ?? 0, value[1] ?? 0); else if (value.length === 3) gl.uniform3f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0); else if (value.length === 4) gl.uniform4f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0); else if (value.length === 16) gl.uniformMatrix4fv(loc, false, value); } } } }
gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo); gl.bufferData(gl.ARRAY_BUFFER, mesh.vertices, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.ebo); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, mesh.indices, gl.DYNAMIC_DRAW);
if (!isCustomPickingMesh) { gl.disableVertexAttribArray(4); gl.vertexAttrib4f(4, 0, 0, 0, 0); }
gl.drawElements(gl.TRIANGLES, mesh.indexCount, gl.UNSIGNED_SHORT, 0);
if (!isCustomPickingMesh) { gl.enableVertexAttribArray(4); } }
private computeBBoxesWithTF(mesh: TextMeshGroup, state: Partial<RenderState>): void { if (!mesh.pickingMeshes || mesh.pickingMeshes.length === 0 || !this.shaders) return;
const gl = this.gl; const programId = state.programId ?? "text"; const tfProgramId = getTFProgramId(programId); const compiled = this.shaders.getCompiledProgram(tfProgramId); if (!compiled) return;
const viewportWidth = this.context.width; const viewportHeight = this.context.height; const projection = state.projectionMatrix ?? this.projectionMatrix; const modelView = state.modelViewMatrix ?? this.modelViewMatrix; const result = new Map<number, { minX: number; minY: number; maxX: number; maxY: number }>();
const FLOATS_PER_TF_VERTEX = 8; // gl_Position(4) + pickColor(4)
for (const pickingMesh of mesh.pickingMeshes) { const outputSize = pickingMesh.indexCount * FLOATS_PER_TF_VERTEX * 4;
gl.useProgram(compiled.program);
const projLoc = compiled.uniforms.get("u_projection") ?? compiled.uniforms.get("ProjMat"); const mvLoc = compiled.uniforms.get("u_modelView") ?? compiled.uniforms.get("ModelViewMat"); if (projLoc) gl.uniformMatrix4fv(projLoc, false, projection); if (mvLoc) gl.uniformMatrix4fv(mvLoc, false, modelView);
const screenSizeLoc = compiled.uniforms.get("ScreenSize"); if (screenSizeLoc) { const screenSize = state.screenSize ?? [viewportWidth, viewportHeight]; gl.uniform2f(screenSizeLoc, screenSize[0]!, screenSize[1]!); }
if (state.componentUniforms && pickingMesh.componentId) { const uniforms = state.componentUniforms[pickingMesh.componentId]; if (uniforms) { for (const [name, value] of Object.entries(uniforms)) { const loc = compiled.uniforms.get(name); if (!loc) continue; if (typeof value === "number") { gl.uniform1f(loc, value); } else if (Array.isArray(value)) { if (value.length === 2) gl.uniform2f(loc, value[0] ?? 0, value[1] ?? 0); else if (value.length === 3) gl.uniform3f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0); else if (value.length === 4) gl.uniform4f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0); } else if (value instanceof Float32Array) { if (value.length === 2) gl.uniform2f(loc, value[0] ?? 0, value[1] ?? 0); else if (value.length === 3) gl.uniform3f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0); else if (value.length === 4) gl.uniform4f(loc, value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0); else if (value.length === 16) gl.uniformMatrix4fv(loc, false, value); } } } }
const sampler2Loc = compiled.uniforms.get("Sampler2"); if (sampler2Loc && this.textures) { const whiteTexture = this.textures.getTextureByKey("__white__"); if (whiteTexture) { gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, (whiteTexture as unknown as { getGLTexture(): WebGLTexture }).getGLTexture()); gl.uniform1i(sampler2Loc, 2); } }
const floatsPerVert = pickingMesh.floatsPerVertex ?? PICKING_FLOATS_PER_VERTEX; const deindexed = new Float32Array(pickingMesh.indexCount * floatsPerVert); for (let i = 0; i < pickingMesh.indexCount; i++) { const srcIdx = pickingMesh.indices[i]! * floatsPerVert; deindexed.set(pickingMesh.vertices.subarray(srcIdx, srcIdx + floatsPerVert), i * floatsPerVert); }
gl.bindVertexArray(this.pickingVao);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo); gl.bufferData(gl.ARRAY_BUFFER, deindexed, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, this.tfBuffer); gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, outputSize, gl.DYNAMIC_READ);
gl.enable(gl.RASTERIZER_DISCARD); gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, this.tfFeedback); gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, this.tfBuffer);
gl.beginTransformFeedback(gl.TRIANGLES); gl.drawArrays(gl.TRIANGLES, 0, pickingMesh.indexCount); gl.endTransformFeedback();
gl.disable(gl.RASTERIZER_DISCARD); gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
const output = new Float32Array(pickingMesh.indexCount * FLOATS_PER_TF_VERTEX); gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, this.tfBuffer); gl.getBufferSubData(gl.TRANSFORM_FEEDBACK_BUFFER, 0, output);
gl.bindVertexArray(null);
for (let i = 0; i < output.length; i += FLOATS_PER_TF_VERTEX) { const clipX = output[i]!; const clipY = output[i + 1]!; const clipW = output[i + 3]!; if (clipW === 0) continue;
const ndcX = clipX / clipW; const ndcY = clipY / clipW; const screenX = (ndcX * 0.5 + 0.5) * viewportWidth; const screenY = (ndcY * 0.5 + 0.5) * viewportHeight;
const pickR = output[i + 4]!; const pickG = output[i + 5]!; const pickB = output[i + 6]!; const encoded = (Math.round(pickR * 255) << 16) | (Math.round(pickG * 255) << 8) | Math.round(pickB * 255); if (encoded === 0) continue; const sourceIndex = encoded - 1;
let bounds = result.get(sourceIndex); if (!bounds) { bounds = { minX: screenX, minY: screenY, maxX: screenX, maxY: screenY }; result.set(sourceIndex, bounds); } else { bounds.minX = Math.min(bounds.minX, screenX); bounds.minY = Math.min(bounds.minY, screenY); bounds.maxX = Math.max(bounds.maxX, screenX); bounds.maxY = Math.max(bounds.maxY, screenY); } } }
this.tfBBoxResult = result; }
getTFBBoxResult(): Map<number, { minX: number; minY: number; maxX: number; maxY: number }> | null { return this.tfBBoxResult; }
readPickingBuffer(x: number, y: number): Uint8Array { const gl = this.gl; const pixel = new Uint8Array(4);
if (this.enablePickingCache && this.pickingCache) { const cache = this.pickingCache; const flippedY = this.pickingHeight - y - 1; const index = (flippedY * this.context.width + x) * 4; if (index >= 0 && index + 3 < cache.length) { pixel[0] = cache[index] ?? 0; pixel[1] = cache[index + 1] ?? 0; pixel[2] = cache[index + 2] ?? 0; pixel[3] = cache[index + 3] ?? 0; } return pixel; }
if (!this.pickingFramebuffer) { return pixel; }
gl.bindFramebuffer(gl.FRAMEBUFFER, this.pickingFramebuffer); gl.readPixels(x, this.pickingHeight - y - 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel); gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return pixel; }
resize(width: number, height: number): void { this.gl.viewport(0, 0, width, height); const drawingWidth = this.gl.drawingBufferWidth; const drawingHeight = this.gl.drawingBufferHeight; this.projectionMatrix = createOrthographicMatrix(0, drawingWidth, drawingHeight, 0, -1000, 1000); }
getProjectionMatrix(): Mat4 { return this.projectionMatrix; }
getModelViewMatrix(): Mat4 { return this.modelViewMatrix; }
getPickingCache(): Uint8Array | null { return this.enablePickingCache && this.pickingCache ? this.pickingCache : null; }
getPickingSize(): { width: number; height: number } { return { width: this.pickingWidth, height: this.pickingHeight }; }
dispose(): void { const gl = this.gl;
if (this.vao) gl.deleteVertexArray(this.vao); if (this.pickingVao) gl.deleteVertexArray(this.pickingVao); if (this.vbo) gl.deleteBuffer(this.vbo); if (this.ebo) gl.deleteBuffer(this.ebo); if (this.pickingFramebuffer) gl.deleteFramebuffer(this.pickingFramebuffer); if (this.pickingTexture) gl.deleteTexture(this.pickingTexture); if (this.pickingRenderbuffer) gl.deleteRenderbuffer(this.pickingRenderbuffer); if (this.tfFeedback) gl.deleteTransformFeedback(this.tfFeedback); if (this.tfBuffer) gl.deleteBuffer(this.tfBuffer); }}