Pan是OrbitControls唯一的工作方式

时间:2014-12-05 22:02:17

标签: three.js

我正在尝试编程一个你可以围绕轨道运行的太阳系并平移所以我在我的项目中添加了一个OrbitControls并且平移工作正常但是由于某些原因旋转和缩放都没有工作。我试过复制其他人的例子,我无法弄清楚我的错误。我不知道它是否只是我的电脑,但我没有理由相信它会是。

<!DOCTYPE html>

<html>

<head>
    <title>Example 01.02 - First Scene</title>
    <script type="text/javascript" src="../libs/three.js"></script>
    <script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
    <script type="text/javascript" src="../libs/stats.js"></script>
    <script type="text/javascript" src="../libs/dat.gui.js"></script>
    <script type="text/javascript" src="../libs/OrbitControls.js"></script>
    <script type="text/javascript" src="../libs/chroma.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>

<!-- 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();
        var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
        var clock = new THREE.Clock();
        // 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, 1, 5000);
        // position and point the camera to the center of the scene
        camera.position.x = -150;
        camera.position.y = 200;
        camera.position.z = 150;
        camera.lookAt(scene.position);

        // create a render and set the size
        renderer = new THREE.WebGLRenderer( {antialias:true} );
        renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

        ccontrols = new THREE.OrbitControls( camera, renderer.domElement );
        ccontrols.damping = 0.2;
        ccontrols.addEventListener( 'change', render );

        var ambiLight = new THREE.AmbientLight(0x747474)
        scene.add(ambiLight);

        var pointLight = new THREE.PointLight(0xffffff);
        pointLight.position.set(0, 0, 0);
        pointLight.distance = 100;
        scene.add(pointLight);

        //create the light box
        var lightboxgeom = new THREE.SphereGeometry(3000,50,50);
        var lightboxmat = new THREE.MeshPhongMaterial({map: THREE.ImageUtils.loadTexture("./TexturesAndModels/lightbox.png"), side: THREE.BackSide});
        var lightbox = new THREE.Mesh(lightboxgeom, lightboxmat);

        //sun and glow
        var sun = createMesh(new THREE.SphereGeometry(20, 20, 20), "texture_sun.jpg", "texture_sun_DISP.jpg", "texture_sun_SPEC.jpg");
        sun.shading = THREE.NoShading;
        var spriteMaterial = new THREE.SpriteMaterial( 
        { 
            map: new THREE.ImageUtils.loadTexture( "TexturesAndModels/GLOW.png" ), 
            useScreenCoordinates: false, alignment: THREE.SpriteAlignment.center,
            color: 0xffcc00, transparent: false, blending: THREE.AdditiveBlending
        });
        var sprite = new THREE.Sprite( spriteMaterial );
        sprite.scale.set(100, 100, 1.0);
        sun.add(sprite);    

        //mercury
        var mercury = createMesh(new THREE.SphereGeometry(1.50, 20, 20), "texture_mercury.jpg", "texture_mercury_DISP.jpg", "texture_mercury_SPEC.jpg", "texture_mercury_NRM.jpg");

        //venus
        var venus = createMesh(new THREE.SphereGeometry(3.80, 20, 20), "texture_venus_surface.jpg", "texture_venus_surface_DISP.jpg", "texture_venus_surface_SPEC.jpg", "texture_venus_surface_NRM.jpg");

        //earth and clouds
        var earth = createMesh(new THREE.SphereGeometry(4.00, 20, 20), "ColorMap.jpg", "Bump2.jpg", "SpecMask2.png", "ColorMap_NRM.jpg");

        //mars
        var mars = createMesh(new THREE.SphereGeometry(2.10, 20, 20), "texture_mars.jpg", "texture_mars_DISP.jpg", "texture_mars_SPEC.jpg", "texture_mars_NRM.jpg");

        //Jupiter
        var jupiter = createMesh(new THREE.SphereGeometry(18.7, 20, 20), "texture_jupiter.jpg", "texture_jupiter_DISP.jpg", "texture_jupiter_SPEC.jpg", "texture_jupiter_NRM.jpg");

        //saturn
        var saturn = createMesh(new THREE.SphereGeometry(18, 20, 20), "texture_saturn.jpg", "texture_saturn_DISP.jpg", "texture_saturn_SPEC.jpg", "texture_saturn_NRM.jpg");

        //uranus
        var uranus = createMesh(new THREE.SphereGeometry(15, 20, 20), "texture_uranus.jpg", "texture_uranus_DISP.jpg", "texture_uranus_SPEC.jpg", "texture_uranus_NRM.jpg");

        //neptune
        var neptune = createMesh(new THREE.SphereGeometry(14, 20, 20), "texture_neptune.jpg", "texture_neptune_DISP.jpg", "texture_neptune_SPEC.jpg", "texture_neptune_NRM.jpg");

        // position the planets
        sun.position.x=0;
        sun.position.y=0;
        sun.position.z=0;
        earth.position.y=0;
        mars.position.y=0;
        venus.position.y=0;
        mercury.position.y=0;
        saturn.position.y=0;
        jupiter.position.y=0;
        uranus.position.y=0;
        neptune.position.y=0;

        // add the planets to the scene
        scene.add(lightbox);
        scene.add(earth);
        scene.add(sun);
        scene.add(mercury);
        scene.add(venus);
        scene.add(mars);
        scene.add(jupiter);
        scene.add(saturn);
        scene.add(uranus);
        scene.add(saturn);

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

        var r = 0;
        var step = 0;

        var controls = new function() {
            this.timeScale = 1;
        }

        var gui = new dat.GUI();
        gui.add(controls, 'timeScale', 0, 10);

        // render the scene
        render();

        function createMesh(geom, imageFile, bump, spec, normal) {
            var texture = THREE.ImageUtils.loadTexture("./TexturesAndModels/" + imageFile)
            var mat = new THREE.MeshPhongMaterial();
            mat.map = texture;

            if (bump) {
                var bump = THREE.ImageUtils.loadTexture("./TexturesAndModels/" + bump)
                mat.bumpMap = bump;
                mat.bumpScale = 0.2;
            }

            if(spec) {
                var spec = THREE.ImageUtils.loadTexture("./TexturesandModels/" + spec)
                mat.specularMap = spec;
            }

            if(normal) {
                var norm = THREE.ImageUtils.loadTexture("./TexturesAndModels/" + normal)
                mat.normalMap = norm;
            }

            var mesh = new THREE.Mesh(geom, mat);
            return mesh;
        }

        function render() {
            stats.update();

            earth.position.x = Math.sin(r*0.1)*150;
            earth.position.z = Math.cos(r*0.1)*150;
            earth.rotation.y = step += controls.timeScale * 0.02;

            mercury.position.x = Math.sin(r*0.4)*58;
            mercury.position.z = Math.cos(r*0.4)*58;
            mercury.rotation.y = step/58.7;

            venus.position.x = Math.sin(r*0.1625)*108;
            venus.position.z = Math.cos(r*0.1625)*108;
            venus.rotation.y = step/243;

            mars.position.x = Math.sin(r*0.05)*228;
            mars.position.z = Math.cos(r*0.05)*228;
            mars.rotation.y = step*1.026;

            jupiter.position.x = Math.sin(r*.008)*483;
            jupiter.position.z = Math.cos(r*.008)*483;
            jupiter.rotation.y = step*2.44;

            saturn.position.x = Math.sin(r*.003)*886;
            saturn.position.z = Math.cos(r*.003)*886;
            saturn.rotation.y = step*2.35;

            uranus.position.x = Math.sin(r*.001)*1784;
            uranus.position.z = Math.cos(r*.001)*1784;
            uranus.rotation.y = step*1.34;

            neptune.position.x = Math.sin(r*.0006)*2794;
            neptune.position.z = Math.cos(r*.0006)*2794;
            neptune.rotation.y = step*1.26;

            r+=controls.timeScale * (Math.PI/180*2);

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

        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 个答案:

没有答案