【问题标题】:Rotate object around arbitrary axis in Three.js,just like the moon around the Earth.在 Three.js 中围绕任意轴旋转物体,就像月球围绕地球旋转一样。
【发布时间】:2018-07-25 16:54:12
【问题描述】:

我已经尝试了很多问题的解决方案,但是对象
仍然绕着他自己的轴,没有任何平移。

【问题讨论】:

    标签: javascript three.js rotatetransform


    【解决方案1】:

    您可能希望将该对象添加为另一个对象的子对象,然后旋转该对象...

    var renderer = new THREE.WebGLRenderer();
    var w = 300;
    var h = 200;
    renderer.setSize( w,h );
    document.body.appendChild( renderer.domElement );
    
    var scene = new THREE.Scene();
      	var camera = new THREE.PerspectiveCamera(
    	45,		// Field of view
    	w/h,	// Aspect ratio
    	0.1,		// Near
    	10000		// Far
    );
    camera.position.set( 15, 10, 15 );
    camera.lookAt( scene.position );
    controls = new THREE.OrbitControls(camera, renderer.domElement);
    
    var light = new THREE.PointLight( 0x808080 );
    light.position.set( 20, 20, 20 );
    scene.add( light );
    var light1 = new THREE.AmbientLight( 0x101010 );
    light1.position.set( 20, 20, 20 );
    scene.add( light1 );
    var light2 = new THREE.PointLight( 0x808080 );
    light2.position.set( -20, 20, -20 );
    scene.add( light2 );
    var light3 = new THREE.PointLight( 0x808080 );
    light3.position.set( -20, -20, -20 );
    scene.add( light3 );
    var mkBody=(rad,color)=>{   
    var sphereGeom = new THREE.SphereGeometry(rad,16,16);
    var material = new THREE.MeshLambertMaterial( { color: color } );
    var mesh = new THREE.Mesh( sphereGeom, material );
    return mesh;
    }
    var sun = mkBody(2,0xffee00)
    scene.add( sun )
    sun.material.emissive.set(0x808000)
    var b1 = mkBody(0.7,0x80ffff)
    b1.position.set(6,0,0)
    sun.add( b1 )
    var b2 = mkBody(0.2,0xeeeeee)
    b2.position.set(1.1,0,0)
    b1.add( b2 )
    sun.onBeforeRender = function(){
        this.rotation.y+=0.01
    }
    b1.onBeforeRender = function(){
        this.rotation.y+=0.1
    }
    renderer.setClearColor( 0x404040, 1);
    
    (function animate() {
        requestAnimationFrame(animate);
        controls.update();
        renderer.render(scene, camera);
    })();
    <script src="https://threejs.org/build/three.min.js"></script>
    <script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>

    另一种选择是变换几何本身...

    var renderer = new THREE.WebGLRenderer();
    var w = 300;
    var h = 200;
    renderer.setSize( w,h );
    document.body.appendChild( renderer.domElement );
    
    var scene = new THREE.Scene();
      	var camera = new THREE.PerspectiveCamera(
    	45,		// Field of view
    	w/h,	// Aspect ratio
    	0.1,		// Near
    	10000		// Far
    );
    camera.position.set( 15, 10, 15 );
    camera.lookAt( scene.position );
    controls = new THREE.OrbitControls(camera, renderer.domElement);
    
    var light = new THREE.PointLight( 0x808080 );
    light.position.set( 20, 20, 20 );
    scene.add( light );
    var light1 = new THREE.AmbientLight( 0x101010 );
    light1.position.set( 20, 20, 20 );
    scene.add( light1 );
    var light2 = new THREE.PointLight( 0x808080 );
    light2.position.set( -20, 20, -20 );
    scene.add( light2 );
    var light3 = new THREE.PointLight( 0x808080 );
    light3.position.set( -20, -20, -20 );
    scene.add( light3 );
    var mkBody=(rad,color)=>{   
        var sphereGeom = new THREE.SphereGeometry(rad,16,16);
        var material = new THREE.MeshLambertMaterial( { color: color } );
        var mesh = new THREE.Mesh( sphereGeom, material );
        return mesh;
    }
    var sun = mkBody(2,0xffee00)
    scene.add( sun )
    sun.material.emissive.set(0x808000)
    var b1 = mkBody(0.7,0x80ffff)
    b1.position.set(6,0,0)
    sun.add( b1 )
    b1.updateMatrixWorld();
    b1.geometry.applyMatrix(b1.matrixWorld); //Transform the actual geometry...
    scene.add(b1); //Now reparent it to the scene
    b1.position.set(0,0,0); //And reset its position...
    
    sun.onBeforeRender = function(){
        this.rotation.y+=0.01
    }
    b1.onBeforeRender = function(){
        this.rotation.y+=0.1
    }
    renderer.setClearColor( 0x404040, 1);
    
    (function animate() {
        requestAnimationFrame(animate);
        controls.update();
        renderer.render(scene, camera);
    })();
    <script src="https://threejs.org/build/three.min.js"></script>
    <script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>

    【讨论】:

    • 非常感谢?,您的回答绝对正确,但是,我希望对象的父对象是场景而不是其他对象。
    • 然后你必须自己计算旋转位置并在每一帧设置它..但我敢打赌你并不真的需要父母作为场景..你为什么认为你需要那个?
    • 我添加了第二个 sn-p,它转换几何以考虑偏移。现在太阳和行星都是场景的孩子......
    • 非常感谢!?完美解决方案!?
    • 对不起,我的名声太低,无法进行手术。对不起:-(
    猜你喜欢
    • 2013-04-25
    • 2017-02-21
    • 2014-01-27
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多