如何使火箭/月球绕地球三转。

时间:2019-04-10 23:35:10

标签: javascript three.js

我一直在进行Three.js项目,该项目涉及一艘绕地球旋转的火箭飞船,该火箭也绕另一颗行星旋转。我似乎使地球绕其轴旋转,但是我不确定如何使地球绕另一个物体旋转。我尝试了几种方法,但似乎无法使它适用于我的项目。

有没有办法使之成为可能?

----------------------------------编辑------------ -----------------

添加了一个JFiddle,尽管它在那里出现了一个错误,表示场景Utils不可用,但我不知道如何链接它。

https://jsfiddle.net/dm9637cL/

我的项目的代码如下

<!DOCTYPE html>

<html>

    <head>
        <title>template</title>
        <link rel="shortcut icon" href="../favicon.ico">
        <script type="text/javascript" src="../libs/three.js"></script>
        <script type="text/javascript" src="../libs/jquery.js"></script>
        <script type="text/javascript" src="../libs/stats.js"></script>
        <script type="text/javascript" src="../libs/dat.gui.js"></script>
        <style>
            body{
                /* set margin to 0 and overflow to hidden, to go fullscreen */
                margin: 0;
                overflow: hidden;
            }
        </style>
    </head>
    <body>

        <div id="Stats-output">
        </div>
        <!-- Div which will hold the Output -->
        <div id="WebGL-output">
        </div>
<script src="../libs/OrbitControls.js" type="text/javascript"></script>
        <!-- Javascript code that runs our Three.js examples -->
        <script type="text/javascript">

            // once everything is loaded, we run our Three.js stuff.
            $(function() {

                var stats = initStats();

                // create a scene, that will hold all our elements such as objects, cameras and lights.
                var scene = new THREE.Scene();

                // create a camera, which defines where we're looking at.
                var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

                // create a render and set the size
                var renderer = new THREE.WebGLRenderer();
                renderer.setClearColor(0xEEEEEE, 1.0);
                renderer.setSize(window.innerWidth, window.innerHeight);
                renderer.shadowMap.enabled = true;

                window.addEventListener('resize', function()
            {
                var width = window.innerWidth;
                var height = window.innerHeight;
                renderer.setSize(width, height);
                camera.aspect = width / height;
                camera.updateProjectionMatrix();

            } );

            controls = new THREE.OrbitControls(camera, renderer.domElement);

            // create the object group that contains all the sub-shapes
            var rocketGroup = new THREE.Object3D();
            scene.add(rocketGroup);
            //display grid
            //scene.add(new THREE.GridHelper(100,10));

            var rocketMaterial = new THREE.MeshLambertMaterial({color: 0xEEEEEE});
            var hingeMaterial = new THREE.MeshLambertMaterial({color: 0xFD1414});
            /////////////////////////////////////////////////////////////////////////////////////////
            var rocketAntennaOne = new THREE.CylinderGeometry( 0, 1, 1, 5);   
            var rocketAntennaOneMesh = new THREE.Mesh(rocketAntennaOne, hingeMaterial, new THREE.MeshPhongMaterial());            
            var rocketAntennaTwo = new THREE.CylinderGeometry( 1, 1.5, 0.5, 5 );            
            var rocketAntennaTwoMesh = new THREE.Mesh(rocketAntennaTwo, hingeMaterial, new THREE.MeshPhongMaterial());
            var rocketAntennaThree = new THREE.CylinderGeometry( 1.5, 2, 0.5, 5 );
            var rocketAntennaThreeMesh = new THREE.Mesh( rocketAntennaThree,hingeMaterial, new THREE.MeshPhongMaterial());
            /////////////////////////////////////////////////////////////////////////////////////////
            var rocketBodyOne = new THREE.CylinderGeometry( 2, 4, 3, 5 );   
            var rocketBodyOneMesh = new THREE.Mesh(rocketBodyOne,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyTwo = new THREE.CylinderGeometry( 4, 5.5, 3, 5 );   
            var rocketBodyTwoMesh = new THREE.Mesh(rocketBodyTwo,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyThree = new THREE.CylinderGeometry( 5.5, 7, 3, 5 );   
            var rocketBodyThreeMesh = new THREE.Mesh(rocketBodyThree,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyFour = new THREE.CylinderGeometry( 7, 7.5, 3, 5 );   
            var rocketBodyFourMesh = new THREE.Mesh(rocketBodyFour,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyFive = new THREE.CylinderGeometry( 7.5, 8, 3.5, 5 );   
            var rocketBodyFiveMesh = new THREE.Mesh(rocketBodyFive,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodySix = new THREE.CylinderGeometry( 8, 8, 5, 5 );   
            var rocketBodySixMesh = new THREE.Mesh(rocketBodySix,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodySeven = new THREE.CylinderGeometry( 8, 7.5, 3.5, 5 );   
            var rocketBodySevenMesh = new THREE.Mesh(rocketBodySeven,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyEight = new THREE.CylinderGeometry( 7.5, 7, 3.5, 5 ); 
            var rocketBodyEightMesh = new THREE.Mesh(rocketBodyEight,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyNine = new THREE.CylinderGeometry( 7, 6.5, 3, 5 ); 
            var rocketBodyNineMesh = new THREE.Mesh(rocketBodyNine,rocketMaterial, new THREE.MeshPhongMaterial());
            var rocketBodyTen = new THREE.CylinderGeometry( 6.5, 6, 3, 5 ); 
            var rocketBodyTenMesh = new THREE.Mesh(rocketBodyTen,rocketMaterial, new THREE.MeshPhongMaterial());
            /////////////////////////////////////////////////////////////////////////////////////////            
            var rocketBodyUpper = new THREE.CylinderGeometry( 3.5, 4, 3.5, 5 );
            var rocketBodyUpperMesh = new THREE.Mesh(rocketBodyUpper,rocketMaterial, new THREE.MeshPhongMaterial());            
            /////////////////////////////////////////////////////////////////////////////////////////            
            var rocketBodyLower = new THREE.CylinderGeometry( 2.75, 3, 2.5, 5 );
            var rocketBodyLowerMesh = new THREE.Mesh(rocketBodyLower,rocketMaterial, new THREE.MeshPhongMaterial());
            //var rocketWindow = new THREE.RingGeometry(5,6,30);
            //var rocketWindowMesh = new THREE.Mesh(rocketWindow,hingeMaterial, new THREE.MeshPhongMaterial());
            ///////////////////////////////////////////////////////////////////////////////////////
            var x = 0;
            var y = 0;                
            var RocketFinOne = new THREE.Shape();        
            RocketFinOne.moveTo( x + 0, y + 0 );
            RocketFinOne.lineTo( x + 0, y + 16);
            RocketFinOne.lineTo( x + 7, y + 20);
            RocketFinOne.lineTo( x + 7, y + 15);
            RocketFinOne.lineTo( x + 2.5, y + 13);        
            var extrudeSettings = {amount: 0.01, bevelEnabled:true, bevelSegments:2,steps:2,bevelSize:1,bevelThickness:1};        
            var geometry = new THREE.ExtrudeGeometry( RocketFinOne,extrudeSettings );
            var material = new THREE.MeshLambertMaterial( { color: 0xFD1414 } );
            var rocketFinOneMesh = new THREE.Mesh( geometry, material ) ;
            var RocketFinTwo = new THREE.Shape();        
            RocketFinTwo.moveTo( x + 0, y + 0 );
            RocketFinTwo.lineTo( x + 0, y + 16);
            RocketFinTwo.lineTo( x + 7, y + 20);
            RocketFinTwo.lineTo( x + 7, y + 15);
            RocketFinTwo.lineTo( x + 2.5, y + 13);        
            var extrudeSettings = {amount: 0.01, bevelEnabled:true, bevelSegments:2,steps:2,bevelSize:1,bevelThickness:1};        
            var geometry = new THREE.ExtrudeGeometry( RocketFinTwo,extrudeSettings );
            var material = new THREE.MeshLambertMaterial( { color: 0xFD1414 } );
            var rocketFinTwoMesh = new THREE.Mesh( geometry, material ) ;
            var RocketFinThree = new THREE.Shape();        
            RocketFinThree.moveTo( x + 0, y + 0 );
            RocketFinThree.lineTo( x + 0, y + 16);
            RocketFinThree.lineTo( x + 7, y + 20);
            RocketFinThree.lineTo( x + 7, y + 15);
            RocketFinThree.lineTo( x + 2.5, y + 13);        
            var extrudeSettings = {amount: 0.01, bevelEnabled:true, bevelSegments:2,steps:2,bevelSize:1,bevelThickness:1};        
            var geometry = new THREE.ExtrudeGeometry( RocketFinThree,extrudeSettings );
            var material = new THREE.MeshLambertMaterial( { color: 0xFD1414 } );
            var rocketFinThreeMesh = new THREE.Mesh( geometry, material ) ;
            var RocketFinFour = new THREE.Shape();        
            RocketFinFour.moveTo( x + 0, y + 0 );
            RocketFinFour.lineTo( x + 0, y + 16);
            RocketFinFour.lineTo( x + 7, y + 20);
            RocketFinFour.lineTo( x + 7, y + 15);
            RocketFinFour.lineTo( x + 2.5, y + 13);        
            var extrudeSettings = {amount: 0.01, bevelEnabled:true, bevelSegments:2,steps:2,bevelSize:1,bevelThickness:1};        
            var geometry = new THREE.ExtrudeGeometry( RocketFinFour,extrudeSettings );
            var material = new THREE.MeshLambertMaterial( { color: 0xFD1414 } );
            var rocketFinFourMesh = new THREE.Mesh( geometry, material ) ;
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        var planetEarth = createPlanetEarthMesh(new THREE.SphereGeometry(100, 400, 400));
        var planetMoon = createMoonMesh (new THREE.SphereGeometry (40,100,100));
                // add the sphere to the scene





            scene.add(planetEarth);
            scene.add(planetMoon);

            //scene.add(EarthMesh);
            //////////////////////////////////////
            scene.add(rocketAntennaOneMesh);
            scene.add(rocketAntennaTwoMesh);
            scene.add(rocketAntennaThreeMesh);
            /////////////////////////////////////
            scene.add(rocketBodyOneMesh);
            scene.add(rocketBodyTwoMesh);  
            scene.add(rocketBodyThreeMesh);  
            scene.add(rocketBodyFourMesh);  
            scene.add(rocketBodyFiveMesh);  
            scene.add(rocketBodySixMesh);
            scene.add(rocketBodySevenMesh);  
            scene.add(rocketBodyEightMesh);
            scene.add(rocketBodyNineMesh);  
            scene.add(rocketBodyTenMesh);
            ///////////////////////////////////////
            scene.add(rocketBodyUpperMesh);
            scene.add(rocketBodyUpperMesh);
            ///////////////////////////////////////
            //scene.add(rocketWindowMesh);
            //////////////////////////////////////
            scene.add( rocketFinOneMesh );
            scene.add( rocketFinTwoMesh );
            scene.add( rocketFinThreeMesh );
            scene.add( rocketFinFourMesh );


            planetEarth.position.y=-100;
            planetMoon.position.x=-100;
            //Specify the position of the rocket cone
            rocketAntennaOneMesh.position.y = 52.05;
            rocketAntennaTwoMesh.position.y = 51.3;
            rocketAntennaThreeMesh.position.y = 50.8;
            rocketBodyOneMesh.position.y = 49.1;
            rocketBodyTwoMesh.position.y = 46.1;
            rocketBodyThreeMesh.position.y = 43.1;
            rocketBodyFourMesh.position.y = 40.1;
            rocketBodyFiveMesh.position.y = 36.9;
            rocketBodySixMesh.position.y = 32.7;
            rocketBodySevenMesh.position.y = 28.5;
            rocketBodyEightMesh.position.y = 25.2;
            rocketBodyNineMesh.position.y = 22;
            rocketBodyTenMesh.position.y = 19;            
            /////////////////////////////////////////////////////////            
            rocketBodyUpperMesh.position.y = 16;
            rocketBodyLowerMesh.position.y = 14;
            ////////////////////////////////////////////////////////
            //rocketWindowMesh.position.y = 100;
            //rocketwindowMesh.position.x = -20;
            ////////////////////////////////////////////////////
            rocketFinOneMesh.position.x=-7;
            rocketFinOneMesh.position.y=3;
            rocketFinOneMesh.rotation.z = 0.1 * Math.PI;

            rocketFinTwoMesh.position.x=6   ;
            rocketFinTwoMesh.position.y=3;
            rocketFinTwoMesh.rotation.z = 0.1 * Math.PI;
            rocketFinTwoMesh.rotation.y = 1 * Math.PI;

            rocketFinThreeMesh.position.x=0;
            rocketFinThreeMesh.position.y=3;
            rocketFinThreeMesh.position.z = 7;
            rocketFinThreeMesh.rotation.z = 0.1 * Math.PI;
            rocketFinThreeMesh.rotation.y = 0.5 * Math.PI;


            rocketFinFourMesh.position.x=0;
            rocketFinFourMesh.position.y=3;
            rocketFinFourMesh.position.z = -6;
            rocketFinFourMesh.rotation.z = 0.1 * Math.PI;
            rocketFinFourMesh.rotation.y = 1.5 * Math.PI;


            //Add the rocketCone to the lowpolyRocket group
            rocketGroup.add(rocketAntennaOneMesh);
            rocketGroup.add(rocketAntennaTwoMesh);
            rocketGroup.add(rocketAntennaTwoMesh);
            /////////////////////////////////////////
            rocketGroup.add(rocketBodyOneMesh);
            rocketGroup.add(rocketBodyTwoMesh);
            rocketGroup.add(rocketBodyThreeMesh);
            rocketGroup.add(rocketBodyFourMesh);
            rocketGroup.add(rocketBodyFiveMesh);
            rocketGroup.add(rocketBodySixMesh);
            rocketGroup.add(rocketBodySevenMesh);
            rocketGroup.add(rocketBodyEightMesh);
            rocketGroup.add(rocketBodyNineMesh);
            rocketGroup.add(rocketBodyTenMesh);
            //////////////////////////////////////////
            rocketGroup.add(rocketBodyUpperMesh);
            rocketGroup.add(rocketBodyLowerMesh);
            /////////////////////////////////////////
            rocketGroup.add(rocketFinOneMesh);
            rocketGroup.add(rocketFinTwoMesh);
            rocketGroup.add(rocketFinThreeMesh);
            rocketGroup.add(rocketFinFourMesh);



                // position and point the camera to the center of the scene
                camera.position.x = -400;
                camera.position.y = 0;
                camera.position.z = 400;
                camera.lookAt(scene.position);

                // add subtle ambient lighting
                var ambientLight = new THREE.AmbientLight(0x0c0c0c);
                scene.add(ambientLight);

                // add spotlight for the shadows
                var spotLight = new THREE.PointLight(0xffffff);
                spotLight.position.set(-200, 50, 150);
                spotLight.castShadow = true;
                scene.add(spotLight);

                // add the output of the renderer to the html element
                $("#WebGL-output").append(renderer.domElement);

                var controls = new function() {

                    this.EarthRotationSpeed = 1.0;
                };

                var gui = new dat.GUI();
                gui.add(controls, 'EarthRotationSpeed', 0, 5);

                // call the render function
                var angle = 0;
                render();

                function render() {
                    stats.update();

                    angle += 0.002 * controls.EarthRotationSpeed;
                    //this.speed=0.005;
                    planetEarth.rotation.y = angle;

                    // render using requestAnimationFrame
                    requestAnimationFrame(render);
                    renderer.render(scene, camera);
                }

                    function createPlanetEarthMesh(geom) {
                    var loader = new THREE.TextureLoader();
                    var planetEarthTexture = loader.load("../assets/textures/planets/Earth.png");
                    var normalEarthTexture = loader.load("../assets/textures/planets/EarthNormal.png");

                    var planetEarthMaterial = new THREE.MeshPhongMaterial({map: planetEarthTexture, bumpMap: normalEarthTexture});

                    // create a multimaterial
                    var planetEarthMesh = THREE.SceneUtils.createMultiMaterialObject(geom, [planetEarthMaterial]);

                    return planetEarthMesh;
                }

                function createMoonMesh(geom) {
                    var loader = new THREE.TextureLoader();
                    var planetMoonTexture = loader.load("../assets/textures/planets/moon.jpg");
                    var normalMoonTexture = loader.load("../assets/textures/planets/moonbump.jpg");

                    var planetMoonMaterial = new THREE.MeshPhongMaterial({map: planetMoonTexture, bumpMap: normalMoonTexture});

                    // create a multimaterial
                    var planetMoonMesh = THREE.SceneUtils.createMultiMaterialObject(geom, [planetMoonMaterial]);

                    return planetMoonMesh;
                }

                function initStats() {

                    var stats = new Stats();

                    stats.setMode(0); // 0: fps, 1: ms

                    // Align top-left
                    stats.domElement.style.position = 'absolute';
                    stats.domElement.style.left = '0px';
                    stats.domElement.style.top = '0px';

                    $("#Stats-output").append(stats.domElement);

                    return stats;
                }
            });

        </script>
    </body>
</html>

0 个答案:

没有答案