随笔 - 6  文章 - 0  评论 - 1  阅读 - 11418

threejs事件插件

基于three.js的事件插件, 自己封装的类发布到了npm上,也可以到github上下载源码

安装:

npm install object3d-events --save

引入:

import Event from 'object3d-events'

使用:

    //初始化事件对象,参数(element事件元素,scene场景实例, camera 摄像机实例, isRayAll是否检测所有 默认true)
    const objEvent = new Event(element, scene, camera, isRayAll);

    const mesh = new THREE.Mesh(
        new THREE.BoxGeometry( 10, 10, 10 ),
        new THREE.MeshBasicMaterial({ color: 0xff0000 })
    )

    //点击事件
    mesh.on('click', obj => {
        obj.material.color.set( 0x00ffff );
    })
    //双击事件
    mesh.on('dblclick', obj => {
        obj.material.color.set( 0x00ffff );
    })
    //鼠标按下事件
    mesh.on('mousedown', obj => {
        obj.material.color.set( 0x00ffff );
    })
    //鼠标松开事件
    mesh.on('mouseup', obj => {
        obj.material.color.set( 0x00ffff );
    })
    //鼠标hover事件
    mesh.on('hover', obj => {
        obj.material.color.set( 0x00ffff );
    }, obj => {
        obj.material.color.set( 0xff0000 );
    })

    //移除点击事件
    mesh.off('click');
    //移除所有事件
    mesh.offAll();

    //事件元素大小变化,需要重置事件对象:
    element.oresize = () => {
        objEvent.resize();  
    }
posted on   95码农  阅读(235)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示