Step-by-step guide for importing a octet-stream 3d model into Three.js

I uploaded a model in .glb format to S3 services and obtained the URL

https://test-image-prevaa-123.s3.amazonaws.com/test/1626336255367.octet-stream
. How can I load this model into three.js? When I tried loading it from my code, the model failed to display. I am new to three.js and encountered the error in the console
Unexpected token g in JSON at position 0
. I suspect that after uploading the .glb file to S3 services, the file format changed to .octet-stream. I am unsure about how to load a .octet-stream file into three.js. Thank you for any suggestions on loading a .octet-stream model into three.js.

Here is my code:

  const data = this
  const loaderFile = new THREE.FileLoader()
      loaderFile.load(newFile, async function (file) {
           const loader = new GLTFLoader()
           loader.parse(file, '', function (glb) {
               const mesh = glb.scene
               mesh.scale.set(2, 2, 2)
               data.scene.add(mesh)
               data.animate(mesh)
            })
       })

Answer №1

To load the URL like any other, simply retrieve the content of the GLTF file. However, assuming that "newFile" is the URL you posted, there are a few adjustments needed in your code.

If you prefer to utilize FileLoader, ensure to set the responseType to arraybuffer as expected by GLTFLoader.parse (refer to the GLTFLoader Docs):

const data = this
const loaderFile = new THREE.FileLoader()

// added this line:
loaderFile.setResponseType( 'arraybuffer' )

loaderFile.load(newFile, async function (file) {
    const loader = new GLTFLoader()
    loader.parse(file, '', function (glb) {
        const mesh = glb.scene
        mesh.scale.set(2, 2, 2)
        data.scene.add(mesh)
        data.animate(mesh)
    })
})

Alternatively, if you prefer to avoid using FileLoader altogether, you can directly utilize GLTFLoader.load:

const data = this
const loader = new GLTFLoader();
loader.load(newFile, function (glb) {
    const mesh = glb.scene
    mesh.scale.set(2, 2, 2)
    data.scene.add(mesh)
    data.animate(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

Automatically resizing font to fit the space available

I am attempting to achieve the task described in the title. I have learned that font-size can be set as a percentage. Naturally, I assumed that setting font-size: 100%; would work, but unfortunately it did not. For reference, here is an example: http://js ...

I encountered an unexpected obstacle while reloading my Next.js application with animejs. The error message reads: "SyntaxError: Unexpected token 'export'." This unwelcome occurrence took place during the

Encountering an error with animejs when reloading my Next.js app: An unexpected token 'export' is causing a SyntaxError. This issue occurred during the page generation process. The error originates from file:///Users/.../node_modules/animejs/lib ...

The issue with Infinite Scrolling/Pagination feature in backbone.js persists

My goal is to implement an infinite scroll feature similar to Twitter using the Pagination plugin for backbone.js. Clicking the button/link #pagination a should load the next page of results from the backend and append it to the current view PhotoListView. ...

Is it possible for an object to be null even when its type is object

There's an issue that I can't seem to replicate. Personally, I'm not experiencing any errors but bugsnag reports that some users are encountering them. Take a look at snippet line 6 for more information. let lang = this.$store.state.lang ...

Is it possible to automatically close the modal by clicking outside of it

How can I make sure that my modal box only closes when clicking outside of it, and not when clicking on the buttons inside? I have a ref to the parent element that successfully closes the modal on click outside, but currently it also closes if I click on t ...

The useNavigate() function seems to be failing to redirect to the '/protected' route

The issue lies in the redirection process after receiving the token from the API. Although the token is successfully saved in local memory, the redirect to the intended page does not occur as expected. Instead, a manual window refresh is required to naviga ...

Extracting information from JSON and presenting it in a structured table format

I've hit a wall with this JavaScript issue on my website. I'm trying to convert API JSON data into a table, and it's working fine when the data is on separate lines. However, when I introduce nested arrays in the JSON data, everything ends u ...

Unable to modify border color for Material-UI OutlinedInput

Having some trouble changing the border color of a v4.13 MaterialUI Outlined Input. No luck with CSS overrides. Tested multiple CSS rules targeting different elements, such as select and OutlinedInput. Here are my latest attempts. What am I missing? cons ...

Hide the 1, 2, 3 page buttons in Datatables pagination and instead display only the Next and Previous buttons for navigation

I recently implemented datadables.net pagination for my HTML table. I am looking to customize the pagination buttons by hiding or removing the 1, 2, 3 ... buttons and only display Next and Previous Buttons. Is there a way to achieve this by setting paging ...

When constructing a file directory URL, it is important to utilize the forward slash "/" in the filename

As I am creating a URL, the process involves taking the filename and using it to create a folder with that name. However, an issue arises if the name contains "/", as it causes the URL to break and creates an undesired location. For example: var fileDir ...

The concept of functions in JavaScript: fact or fiction?

Is there a way to show the message "Yes" or "No" based on the return value of a function without directly using the words true and false, and without utilizing alert? Any suggestions would be greatly appreciated. Thank you. function myFunction { if (Ma ...

Interacting with APIs using jQuery, managing responses with AJAX, handling parsererror in our development environment XAMPP on Windows 8

I've come across numerous questions regarding the dreaded "parsererror" but I just can't seem to find a solution that fits my specific issue. Here's the code causing me grief: <!DOCTYPE html> <html> <head> <meta http-eq ...

What could be the reason for webpack not making jQuery available as a global variable?

For my current project, I am utilizing several npm modules by integrating them using yarn and webpack. These essential modules include jquery, bootstrap3, moment, jquery-tablesort, jquery-ujs, bootstrap-select, and livestamp. Some of these plugins require ...

having difficulty transmitting parameter to angular directive

After assigning a collection to a source variable, I am trying to activate a third party control (bootstrap-select) using a directive that watches the assigned collection. angular .module('app').directive('bootstrapDropdown', ['$t ...

Bidirectional data binding in AngularJS custom directive

I have recently started working with Angular.js and I am attempting to create a custom directive with a controller containing functions that is connected to another controller. I want the object in the main controller ($scope.MyObj) to update in sync wit ...

Listening for Angular 2 router events

How can I detect state changes in Angular 2 router? In Angular 1.x, I used the following event: $rootScope.$on('$stateChangeStart', function(event,toState,toParams,fromState,fromParams, options){ ... }) In Angular 2, using the window.addEv ...

Utilize javascript to activate the hyperlink

Clicking the following link will open a video: <a href="<?php echo $rows['image1'] ; ?> " rel="vidbox" title="<?php echo $rows['name']." <br>".nl2br($rows['detail']) ; ?>" id="m2"><?php ...

The most recent version of Autonumeric now permits the inclusion of a decimal point, even if the decimalPlaces parameter

I need to ensure that only whole numbers are allowed in the input textboxes, while also displaying a currency symbol and commas. I am using the most recent version of Autonumeric JS for this purpose. Even after setting the decimalPlaces property to 0, I a ...

What is the best way to implement a dynamic Menu Component in next.js?

Hello friends! I am currently working on a Next.js web app with a Menu Component that fetches data dynamically through GraphQL. I really want to achieve server-side rendering for this menu component. My initial attempt to use getStaticProps() to render the ...

Challenges faced with Vuetify's vertical and non-linear stepper components

I've been struggling to grasp the concept of Vuetify's stepper feature. Despite my efforts, I haven't been successful in combining different steppers from their page that each have elements I need but lack others. One example is this one on ...