1
头图

I encountered a small problem yesterday, and I have been doing it for a long time: I used Three.js to draw 3D objects in webgl, I copied a piece of code from the Internet, and it reported an error:

THREE.Geometry is not a constructor

Nani, can't find the Geometry object?

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;
}

At first I thought it was a module import problem, but after a while I found out that there is really no Geometry in the THREE module! Is the code posted on the Internet on April Fool's Day? I didn't decide to ask google, and finally confirmed that they were upgraded. The new version of Three.js no longer provides Geometry objects, instead of BufferGeometry , the code is modified as follows:

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;
}

songofhawk
303 声望24 粉丝