【问题标题】:Drag a loaded object with threejs?用threejs拖动加载的对象?
【发布时间】:2015-08-15 03:39:28
【问题描述】:

我一直在尝试让加载的对象在threejs 中可拖动。下面的第一段代码(只是一个 sn-p)显示了一个立方体,我可以旋转视图,但不能旋转立方体本身。

第二个代码块创建了一个可拖动的框,但我希望能够做的不仅仅是拖动框。就像拖一个茶壶一样!因此希望拖动一个加载的对象。

为什么我用threejs创建的时候可以移动盒子,加载的时候不行?我该如何解决这个问题?

代码来源于:http://threejs.org/examples/webgl_interactive_draggablecubes.html

// var geometry = new THREE.BoxGeometry( 40, 40, 40 );

var loader = new THREE.OBJLoader();
loader.load('static/obj/cube.obj', function(object) {

    // var object = new THREE.Mesh( geometry, 
                    new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );

    object.position.x = Math.random() * 100 - 50;
    object.position.y = Math.random() * 60 - 30;
    object.position.z = Math.random() * 80 - 40;

    scene.add( object );

    objects.push( object );

});

var geometry = new THREE.BoxGeometry( 40, 40, 40 );

// var loader = new THREE.OBJLoader();
// loader.load('static/obj/cube.obj', function(object) {

var object = new THREE.Mesh( geometry, 
             new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );

object.position.x = Math.random() * 100 - 50;
object.position.y = Math.random() * 60 - 30;
object.position.z = Math.random() * 80 - 40;

scene.add( object );

objects.push( object );

// });

【问题讨论】:

    标签: three.js draggable


    【解决方案1】:

    下面的代码可以工作,并允许我拖动对象,因为我拖动对象的方式需要一个网格作为场景的子节点。

    if (objfile) {
    
            var texture = new THREE.Texture();
            var img = new THREE.ImageLoader();
            img.load('static/material/red.png', function(image) {
                texture.image = image;
                texture.needsUpdate = true;
            });
    
            var objModel = new THREE.OBJLoader();
            var mesh;
    
            objModel.load('static/obj/' + objfile + '.obj', function(object) {
                object.traverse(function(child) {
                    if (child instanceof THREE.Mesh) {
                        child.material.map = texture;
                        mesh = child;
                    }
                });
    
                scene.add(mesh);
            });
        }
    

    【讨论】:

      【解决方案2】:

      加载一个 obj 代替了新的 THREE.boxGeometry() 部分而不是新的 THREE.Mesh()。这段代码似乎没有错误。

      不管怎样,看看下面的代码,注意在 loader.load 中我将第二个参数从 object 更改为 geometry 并取消了网格制作的注释:

      var loader = new THREE.OBJLoader();
      loader.load('static/obj/cube.obj', function(geometry) {
      
          var object = new THREE.Mesh( geometry, 
                          new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
      
          object.position.x = Math.random() * 100 - 50;
          object.position.y = Math.random() * 60 - 30;
          object.position.z = Math.random() * 80 - 40;
      
          scene.add( object );
      
          objects.push( object );
      
      });
      

      现在 OBJLoader 获取几何体,并使用它来制作网格。这更有意义不是吗?

      【讨论】:

      • 感谢您的回答!虽然当我按照你的建议做时,我的对象没有加载。
      • 它说three.min.js 中有错误,但我从来没有搞砸过。 “未捕获的 TypeError:无法读取未定义的属性 'length' 或 'center'。”
      • 我认为它引用的是你的 obj 文件而不是这段代码
      猜你喜欢
      • 2018-04-11
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多