DEV Community

Gabriel Lazcano
Gabriel Lazcano

Posted on

Import 3D models to three.js

Original article with code snippets (recommended): https://gabriellazcano.com/blog/import-models-to-three-js/

I’ve found threejs pretty tricky at the current state of the library. They are always making improvements and breaking some applications with the new updates. So here, I"m going to talk about how I’ve found it’s good to structure your threejs application for importing 3D objects. Here is a working example.

https://gabriellazcano.com/blog/import-models-to-three-js/

The current version of threejs doesn't require any special tags in the HTML, only the <script type="module"> tag where we are going to develop the logic of the program.

When using type="module" we can use the features of es5 such as import so we are going to use them. We have to import threejs and GLTFLoader. And any other component you use, such as OrbitControls. I'm going to define some variables gloablly as I'm assigning them afterwards in the init() function. You can extend on the configuration in this function in the documentation

For adjusting the size when changing resizing the window we have to add an event listener and to change both the aspect ratio and the size of the renderer.

Now we have to load the object and define the lighting for the scene.

To add a simple animation rotation to the model we have to add or substract to the rotation of the object in every animation frame, for example. However, there are much more ways of animating and with different purposes.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay