1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| class FrameBufferManager { private gl: WebGL2RenderingContext; private frameBuffers: Map<string, FrameBufferObject> = new Map(); interface FrameBufferObject { fbo: WebGLFramebuffer; colorTextures: WebGLTexture[]; depthTexture?: WebGLTexture; depthRenderbuffer?: WebGLRenderbuffer; width: number; height: number; format: TextureFormat; msaa: boolean; mipLevels: number; } interface TextureFormat { internalFormat: number; format: number; type: number; name: string; } private static readonly FORMATS = { RGBA8: { internalFormat: WebGL2RenderingContext.RGBA8, format: WebGL2RenderingContext.RGBA, type: WebGL2RenderingContext.UNSIGNED_BYTE, name: 'RGBA8' }, RGBA16F: { internalFormat: WebGL2RenderingContext.RGBA16F, format: WebGL2RenderingContext.RGBA, type: WebGL2RenderingContext.HALF_FLOAT, name: 'RGBA16F' }, RGBA32F: { internalFormat: WebGL2RenderingContext.RGBA32F, format: WebGL2RenderingContext.RGBA, type: WebGL2RenderingContext.FLOAT, name: 'RGBA32F' }, DEPTH24_STENCIL8: { internalFormat: WebGL2RenderingContext.DEPTH24_STENCIL8, format: WebGL2RenderingContext.DEPTH_STENCIL, type: WebGL2RenderingContext.UNSIGNED_INT_24_8, name: 'DEPTH24_STENCIL8' } }; public createFrameBuffer( name: string, width: number, height: number, config: { colorCount?: number; colorFormat?: keyof typeof FrameBufferManager.FORMATS; hasDepth?: boolean; depthFormat?: keyof typeof FrameBufferManager.FORMATS; msaa?: boolean; mipLevels?: number; } = {} ): FrameBufferObject { console.log(`🖼️创建帧缓冲: ${name} (${width}x${height})`); const fbo = this.gl.createFramebuffer()!; this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, fbo); const colorCount = config.colorCount || 1; const colorFormat = FrameBufferManager.FORMATS[config.colorFormat || 'RGBA8']; const colorTextures: WebGLTexture[] = []; for (let i = 0; i < colorCount; i++) { const colorTexture = this.createTexture2D(width, height, colorFormat, config.mipLevels || 1); this.gl.framebufferTexture2D( this.gl.FRAMEBUFFER, this.gl.COLOR_ATTACHMENT0 + i, this.gl.TEXTURE_2D, colorTexture, 0 ); colorTextures.push(colorTexture); } const drawBuffers = Array.from({length: colorCount}, (_, i) => this.gl.COLOR_ATTACHMENT0 + i); this.gl.drawBuffers(drawBuffers); let depthTexture: WebGLTexture | undefined; let depthRenderbuffer: WebGLRenderbuffer | undefined; if (config.hasDepth) { if (config.depthFormat) { const depthFormat = FrameBufferManager.FORMATS[config.depthFormat]; depthTexture = this.createTexture2D(width, height, depthFormat, 1); this.gl.framebufferTexture2D( this.gl.FRAMEBUFFER, this.gl.DEPTH_STENCIL_ATTACHMENT, this.gl.TEXTURE_2D, depthTexture, 0 ); } else { depthRenderbuffer = this.gl.createRenderbuffer()!; this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, depthRenderbuffer); this.gl.renderbufferStorage(this.gl.RENDERBUFFER, this.gl.DEPTH24_STENCIL8, width, height); this.gl.framebufferRenderbuffer( this.gl.FRAMEBUFFER, this.gl.DEPTH_STENCIL_ATTACHMENT, this.gl.RENDERBUFFER, depthRenderbuffer ); } } const status = this.gl.checkFramebufferStatus(this.gl.FRAMEBUFFER); if (status !== this.gl.FRAMEBUFFER_COMPLETE) { throw new Error(`帧缓冲不完整: ${this.getFramebufferStatusString(status)}`); } this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null); const frameBuffer: FrameBufferObject = { fbo: fbo, colorTextures: colorTextures, depthTexture: depthTexture, depthRenderbuffer: depthRenderbuffer, width: width, height: height, format: colorFormat, msaa: config.msaa || false, mipLevels: config.mipLevels || 1 }; this.frameBuffers.set(name, frameBuffer); console.log(`✅帧缓冲创建成功: ${colorCount}个颜色附件, 深度: ${config.hasDepth}`); return frameBuffer; } private createTexture2D(width: number, height: number, format: TextureFormat, mipLevels: number): WebGLTexture { const texture = this.gl.createTexture()!; this.gl.bindTexture(this.gl.TEXTURE_2D, texture); this.gl.texStorage2D(this.gl.TEXTURE_2D, mipLevels, format.internalFormat, width, height); if (mipLevels > 1) { this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_LINEAR); this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR); } else { this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR); this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR); } this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE); this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE); return texture; } public bindFrameBuffer(name: string, clearColor?: [number, number, number, number]): boolean { const frameBuffer = this.frameBuffers.get(name); if (!frameBuffer) { console.error(`帧缓冲不存在: ${name}`); return false; } this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, frameBuffer.fbo); this.gl.viewport(0, 0, frameBuffer.width, frameBuffer.height); if (clearColor) { this.gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT); } return true; } }
|