首页 > 解决方案 > Three.js r125 BufferGeometry `vertices` 不存在

问题描述

我正在更新 Three.js,我发现当我升级到 r125 时,vertices由于缺少方法,尝试设置 BufferGeometry 失败。它似乎也已删除verticesNeedUpdate迁移指南似乎没有对此发出警告,并且更改日志似乎也没有从我所看到的情况下解决它。

不幸的是,我没有编写原始代码,所以我不确定如何解决它。代码如下所示:

this.geometry.vertices[0].x = this.geometry.vertices[2].x = -this.canvas.width / 2;
this.geometry.vertices[1].x = this.geometry.vertices[3].x = this.canvas.width / 2;
this.geometry.vertices[0].y = this.geometry.vertices[1].y = this.canvas.height / 2;
this.geometry.vertices[2].y = this.geometry.vertices[3].y = -this.canvas.height / 2;
this.geometry.verticesNeedUpdate = true;

使用下面的 Don's Answer 更新

应用 Don 建议的更改后,我们得出以下结论:

    const negativeWidth = -this.canvas.width / 2;
    const positiveWidth = this.canvas.width / 2;
    const positiveHeight = this.canvas.height / 2;
    const negativeHeight = -this.canvas.height / 2;

    this.geometry.attributes.position.setXY(0, negativeWidth, positiveHeight);
    this.geometry.attributes.position.setXY(1, positiveWidth, positiveHeight);
    this.geometry.attributes.position.setXY(2, negativeWidth, negativeHeight);
    this.geometry.attributes.position.setXY(3, positiveWidth, negativeHeight);
    this.geometry.attributes.position.needsUpdate = true;

标签: three.js

解决方案


three.js r125 的第一个变更日志条目是相关的:

几何图形已从核心中移除。它现在位于examples/jsm/deprecated/Geometry.js.

该类THREE.Geometry已被弃用一段时间,但项目存储库之外的一些旧代码和示例仍然引用它。推荐的替代品是THREE.BufferGeometry,它的性能更高。该类BufferGeometry没有.vertices属性,因此这可能是您看到的特定错误的原因。相反,您可以像这样更新顶点:

geometry.attributes.position.setXYZ( index, x, y, z );
geometry.attributes.position.needsUpdate = true;

推荐阅读