小程序webgl透明背景
发布于 6 年前 作者 zhangjuan 5076 次浏览 来自 问答

小程序canvas中webgl能实现透明背景吗?目前测试大部分机型都是黑底,使用

gl.clearColor(0.0, 0.0, 0.0, 0);也无法实现透明

4 回复

webgl 暂时不支持透明。

麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

这个解决了吗

还有问题的可以试下这个代码,我是从 threeJS 源码拿出来的,在几台安卓机器上都可以透明,不加则不透明。webGLCanvasId 设置你们的ID。如果用 threeJS,canvas 和 glContext 要传到 threeJS 里面。

wx.createSelectorQuery()

        .select(`#${webGLCanvasId}`)

        .node()

        .exec((res) => {

          const canvas = res[0].node;

          const glContext = canvas.getContext(‘webgl’, {

            alpha: true,

            depth: true,

            stencil: true,

            antialias: true,

            premultipliedAlpha: true,

            preserveDrawingBuffer: false,

            powerPreference: ‘default’,

            failIfMajorPerformanceCaveat: false,

            xrCompatible: true

          });

          // 安卓手机需要调用这个来设置透明

           glContext.clearColor(0, 0, 0, 0);

          glContext.clear(glContext.COLOR_BUFFER_BIT);

        });

回到顶部