11

我有两个在 three.js 中创建的 3d 对象“网格”。我想为这些对象添加点击事件,以便当我点击其中一个时,它会按比例放大,而当我点击另一个时,它会旋转。

我尝试使用此方法向对象添加单击事件,但它不起作用。

<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>
4

3 回答 3

13

你可以使用这个事件管理器three.interaction

看看这个演示

import { Scene, PerspectiveCamera, WebGLRenderer, Mesh, BoxGeometry, MeshBasicMaterial } from 'three';
import { Interaction } from 'three.interaction';

const renderer = new WebGLRenderer({ canvas: canvasElement });
const scene = new Scene();
const camera = new PerspectiveCamera(60, width / height, 0.1, 100);

// new a interaction, then you can add interaction-event with your free style
const interaction = new Interaction(renderer, scene, camera);

const cube = new Mesh(
  new BoxGeometry(1, 1, 1),
  new MeshBasicMaterial({ color: 0xffffff }),
);
scene.add(cube);
cube.cursor = 'pointer';
cube.on('click', function(ev) {});
cube.on('touchstart', function(ev) {});
cube.on('touchcancel', function(ev) {});
cube.on('touchmove', function(ev) {});
cube.on('touchend', function(ev) {});
cube.on('mousedown', function(ev) {});
cube.on('mouseout', function(ev) {});
cube.on('mouseover', function(ev) {});
cube.on('mousemove', function(ev) {});
cube.on('mouseup', function(ev) {});
// and so on ...

/**
 * you can also listen on parent-node or any display-tree node,
 * source event will bubble up along with display-tree.
 * you can stop the bubble-up by invoke ev.stopPropagation function.
 */
scene.on('touchstart', ev => {
  console.log(ev);
})
scene.on('touchmove', ev => {
  console.log(ev);
})

于 2017-12-27T03:35:55.423 回答
8

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

  1. http://soledadpenades.com/articles/three-js-tutorials/object-picking/
  2. http://jensarps.de/2012/08/10/mouse-picking-collada-models-with-three-js/
于 2014-02-17T13:19:55.263 回答
0

您可以使用pickingRay正交相机执行单击,如下代码所示:

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

var rayCaster = projector.pickingRay(vector, camera);

var intersectedObjects = rayCaster.intersectObjects(scene.children, true);

但是,如果您想在透视相机上执行点击,您必须使用以下unprojectVector类似的方式执行点击:

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

var rayCaster = projector.unprojectVector(vector, camera);

var intersectedObjects = rayCaster.intersectObjects(objects);
于 2014-02-17T13:42:18.530 回答