Three.js 新旧版本创建几何对象差异

昨天遇到个小问题,也搞了挺长时间:用Three.js在webgl中绘制三维对象,从网上抄的一段代码,运行报错:

THREE.Geometry is not a constructor

纳尼,找不到Geometry对象?

function createLine(){
        let start = new THREE.Vector3(7, 1.8, -2);
        let end = new THREE.Vector3(1.6,1.8, -2);

        // 就是这里创建的对象找不到 *****
        let geometry = new THREE.Geometry();
        // ****************************

        let lineMaterial = new THREE.LineBasicMaterial({color: 0x00ff00});
        geometry.vertices.push(start);
        geometry.vertices.push(end);
        let line = new THREE.Line(lineGeometry, lineMaterial);
        scene.add(line);
        return line;
}

一开始还以为是模块导入问题,折腾了一阵才发现,THREE模块中竟然真的就没有Geometry!难道网上的代码是愚人节贴的?遇事不决问google,终于确认人家是升级了。新版的Three.js不再提供Geometry对象,代之以BufferGeometry,代码修改如下:

function createLine(){
        let start = new THREE.Vector3(7, 1.8, -2);
        let end = new THREE.Vector3(1.6,1.8, -2);

        let lineMaterial = new THREE.LineBasicMaterial({color: 0x00ff00});
        let pointsArray = [start, end];
        let lineGeometry = new THREE.BufferGeometry().setFromPoints(pointsArray);
        let line = new THREE.Line(lineGeometry, lineMaterial);
        scene.add(line);
        return line;
}

你可能感兴趣的:(Three.js 新旧版本创建几何对象差异)