gpt4 book ai didi

JavaScript/ThreeJS 类型错误 : Cannot read property 'set' of undefined

转载 作者:行者123 更新时间:2023-11-30 19:49:53 25 4
gpt4 key购买 nike

我正在尝试使用名为 Model 的 ThreeJS 创建自定义对象,该对象由我定义的其他自定义对象组成,例如 Part。这是我得到错误的地方:

const Model = function() {
this.mesh = new THREE.Object3D();
this.mesh.name = 'model';

//creates instance of part
this.lowerPart = new Part();
this.lowerPart.position.set(1, 2, 3, -75); //TypeError here
this.lowerPart.rotation.set(0, 0, 0);
this.mesh.add(this.lowerPart);
}

但是,当我运行我的程序时,它说它无法读取未定义的属性“set”,引用 this.lowerPart.position.set(1, 2, 3, -75);

这是我基本定义 Part 的方式:

 const Part = function () {
this.mesh = new THREE.Object3D();
this.mesh.name = 'part';

const partShape = new THREE.Shape();
partShape.lineTo( 40, 80 );
partShape.lineTo( 60, 80 );
partShape.lineTo( 60, 100 );
partShape.lineTo( 40, 100 );

//extrude settings defined....

const partGeometry = new THREE.ExtrudeGeometry(partShape, extrudeSettings);
const partMesh = new THREE.Mesh(partGeometry, new THREE.MeshStandardMaterial({
color: 0x1c4bc9,
flatShading: true
}));
this.mesh.add(partMesh);
};
Part.prototype = Object.create(THREE.Object3D.prototype);
Part.prototype.constructor = Part;

在我的函数 Model 中要注意的另一件事是,当创建 this.lowerPart 时,它被标记为未使用。我不确定原因,因为它是具有这些属性的 Object3D 类型。

控制台也证明this.lowerPartPart的一个实例。

我查看并尝试了大多数与我的问题相关的建议 StackOverflow 问题。在所有这些中,这似乎是最相关的:Uncaught TypeError: Cannot read property 'set' of undefined但是,它对我不起作用。

非常感谢任何关于如何解决我的问题的建议!

最佳答案

如果要从THREE.Object派生Part,还必须在Part的构造函数中执行下面这行代码(它应该是构造函数中的第一行):

THREE.Object3D.call( this );

只有这样,才能使用 Part 实例访问 positionrotationscale 等属性。

关于JavaScript/ThreeJS 类型错误 : Cannot read property 'set' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54587170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com