是否有一个插件可以为three.js自学项目添加轮换?

时间:2013-04-01 19:42:52

标签: three.js trackball

我开始玩three.js并且想知道是否有一个简单的插件来添加轨迹球功能,所以我可以看到我在代码中所做的更改是如何影响简单对象的“底面”。

我试图效仿:

http://mrdoob.github.com/three.js/examples/misc_controls_trackball.html

但是我收到了很多EventDispatcher错误。

通过尝试,我的意思是:

  • 添加了“TrackballControls.js”
  • 添加了“Detector.js”
  • 添加了这段代码:

    if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
    var controls = new THREE.TrackballControls( camera );
    controls.rotateSpeed = 1.0;
    controls.zoomSpeed = 1.2;
    controls.panSpeed = 0.8;
    controls.noZoom = false;
    controls.noPan = false;
    controls.staticMoving = true;
    controls.dynamicDampingFactor = 0.3;
    controls.keys = [ 65, 83, 68 ];
    controls.addEventListener( 'change', render );
    

我注意到示例中使用的three.js不同于(但不确定是否重要):

http://cdnjs.cloudflare.com/ajax/libs/three.js/r53/three.min.js

任何建议都非常有用,因为我刚开始。

感谢您的时间!

更新: 我尝试从这里使用最新版本的Three.js:

https://github.com/mrdoob/three.js/blob/master/build/three.js

我找不到TrackballControls.js和Detector.js的官方版本,所以我使用了这些版本:

http://threejsdoc.appspot.com/doc/three.js/src.source/extras/controls/TrackballControls.js.html

http://mrdoob.github.com/three.js/examples/js/Detector.js

这是我无法工作的代码(它是http://www.aerotwist.com/tutorials/getting-started-with-three-js/http://mrdoob.github.com/three.js/examples/misc_controls_trackball.html的组合,并试图将对象变成环/圆环):

<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Getting Started with Three.js | Aerotwist</title>
  <meta name="description" content="The home of Creative Coder and developer Paul Lewis. Tutorial, experiments and considered opinions found here.">
  <meta name="author" content="Paul Lewis">


  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="Three.js"></script>
    <script src="TrackballControls.js"></script>
    <script src="Detector.js"></script>
</head>
<style>
  #container {
    background: #000;
    width: 400px;
    height: 300px;
  }
</style>
<body>
<div id="container"></div>
</body>
<script type="text/javascript">
  function animate() {
    requestAnimationFrame( animate );
    controls.update();
  }
  function render() {
    renderer.render( scene, camera );
  }
  var WIDTH = 400,
      HEIGHT = 300;
  // set some camera attributes
  var VIEW_ANGLE = 45,
      ASPECT = WIDTH / HEIGHT,
      NEAR = 0.1,
      FAR = 10000;
  // get the DOM element to attach to
  // - assume we've got jQuery to hand
  var $container = $('#container');
  // create a WebGL renderer, camera
  // and a scene
  var renderer = new THREE.WebGLRenderer();
  var camera =
    new THREE.PerspectiveCamera(
      VIEW_ANGLE,
      ASPECT,
      NEAR,
      FAR);
  var scene = new THREE.Scene();

  // TrackBall code from http://mrdoob.github.com/three.js/examples/misc_controls_trackball.html
  var controls = new THREE.TrackballControls( camera );
  controls.rotateSpeed = 1.0;
  controls.zoomSpeed = 1.2;
  controls.panSpeed = 0.8;
  controls.noZoom = false;
  controls.noPan = false;
  controls.staticMoving = true;
  controls.dynamicDampingFactor = 0.3;
  controls.keys = [ 65, 83, 68 ];
  //controls.addEventListener( 'change', render ); // this was how it used to be at: http://mrdoob.github.com/three.js/examples/misc_controls_trackball.html
  controls.domElement.addEventListener( 'change', render ); // I changed this to fix error message: "controls.addEventListener is not a function"
  // --------------------------
  // add the camera to the scene
  scene.add(camera);
  // the camera starts at 0,0,0
  // so pull it back
  camera.position.z = 300;
  // start the renderer
  renderer.setSize(WIDTH, HEIGHT);
  // attach the render-supplied DOM element
  $container.append(renderer.domElement);
  // ------------------------
  // create the sphere's material
  var sphereMaterial =
    new THREE.MeshLambertMaterial(
      {
        color: 0xCC0000
      });
  // -------------------------------
  // ################ try torus
  var centerpiece = new THREE.TorusGeometry();
  var sphere = new THREE.Mesh(
    centerpiece,
    sphereMaterial);
  // add the sphere to the scene
  scene.add(sphere);
  // ---------------------------
  // create a point light
  var pointLight =
    new THREE.PointLight(0xFFFFFF);
  // set its position
  pointLight.position.x = 10;
  pointLight.position.y = 50;
  pointLight.position.z = 130;
  // add to the scene
  scene.add(pointLight);
  // -------------------------
  // draw!
  renderer.render(scene, camera); 
</script>

每当我点击页面上的任何地方时,我都会收到以下错误消息:

_eye.copy(...).subSelf is not a function

有什么想法吗?

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

确保您使用的是同一版本的所有文件。理想情况下来自最新的。