Clicking on an object will trigger the threejs to traverse through

I need to implement a wireframe for an object when a button is clicked. I am using the traverse function to achieve this, and it works perfectly fine in the OBJMTLLoader. However, when I try to use it in a separate function as shown below and then click the button, it results in:

object is undefined

 function wireframe(object){
                 //alert('hhhhhh');

                object.traverse( function ( child ) {

                    if ( child instanceof THREE.Mesh )
                    {
                    //child.geometry.computeFaceNormals();
                    var  geometry = child.geometry;
                    //console.log(geometry);
                    //geometry.dynamic = true;
                    material = child.material;
                     mesh = new THREE.Mesh(geometry, material);
                        scene.add(mesh);

                    var useWireFrame = true;
                        if (useWireFrame) {
                            mesh.traverse(function (child) {
                                if (child instanceof THREE.Mesh) 
                                {
                                //child.material.wireframe = true;

                                var wfh = new THREE.WireframeHelper( mesh, 0xffffff );
                                wfh.material.wireframe = true;
                                wfh.material.linewidth = 2; // looks much better if your PC will support it
                                scene.add( wfh );                       

                                }
                            });
                        }

                    }
                    });



                }

Is it possible to traverse on the object onclick? Why am I encountering this error?

Answer №1

If you want to give your object a wireframe appearance, there are a few methods you can try. One option is to include a THREE.WireframeHelper in the scene, like you did with the ladybug model. When the user clicks the On button, you can use the add() function to add the wireframe to the scene. Conversely, when the Off button is pressed, you can use the remove() function to take it out of the scene.

For the male model that doesn't seem to be working, you may need to locate the object material and make adjustments there.

To load your model normally, follow these steps:

// This loads the model asynchronously
// Remember to assign a name to the object for easier searching later on.
var loader = new THREE.OBJMTLLoader();
loader.load( 'obj/male02/male02.obj', 'obj/male02/male02_dds.mtl',
    function ( object ) { object.name = 'desired-name'; scene.add( object ); } );

function applyWireframe() {
    var object = scene.getObjectByName("desired-name", true); // Perform a recursive search

    object.traverse(function(child) {
        if (child instanceof THREE.Mesh)
            child.material.wireframe = true;
    });
}

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

HTML elements not displaying in Ajax form

I am encountering an issue with my ajax based feedback form where the form is displaying the html from the response instead of processing it correctly. Here is the JQuery code: $(document).ready(function() { var form_holder = $('#form-holder'); ...

How can you obtain constructor parameter from JSON directly within the constructor itself?

I decided to store the fixed parameters required for creating an object in a JSON file as an array. My goal was to have the constructor of the object fetch these parameters from the file. Although reading a JSON file is efficient, I wanted to explore othe ...

Issues with the "noImplicitAny" setting in TS compilation

Having the following code snippet: let z; z = 50; z = 'z'; paired with the configuration in my tsconfig.json file: { "compilerOptions": { "target": "es5", "module": "commonjs", "sourceMap": false, "noEmitOnError": true, " ...

Using jQuery, you can easily deactivate the form submission button for both text and checkbox input fields

I have a form consisting of three input fields (type=text, multiple checkboxes, and a disabled submit button). I need the submit button to become enabled only when the user has filled out all three text inputs and selected at least one checkbox. While I f ...

Refreshing Ajax in a different tab causes the selectize element to become unbound from the editing form in Rails 5

In my Rails 5.1 application, I have multiple views with the main view being the calls index view. In this view, I perform an ajax refresh of partials and use a callback to re-initialize the selectize JS element in the calls/index view as shown below: < ...

Tried to enroll the RCTBridgeModule category as RCTFileReaderModule

Trying to register the RCTBridgeModule class RCTFileReaderModule with the name 'FileReaderModule' failed because the name was already taken by the FileReaderModule class. This issue arises when attempting to launch an application on iOS using th ...

Complete Guide to Node.JS CRUD Delete Functionality

Currently, I am developing a node.js CRUD application that interacts with MongoDB. The main features of this app are allowing users to upload photos, edit details related to the photos, and delete them from the database. However, I am facing challenges i ...

Updating the parameter names in an AJAX request

Can the data parameters sent with AJAX be dynamically changed to a new name? For instance: let NewParamName = `Post_${incremented_value}`; $.post("index.php" { NewParamName : "someValue" }); Even after sending the request, the name in the parameters rem ...

Avoid TypeError: cannot read property '0' of undefined in a Vue.js/Django project

I am currently working on a Django/Vue.js application. Upon submitting the login form, the Django view redirects to the user's username page, where the Vue.Js file retrieves data from the server. Below is the code snippet: async created(){ await ...

Having issues with Three.js picking when using custom geometries

I'm struggling with choosing an implementation method. I've come across several examples that do what I need, but I just can't seem to get it working properly. I primarily referenced this specific example Essentially, I have various meshes ...

Troubleshooting Issue with Ionic's UI Router

Recently, I created a basic Ionic app to gain a better understanding of UI router functionality. To my surprise, when I ran the app, nothing appeared on the screen. Additionally, the developer tools in Google Chrome did not show any errors or information. ...

Ways to retrieve the current tab title that I have designated in the Jquery Tabs UI

I found this helpful resource at http://jqueryui.com/demos/tabs/#manipulation and I am currently using it. My goal is to retrieve the title of the tab that is currently selected and which I had previously named (for example, in the href attribute). How can ...

What is the significance of the error message "Uncaught (in promise) Object"?

I am facing an error in my code and need help to correct and debug it. I would like the code execution to halt when an issue arises. Here is my code snippet: export function postRequestOtpCode(phone: string, token: string): CancellableAxiosPromise { ax ...

Navigating JSON Data with ES6 Iteration

Issue Description There are two APIs I am working with. The first one, let's call it API #1, provides JSON data related to forum posts as shown below: [{ "userId": 1, "id": 10, "title": "Tt1", "body": "qBb2" }, { "userId": 2, ...

Unable to display and conceal div elements

<ol class="novice"> <li> <p class="glava">HTML</p> <div class="vsebina"> <p>Hyper Text Markup Language (slovensko jezik za označevanje nadbesedila...</p> </div> </li> <li> ...

Secure AJAX call to PHP service in WordPress, with proper authentication

I have a basic website that utilizes d3.js to generate graphs. The graph data is retrieved via AJAX from a php service using GET requests with specific parameters. The php service then responds with the data in JSON format. Now, I am looking to secure thi ...

Scraping the Web with Python: Harnessing the Power of Selenium and

Seeking assistance with a problem I've encountered. I'm attempting to extract numbers from a specific website (link provided in the code below). Since the website utilizes JavaScript, my approach involves using selenium to load the page first and ...

Modify the color of the title in a bootstrap vue tab

Utilizing bootstrap-vue.js, I created a tab that looks like this: https://i.sstatic.net/EW76k.png The default color of the tab title doesn't fit with my project theme, so I attempted to change it. I found information on how to modify the tab title in ...

Attempting to develop a filtering feature using ReactJS

Having some trouble with the if statement in my filter list function. Can't seem to figure out why it's not working properly. Here is the code snippet: filterList (event) { var updatedList = this.props.array; var filterText = this.stat ...

Using Angular and chartJS to generate a circular bar graph

I am looking to enhance my standard bar chart by creating rounded thin bars, similar to the image below: https://i.sstatic.net/uugJV.png While I have come across examples that suggest creating a new chart, I am unsure of how to implement this within the ...