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

发布于 2022-09-13 00:01:08 字数 1725 浏览 18 评论 0

照着官网的例子打的,运行时报了这个错误
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版本太高了,换了几个版本还是一样,有没有大佬解惑一下

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2022-09-20 00:01:08

你好朋友,我发现同样的问题
构造器以及下面的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现在已经被添加")

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

泛泛之交 2022-09-20 00:01:08
<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>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文