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
| class WebGLCompatibilityDetector { private canvas: HTMLCanvasElement; private supportInfo: WebGLSupportInfo; interface WebGLSupportInfo { webgl1: boolean; webgl2: boolean; version: string; vendor: string; renderer: string; extensions: string[]; maxTextureSize: number; maxCubeMapSize: number; maxVertexUniforms: number; maxFragmentUniforms: number; maxVaryingVectors: number; maxVertexAttributes: number; maxTextureUnits: number; precision: { vertexShader: PrecisionInfo; fragmentShader: PrecisionInfo; }; } interface PrecisionInfo { highpFloat: boolean; mediumpFloat: boolean; lowpFloat: boolean; highpInt: boolean; mediumpInt: boolean; lowpInt: boolean; } constructor() { this.canvas = document.createElement('canvas'); this.detectWebGLSupport(); } private detectWebGLSupport(): void { console.log('🔍 检测WebGL支持情况...'); let gl1: WebGLRenderingContext | null = null; try { gl1 = this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl') as WebGLRenderingContext; } catch (e) { console.warn('WebGL 1.0 获取失败:', e); } let gl2: WebGL2RenderingContext | null = null; try { gl2 = this.canvas.getContext('webgl2') as WebGL2RenderingContext; } catch (e) { console.warn('WebGL 2.0 获取失败:', e); } const gl = gl2 || gl1; if (!gl) { this.supportInfo = this.getUnsupportedInfo(); return; } this.supportInfo = { webgl1: !!gl1, webgl2: !!gl2, version: gl.getParameter(gl.VERSION), vendor: gl.getParameter(gl.VENDOR), renderer: gl.getParameter(gl.RENDERER), extensions: gl.getSupportedExtensions() || [], maxTextureSize: gl.getParameter(gl.MAX_TEXTURE_SIZE), maxCubeMapSize: gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE), maxVertexUniforms: gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS), maxFragmentUniforms: gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS), maxVaryingVectors: gl.getParameter(gl.MAX_VARYING_VECTORS), maxVertexAttributes: gl.getParameter(gl.MAX_VERTEX_ATTRIBS), maxTextureUnits: gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS), precision: this.detectPrecisionSupport(gl) }; console.log('�?WebGL检测完�?', this.supportInfo); } private detectPrecisionSupport(gl: WebGLRenderingContext | WebGL2RenderingContext): { vertexShader: PrecisionInfo; fragmentShader: PrecisionInfo; } { const checkPrecision = (shaderType: number): PrecisionInfo => { return { highpFloat: gl.getShaderPrecisionFormat(shaderType, gl.HIGH_FLOAT)?.precision > 0, mediumpFloat: gl.getShaderPrecisionFormat(shaderType, gl.MEDIUM_FLOAT)?.precision > 0, lowpFloat: gl.getShaderPrecisionFormat(shaderType, gl.LOW_FLOAT)?.precision > 0, highpInt: gl.getShaderPrecisionFormat(shaderType, gl.HIGH_INT)?.precision > 0, mediumpInt: gl.getShaderPrecisionFormat(shaderType, gl.MEDIUM_INT)?.precision > 0, lowpInt: gl.getShaderPrecisionFormat(shaderType, gl.LOW_INT)?.precision > 0 }; }; return { vertexShader: checkPrecision(gl.VERTEX_SHADER), fragmentShader: checkPrecision(gl.FRAGMENT_SHADER) }; } public generateCompatibilityReport(): string { const info = this.supportInfo; let report = '📊 WebGL兼容性报告\n'; report += '=' * 50 + '\n'; report += `WebGL 1.0 支持: ${info.webgl1 ? '�? : '�?}\n`; report += `WebGL 2.0 支持: ${info.webgl2 ? '�? : '�?}\n`; report += `版本: ${info.version}\n`; report += `厂商: ${info.vendor}\n`; report += `渲染�? ${info.renderer}\n`; report += `\n📐 限制信息:\n`; report += `最大纹理尺�? ${info.maxTextureSize}x${info.maxTextureSize}\n`; report += `最大立方体贴图尺寸: ${info.maxCubeMapSize}x${info.maxCubeMapSize}\n`; report += `顶点着色器最大Uniform�? ${info.maxVertexUniforms}\n`; report += `片段着色器最大Uniform�? ${info.maxFragmentUniforms}\n`; report += `最大Varying�? ${info.maxVaryingVectors}\n`; report += `最大顶点属性数: ${info.maxVertexAttributes}\n`; report += `最大纹理单元数: ${info.maxTextureUnits}\n`; report += `\n🎯 精度支持:\n`; report += `片段着色器highp: ${info.precision.fragmentShader.highpFloat ? '�? : '�?}\n`; report += `片段着色器mediump: ${info.precision.fragmentShader.mediumpFloat ? '�? : '�?}\n`; report += `\n🔌 扩展支持: (${info.extensions.length}�?\n`; info.extensions.slice(0, 10).forEach(ext => { report += ` - ${ext}\n`; }); if (info.extensions.length > 10) { report += ` ... 还有${info.extensions.length - 10}个扩展\n`; } return report; } public getRecommendedShaderVersion(): 'webgl1' | 'webgl2' | 'unsupported' { if (this.supportInfo.webgl2) return 'webgl2'; if (this.supportInfo.webgl1) return 'webgl1'; return 'unsupported'; } public hasExtension(extensionName: string): boolean { return this.supportInfo.extensions.includes(extensionName); } }
|