Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Three.js

You're reading from   Learn Three.js Program 3D animations and visualizations for the web with JavaScript and WebGL

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781803233871
Length 554 pages
Edition 4th Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jos Dirksen Jos Dirksen
Author Profile Icon Jos Dirksen
Jos Dirksen
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Getting Up and Running
2. Chapter 1: Creating Your First 3D Scene with Three.js FREE CHAPTER 3. Chapter 2: The Basic Components that Make up a Three.js Application 4. Chapter 3: Working with Light Sources in Three.js 5. Part 2: Working with the Three.js Core Components
6. Chapter 4: Working with Three.js Materials 7. Chapter 5: Learning to Work with Geometries 8. Chapter 6: Exploring Advanced Geometries 9. Chapter 7: Points and Sprites 10. Part 3: Particle Clouds, Loading and Animating Models
11. Chapter 8: Creating and Loading Advanced Meshes and Geometries 12. Chapter 9: Animation and Moving the Camera 13. Chapter 10: Loading and Working with Textures 14. Part 4: Post-Processing, Physics, and Sounds
15. Chapter 11: Render Postprocessing 16. Chapter 12: Adding Physics and Sounds to Your Scene 17. Chapter 13: Working with Blender and Three.js 18. Chapter 14: Three.js Together with React, TypeScript, and Web-XR 19. Index 20. Other Books You May Enjoy

Exploring the HTML structure for Three.js applications

In this section, we’ll look at the source of the geometries.html file. You can do this by looking at the source in the browser or opening the file from the dist/chapter-1 folder in the same location where you downloaded the source for this book:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <style>
    body {
      margin: 0;
    }
  </style>
  <script defer src="../js/vendors-node_modules_three_
    build_three_module_js.js"></script>
  <script defer src="../js/vendors-node_modules_lil-gui_
    dist_lil-gui_esm_js.js"></script>
  <script defer src="../js/vendors-node_modules_three_
    examples_jsm_controls_OrbitControls_js.js"></script>
  <script defer src="../js/geometries.js"></script>
</head>
<body>
</body>
</html>

This code is generated when you run the npm run build step. This will combine all the sources and external libraries you’ve used into separate source files (called bundles) and add them to this page. So, you don’t need to do this yourself. The first three <script> tags refer to any of the external libraries we use. Later in the book, we’ll introduce other libraries such as React.js and Tween.js. Those will be included in the same manner automatically. The only other elements here are <style> and <body>. <style> is used to disable any margins in the page, so we can use the complete browser viewport to show our 3D scenes. Furthermore, we’ll add the 3D scene programmatically into an empty <body> element, which we’ll explain in the next section.

If you do want to add custom HTML elements here, you can, of course, do that. In the root of the downloaded code, you’ll find a template.html file, which is used by the build process to create the individual HTML files for the examples. Anything you add there will be added to all the examples. We won’t dive too deep into how this works since that’s outside the scope of this book. However, if you want to learn more about how this works, a couple of good resources on webpack (which we use for this) are as follows:

  • The getting started with webpack guide: https://webpack.js.org/guides/getting-started/. This site contains a tutorial that explains the reason why we need webpack for JavaScript development, and how the basic concepts work.
  • Information on the HTML webpack plugin: https://github.com/jantimon/html-webpack-plugin. Here, you can find information on the webpack plugin we use to combine the sources into the separate HTML pages you see when you open the browser after running npm run build and then running npm run serve.

Note that we don’t have to explicitly initialize our scene or call JavaScript. Whenever we open this page and the geometries.js file is loaded, the JavaScript from that file will run and create our 3D scene.

Now that we’ve set up the basic structure, we can create and render our first scene.

You have been reading a chapter from
Learn Three.js - Fourth Edition
Published in: Feb 2023
Publisher: Packt
ISBN-13: 9781803233871
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime