javascript - Modernizr:测试 WebGL 与 WebGL 扩展

标签 javascript three.js webgl modernizr

我在我的网页上使用了 THREE.js 场景和图形对象。我知道,至少 THREE.js 使用了 WebGL

我想利用 Modernizr 检查当前浏览器与 WebGL 的兼容性,如果浏览器不兼容,则向用户提示一条消息.

选择 browser features 时要进行 Modernizr 测试,我看到两个与我的目标相关的特性

WebGL:在浏览器中检测 WebGL。

WebGl 扩展:检测 WebGL 中对 OpenGL 扩展的支持。如果支持 WebGL 扩展 API,则为 true,然后将支持的扩展公开为子属性,例如:

因此,为了让 THREE.js 正常工作,我是否需要测试 WebGL ExtensionsWebGL 或者 只是WebGL

最佳答案

这取决于您是否使用需要扩展的功能。 Three.js 本身不需要任何扩展。如果您使用 WEBGL_depth_texture 扩展,某些东西(例如阴影)可能会运行得更快。

如果您不知道您个人需要哪些扩展,请考虑插入一些代码来隐藏它们并查看您的应用是否仍在运行

例子:

// disable all extensions

WebGLRenderingContext.prototype.getExtension = function() {
  return null;
}
WebGLRenderingContext.prototype.getSupportedExtensions = function() {
  return [];
}

// now init three.js

如果你想允许特定的扩展,你可以这样做

var allowedExtensions = [
  "webgl_depth_texture",
  "oes_texture_float",
];

WebGLRenderingContext.prototype.getExtension = function(origFn) {
  return function(name) {
    if (allowedExtensions.indexOf(name.ToLowerCase()) >= 0) {
      return origFn.call(this, name);
    }
    return null;
  };
}(WebGLRenderingContext.prototype.getExtension);

WebGLRenderingContext.prototype.getSupportedExtensions = function(origFn) {
  return function() {
    return origFn.call(this).filter(function(name) {
      return allowedExtensions.indexOf(n) >= 0;
    });
  };
}(WebGLRenderingContext.prototype.getSupportedExtensions);

关于javascript - Modernizr:测试 WebGL 与 WebGL 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37734756/

相关文章:

javascript - 我可以跳过对象声明中的属性名称吗?

javascript - 使用 Meteor.users.find() 获取 profile.name

javascript - HTML5 Canvas 问题?

javascript - JQuery click() 在控制台中触发事件,但从 JS 文件调用默认值

glsl - 使用 Phong 照明和定向照明

javascript - 三.js/Webgl X-RAY效果

javascript - iOS 移动设备上的 Three.js 渲染问题

javascript - THREE.js 如何停止 chop 由 2D Canvas 制成的纹理中的文本

javascript - 2D 纹理渲染在 Safari 中闪烁,在 Chrome/Firefox 中渲染良好

javascript - 在 ThreeJS 中从 2D 对象绘制 3D 几何