如何在three.js中更新ShapeGeometry
问题描述:
我是three.js的新手,我尝试更新ShapeGeometry,但没有奏效。现在我必须删除并读取shapeMesh加时。如何在three.js中更新ShapeGeometry
this.mesh.geometry.dispose()
this.mesh.material.dispose()
this.scene.remove(this.mesh)
//vertex
this.triangleShape = new THREE.Shape()
this.triangleShape.moveTo( -612+this.Eposition.left, 310-this.Eposition.top-25)
for(let i = 0 ; i < length ; i++) {
this.triangleShape.lineTo(data[i][0],data[i][1])
}
this.triangleShape.lineTo( -612+this.Eposition.left+141, 310-this.Eposition.top-25)
this.triangleShape.lineTo( -612+this.Eposition.left+141, 310-this.Eposition.top)
this.triangleShape.lineTo( -612+this.Eposition.left, 310-this.Eposition.top)
this.triangleShape.lineTo( -612+this.Eposition.left, 310-this.Eposition.top-25)// close path
let geometry = new THREE.ShapeGeometry(this.triangleShape)
this.mesh = new THREE.Mesh(geometry,this.material)
this.scene.add(this.mesh)
伊夫试着写this.mesh.geometry.verticesNeedUpdate = true
,但似乎不起作用
答
不幸的是,你不能更新由ShapeGeometry所用的形状,因为形状只是一个创建的几何图形的更高级别描述从它,不是几何本身的一部分。
这里的要点是:形状是用矢量图形和曲线等来描述的。当您创建ShapeGeometry时,three.js将使用该描述来实际创建几何缓冲区(在此步骤中,曲线将被分解成线段列表,并且封闭路径将变成三角形,请参阅ShapeGeometry.addShape()
的实现) 。
只要它在性能方面工作(形状不太复杂等),您当然可以为每次更新创建一个新的几何体,但是由于此过程也可能非常昂贵,您应该考虑实现自己的几何体或BufferGeometry来做你需要做的事情(并不那么复杂,只要看一下像Cylinder [Buffer] Geometry这样简单的例子)。