Check out a free preview of the full Advanced Creative Coding with WebGL & Shaders course

The "Geometry, Materials & Mesh" Lesson is part of the full, Advanced Creative Coding with WebGL & Shaders course featured in this preview video. Here's what you'd learn in this lesson:

Matt explains how coordinates and color attributes are defined in the three.js library, and how the library is used to create 2D, 3D and 4D coordinates.


Transcript from the "Geometry, Materials & Mesh" Lesson

>> So Three.js, there's some links here. You're probably gonna wanna keep the docks open for a lot of the day. You can also go through some of the examples. They have got tons of examples, and you can browse the source code for the examples. But the first thing that I like to introduce with Three.js is this coordinate, this Vector3 sorta coordinate.

So, we've already got this like little coordinate system here. And we have a little coordinate. But how do we define that coordinate in code? And so we define it in code with this sort of Vector3 data type. So there's like a vector class which can be 2 for 2D coordinates, or 3 for 3D coordinates, or 4 for 4D coordinates.

We're probably gonna be working with Vector3 most often. And we just define it like this new Vector3. We pass in the x, y, and z. And you can actually edit this code and see how it changes the little graphic. So the x, the y, and z and so forth.

We can set individual components on the vector just by doing position.x is equal to something or position.y is equal to something. And that will change the components the x, y, z. And then we can also use this set scalar function, which is gonna place all three components, the x, y, and z to the same value.

So they're gonna say x, y, and z are now gonna be equal to 0.5. Or anything in the past there's 0, just make them all go to the center. And then another function we use a lot is this copy function. So copying the x, y, z components from a different Vector3.

So that's pretty common. And then also going from into arrays. So sometimes we'll have APIs that will be array based. So the x, y, z coordinates will be in just a simple array. And so we can use this method from array to take the x, y, z and put that into the Vector3.

Or alternatively, the other way around, we can use the method toArray which is gonna return a new array that has this sort of structure x, y, z. So that's the basics of vectors. And another concept that's really commonly used is colors. So instead of just using strings like we would in CSS or something like that, in Three.js, we have to use this color interface.

And so we create a new color. We pass in the string, maybe a Hex code or maybe an RGB or HSL string or something like that or just a color like tomato, or green, or blue. And then we can change the RGB values of this color. So you could say color.r is equal 0 or 1, color.g is equal to something, color.b is equal to something.

These values are between 0 and 1, instead of being between 0 to 255. They're all normalized. Same with HSL. When we use things like color.setHSL, it's all normalized between 0 and 1. So the hue is degrees between 0 and 1, where 0 would be 0 and 1 would be 360 degrees.

And then the saturation would be a percentage between 0 and 100%. The same with the lightness. And so that's how we start to use colors. Now, you can also just pass in the RGB values in the constructor instead of passing in a stream. So it'll all be between 0 and 1.

Another concept that we're gonna be talking a lot about is geometries. And so geometries are sort of the volume, the make up of a mesh. How it looks in 3D. So here we've got a plane. A plane is basically just these two triangles that when we put them together, they make up a square or rectangle of some kind.

And it's called either a plane or a quad sometimes cuz it has four vertices. And this idea, this idea of using triangles you're gonna see it everywhere in WebGL. And that's because triangles are the building blocks of everything we make in WebGL. And so if we wanted to draw a square, we can't just say draw a square, we have to say draw two triangles.

That's the most primitive type of rendering that we can do. So everything is built on top of these triangles. And another geometry to be familiar with here, which we're gonna be using a lot today is icosahedron geometries, which creates icospheres and shapes like this. But there's tons of geometries.

You can go through the the docs and see. They also have lots of examples of how to manipulate the parameters here. So we can create a cube with lots of subdivisions. And so quite often if you're creating, let's say, like a terrain or some sort of interesting like parametric object.

Instead of manually defining the layout, the vertices and the indices, and all the structure of the geometry, it's usually easier just to take a box geometry or to take a plain geometry. Subdivide it by giving it lots of width or height segments. And then once the geometry's created, we can manipulate the vertices.

We can change them and create something like a procedural terrain or something like that out of it. And then, couple more concepts would be material, so far we've talked about geometry. Material is more like the surface quality of a mesh. So we have basic material, which is the one we're gonna be using the most.

It just gives you the same color that you give in. So if you say I want a pure red or pure blue or something, or whatever the color is, it'll give you that color back. Almost pixel perfect, or it should be pixel perfect in theory. It's unlit, which means it doesn't respond to lights in the scene.

So if there's a light in the scene, it's not gonna respond to it. And if there's no lights, it's still gonna be perfectly lit. You can change the wireframe on off. And then when we get into some other materials, some of the lit materials like Phong, Toon, and Standard, these only respond to lights in the scene.

So if we don't have any lights in the scene, it's gonna look like this. It's gonna be this black mesh. And that's just like if we turn the lights off in the room, the the meshes around us, the objects around us are not gonna have any light reflecting off them.

One more thing to notice would be this flat shading option, which is another option you can pass into the mesh. And this makes it go from a smooth shade to a faceted look. So for low poly stuff, you're gonna wanna turn on flat shading. But for sort of smoother objects, you're gonna want to turn that off.

Cool. So now we have the idea of geometry. We had the idea of material and we can combine those two things to create a mesh. And so this is how it looks in code. You just create a SphereGeometry, you're passing some properties. Typically SphereGeometries you'll do twice the amount of, I guess it would be the horizontal segments, as you'd have the vertical segments there.

And yeah, you can go really low and you can start to create some more like low poly shapes, are really hard to create really smooth circles and spheres. And then you have a material, this could be MeshPhongmaterial or it could be MeshBasicMaterial. If you just want a simple color, you can pass in wireframe on, off, etc.

And then you build a mesh using these two interfaces here, you just specify the geometry, you specify the material. Something really important is that we like to reuse geometries whenever possible. So it's a good idea to create a single geometry, and then reuse that across lots of different meshes.

And so we're gonna do that in our work today as well.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now