Loading STL files can sometimes lead to issues when accessing the world matrix incorrectly

While working on a three.js project, I encountered some issues with loading vertices from an STL file and converting them to world coordinates. It seems like the matrix application isn't working properly, and I suspect it could be related to the loading process itself.

loader.load( './assets/models/trajan_print.stl', function ( geometry ) {
  var mesh = new THREE.Mesh( geometry, material );
  mesh.name = "target";
  mesh.position.set( 0, - 300, - 400 );
  mesh.rotation.set( - Math.PI / 2, 0, Math.PI );
  mesh.scale.set( 5, 5, 5 );
  //mesh.castShadow = true;
  //mesh.receiveShadow = true;
  mesh.visible = false;
  SCENE.add( mesh );
  model.setTargets(mesh);
} );

One key function to pay attention to is the last one, model.setTargets(mesh). This function is crucial for obtaining the vertices of the object in world coordinates, but it seems to have a glitch:

    setTargets(mesh){
        this.matrixWorld = mesh.matrixWorld;  
        var buffer = mesh.geometry.attributes.position.array;
        for(var i = 0; i < buffer.length / 3; i = i + 3){
            var point = new THREE.Vector3(buffer[i], buffer[i+1], buffer[i+2]);
            point.applyMatrix4(this.matrixWorld);
            this.unassignedVertices.push(point);
        }
    }

Interestingly, if I perform the same operation outside of this function, it works perfectly fine. This function is called only when this.unassignedVertices is true, as it was a way to handle the asynchronous loading process.

    insertParticle(part) {
        var point = this.unassignedVertices.pop();
        point.applyMatrix4(this.matrixWorld); 
        part.setTargetPoint(point);
        this.octree.add(part);
        this.particles.add(part);
    }

Another issue, which is linked to setTargets(mesh), is that it seems like only half of the vertices from

mesh.geometry.attributes.position.array
are being loaded. This could be caused by other factors in the code, so I'm wondering if there's something specifically in the function that could be causing this. Am I loading it incorrectly? Am I converting it improperly? Am I missing points?

Additional context: the model loads and displays correctly if I remove the visible = false tag.

Answer №1

If you happen to encounter this issue, keep in mind that the array may contain duplicate positions that do not all relate to vertices. A straightforward solution would involve checking if(position.x == ... to resolve the problem and bring it in line with expectations.

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

Unlocking the Secrets: Javascript Tricks for Retrieving Object Values

I have an item that contains data which I need to display. {"text": "active user active user213123 idle user234234234 loggedout userafdadf" }, To extract the content, I used the following code: Response = message.s ...

Using Selenium to assign properties to JavaScript Objects

I have a JavaScript Object that needs to be set using Selenium WebDriver. var examplePlayResponse = { "prizeIndex" : 1, "mode" : "NORMAL", "id" : "abc123", "version" : "1.0", "gameCode" : "xyz789", "randomSeed" ...

Exploring the Methods to Filter JSON Data in Node.js

There have been significant changes in technologies over the past couple of years since I last checked answers to this question. Currently, I am dealing with JSON files that are being transmitted from a database to my server. My main concern is how to eff ...

Retrieving properties from a selector's output function

My situation is similar to the scenario described in the accessing react props in selectors documentation. However, in my case, let's assume that the visibilityFilter is coming from the props. Is there a way to achieve something like the following? e ...

Cutting an in-memory Base64 PNG using ONLY JavaScript on the client side without relying on canvas

Scenario: Working with JavaScript in a SDK environment, either on node.js or a browser. Situation: I have a base64 string containing a PNG image encoded using base64 (extracted from selenium webdriver's takeScreenshot function). Inquiry: How can I c ...

Retrieve a comprehensive inventory of all routes within a Vue project and automatically create a sitemap for the website - Vue Project

Imagine I've set up a route like this: import Vue from "vue"; import Router from " vue-router"; import BookRoutes from "./routes/book"; Vue.use(Router) const router = new Router({ routes:[ { path ...

What is the process for activating a script file once I have replaced HTML content using a backend method?

After receiving HTML content from the backend, including the <script> tags, I noticed that the scripts.js file does not seem to be activated. While the HTML content displays fine, changing the path to style.css successfully alters the appearance of ...

Learn the steps to merging all yarn files using gulp

After successfully setting up yarn and getting the hang of how it functions, I've also started to grasp the basics of gulp. I was relieved to find out how to install version 4 and avoid those deprecated errors that came with the default version. As o ...

PHP: Communicating Data with JavaScript

What if my PHP script requires some time to complete its operations? How can I keep the client updated on the progress of the operation, such as during a file download where the estimated time and data size need to be communicated? In PHP, calculating all ...

Transforming all commas to plus signs in a JavaScript codebase for the entirety of

Currently, I am using winston for logging and have created a common method to log throughout the project. However, I am facing an issue with many logging statements that look like this: logger.info("here is the data" , data) The problem arises when trying ...

Can a package-lock.json file be created without running an npm install command?

Is there a way to create only a package-lock.json file without having to redo the npm install process? I'm looking to generate the lock file without reinstalling any of the package files. Is this achievable? ...

Trigger a callback in KnockoutJS once all bindings have been successfully set up

I am facing a frustrating bug that is detailed here: <select> only shows first char of selected option and I am looking for a solution to remove the display:none from my select boxes in order to resolve this issue. Any ideas? Here's the binding ...

Which library do you typically employ for converting .mov files to mp4 format within a React application using Typescript?

As a web programming student, I have encountered a question during my project work. In our current project, users have the ability to upload images and videos. Interestingly, while videos can be uploaded successfully on Android devices, they seem to face ...

Spin: twist beyond 360 degrees or less than 0 degrees

Attempting to rotate an arrow indicating wind direction using the transform: rotate() property. The data for rotation is retrieved from an API fetch, and conventional measurement of wind direction involves indicating where the wind is coming from. Therefo ...

Activate Jquery as the user navigates through it with scrolling

Is there a way to activate a JQuery event when the user first scrolls over a particular div? I attempted to utilize waypoint for this purpose, but unfortunately, it did not work as expected. Below is the code I used with no errors: var waypoints = $(&apo ...

Scroll bar malfunction in Highcharts

I am struggling to get the scroll bar working so that all categories can be displayed. I have tried different approaches but haven't been able to figure out where I'm going wrong. See the code in action here: http://jsfiddle.net/manraj/7racxxu0/ ...

Is there a quicker method to update the state of an array of objects?

Here is my React state example: const [questionList, setQuestionList] = useState([ { _type: "radio", answer: "", point: "", question: "", options: ["A", "B"], ...

Tips for preventing multiple counter buttons from conflicting with one another

Currently, I am in the process of creating an online restaurant platform that allows customers to place food orders. To streamline this process, I am developing individual cards for each food item available on the menu. In addition, I am implementing butto ...

Preventing navigation without using the <Prompt> component in React Router DOM V6

I have recently discovered that in react-router-dom v5, it was possible to utilize the <Prompt> component to prompt the user before a page transition occurs, allowing them to prevent it. However, this feature has been removed in v6 with plans for a m ...

Sharing package JSON file dependencies with child engines and addons in Ember.js

I am seeking information on how Ember Js can share the parent app's package.json file dependency (xyz:3.0.0) with child engines and addons without them needing to redeclare the dependencies in their own package.json files. This is to reduce the overal ...