Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Animating the 3D cube with Three.js
Aneeqa Khan
Aneeqa Khan

Posted on

     

Animating the 3D cube with Three.js

Introduction

In today's digital world, web technology has become incredibly powerful. We've reached a point where you can experience captivating 3D graphics right in your web browser. Imagine this—no need for plugins or external tools; it's all within the realm of pure web content. And guess what's driving this exciting change? It's WebGL, a fantastic technology that makes rendering both 2D and 3D graphics possible. But hey, let's be real—it might seem a bit tricky, especially for those starting out. That's where Three.js comes in as a superhero! Three.js is like your trusty sidekick—it simplifies the complex bits and offers a much friendlier path into the enchanting world of 3D web graphics. In this article, let's embark on a journey to explore the basics of bringing 3D graphics to life using WebGL and the magical powers of Three.js.

WebGL and Three.js: A Perfect Match

WebGL is a low-level, complex API. While it provides tremendous power and flexibility, it also comes with a steep learning curve. On the other hand, Three.js provides a higher-level abstraction, enabling developers to create 3D scenes without delving deep into the intricacies of WebGL.

Starting Simple: A Rotating Cube

Let's animate a 3D cube using Three.js to illustrate how easy it is to get started.

1. Setup

First, include the Three.js library in your project:

<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
Enter fullscreen modeExit fullscreen mode

2. Create a Simple Scene

We need three main components: a scene, a camera, and a renderer.

constscene=newTHREE.Scene();constcamera=newTHREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000);constrenderer=newTHREE.WebGLRenderer({antialias:true});renderer.setSize(window.innerWidth,window.innerHeight);document.body.appendChild(renderer.domElement);
Enter fullscreen modeExit fullscreen mode

3. Add the Cube with texture

To display a cube, we'll need a box geometry and a material to cover it.

constgeometry=newTHREE.BoxGeometry();consttextureLoader=newTHREE.TextureLoader();constmaterials=[newTHREE.MeshBasicMaterial({map:textureLoader.load('https://threejs.org/examples/textures/crate.gif')}),newTHREE.MeshBasicMaterial({map:textureLoader.load('https://threejs.org/examples/textures/crate.gif')}),newTHREE.MeshBasicMaterial({map:textureLoader.load('https://threejs.org/examples/textures/crate.gif')}),newTHREE.MeshBasicMaterial({map:textureLoader.load('https://threejs.org/examples/textures/crate.gif')}),newTHREE.MeshBasicMaterial({map:textureLoader.load('https://threejs.org/examples/textures/crate.gif')}),newTHREE.MeshBasicMaterial({map:textureLoader.load('https://threejs.org/examples/textures/crate.gif')})];constcube=newTHREE.Mesh(geometry,materials);scene.add(cube);
Enter fullscreen modeExit fullscreen mode

4. Animate the Cube

We'll create an animation loop that rotates the cube.

functionanimate(){requestAnimationFrame(animate);cube.rotation.x+=0.01;cube.rotation.y+=0.01;renderer.render(scene,camera);}animate();
Enter fullscreen modeExit fullscreen mode

5. Position the Camera

Lastly, we need to position the camera slightly out so that it can view the cube:

camera.position.z=5;
Enter fullscreen modeExit fullscreen mode

Now, you'll see a spinning cube on a blank canvas. The ease of setting up this scene demonstrates the power of Three.js.

cube-gif

Conclusion

With just a few lines of code, we've delved into the world of 3D graphics on the web. The combination of WebGL for rendering power and Three.js for abstraction makes it accessible for developers to create stunning visual content directly within the browser. As you explore deeper into this realm, you'll find that the possibilities—from intricate game worlds to detailed simulations—are nearly limitless. Happy coding!

Connect with me

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
michaeltharrington profile image
Michael Tharrington
I'm a friendly, non-dev, cisgender guy from NC who enjoys playing music/making noise, hiking, eating veggies, and hanging out with my best friend/wife + our 3 kitties + 1 greyhound.
  • Email
  • Location
    North Carolina
  • Education
    BFA in Creative Writing
  • Pronouns
    he/him
  • Work
    Senior Community Manager at DEV
  • Joined

Very cool, Aneeqa! Animations like this are so fun. Loving the wooden texture of the box.

I'm wondering, how hard would it be to make it open up?

CollapseExpand
 
oz profile image
Evgeniy OZ
My main tools: Rust, TypeScript, Angular, MySQL, Redis.
  • Location
    Barcelona
  • Work
    https://www.upwork.com/freelancers/~01d95397aacaef6e88
  • Joined

Even official docs have more info in “Creating a scene”:threejs.org/docs/index.html#manual...

CollapseExpand
 
uhlukas profile image
👑👨🏻‍💼 | 🅓🅐🅢🅒 🅕🅞🅤🅝🅓🅔🅡 & 🅒🅔🅞 
ℹ️ | Founder & Chief Executive Officer “Discord Account Support Corporation©”

That Sounds Cool

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer by profession, Artist by heart
  • Location
    London, United Kingdom
  • Education
    MCS
  • Pronouns
    she/her
  • Work
    Finding work
  • Joined

More fromAneeqa Khan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp