Is it possible to include a concealed bounding box in a three.js environment?

I need to expand the clickable area for an object by detecting a click on its bounding box instead of just the object itself. Here is how I load the object:

var loader2 = new THREE.ObjectLoader();
          loader2.load( "models/Platform/Platform.json", function(object, materials){
            object.rotation.x = - (Math.PI / 2);
            object.rotation.y = Math.PI;
            object.scale.set(.025, .025, .025);
            object.position.set(0, 1, .4);
            var bbox = new THREE.BoundingBoxHelper(object, 0xffffff);
            bbox.update();
            scene.add(object);
            scene.add(bbox);
            objects.push(bbox);
          });

To detect the click, I use the following code:

raycaster = new THREE.Raycaster();
        mouse = new THREE.Vector2();
        
        document.addEventListener( 'mousedown', onDocumentMouseDown, false );
        document.addEventListener( 'touchstart', onDocumentTouchStart, false );
        
        window.addEventListener( 'resize', onWindowResize, false );
        
        function onDocumentTouchStart( event ) {
          event.preventDefault();
          event.clientX = event.touches[0].clientX;
          event.clientY = event.touches[0].clientY;
          onDocumentMouseDown( event );
        }
        function onDocumentMouseDown( event ) {
          console.log("here");
          event.preventDefault();
          mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
          mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
          raycaster.setFromCamera( mouse, camera );
          console.log(mouse.x);
          console.log(mouse.y);
          var intersects = raycaster.intersectObjects( objects, true );
          if ( intersects.length > 0 ) {
            console.log("click");
          }

The clickable bounding box works as expected, but it is visible on the screen:

https://i.sstatic.net/6nqGP.png

I want the bounding box to be transparent or invisible. Is there a way to make the bounding box attached to the object clickable but not visible?

I read that removing "scene.add(bbox);" will make the bounding box invisible, but then the click won't register because it's not in the scene. Any solutions for this issue?

Appreciate your help!

Answer №1

To make the material disappear, you can use this line of code:

box.material.visible = false;

Answer №2

It appears that there are two possible solutions to this issue.

As recommended by @prisoner849:

bbox.material.opacity = 0;
bbox.material.transparent = true;

Another suggestion by @tomacco, further refined by @WestLangley:

bbox.material.visible = false;

Both of these resolutions proved effective in resolving the problem!

https://i.sstatic.net/Le2t9.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Loading a webpage once the loading bar has completed

Is there a way to modify the JavaScript code for my progress bar that loads from 1 to 100 percent so that it automatically redirects to another page when reaching 100%? Here is the current JavaScript code: var i = 0; function move() { if (i == 0) { ...

What is the best method for retrieving options based on a data attribute?

All <option> elements in a <select> have data-value attributes set (example provided below). Upon page load, I must find and iterate through the values of all <option> elements to pick out one that suits further processing. We plan on us ...

Utilizing the variable $this outside of an object context within an Ajax function

Encountering the following error: Fatal error: Uncaught Error: Using $this when not in object context in E:\xampp\htdocs\StudentGuideBook\Version1.0\models\AjaxChecking.php:4 Stack trace: #0 {main} thrown in E:\xampp&b ...

How to retrieve the nearest sibling in AngularJS without using jQuery

Is there a way to access the nearest element in Angular without using jQuery, like the example below demonstrates with jQuery? link: function (scope, element, attrs) { function handler($event) { if(!($event.target).closest(element).length) { ...

When attempting to send emails, SendGrid encounters an error and fails to provide an error code

Earlier today, I successfully sent out a series of emails using SendGrid. It was quite a large number of emails, as I needed to create multiple user accounts with attached email addresses. Thankfully, everything went smoothly and all the emails were delive ...

Checking for the uniqueness of a username with joi: A step-by-step guide

A while back, I came across an interesting article that discussed using Joi for asynchronous validation to check the uniqueness of a username by querying the database. Sadly, I can't seem to locate it now and I'm curious about how we can implemen ...

What is the process for generating Json using information from a form?

Recently, I embarked on a project that involves creating online tours via a form filled out by the administrator. The data submitted through the form is then mapped into a Mongoose Schema and transformed into JSON. In the createcontent.js file, I utilized ...

Ensure that a function completes before moving on in JavaScript

I am attempting to modify the save method so that it waits for this.collection.create() to complete before running, in order to prevent a potential crash. class UserRepository extends BaseRepository<User> { constructor() { super(); ...

Utilizing an additional parameter in React to dynamically change the API URL

Hello everyone! I'm facing a slight issue with one of my React applications: I'm attempting to retrieve Weather alerts using an API. Here's how my App.js file is set up: import React, { Component } from 'react'; import './App ...

How to stop Mouseenter event from bubbling up in an unordered list of hyperlinks using Vue 3

I've experimented with various methods to prevent event bubbling on the mouseenter event, but I'm still encountering an issue. When I hover over a link, the event is triggered for all the links as if they were all being hovered over simultaneousl ...

Incorporate Data into Table Rows inside the Material-UI Accordion

I am tasked with creating a table filled with two mock data accordions and one accordion containing database data using ReactJS in Material-UI. I have successfully implemented an accordion with the table data, but I am struggling to display only the table ...

Mastering div manipulation with jQuery: A step-by-step guide

I have three divs with the classes "col-md-2," "col-md-8," and "col-md-2." What I want is that when a button in the "col-md-8" div is clicked, both of the other divs should be hidden and the "col-md-8" div should expand to occupy the full width of "col-md ...

Examine both statements at the same time

The concept: I am in the process of creating a bot that will access Instagram using puppeteer. If the login is successful, the bot will continue as normal. However, if an error with ID slfErrorAlert occurs during login, the bot will stop. The issue: ...

What is the best way to retrieve the current state from a different component?

My goal is to access a user set in another component. I passed the state from the highest component (which is the app) by utilizing this function for state change. handleRegisterSubmit(e, username, password) { e.preventDefault(); axios.post('/au ...

Tips for identifying the version of a package that is installed using a package-lock.json file containing lockfileVersion = 3

After upgrading from Node 16 (npm 8) to Node 18 (npm 9), I noticed a difference in the structure of the package-lock.json files. Files generated with npm 8 have a lockfileVersion: 2, while those generated with npm 9 have a lockfileVersion: 3. The changes a ...

What is causing the object, which is clearly defined (as shown in the callback to JSONLoader), to be interpreted as undefined

Why is the THREE.Mesh() object showing as undefined even though it was defined in a THREE.JSONLoader()? Here is the code snippet... 1: var player; 2: var playerCallback = function(geo, mats){ 3: player = new THREE.Mesh(geo, new THREE.MeshFaceMaterial( ...

Error: [$controller:ctrlreg] - The controller registration has failed

I am currently exploring the world of AngularJs and attempting to display options from a json file. However, I keep encountering the following error message: "Error: [$controller:ctrlreg]" Below is the code snippet I am working with: var sj = angular. ...

How come the deleteOne and findById methods in mongoose are able to function properly even after the document has

During my development of an API using nodejs to interact with a MongoDB database, I encountered a peculiar issue after deleting a document. My API consists of various endpoints for retrieving all animals in the database, fetching a specific animal using i ...

Endless Scroll Feature in Javascript

A brief tale: I am in search of a way to extract data from a website that features infinite scroll without actually implementing the infinite scroll feature myself. My plan is to create a script that will auto-scroll the page from the browser console, wai ...

Check for weaknesses in your project using NPM audit

While trying to install the npm package to retrieve the node module file, I encountered a total of 184 vulnerabilities (153 low, 1 moderate, 30 high) after running npm install. I am considering running npm audit fix to address these vulnerabilities, but ...