微信小游戏中three.js离屏画布的示例代码
#代码知识 发布时间: 2026-01-12
国庆8天长假,重庆之行因故未成,偶得闲,用three.js结合cannon.js写个3D小游戏耍耍。
在微信小游戏中,把three.js的3D内容在离屏画布处理,然后复制到在屏画布,方法是:
let c_toolbarHeight=140;
let sysInfo=wx.getSystemInfoSync();
require('./js/libs/weapp-adapter.js');
var canvas_webGL=window.canvas;
canvas_webGL.width = sysInfo.screenWidth * sysInfo.pixelRatio;
canvas_webGL.height = (sysInfo.screenHeight-c_toolbarHeight) * sysInfo.pixelRatio;
var ctx_webGL=canvas_webGL.getContext('webgl');
let options={context: ctx_webGL}
let renderer = new THREE.WebGLRenderer(options);
renderer.setSize(sysInfo.screenWidth, sysInfo.screenHeight-c_toolbarHeight);
renderer.setPixelRatio(sysInfo.pixelRatio);
function render(){
//清除canvas_bkg的3D区域
wx.tmGlobal.eraseZone(0,
c_toolbarHeight,
sysInfo.screenWidth,
sysInfo.screenHeight);
renderer.render(scene, camera);
wx.tmGlobal.ctx_bkg.drawImage(canvas_webGL,
0,c_toolbarHeight*sysInfo.pixelRatio);
//画一条横的红线
wx.tmGlobal.ctx_bkg.strokeStyle = '#FF8C00';
wx.tmGlobal.ctx_bkg.lineWidth = 2;
wx.tmGlobal.ctx_bkg.beginPath();
wx.tmGlobal.ctx_bkg.moveTo(0,
(c_toolbarHeight)*sysInfo.pixelRatio);
wx.tmGlobal.ctx_bkg.lineTo(
sysInfo.screenWidth*sysInfo.pixelRatio,
(c_toolbarHeight)*sysInfo.pixelRatio);
//画游戏结束临界线
wx.tmGlobal.ctx_bkg.moveTo(0,
(c_toolbarHeight+c_yugaoHeight)*sysInfo.pixelRatio);
wx.tmGlobal.ctx_bkg.lineTo(
sysInfo.screenWidth*sysInfo.pixelRatio,
(c_toolbarHeight+c_yugaoHeight)*sysInfo.pixelRatio);
wx.tmGlobal.ctx_bkg.stroke();
//把canvas_bkg画到在屏画布
wx.tmGlobal.ctx_main.clearRect(0,0,
wx.tmGlobal.canvas_main.width,wx.tmGlobal.canvas_main.height);
wx.tmGlobal.ctx_main.drawImage(wx.tmGlobal.canvas_bkg,0,0);
}
在vivo和iphone手机都表现正常,但是,华为手机显示不出来:
https://developers.weixin.qq.com/community/develop/doc/00026c3c1c8eb010de384a82d51000?jumpto=
其它用户也提了好久了,腾讯或华为都没有解决,试来试去,终于找到了另一种写法:
renderer = new THREE.WebGLRenderer();
let target = new THREE.WebGLRenderTarget(
sysInfo.screenWidth*sysInfo.pixelRatio,
(sysInfo.screenHeight-c_toolbarHeight)*sysInfo.pixelRatio);
renderer.setRenderTarget(target);
gl=renderer.getContext();
var canvas_huawei=wx.createCanvas();
canvas_huawei.width=sysInfo.screenWidth*sysInfo.pixelRatio;
canvas_huawei.height=(sysInfo.screenHeight-c_toolbarHeight)*sysInfo.pixelRatio;
var ctx_huawei=canvas_huawei.getContext('2d');
var canvas_huawei2=wx.createCanvas();
canvas_huawei2.width=sysInfo.screenWidth*sysInfo.pixelRatio;
canvas_huawei2.height=(sysInfo.screenHeight-c_toolbarHeight)*sysInfo.pixelRatio;
var ctx_huawei2=canvas_huawei2.getContext('2d');
var imageData = ctx_huawei.createImageData(
sysInfo.screenWidth*sysInfo.pixelRatio,
(sysInfo.screenHeight-c_toolbarHeight)*sysInfo.pixelRatio);
var pixels = new Uint8Array(imageData.data.length);
function render(){
//擦除背景画布的webGL区域(因为webGL是用的透明绘制)
wx.tmGlobal.eraseZone(0,
c_toolbarHeight,
sysInfo.screenWidth,
sysInfo.screenHeight);
renderer.render(scene, camera);
gl.readPixels(
0,
0,
gl.drawingBufferWidth,
gl.drawingBufferHeight,
gl.RGBA,gl.UNSIGNED_BYTE,pixels);
imageData.data.set(pixels);
ctx_huawei.putImageData(imageData,0,0);
//清除
ctx_huawei2.clearRect(0,0,canvas_huawei2.width,canvas_huawei2.height);
//上下镜像翻转
ctx_huawei2.translate(0,canvas_huawei2.height);
ctx_huawei2.scale(1, -1);
ctx_huawei2.drawImage(canvas_huawei,0,0);
//恢复
ctx_huawei2.translate(0,canvas_huawei2.height);
ctx_huawei2.scale(1, -1);
wx.tmGlobal.ctx_bkg.drawImage(canvas_huawei2,
0,c_toolbarHeight*sysInfo.pixelRatio);
......
} 代码知识SEO
上一篇 : Django如何使用asyncio协程和ThreadPoolExecutor多线程
下一篇 : spring boot实现profiles动态切换的示例
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!