{"id":15287949,"url":"https://github.com/danielblagy/three_mmi","last_synced_at":"2025-04-13T06:09:27.942Z","repository":{"id":57679093,"uuid":"328704729","full_name":"danielblagy/three_mmi","owner":"danielblagy","description":"A utility class that enables to easily set up a simple callback for mouse interacting with threejs mesh.","archived":false,"fork":false,"pushed_at":"2022-05-13T13:36:58.000Z","size":598,"stargazers_count":17,"open_issues_count":3,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T06:08:51.861Z","etag":null,"topics":["javascript","javascript-library","js","js-3d","mouse-event","mouse-events","threejs","threejs-example","threejs-mesh"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielblagy.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-11T15:17:17.000Z","updated_at":"2025-03-30T04:24:10.000Z","dependencies_parsed_at":"2022-08-28T06:00:54.503Z","dependency_job_id":null,"html_url":"https://github.com/danielblagy/three_mmi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielblagy%2Fthree_mmi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielblagy%2Fthree_mmi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielblagy%2Fthree_mmi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielblagy%2Fthree_mmi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielblagy","download_url":"https://codeload.github.com/danielblagy/three_mmi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670435,"owners_count":21142904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","javascript-library","js","js-3d","mouse-event","mouse-events","threejs","threejs-example","threejs-mesh"],"created_at":"2024-09-30T15:39:54.330Z","updated_at":"2025-04-13T06:09:27.920Z","avatar_url":"https://github.com/danielblagy.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MouseMeshInteraction\nis a utility class that lets you easily set up mouse event handlers for threejs meshes (THREE.Mesh objects).\n\n##\n[Making Xylophone Simulation (Youtube video)](https://youtu.be/kAAgb1qYMbA)\n\n##  \n[Making the example (Youtube video)](https://www.youtube.com/watch?v=hSBYYDx-KL0)\n\n##  \n[File System Visualizer that uses three_mmi (Github repository)](https://github.com/danielblagy/wm3dfsv)\n\n## Download:\n\nTo download the script, go to [three_mmi.js](three_mmi.js), open in raw view, right click -\u003e Save as... (or Ctrl + S)\n\nThe \"module\" version of the script is at [module/three_mmi.js](module/three_mmi.js)\n\n## Include:\n\nYou can include three_mmi as a simple js script in index.html:\n```js\n/* index.html */\n// inside of html body\n\u003cscript src=\"js/three.js\"\u003e\u003c/script\u003e\n// comes after including three.js\n\u003cscript src=\"js/three_mmi.js\"\u003e\u003c/script\u003e\n```\n\n## npm install\n\nIf you want to use three mmi in a nodejs project, you can install the module with npm.\n\n```\nnpm i @danielblagy/three-mmi\n```\n\nor you can include it as a module, for example:\n```js\n/* script.js */\nimport * as THREE from 'three'\nimport MouseMeshInteraction from './three_mmi'\n```\n\n## Usage:\nInitialize MouseMeshInteraction object (doesn't have to come before creating mesh objects)\n```js\n// pass threejs scene and camera\nconst mmi = new MouseMeshInteraction(scene, camera);\n```\nCreate an interactable mesh\n```js\n// create an interactable mesh\nconst mesh = new THREE.Mesh(geometry, material);\n// specify a name for the mesh (needed for mmi to work, you can give the same name to multiple meshes)\nmesh.name = 'my_interactable_mesh';\nscene.add(mesh);\n```\nAdd a handler of a mouse event for a mesh with a specified name\nSupported mouse events:\n* 'click' (left mouse button click)\n* 'dblclick' (left mouse button double click)\n* 'contextmenu' (right mouse button click, triggered before opening the context menu)\n* 'mouseenter' \t(mouse cursor is moved onto the element that has the listener attached)\n* 'mouseleave' \t(mouse cursor is moved off the element that has the listener attached)\n* 'mousedown' \t(mouse button is pressed on an element)\n* 'mouseup' \t(mouse button is released over an element)\n```js\n// create a handler for when user clicks on a mesh with the name 'my_interactable_mesh'\nmmi.addHandler('my_interactable_mesh', 'click', function(mesh) {\n\tconsole.log('interactable mesh has been clicked!');\n\tconsole.log(mesh);\n});\n```\nInside of render \u0026 update loop, call update function\n```js\n// put mmi.update() inside the graphics update function\nfunction animate() {\n\trequestAnimationFrame( animate );\n\t\n\tmmi.update();\n\t\n\trenderer.render( scene, camera );\n}\nanimate();\n```\nThat's it!\n\n## Quickstart Template (as a js script):\nFor a project with the following structure:\n- index.html\n- js/three.js\n- js/three_mmi.js\n\n```\n/* index.html */\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\t\u003chead\u003e\n\t\t\u003cmeta charset=\"utf-8\"\u003e\n\t\t\u003ctitle\u003ethree_mmi Quickstart Template\u003c/title\u003e\n\t\u003c/head\u003e\n\t\u003cbody\u003e\n\t\t\u003cscript src=\"js/three.js\"\u003e\u003c/script\u003e\n\t\t\u003cscript src=\"js/three_mmi.js\"\u003e\u003c/script\u003e\n\t\t\u003cscript\u003e\n\t\t\tconst scene = new THREE.Scene();\n\t\t\t\n\t\t\tconst camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);\n\t\t\tcamera.position.set(0, 0, 50);\n\t\t\t\t\t\n\t\t\tconst renderer = new THREE.WebGLRenderer({antialias : true});\n\t\t\trenderer.setSize(window.innerWidth, window.innerHeight);\n\t\t\trenderer.setClearColor(0xe0b2a4, 1.0);\n\t\t\tdocument.body.appendChild(renderer.domElement);\n\t\t\t\n\t\t\t// create an interactable light buld mesh\n\t\t\t\n\t\t\tconst gray_color = new THREE.Color(0x57554f);\n\t\t\tconst yellow_color = new THREE.Color(0xe0c53a);\n\t\t\t\n\t\t\tconst bulb_geometry = new THREE.BoxGeometry(5, 5, 5);\n\t\t\t\n\t\t\tconst bulb_material = new THREE.MeshBasicMaterial( { color: yellow_color } );\n\t\t\t\n\t\t\tvar bulb_mesh = new THREE.Mesh(bulb_geometry, bulb_material);\n\t\t\t// add a name to the mesh (needed for mmi to work, you can give the same name to multiple meshes)\n\t\t\tbulb_mesh.name = 'bulb';\n\t\t\tbulb_mesh.position.set(0, 0, 0);\n\t\t\tscene.add(bulb_mesh);\n\t\t\t\n\t\t\t// initialize instance of class MouseMeshInteraction, passing threejs scene and camera\n\t\t\tconst mmi = new MouseMeshInteraction(scene, camera);\n\t\t\t\n\t\t\t// add a handler on mouse click for mesh (or meshes) with the name 'bulb'\n\t\t\tmmi.addHandler('bulb', 'click', function(mesh) {\n\t\t\t\tconsole.log('bulb mesh is being clicked!');\n\t\t\t\t// switch between colors\n\t\t\t\tif (mesh.material.color === gray_color) {\n\t\t\t\t\tmesh.material.color = yellow_color;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tmesh.material.color = gray_color;\n\t\t\t\t}\n\t\t\t});\n\t\t\t\n\t\t\tfunction render() {\n\t\t\t\trequestAnimationFrame(render);\n\t\t\t\t// update the mmi\n\t\t\t\tmmi.update();\n\t\t\t\trenderer.render(scene, camera);\n\t\t\t}\n\t\t\t\n\t\t\trender();\n\t\t\u003c/script\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Quickstart Template (as a js module):\n```js\n/* script.js */\n\nimport * as THREE from 'three'\nimport MouseMeshInteraction from 'three_mmi'\n\nconst scene = new THREE.Scene();\n\t\t\t\nconst camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);\ncamera.position.set(0, 0, 50);\n\t\t\nconst renderer = new THREE.WebGLRenderer({antialias : true});\nrenderer.setSize(window.innerWidth, window.innerHeight);\nrenderer.setClearColor(0xe0b2a4, 1.0);\ndocument.body.appendChild(renderer.domElement);\n\n// create an interactable light buld mesh\n\nconst gray_color = new THREE.Color(0x57554f);\nconst yellow_color = new THREE.Color(0xe0c53a);\n\nconst bulb_geometry = new THREE.BoxGeometry(5, 5, 5);\n\nconst bulb_material = new THREE.MeshBasicMaterial( { color: yellow_color } );\n\nvar bulb_mesh = new THREE.Mesh(bulb_geometry, bulb_material);\n// add a name to the mesh (needed for mmi to work, you can give the same name to multiple meshes)\nbulb_mesh.name = 'bulb';\nbulb_mesh.position.set(0, 0, 0);\nscene.add(bulb_mesh);\n\n// initialize instance of class MouseMeshInteraction, passing threejs scene and camera\nconst mmi = new MouseMeshInteraction(scene, camera);\n\n// add a handler on mouse click for mesh (or meshes) with the name 'bulb'\nmmi.addHandler('bulb', 'click', function(mesh) {\n\tconsole.log('bulb mesh is being clicked!');\n\t// switch between colors\n\tif (mesh.material.color === gray_color) {\n\t\tmesh.material.color = yellow_color;\n\t}\n\telse {\n\t\tmesh.material.color = gray_color;\n\t}\n});\n\n// just to test if the new features are conflicting with previously supported events\n//\t\t(everything seems to be OK)\nmmi.addHandler('bulb', 'dblclick', function(mesh) {\n\tconsole.log('bulb mesh is double clicked!');\n});\nmmi.addHandler('bulb', 'contextmenu', function(mesh) {\n\tconsole.log('bulb mesh is pressed with the right button!');\n});\n\nconst green_color = new THREE.Color(0x00bb00);\nconst orange_color = new THREE.Color(0xffaa00);\nconst red_color = new THREE.Color(0xff0a0a);\nconst test_mesh_geometry = new THREE.BoxGeometry( 5, 5, 5 ); \nconst test_mesh_material = new THREE.MeshBasicMaterial( { color: green_color } );\nvar test_mesh = new THREE.Mesh(test_mesh_geometry, test_mesh_material);\ntest_mesh.name = 'new_features_mesh';\ntest_mesh.position.set(10, 0, 10);\nscene.add(test_mesh);\n\t\t\nmmi.addHandler('new_features_mesh', 'mouseenter', function(mesh) {\n\tconsole.log('mouse is over the mesh!  ', mesh);\n\tmesh.material.color = orange_color;\n});\n\nmmi.addHandler('new_features_mesh', 'mouseleave', function(mesh) {\n\tconsole.log('mouse has left!  ', mesh);\n\tmesh.material.color = green_color;\n});\n\nmmi.addHandler('new_features_mesh', 'mousedown', function(mesh) {\n\tconsole.log('mouse button is pressing on the mesh!  ', mesh);\n\tmesh.material.color = red_color;\n});\n\nmmi.addHandler('new_features_mesh', 'mouseup', function(mesh) {\n\tconsole.log('mouse button is released on the mesh!  ', mesh);\n\tmesh.material.color = orange_color;\n});\n\nmmi.addHandler('new_features_mesh', 'click', function(mesh) {\n\tconsole.log('mouse button is clicked on the mesh!  ', mesh);\n});\n\nfunction render() {\n\trequestAnimationFrame(render);\n\t// update the mmi\n\tmmi.update();\n\trenderer.render(scene, camera);\n}\n\nrender();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielblagy%2Fthree_mmi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielblagy%2Fthree_mmi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielblagy%2Fthree_mmi/lists"}