Giter VIP home page Giter VIP logo

three.interaction.js's Introduction

three.interaction

npm javascript style guide Standard Version

a full-interaction event manager, help three.js binding interaction event more simple

install

npm install -S three.interaction

usage

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

Documentation

documentation

Examples

examples cube

examples cube overlap

three.interaction.js's People

Contributors

jasonchen1982 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

three.interaction.js's Issues

filter objects in scene to raycast, as raycaster is heavy

After digging the docs, I found no option to filter objects in the scene to do raycasting. When my scene have some big mesh, the performance is not good. If I do the raycast manually, instead of

raycaster.intersectObjects(this.scene.children, true)

I will do

raycaster.intersectObjects(objectsShouldListenToEvent, true)

So I think an option such as func filterSceneChildren or array objectsShouldListenToEvent is good for cases like mine

How to judge a click on a blank scene ?

With this tool, I can normally listen for mouse events for the model.
But i don't know how to judge a click on a blank scene .

    const pointer = new THREE.Vector2();
    document.addEventListener( 'pointermove', onPointerMove );
    function onPointerMove( event:any ) {

        pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

    }
    const nDiv:any = contain3DEl.current;
    nDiv.addEventListener('click', ()=>{
        const raycaster = new THREE.Raycaster();
        raycaster.setFromCamera( pointer, camera );
        const intersects = raycaster.intersectObjects( modelNodeList );
        if ( intersects.length <= 0 ) {
            setSelectedModelObject(null);
            props.onModelNodeSelected('');
            PubSub.publish('menu_item_cancel_selected', '');
        }
    })

like intersects<=0 effect

@jasonChen1982

NPM install fails

Installing with NPM now gives this error:
Any thoughts ?

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/three
npm ERR!   three@"^0.117.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer three@"^0.89.0" from [email protected]
npm ERR! node_modules/three.interaction
npm ERR!   three.interaction@"" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Question: Pointer events behaviour of untrusted events

Hi.

I want to understand the event behaviour of three.interaction.
And I want to fire events by script into the scene (click and mousedown/mouseup).
But just click events will be recognized.

var element = document.getElementById("webgl");
element.addEventListener('mousedown', function(ev) {
    console.log(ev);
});
element.addEventListener('click', function(ev) {
    console.log(ev);
});

var pageX= 493, pageY= 730;
var screenX= 3949, screenY= 493;
var ev;

// Recognized and animated
ev = document.createEvent("MouseEvents");
ev.initMouseEvent("click", true, true, window, 0, screenX, screenY, pageX, pageY, false, false, false, false, 0, null);
element.dispatchEvent(ev);

// Not recognized
ev = document.createEvent("MouseEvents");
ev.initMouseEvent("mousedown", true, true, window, 0, screenX, screenY, pageX, pageY, false, false, false, false, 0, null);
element.dispatchEvent(ev);

Both events are printed in the debug console, but the threejs scene doesn't catch the mousedown event.

Double click event

Hi,

thanks for the fantastic tool, can you please help me to figure out how to identify a double click event?
When I interact with objects I usually get a single click but I now need to differentiate when the user double click (+ hold) for example when using OrbitControls for moving around the camera.

  1. Click event on an object (OK)
  2. Click event anywhere outside of an object (OK)
  3. How do I know if the user single-clicked with the intention to interact with the object or rather double-clicked in to use the OrbitControls instead?

I was thinking of measuring the time between each single click event in order to figure out if it is a double click but it sounds a bit archaic... any idea?

Thanks a mill!

How to import module in a vanilla JavaScript file?

Only see documentation on support for nodejs based projects, but it would be very helpful and could broaden the reach of this package if support for including an a minified script in JavaScript was added as well.

事件触发

当2个物体重叠时,2个物体的onclick事件都会触发,如何只触发离相机最近的物体的事件

interaction bug

image
Sometimes things are clearly detected,
These objects is children of listening object.

But returned hit is false.

请问多场景怎么使用

我正用使用您的three.interaction库,有个问题想请教下,在一个页面中我有两个场景(scene)每个场景添加一个相机,2个场景共用一个渲染器,我想与两个不同的场景中的物体交互,但new Interaction实例时只能传一套场景和相机,想请问下有好的解决办法吗?

只有 `mousemove` 事件生效

import * as THREE from "three";
import { Interaction } from "three.interaction";

new Interaction(renderer, scene, camera);
var cube = new THREE.Mesh(
  new THREE.BoxGeometry(3, 3, 3),
  new THREE.MeshPhongMaterial({ color: 0xffffff })
);
scene.add(cube);

cube.on("click", () => {
  console.log("click");
});
cube.on("mousemove", () => {
  console.log("mousemove");
});
cube.on("mousedown", () => {
  console.log("mousedown");
});
cube.on("mouseup", () => {
  console.log("mouseup");
});
cube.on("mouseout", () => {
  console.log("mouseout");
});
cube.on("mouseover", () => {
  console.log("mouseover");
});

2 high severity vulnerabilities

% npm install -S three.interaction

added 2 packages, and audited 3 packages in 5s

2 high severity vulnerabilities

Some issues need review, and may require choosing
a different dependency.

% npm audit
npm audit report

three <0.125.0
Severity: high
Denial of service in three - GHSA-fq6p-x6j3-cmmq
No fix available
node_modules/three
three.interaction *
Depends on vulnerable versions of three
node_modules/three.interaction

2 high severity vulnerabilities

Some issues need review, and may require choosing
a different dependency.

No License

I do not see a license associated with the repository.

ES6 directly into Browser

Your lib is fantastic and really powerful.

We are loading ES6 directly into modern browsers for optimized loading with Import promise etc.
Noticing that three.interaction causes error when used directly.

Example:

<script type="module">     
    import * as THREE from "./node_modules/three/build/three.module.js";
    import { Interaction } from "./node_modules/three.interaction/build/three.interaction.module.js";
</script>

Produces error:

Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".

As incremental loading becomes more popular with new browser import Promise, bundle based approach will become less appealing.

Any thoughts?
Is there a way to use /three.interaction.module.js directly in a browser ?
Perhaps a version on https://www.unpkg.com/ ?
Maybe there is a way to update / extend to work with bundlers and directly at the same time?

Can you add double - click and right - click events

Your library is very useful to me, but there are still some problems:
1, Hope to be able to increase double-click events and right-click events;
2. When adding an event to the Scene, the event should be triggered even if the object is not hit, which is helpful for some logical processing of the program;

Property 'on' does not exist on type 'Mesh'

I'm not able to use this lib in Angular2. I keep getting this error: Property 'on' does not exist on type 'Mesh'
My code:
var interaction = new Interaction(this.renderer, this.scene, this.camera)
var cube = new THREE.Mesh(
new THREE.BoxGeometry(3, 3, 3),
new THREE.MeshPhongMaterial({ color: 0xffffff })
);
this.scene.add(cube);
cube.on('pointerdown', function(ev) {
console.log('pointerdown');
});

"destroy()" function bug

When I called the destroy method, an error occurred

three.interaction.module.js:2426 Uncaught TypeError: this.removeAllListeners is not a function
at Interaction.destroy (three.interaction.module.js:2426)

Gltf object click listener

I've followed documentation for adding click listener on gltf object , but event not triggered .
`loader = new GLTFLoader()

loader.load(url, gltf => {
  model = gltf.scene
  model.scale.set(scale, scale, scale)
  model.rotation.x = rotation
  model.position.x = x
  model.position.y = y
  model.position.z = z

  root.add(model)
  model.on('click', function(ev) {
   console.log(' model just clicked ! ');
   callback();
  });
})`

would you please tell me where am i doing wrong ?

EventDispatcher cannot be invoked without 'new'"

import {  EventDispatcher, Raycaster, Vector2 } from "three";

export class EventManage extends EventDispatcher{
    mouse = new Vector2()
    raycaster = new Raycaster()
    dom
    camera
    scene
    last = null
    /**
     * 
     *  @param {HTMLElement} dom
     * @param {Camera} camera
     * @param {Scene} scene
     */
    constructor(
        dom, camera, scene
    ) {
        super({})
        this.dom = dom
        this.camera = camera
        this.scene = scene

        const mouse = this.mouse
        const raycaster = this.raycaster

        let last = null

        dom.addEventListener('mousemove', (e) => {
            mouse.x = e.offsetX / dom.offsetWidth * 2 - 1
            mouse.y = -e.offsetY * 2 / dom.offsetHeight + 1
            raycaster.setFromCamera(mouse, camera);
            const intersection = raycaster.intersectObjects(this.scene.children)
            if (intersection.length > 0) {
                // console.log(intersection[0]);
                let object = intersection[0].object
                object.dispatchEvent({
                    type: 'mousemove',
                    point: intersection[0].point
                })
                if (last && last.uuid == object.uuid) {
                } else {
                    if (last)
                        last.dispatchEvent({
                            type: 'mouseout',
                            point: intersection[0].point
                        })
                    object.dispatchEvent({
                        type: 'mousein',
                        point: intersection[0].point
                    })
                }
                last = object
            }

        })
        // dom.addEventListener('mouseup', (e) => {
        //     raycaster.setFromCamera(mouse, camera);
        //     const intersection = raycaster.intersectObjects(this.scene.children)
        //     if (intersection.length > 0) {
        //         // console.log(intersection[0]);
        //         intersection[0].object.dispatchEvent({
        //             type: 'mouseup',
        //             point: intersection[0].point
        //         })
        //     }
        // })
        // dom.addEventListener('mousedown', (e) => {
        //     raycaster.setFromCamera(mouse, camera);
        //     const intersection = raycaster.intersectObjects(this.scene.children)
        //     if (intersection.length > 0) {
        //         // console.log(intersection[0]);
        //         intersection[0].object.dispatchEvent({
        //             type: 'mousedown',
        //             point: intersection[0].point
        //         })
        //     }
        // })
        dom.addEventListener('click', (e) => {
            raycaster.setFromCamera(mouse, camera);
            const intersection = raycaster.intersectObjects(this.scene.children)
            
            if (intersection.length > 0) {
                // console.log(intersection[0]);
                intersection[0].object.dispatchEvent({
                    type: 'click',
                    point: intersection[0].point
                })
            }
        })

        console.log(this);
    }
}
permission.js:29 TypeError: Class constructor EventDispatcher cannot be invoked without 'new'
    at new EventManage (index.js:18:1)
    at PredictionManage.GeologyMesh (index.js:43:1)
    at new PredictionManage (index.js:4:1)
    at VueComponent.mounted (selector.js?type=script&index=0!./src/views/GeologicalPredictionAndPrediction/components/geologyPredictionManage/index.vue:24:40)
    at invokeWithErrorHandling (vue.esm.js:3793:61)
    at callHook$1 (vue.esm.js:3194:13)
    at Object.insert (vue.esm.js:4981:13)
    at invokeInsertHook (vue.esm.js:7011:38)
    at VueComponent.patch [as __patch__] (vue.esm.js:7222:9)
    at Vue._update (vue.esm.js:2932:25)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.