Skip to content

Instantly share code, notes, and snippets.

@marffinn
Last active June 5, 2019 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marffinn/9e4fe31f2f83881e345ea66f39c35a5c to your computer and use it in GitHub Desktop.
Save marffinn/9e4fe31f2f83881e345ea66f39c35a5c to your computer and use it in GitHub Desktop.
Jhonny Freitas
// this is basically the function i've written not long ago - yours may differ
// from mine but the most important thing is to set the name for the component that is imported/loaded
// Note that instead of name there are values like position, scale material name, color hex code and basically everything
// that the imported objects' array contains
var load_consecutive = function(objectFetch, nameFetch, scaleFetch, positionFetch, materialFetch) {
loader.load(objectFetch, function(object) {
object = new THREE.Mesh(object, materialFetch);
object.material.needsUpdate = true;
object.position.set(0, positionFetch, 0);
object.scale.set(scaleFetch, scaleFetch, scaleFetch);
object.name = nameFetch; // here You set the name for the part of the model you wish to name/choose/see the name
raycastArray.push(object);
group_main.add(object);
});
}
let raycaster = new THREE.Raycaster();
let mouse = new THREE.Vector2();
let raycastArray = [];
let objectChosen = '';
function onDocumentMouseDown(event) {
event.preventDefault();
mouse.x = (event.clientX / renderer.domElement.width) * 2 - 1;
mouse.y = -(event.clientY / renderer.domElement.height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(raycastArray);
if (intersects.length > 0) {
console.log(intersects[0].object.name); // here You have the console.log function that return the name.
objectChosen = intersects[0].object.name;
}
}
document.addEventListener('mousedown', onDocumentMouseDown, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment