三个js可点击的对象 [英] three js clickable objects

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

问题描述

我在three.js中创建了两个3d对象网格。
我想将click事件添加到这些对象中,这样当我点击其中一个对象时,它会被放大,当我点击另一个时,它会旋转。

I have two 3d objects "meshes" that I created in three.js. I want to add click event to these objects so that when I click on one of them, it is scaled up and when I click on the other one, it rotates.

我尝试使用此方法向对象添加click事件并且它无法正常工作。

I tried this method to add a click event to an object and it's not working.

<script src='threex.domevent.js'> </script>
<script>

        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera(20, window.innerWidth/window.innerHeight, 1, 1000);
        camera.position.z = 100;

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

        var geometry = new THREE.CubeGeometry(1,1,1);
        var material = new THREE.MeshBasicMaterial({color: 0x00ff00});
        var cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        cube.addEventListener( 'click',function(){cubeScale()} , false );

        function cubeScale()
            {
            cube.scale.x *= 20;
            }


        var render = function () {
            requestAnimationFrame(render);
            cube.rotation.y += 0.1;
            renderer.render(scene, camera);
        };

        render();

    </script>


推荐答案

你绝对需要实施 raycaster 选择/单击/选择three.js中的某个对象。我已经在我的一个项目中使用three.js和raycaster对collada模型实现了它。供您参考,这些链接可以帮助您:

Absolutely you need to implement raycaster to select/click/pick certain object in three.js. I've already implemented it in one of my project using three.js and raycaster against collada model. For your reference, these links can help you:


  1. http://soledadpenades.com/articles/three-js-tutorials/object-picking/

  2. < a href =http://jensarps.de/2012/08/10/mouse-picking-collada-models-with-three-js/ =noreferrer> http://jensarps.de/2012/08 / 10 / mouse-picking-collada-models-with-three-js /

  1. http://soledadpenades.com/articles/three-js-tutorials/object-picking/
  2. http://jensarps.de/2012/08/10/mouse-picking-collada-models-with-three-js/

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

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