三个js自定义几何

时间:2013-06-28 11:13:48

标签: javascript html5 three.js webgl

我正在尝试学习threeJS来构建一个3D webapp,我需要自定义3D几何形状。我找到了这样做的代码。 但是当我打开我的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>

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

1 个答案:

答案 0 :(得分:0)

可能存在多个错误。您可能想检查您的控制台。 一些错误来源可能是:

  1. 没有THREE.Vertex。这已被THREE.Vector取代。请参阅Migration页面。所以你应该使用geom.vertices.push(v1)

  2. 再次参见“迁移”页面。它是scene.add()而不是scene.addObject()

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