Issue encountered in Three.js while attempting to parse binary STL file: RangeError indicating out of bounds access problem

One of my main objectives is to import private STL files using three.js from a secure Laravel 8 storage directory.

Unfortunately, the traditional method of using the .load function from STLLoader.js does not work in this scenario:

const loader = new THREE.STLLoader();
const material = new THREE.MeshPhongMaterial({ color: 0x888888, specular: 0x111111, shininess: 2 });

loader.load( '../storage/app/private/file.stl', function ( geometry ) {
    const mesh = new THREE.Mesh( geometry, material );

    scene.add( mesh );
});

This approach fails because the file path needs to point to the private storage outside the web-root folder, causing it to be inaccessible.

To overcome this challenge, I have adopted a new strategy involving an Ajax post request to an STLController where I utilize the get method for retrieving file contents from Laravel in the following manner:

return Storage::get('private/file.stl');

This method allows me to use the response data with the .parse function from STLLoader.js in conjunction with three.js.

const loader = new THREE.STLLoader();
const material = new THREE.MeshPhongMaterial({ color: 0x888888, specular: 0x111111, shininess: 2 });

$.ajax({
    type:'POST',
    url: "get_stl_file_contents",
    success: function(data) {

        geometry = loader.parse(data);

        mesh = new THREE.Mesh(geometry, material);
        scene.add(mesh);
    },
});

This technique works seamlessly for ASCII-formatted STL files. However, when dealing with binary format, I consistently encounter the same ERROR message:

[ERROR] RangeError: Out of bounds access - (getFloat32)

To proceed, I require a method to convert binary response data to ASCII for current usage or find an alternative approach to load/parse STL files for three.js directly from raw binary content.

I appreciate any assistance and guidance! 🤗

Answer â„–1

I found a solution to my issue:

To solve the problem, I decided to set up a specific web route that controls access to certain files using the three.js STLLoader(). Here is an example of how I implemented this:

web.php

Route::middleware('auth:sanctum', 'verified')->group(function () {
    [...]
    Route::get('data/users/{user_id}/{filename}', [FileController::class, 'get_file']);
});

FileController.php

public function get_file($user_id, $filename): \Illuminate\Http\Response
{
    if (Gate::allows('access-stl') || Auth::id() == $user_id) {
        return $this->return_user_file($user_id, $filename);
    } else {
        abort(403);
    }
}

public function return_user_file($user_id, $filename): \Illuminate\Http\Response
{
    $path = storage_path('app/data/users/'.$user_id.'/'.$filename);

    try {
        $file = File::get($path);
        $type = File::mimeType($path);
        $response = Response::make($file);
        $response->header('Content-Type', $type);

        return $response;
    } catch (FileNotFoundException) {
        abort(404);
    }
}

script.js

const scene = new THREE.Scene();
const loader = new THREE.STLLoader();
const material = new THREE.MeshPhongMaterial({ color: 0x888888, specular: 0x111111, shininess: 2 });

loader.load('data/users/1/file.stl', function (geometry) {
    const mesh = new THREE.Mesh(geometry, material);

    scene.add(mesh);
});

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

IE does not support hover effects

Having trouble with a hover effect that works in Chrome but not in MSIE. Are there any alternatives or fixes to make it work in MSIE? The hover/rollover effects I've tried all seem to have this issue in IE. normalize.css demo.css set2.css font- ...

Ensuring the node array is easily accessible within the view

I am currently working on building a simple messaging system using express/nodejs. Although I have successfully implemented the feature to send messages to all users, I now want to enable users to have private 1-to-1 conversations. My aim is straightforwa ...

Utilizing Angular's web architecture involves nesting components and concealing them with the ng-hide directive

I'm currently working on an Angular 1.X application and have encountered a scenario where a component is shared across three different pages. Within the main component, there are several nested components but only two of them should be displayed on a ...

Determine if the "Enter" key has been pressed and validate the

How can I implement Enter key press functionality to login to the system using JavaScript? Below is the code snippet: @using (Html.BeginForm("Login", "InventoryBarcode", FormMethod.Post, new { id = "main" })) { <label for="LoginName" class="uname ...

Security Measures for Parsing Arrays using eval() Function

When I receive a string of an array containing objects via an http request, I use eval() to parse it. Since I am expecting an array object after parsing, how can I secure this eval() process beyond using if (Array.isArray(parsedObj)) ... Is there a bette ...

What steps can I take to create a textbox that expands as more text is

Looking to create a unique textbook design that starts out with specific width and height dimensions, but expands downward as users type beyond the initial space. Wondering if CSS can help achieve this functionality? In a standard textbox, only a scroll ba ...

``Is it possible to adjust the style of an HTML element based on the style of another element?

Within my web application, I am dynamically changing the style of an HTML element using JavaScript. Below is the code snippet: function layout() { if(document.getElementById('sh1').getAttribute('src') == "abc.png") { docum ...

Angular JS causing text alignment issues in table cells when viewed on mobile devices

I have created a web application that requires both desktop and mobile views, utilizing Angular JS. Everything is functioning as expected except for the text alignment within tables. I attempted using <td align="left">, but it did not produce any cha ...

Troubleshooting a Global Variable Problem in Node.js Express Server

Just to clarify from the start: This post is not about a bug! Challenge I encountered an issue with my Node.js and Express backend code when multiple requests were made from the front end simultaneously. Consider this example of one of my endpoints: va ...

Popup showing values that are not defined

I've encountered an issue with tooltips on my bar graph. The tooltips should display the values of boarding and alightings corresponding to each stopname column, but I'm seeing undefined values like this Below is my code snippet: <!DOCTY ...

Unable to clone curved text in fabric.js version 5.3.0

I am currently using fabric.js version 5.3.0 and I have a requirement to clone curved text and add it to the canvas. Description: The issue I am facing is that the cloning functionality does not work properly with curved text. The cloned object does not r ...

Using jQuery to deactivate the submit button when a field is empty or a checkbox is left unchecked

Today is my first attempt at using jQuery, and I could really use some assistance as I'm struggling to achieve the desired outcome. I'm dealing with a table of documents, each containing a checkbox for selection purposes. Additionally, there&apo ...

Develop a unique Kotlin/JS WebComponent featuring custom content

I am trying to create a custom tag using Kotlin that includes default content. While the example I found works well, I am having trouble adding default content (such as an input element) inside the custom tag. After attempting various approaches, I have o ...

Ensure that the dropdown menu remains visible even after the mouse no longer hovers over it

Looking to create a main menu with dropdown items that appear when the user hovers over them? You may have noticed that typically, the dropdown disappears once the hover event is lost. But what if you want the menu to stay visible and only disappear upon c ...

Steps for correctly displaying a success message after clicking and copying to the clipboard:

In the process of developing a color palette, I am incorporating a clipboard icon enclosed in a Tooltip component alongside each color. The functionality involves copying the color's name to the user's clipboard upon clicking the icon. Subsequent ...

Issue with ngStyle function in Internet Explorer

Within my Angular 4 application, I have defined styles as shown below: [ngStyle]="{'border': getInterleaveColor(i)}" The following function is also included: getInterleaveColor(auditNumber) { var borderProperties = '2px solid'; ...

Retrieving Information from an Angular 2 Component

Struggling to figure this out, I am attempting to dynamically add user video data that includes a video URL. My goal is to access the data from the component so I can use it in my HTML. I've attempted the following approach. app.component.ts import ...

Ascending to the Peak within a div

<script type="text/javascript"> $(document).ready(function(){ updateContent(); }); function updateContent(){ $('#mainDiv').load('home.php', function(){ scrollToTop(); }); } ...

Using VueJS to fetch and display data from a JSON file using

Currently, I am dealing with a JSON value in the following format: { "T1" : "online", "T2" : "offline" } Additionally, I have an online API which only sends me the following response: { StatusCode :"T1" } My task is to extract the code from the API res ...

Incorporate dynamic error messages with accompanying icons into a single jQuery dialog box

I've set up a global jQuery dialog to showcase all error messages within the application. I've successfully linked the error message to the dialog, but I'm facing issues with displaying the icon alongside it. To keep it simple, I've use ...