Javascript 方法在带有自定义网格的 BabylonJS 中不起作用

Javascript method not working in BabylonJS with a custom Mesh

我是 BabylonJS 的新手,我创建了一个类似的自定义网格(我不知道这是否正确):

function DragSphere() {
    this.sphere = BABYLON.Mesh.CreateSphere("sphere", 15, 2, scene);
    this.sphere.material = new BABYLON.StandardMaterial("sphereMat", scene);
    ...
    return (this.sphere);
}

DragSphere.prototype.setRGBColor = function(r, g, b) {
    this.sphere.material.diffuseColor = new BABYLON.Color3(r/255, g/255, b/255);
}

所以我想使用我的DragSphereSetRGBColor方法来更新它的颜色,但是浏览器似乎不同意我的看法:

 Uncaught TypeError: sphere.setRGBColor is not a function

从构造函数中删除 return (this.sphere); 语句。

如果您使用的是 TypeScript,则只能使用 new 运算符调用 void 函数。如上所述,在其签名中使用 return 类型调用函数将导致 setRGBColor 函数在转译的 JavaScript.

中未定义

您可以在 Babylon JS playground 中试试这个。