three.js画线,报错THREE.Geometry is not a constructor

照着官网的例子打的,运行时报了这个错误
image.png

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
    <style>
    body{margin: 0;}
    #title{font-size: 40px;font-weight: bold;color: red;text-align: center;}
    #threedCanvas{width: 800px;height: 600px;border: solid 0px #000;margin: 0 auto;}
        canvas{width: 100%;height: 100%;}
  </style>
</head>
<body>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/three.js/r128/three.min.js"></script>
    <script>
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
camera.position.set( 0, 0, 100 );
camera.lookAt( 0, 0, 0 );

var scene = new THREE.Scene();
//create a blue LineBasicMaterial
var material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3( -10, 0, 0) );
geometry.vertices.push(new THREE.Vector3( 0, 10, 0) );
geometry.vertices.push(new THREE.Vector3( 10, 0, 0) );
var line = new THREE.Line( geometry, material );
scene.add( line );
renderer.render( scene, camera );
    </script>
</body>
</html>

一开始以为是three版本太高了,换了几个版本还是一样,有没有大佬解惑一下

阅读 30.2k
2 个回答

你好朋友,我发现同样的问题
构造器以及下面的api都已经被废弃。

geometry.vertices.push(new THREE.Vector3( -10, 0, 0) );
geometry.vertices.push(new THREE.Vector3( 0, 10, 0) );
geometry.vertices.push(new THREE.Vector3( 10, 0, 0) );

然后google了一番发现是版本的问题,125版本就不再支持这个api了,如果还需要就用他的子类BufferGeometry。image.png
地址:https://github.com/mrdoob/thr...
我贴一下我的代码,也给了注释希望能帮到你!

    const material = new THREE.LineBasicMaterial({color:'#fff'})
    //这里用这个构造
    const geometry = new THREE.BufferGeometry()
    const pointsArray = new Array()
    //加2000个顶点,范围为-1到1
    for(let i = 0;i<2000;i++){
      const x = Math.random() * 2 - 1 //范围在-1到1
      const y = Math.random() * 2 - 1
      const z = Math.random() * 2 - 1 
      pointsArray.push(new THREE.Vector3(x,y,z))
      //顶点
      //geometry.vertices.push(new THREE.Vector3(x,y,z))
    }
    //用这个api传入顶点数组
    geometry.setFromPoints(pointsArray)
    //下述基本一样
    const mesh = new THREE.Line(geometry,material)
    mesh.position.set(0,6,0)
    scene.add(mesh)
    projects.push(mesh)
    console.log("line现在已经被添加")

互联网第一次解决问题,我也是新人,一起加油啦!

<html>
    <head>
        <style>
            body { margin: 0; }
            canvas { width: 100%; height: 100% }
        </style>
    </head>
    <body>
        <script src="./three.js"></script>
        <script>
            var scene = new THREE.Scene();
            var geometry = new THREE.BufferGeometry();
            var vertices = new Float32Array( [-10, 0, 0, 0, 10, 0, 10, 0, 0] );
            geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
            // geometry.vertices.push(new THREE.Vector3( -10, 0, 0) );
            // geometry.vertices.push(new THREE.Vector3( 0, 10, 0) );
            // geometry.vertices.push(new THREE.Vector3( 10, 0, 0) );
            var material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
            var line = new THREE.Line( geometry, material );
            scene.add( line );

            var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
            camera.position.set( 0, 0, 100 );
            camera.lookAt( 0, 0, 0 );

            var renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            document.body.appendChild( renderer.domElement );
            renderer.render( scene, camera );
        </script>
    </body>
</html>

自己把文档里面的案例替换了一下,需要的自取

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏