Three.js中更新Line时的内存泄露

之前用这种方式创建Line和在render中更新line两点位置:

function createLinkLine(parent, son) {
  let points = [parent.position, son.position];
  let geometry = new THREE.BufferGeometry();
  geometry.setFromPoints(points);
  let line = new THREE.Line(geometry, g_linkLineMaterial);
  line.wxhParent = parent;
  line.wxhSon = son;
  g_scene.add(line);
  return line;
}
   for (let i = 0; i < g_lines.length; i++) {
     let points = [g_lines[i].wxhParent.position, g_lines[i].wxhSon.position]; 
     //如果不dipose后重建,微信中gc会来不及回收一直涨
     //加了dispose后,抖音里面内存一直涨
     let geometry=g_lines[i].geometry;
     geometry.dispose();
     geometry = new THREE.BufferGeometry();
     geometry.setFromPoints(points);
     g_lines[i].geometry = geometry;
   }

在微信小程序中没有问题,但是发现在抖音小游戏中会有内存泄露,不是GC来不及回收,内存一直涨。

google找到解法:

javascript - Drawing a line with three.js dynamically - Stack Overflow

function createLinkLine(parent, son) {
  let positions = new Float32Array(2 * 3);
  positions[0]=parent.position.x;
  positions[1]=parent.position.y;
  positions[2]=parent.position.z;
  positions[3]=son.position.x;
  positions[4]=son.position.y;
  positions[5]=son.position.z;
  let geometry = new THREE.BufferGeometry();
  geometry.setAttribute('position',new THREE.BufferAttribute(positions,3));
  let line = new THREE.Line(geometry, g_linkLineMaterial);
  line.wxhParent = parent;
  line.wxhSon = son;
  g_scene.add(line);
  return line;
}
  for (let i = 0; i < g_lines.length; i++) {
    //这个方法没有内存泄露
    let points=g_lines[i].geometry.attributes.position.array;
    points[0]=g_lines[i].wxhParent.position.x;
    points[1]=g_lines[i].wxhParent.position.y;
    points[2]=g_lines[i].wxhParent.position.z;
    points[3]=g_lines[i].wxhSon.position.x;
    points[4]=g_lines[i].wxhSon.position.y;
    points[5]=g_lines[i].wxhSon.position.z;
    g_lines[i].geometry.attributes.position.needsUpdate = true;
  }

游戏源码下载地址:微信小游戏3D合成消除2048源码-Javascript文档类资源-CSDN下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火星牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值