在three.js的球体上的纬度经度

时间:2013-08-09 21:43:33

标签: three.js geometry latitude-longitude ray-picking mouse-picking

我在threeJS中有一个可以向任何方向旋转的地球仪。就像目前一样,无论旋转如何,我都可以读取位于地球中心的正确经度和纬度。地球的方向是raycaster.ray.origin.x和raycaster.ray.origin.y读取0,0,其中本初子午线和赤道交叉。 raycaster.ray.origin.z读取500。

然而,我想要去的地方是根据鼠标在地球上的点击位置来确定纬度和经度。我试图从raycaster.ray.direction.x,y,z计算它,但是我得到了读数是否我在对象上。

如果我从原点0,0读取方向,我的raycaster.ray.direction.x和y都将读取0并且z将读取-100(我将这些乘以100以使用更好的数字)。在圆周上,X将在地球的顶部和底部读取0,从左到右读取-40到40。 Y将从上到下读取40到-40,从左到右读取0,并且Z方向将围绕整个圆周-90。然而,数字将继续增加/减少,一直到屏幕的边缘。

当地球仪朝向北极/南极时也是如此,但是,中心的y在圆周上为-100/100和-90/90,x为 - 100/100四分之一在全球各地和-90/90周围。

是一种获取我点击的位置的值的方法,只是在对象而不是整个屏幕上?我不知道如何将方向与原点相关联。我觉得我到了某个地方,但随后又感到困惑。这是我得到的一切。任何帮助将非常感激。

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>latAndLong</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                color: #808080;
                font-family:Monospace;
                font-size:13px;
                text-align:center;

                background-color: #ffffff;
                margin: 0px;
                overflow: hidden;
            }

            #info {
                position: absolute;
                top: 0px; width: 100%;
                padding: 5px;
            }

            a {

                color: #0080ff;
            }

        </style>
    </head>
    <body>

        <div id="container"></div>
        <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - earth demo</div>

        <script src="javascript/mrdoob-three.js-ad419d4/build/three.min.js"></script>

        <script src="javascript/TrackballControls.js"></script>

        <script>

            var container, stats;
            var camera, scene, renderer;
            var group;
            var mouseX = 0, mouseY = 0;
            var mesh;
            var windowHalfX = window.innerWidth / 2;
            var windowHalfY = window.innerHeight / 2;
            var earthgeometry;
            var earthmaterial;
            var earthloader;

            var vector;
            var raycaster;
            projector = new THREE.Projector();


            var text, plane;


            init();
            animate();

            function init() {

                container = document.getElementById( 'container' );

                camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
                camera.position.z = 500;

                scene = new THREE.Scene();

                group = new THREE.Object3D();
                scene.add( group );

                // earth

                earthloader = new THREE.TextureLoader();
                earthloader.load( 'images/oMap.png', function ( texture ) {

                 earthgeometry = new THREE.SphereGeometry( 200, 50, 50 );

                 earthmaterial = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );
                 mesh = new THREE.Mesh( earthgeometry, earthmaterial );
                    group.add( mesh );

                } );

                //init renderer
                renderer = new THREE.CanvasRenderer();
                renderer.setSize( window.innerWidth, window.innerHeight );

                container.appendChild( renderer.domElement );


                window.addEventListener( 'resize', onWindowResize, false );


                // trackball mouse controls
                // Control Camera with Mouse

                controls = new THREE.TrackballControls( camera, container );

                controls.rotateSpeed = 1.0;

                controls.zoomSpeed = 1.2;

                controls.panSpeed = 0.8;

                controls.noZoom = true;

                controls.noPan = true;

                controls.staticMoving = true;

                controls.dynamicDampingFactor = 0.3;

                controls.keys = [ 65, 83, 68 ];

                controls.addEventListener( 'change', render );
            }

            function onWindowResize() {

                windowHalfX = window.innerWidth / 2;
                windowHalfY = window.innerHeight / 2;

                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();

                renderer.setSize( window.innerWidth, window.innerHeight );

            }

            function animate() {

                requestAnimationFrame( animate );

                // Update the Camera Controls
                controls.update();


                render();
            }

            function render() {



                /*camera.position.x += ( mouseX -camera.position.x) * 0.05;
                camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
                camera.lookAt( scene.position );*/



                renderer.render( scene, camera );

            }

            container.onmouseup = function(event) {
                vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5);
                projector.unprojectVector( vector, camera );
                raycaster = new THREE.Raycaster(camera.position, vector.sub( camera.position ).normalize());
                    //  var intersects = raycaster.intersectObjects(earthgeometry, true);
                    //  not working
                    //  console.log(intersects[0].position.x + ' intersect x');
                    //  not working
                    //  console.log(intersects[0].postion.y + ' intersect y');

                //  console.log(vector.x + 'vector x');
                //  console.log(raycaster.ray.origin.x + ' origin x');
                //  console.log(vector.y + 'vector y');
                //  console.log(raycaster.ray.origin.y + ' origin y');
                //  console.log(vector.z + 'vector z');
                //  console.log(raycaster.ray.origin.z + ' origin z');
                //  console.log(vector);
                //  console.log(raycaster);
                //  console.log(earthgeometry);
                //  console.log(earthmaterial);
                //  console.log(earthloader);
                //  console.log(raycaster.ray.origin.z + 'ray origin z');
                //  console.log(group);
                //  console.log(scene);


                //Gets latitude and longitude for the center of globe facing you


                var long;
                if(raycaster.ray.origin.x <= 0 && raycaster.ray.origin.z >=0 ) {
                    long = raycaster.ray.origin.x * -.18;
                    console.log('West');
                    console.log(long);
                    console.log(raycaster.ray.direction.x * 100 + 'ray x');
                } else if (raycaster.ray.origin.x <= 0 && raycaster.ray.origin.z <=0 ) {
                    long = (raycaster.ray.origin.x + 500) * .18 + 90;
                    console.log('West');
                    console.log(long);
                    console.log(raycaster.ray.direction.x * 100 + 'ray x');
                } else if (raycaster.ray.origin.x >= 0 && raycaster.ray.origin.z >=0 ) {
                    long = raycaster.ray.origin.x * .18;
                    console.log('East');
                    console.log(long);
                    console.log(raycaster.ray.direction.x * 100 + 'ray x');
                } else {
                    long = (raycaster.ray.origin.x - 500) * -.18 + 90;
                    console.log('East');
                    console.log(long);
                    console.log(raycaster.ray.direction.x * 100 + 'ray x');
                }

                    var lat;
                if (raycaster.ray.origin.y >= 0) {
                lat = raycaster.ray.origin.y * .18;
                console.log('North');
                console.log(lat);
                console.log(raycaster.ray.direction.y * 100 + 'ray y');
                console.log(raycaster.ray.direction.z * 100 + 'ray z');
                } else {
                lat = raycaster.ray.origin.y * -.18;
                console.log('South');
                console.log(lat);
                console.log(raycaster.ray.direction.y * 100 + 'ray y');
                console.log(raycaster.ray.direction.z * 100 + 'ray z');
                }
            }



        </script>

    </body>
</html>

0 个答案:

没有答案