"WebGL - Up and Running"책에서 커스텀 지오메트리가 매우 드라마틱하게 만들어졌습니다. 그러나 두 번째 줄부터 마지막 줄까지가 눈썹을 돋 웠습니다. 코드는 다음과 같습니다 :"WebGL - Up and Running"oddity : 생성자 인스턴스를 프로토 타입으로 사용해야합니까?
Saturn.Rings = function (innerRadius, outerRadius, nSegments) {
THREE.Geometry.call(this);
var outerRadius = outerRadius || 1,
...snip snip snip...
this.computeCentroids();
this.computeFaceNormals();
this.boundinSphere = { radius: outerRadius };
};
Saturn.Rings.prototype = new THREE.Geometry();
Saturn.Rings.prototype.constructor = Saturn.Rings;
제 질문은 : 왜 프로토 타입 체인에서 생성자 인스턴스를 사용합니까? 에서 상속받은 새로운 빈 개체가 Saturn.Rings.prototype
인 이유는 무엇입니까? 상속받을 가치가있는 모든 것을 THREE.Geometry.prototype
에 위치시켜야하지 않습니까? 왜 그는 프로토 타입을 Geometry 생성자의 인스턴스로 만듭니다? (새로운 프로토 타입이 THREE.Geometry
의 프로토 타입을 상속 받음에 따라 프로토 타입 체인이 여전히 정확하다는 것을 알고 있습니다.
또한 위 코드의 두 번째 줄에는 의 인스턴스가 THREE.Geometry
생성자를 통해 배치됩니다. 따라서, 그들은 꿈꾸는 모든 것을 얻을 수 있습니다. THREE.Geometry
건설 과정. 따라서 프로토 타입에 동일한 작업을 수행 할 필요가 없습니다.
THREE.CubeGeometry = function (width, height, depth, segmentsWidth, segmentsHeight, segmentsDepth, materials, sides) {
THREE.Geometry.call(this);
...snip snip snip...
}
this.computeCentroids();
this.mergeVertices();
};
THREE.CubeGeometry.prototype = Object.create(THREE.Geometry.prototype);
웨이 깔끔한 :
비슷한 형상 three.js를 자체 처리하는 방법을 살펴 수 있습니다. 유일한 문제는 프로토 타입의 'constructor'속성을 THREE.CubeGeometry
을 가리 키도록 설정하는 것을 잊어 버렸지 만 또 다른 문제입니다.
약간의 기록은 [three.js issue] (https://github.com/mrdoob/three.js/issues/2080)를 참조하십시오. – WestLangley