如何在THREE.js中获取CLICKED元素

Nam*_*ANG 10 javascript html5 three.js

我使用THREE.js在画布中有很多元素,

他们可能会互相覆盖.

所以现在,点击一个点,

我怎么能得到这个元素?

更新:大家好,这个演示有很多帮助:http: //mrdoob.github.com/three.js/examples/canvas_interactive_cubes.html

Mic*_*man 21

使用以下代码.这将允许您添加单击事件,并在发生这种情况时执行所需操作.您可以查看页面的来源以查看他们正在做什么,这是我从中获取此代码.

document.addEventListener( 'mousedown', onDocumentMouseDown, false );

var projector = new THREE.Projector();

function onDocumentMouseDown( event ) {

    event.preventDefault();

    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.Ray( camera.position, 
                             vector.subSelf( camera.position ).normalize() );

    var intersects = ray.intersectObjects( objects );

    if ( intersects.length > 0 ) {

        intersects[ 0 ].object.materials[ 0 ].color.setHex( Math.random() * 0xffffff );

        var particle = new THREE.Particle( particleMaterial );
        particle.position = intersects[ 0 ].point;
        particle.scale.x = particle.scale.y = 8;
        scene.add( particle );

    }

    /*
    // Parse all the faces
    for ( var i in intersects ) {
        intersects[ i ].face.material[ 0 ].color
            .setHex( Math.random() * 0xffffff | 0x80000000 );
    }
    */
}
Run Code Online (Sandbox Code Playgroud)

  • 声明并初始化`projector`变量在哪里? (4认同)

Dre*_*kes 5

您链接到的示例有一个简单的API.

把它放在你的HTML中.您必须下载脚本并确保加载.

<script src='threex.domevent.js'></script>
Run Code Online (Sandbox Code Playgroud)

然后,在您的网格对象上,调用以下内容:

mesh.on('click', function()
{
    // response to click...
    mesh.scale.x *= 2;
});
Run Code Online (Sandbox Code Playgroud)

或者一个更有趣的例子,可以平滑地动画对象的旋转和颜色:

mesh.on('click', function(event)
{
    var object3d = event.target,
        rotation, color;
    if (object3d.rotation.x < Math.PI / 4) {
        rotation = {x: Math.PI / 2};
        color = {r: 1, g: 0.5, b: 0};
    } else {
        rotation = {x: 0};
        color = {r: 0.5, g: 0.75, b: 0.25};
    }
    new TWEEN.Tween(object3d.rotation)
        .to(rotation, 800)
        .easing(TWEEN.Easing.Bounce.EaseOut)
        .start();
    new TWEEN.Tween(object3d.material.color)
        .to(color, 300)
        .easing(TWEEN.Easing.Quartic.EaseIn)
        .start();
})
Run Code Online (Sandbox Code Playgroud)

  • 它说Uncaught TypeError:Object [object Object]没有方法'on'mazeJs.js:198 (2认同)

jas*_*n82 5

也许这个工具可以帮助你,一个全交互管理器,帮助 Three.js 轻松绑定交互事件

更多细节参见三.交互

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 at parent or any display-tree node,
 * source event will bubble up along with display-tree.
 */
scene.on('touchstart', ev => {
  console.log(ev);
})
scene.on('touchmove', ev => {
  console.log(ev);
})
Run Code Online (Sandbox Code Playgroud)