JavaScript交互式3D对象

时间:2013-11-24 20:54:17

标签: javascript html three.js

我正在尝试使用JavaScript创建一个交互式Sphere来为HCI分配,问题是我是JavaScript和Three.js的新手。

我所追求的是,​​当点击球体时,它会显示特定主题的统计数据。我已经创建了球体并使其成为一个物体,但我在球体的相互作用方面遇到了麻烦。当点击球体时我不关心是否打开div或警报但是我只需要它作为虚拟版本工作

下面的

是JavaScript和THREE.js中的一个例子:

    var sphere = new Object({}); //declared sphere as an object first.
     var angularSpeed = 0.2; 
     var lastTime = 0;

     function animate (){
      //update
      var time = (new Date()).getTime();
      var timeDiff = time - lastTime;
      var angleChange = angularSpeed * timeDiff * 0.1 * Math.PI / 1000;
      sphere.rotation.y -= angleChange;
      sphere2.rotation.sphere -=angleChange;
      lastTime = time;

      // render
      renderer.render(scene, camera);
      requestAnimationFrame(function(){ //request new frame
        animate();
        });
    }

      // renderer
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      // camera
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1,          1000);
      camera.position.z = 500;

      // scene
      var scene = new THREE.Scene();


      //material
      var material = new THREE.MeshLambertMaterial({
      map:THREE.ImageUtils.loadTexture('images/earth2.jpg')});


      //sphere geometry
   sphere = new THREE.Mesh(new THREE.SphereGeometry( 100, 50, 50 ), material);

   sphere.overdraw = true;
   sphere.rotation.x = Math.PI * 0.1;
   sphere.position.x= 0; // moves position horizontally (abscissa) + = right and - = left
   sphere.position.y= 0;  // moves position virtually (ordinate) + = right and - = left
   sphere.position.z= 0; // moves position z (applicate) + = forwards and - = backwards 

      scene.add(sphere);

      //animate
      animate();

      var sphere = new Object:({event});
   function statistics(){
   alert('You clicked on the div!') // displays the statistics before the rendering
    };

        sphere.onMouseDown=statistics(event);    

1 个答案:

答案 0 :(得分:0)

.onMouseDown仅适用于HTML元素。您不能将此函数用于THREE.js对象,但Raycaster正是您想要的!

<强> jsfiddle

            var container, stats;
    var camera, scene, projector, raycaster, renderer, selected, sphere;

    var mouse = new THREE.Vector2(), INTERSECTED;

    init();
    animate();

    function init() {

        container = document.createElement( 'div' );
        document.body.appendChild( container );

        camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );

        scene = new THREE.Scene();

        var light = new THREE.DirectionalLight( 0xffffff, 2 );
        light.position.set( 1, 1, 1 ).normalize();
        scene.add( light );

        var light = new THREE.DirectionalLight( 0xffffff );
        light.position.set( -1, -1, -1 ).normalize();
        scene.add( light );

        sphere = new THREE.Mesh(new THREE.SphereGeometry( 20, 50, 50 ), new THREE.MeshNormalMaterial());

        sphere.overdraw = true;
        scene.add(sphere);

        projector = new THREE.Projector();
        raycaster = new THREE.Raycaster();

        renderer = new THREE.WebGLRenderer();
        renderer.setClearColor( 0xf0f0f0 );
        renderer.setSize( window.innerWidth, window.innerHeight );
        renderer.sortObjects = false;
        container.appendChild(renderer.domElement);

        document.addEventListener( 'mousemove', onDocumentMouseMove, false );
        window.addEventListener( 'resize', onWindowResize, false );
        renderer.domElement.addEventListener( 'mousedown', onCanvasMouseDown, false);
    }

    function animate() {

        requestAnimationFrame( animate );
        render();

    }

    function render() {
        camera.lookAt(new THREE.Vector3(0,0,0));
        camera.position = new THREE.Vector3(0,100,100);

        // find intersections
        var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
        projector.unprojectVector( vector, camera );
        raycaster.set( camera.position, vector.sub( camera.position ).normalize() );

        selected = raycaster.intersectObjects( scene.children );
        renderer.render( scene, camera );

    }

    function onWindowResize() {

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

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

    }

    function onDocumentMouseMove( event ) {

        event.preventDefault();

        mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

    }

    //detect mouse click on the sphere
    function onCanvasMouseDown( event ){
        if(selected[0].object==sphere){
            statistics();
        }
    }

    function statistics(){
       alert('You clicked on the div!') // displays the statistics before the rendering
    };