0

我正在尝试学习threeJS来构建一个需要自定义3D几何形状的3D webapp。我找到了如何做到这一点的代码。但是当我打开我的 html 文件时,浏览器没有显示任何内容。

这是我的代码:

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="Three.js"></script>
    <script src="Detector.js"></script>
    <script src="three.min.js"></script>
    <script defer="defer">
      // revolutions per second
      var angularSpeed = 0.2; 
      var lastTime = 0;

      // this function is executed on each animation frame
      function animate(){
        // update
        var time = (new Date()).getTime();
        var timeDiff = time - lastTime;
        var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
        //geom.rotation.y += angleChange;
        lastTime = time;

        // render
        renderer.render(scene, camera);

        // request new frame
        requestAnimationFrame(function(){
            animate();
        });
      }

      // renderer
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      // camera
      var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
      camera.position.z = 500;

      // scene
      var scene = new THREE.Scene();

      var geom = new THREE.Geometry(); 
      var v1 = new THREE.Vector3(-10,10,0);
      var v2 = new THREE.Vector3(10,10,0);
      var v3 = new THREE.Vector3(10,-10,0);

      geom.vertices.push(new THREE.Vertex(v1));
      geom.vertices.push(new THREE.Vertex(v2));
      geom.vertices.push(new THREE.Vertex(v3));

      geom.faces.push( new THREE.Face3(0, 1, 2));
      geom.computeFaceNormals();

      var object = new THREE.Mesh( geom, new THREE.MeshNormalMaterial() );
      //object.doubleSided = true;
      object.overdraw = true;
      scene.addObject(object);

      animate();
    </script>
  </body>
</html>

请告诉我代码中是否有问题。

4

1 回答 1

0

可能有多个错误。您可能需要检查您的控制台。一些错误来源可能是:

  1. 没有三个顶点。这已被 THREE.Vector 取代。请参阅迁移页面。所以你应该只使用geom.vertices.push(v1)

  2. 再次,请参阅迁移页面。它scene.add()不是scene.addObject()

最后,包装animate在另一个函数中requestAnimationFrame是多余的。您可以直接传递animate 函数对象requestAnimationFrame(animate)

于 2013-07-05T09:33:05.197 回答