可点击的三个 Js 对象/凸项 [英] Clickable three Js Objects / Convex Items

查看:29
本文介绍了可点击的三个 Js 对象/凸项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了 Three.js 库中的一个示例,并尝试在对象上添加单击事件.

I modified an example from Three.js library and i try to add click event on objects.

经过多次尝试......我仍然无法听到点击.

After a lot of attempts... I'm still not capable to listen clicks.

此代码产生凸形式.

这是我的代码:

        if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

        var container, stats;

        var camera, scene, renderer;
        var  objects = [];

        init();
        animate();

        function init() {

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

            camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
            camera.position.y = 400;

            scene = new THREE.Scene();

            var light, object, object2, materials;

            scene.add( new THREE.AmbientLight( 0x404040 ) );

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

            var map = THREE.ImageUtils.loadTexture( 'textures/ash_uvgrid01.jpg' );
            map.wrapS = map.wrapT = THREE.RepeatWrapping;
            map.anisotropy = 16;

            materials = [
                new THREE.MeshLambertMaterial( { ambient: 0xbbbbbb, map: map } ),
                new THREE.MeshBasicMaterial( { color: 0x242424, wireframe: true, transparent: false, opacity: 0.1 } )
            ];


            // random convex

            points = [];
            for ( var i = 0; i < 15; i ++ ) {

                points.push( randomPointInSphere( 50 ) );

            }


            object = THREE.SceneUtils.createMultiMaterialObject( new THREE.ConvexGeometry( points ), materials );
            object.position.set( -250, 0, 200 );
            scene.add( object );


            objects.push( object );

            object.callback = function() { console.log('blabla');}





            renderer = new THREE.WebGLRenderer( { antialias: true } );
            renderer.setSize( window.innerWidth, window.innerHeight );

            container.appendChild( renderer.domElement );

            stats = new Stats();
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.top = '0px';
            container.appendChild( stats.domElement );



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

        }

        function onWindowResize() {

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

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

        }


        function randomPointInSphere( radius ) {

            return new THREE.Vector3(
                ( Math.random() - 0.5 ) * 1 * radius,
                ( Math.random() - 0.5 ) * 1 * radius,
                ( Math.random() - 0.5 ) * 1 * radius
            );

        }

        function animate() {

            requestAnimationFrame( animate );

            render();
            stats.update();

        }

        function render() {

            var timer = Date.now() * 0.0001;

            camera.position.x = Math.cos( timer ) * 800;
            camera.position.z = Math.sin( timer ) * 800;

            camera.lookAt( scene.position );

            for ( var i = 0, l = scene.children.length; i < l; i ++ ) {

                var object = scene.children[ i ];

                object.rotation.x = timer * 5;
                object.rotation.y = timer * 2.5;

            }

            renderer.render( scene, camera );

        }

        function addMeteor()
        {
                    var map = THREE.ImageUtils.loadTexture( 'textures/ash_uvgrid01.jpg' );
            map.wrapS = map.wrapT = THREE.RepeatWrapping;
            map.anisotropy = 16;

                    var materials = [
                new THREE.MeshLambertMaterial( { ambient: 0xbbbbbb, map: map } ),
                new THREE.MeshBasicMaterial( { color: 0x242424, wireframe: true, transparent: false, opacity: 0.1 } )
            ];
            objects.push( materials );
                var object3;
                var points3 = [];
            for ( var i = 0; i < 10; i ++ ) {

                points3.push( randomPointInSphere( 50 ) );

            }

            object3 = THREE.SceneUtils.createMultiMaterialObject( new THREE.ConvexGeometry( points3 ), materials );
            object3.position.set( -50, Math.floor((Math.random()*window.innerWidth)), Math.floor((Math.random()* window.innerHeight)) );
            scene.add( object3 );
            objects.push( object3 );

        }
            projector = new THREE.Projector();
        // listeners
            document.addEventListener( 'mousedown', onDocumentMouseDown, false)

            // keyboard handler
            function onDocumentMouseDown( event ) {

                event.preventDefault();
                 console.log('blabla');

                var vector = new THREE.Vector3( 
                    ( event.clientX / window.innerWidth ) * 2 - 1, 
                    - ( event.clientY / window.innerHeight ) * 2 + 1, 
                    0.5 );

                projector.unprojectVector( vector, camera );

                var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
                 console.log(objects);   
                var intersects = ray.intersectObjects( objects ); 
                  console.log(intersects);   

                if ( intersects.length > 0 ) {

                    intersects[0].object.callback();

                    console.log('blabla');

                }

            }


        renderer.render( scene, camera );

谁能帮忙.

我应该将 Mesh 推入 Objects 数组.但哪一个?

I was supposed to push Mesh into Objects array. But wich one ?

推荐答案

THREE.SceneUtils.createMultiMaterialObject() 创建一个具有两个子网格的对象.

THREE.SceneUtils.createMultiMaterialObject() creates an object with two child meshes.

因此,您需要在调用 Raycaster.intersectObjects() 时将 recursive 标志设置为 true.

So, you need to set the recursive flag to true when calling Raycaster.intersectObjects().

var intersects = ray.intersectObjects( objects, true );

three.js r.62

three.js r.62

这篇关于可点击的三个 Js 对象/凸项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆