Html 三个 js 可点击对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17638933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:07:31  来源:igfitidea点击:

three js clickable objects

javascripthtmlthree.js

提问by Mashael

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.

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

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>

回答by Ratih Nurmalasari

Absolutely you need to implement raycasterto 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:

绝对需要在three.js中实现raycaster来选择/单击/选择某些对象。我已经在我的一个项目中使用 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/
  1. http://soledadpenades.com/articles/three-js-tutorials/object-picking/
  2. http://jensarps.de/2012/08/10/mouse-picking-collada-models-with-three-js/

回答by jasonChen82

you can use this event manager three.interaction

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

and see this demo

并查看此演示

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);
})

回答by Filipe

You can use pickingRayto perform clicking with orthographic camera like that follows in this code:

您可以使用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);

But if you want perform clicking on perspective camera you have to perform an clicking using the unprojectVectorlike that follows below:

但是如果你想在透视相机上执行点击,你必须使用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);