最近研究Cesium的實例化,盡管該技術(shù)需要在WebGL2.0,也就是OpenGL ES3.0才支持。調(diào)試源碼的時候眼前一亮,發(fā)現(xiàn)VAO和glDrawBuffers都不是WebGL1.0的標(biāo)準(zhǔn)函數(shù),都是擴(kuò)展功能,看來WebGL2.0標(biāo)準(zhǔn)的推廣勢在必行啊。同時發(fā)現(xiàn),通過ANGLE_instanced_arrays的擴(kuò)展,也可以在WebGL1.0下實現(xiàn)實例化,創(chuàng)建實例化方法的代碼如下:
var glDrawElementsInstanced; var glDrawArraysInstanced; var glVertexAttribDivisor; var instancedArrays; // WebGL2.0標(biāo)準(zhǔn)直接提供了實例化接口 if (webgl2) { glDrawElementsInstanced = function(mode, count, type, offset, instanceCount) { gl.drawElementsInstanced(mode, count, type, offset, instanceCount); }; glDrawArraysInstanced = function(mode, first, count, instanceCount) { gl.drawArraysInstanced(mode, first, count, instanceCount); }; glVertexAttribDivisor = function(index, divisor) { gl.vertexAttribDivisor(index, divisor); }; } else { // WebGL1.0下 // 擴(kuò)展ANGLE_instanced_arrays instancedArrays = getExtension(gl, ['ANGLE_instanced_arrays']); if (defined(instancedArrays)) { glDrawElementsInstanced = function(mode, count, type, offset, instanceCount) { instancedArrays.drawElementsInstancedANGLE(mode, count, type, offset, instanceCount);