Want to learn more?

That's the end of the free part of this lesson

WebGPU & TSL Course new $45 VAT incl.
  • 21 lessons · 24 hours of video
  • Quizzes · Certificate
  • Members-only Discord server · Future updates
00:00/00:00
3:22
00:03:22

Shortcuts ⌨️

  • SPACE to play / pause
  • ARROW RIGHT or L to go forward
  • ARROW LEFT or J to go backward
  • ARROW UP to increase volume
  • ARROW DOWN to decrease volume
  • F to toggle fullscreen
  • M to toggle mute
  • 0 to 9 to go to the corresponding part of the video
  • SHIFT + , to decrease playback speed
  • SHIFT + . or ; to increase playback speed

Unlock content 🔓

To get access to 93 hours of video, a members-only Discord server, subtitles, lesson resources, future updates and much more join us for only $95!

Want to learn more? 👍

85%

That's the end of the free part of this lesson

WebGPU & TSL Course new $45 VAT incl.
  • 21 lessons · 24 hours of video
  • Quizzes · Certificate
  • Members-only Discord server · Future updates
Next lesson
WebGPU & TSL
Chapter 02 — Advanced Projects

76. Patterns

Difficulty: Medium01:03:39

Introduction 00:00

In the previous lessons, we discovered TSL and its many features. So many that you probably forgot most of them already. We need to practice them, bit by bit, which is why we’ll redo some of the Shaders lesson in a familiar environment.

Let’s start with the Shader Patterns. Most of the patterns are simple; we can focus on fragments only (don’t worry, more vertex manipulation in the upcoming lessons), and we’ll discover some tricks and make mistakes along the way.

Don’t worry, we’re not going to do all the patterns.

Like in the original lesson, if you’re looking for a challenge, try to do the patterns on your own. And remember that there are always many solutions. Some solutions can be more clever, some more performant, some make more sense, and some are more maintainable. As long as you practice, you’re in the right direction.

Setup 01:14

The starter already contains the following:

  • The plane on which we are going to draw the pattern
  • OrbitControls to rotate around
  • A floor to have a little bit of scenery
  • Running the WebGPURenderer with the Inspector

Pattern 1 01:39

Let’s start with the first pattern.

What you see is the UV coordinates of the plane, and it will be the main input of every upcoming pattern. But the first question is, how do we display this UV on the plane?

The material is an instance of MeshBasicNodeMaterial, meaning that we can already assign nodes.

We went for the Basic instead of a shaded material such as Phong, Lambert, or Standard to focus on the pattern without it being affected by light or shadow.

To output the pattern, we use the colorNode which expects a vec3() node:

// Pattern 1
material.colorNode = vec3()

Since we didn’t provide any argument to the vec3(), it used the default values which are 0 for the x, y and z (more like r, g and b since we use it as a color).

x, y, z, w, and r, g, b, a are interchangeable when it comes to TSL vectors.

Let’s try with some values.

material.colorNode = vec3(1, 0.5, 0.2)

It’s working, but now we want the UV gradient, which we can get from the uv() node as the first argument of vec3() and 1 as the last argument:

material.colorNode = vec3(uv().x, uv().y, 1)

This is a valid solution, but there is a shorter version. When sending a vector as an argument to a bigger vector, it’ll take the respective properties in the order it was provided.

This means that we don’t have to explicitly provide the x and the y separately:

material.colorNode = vec3(uv(), 1)

Pattern 2 04:11

Here’s another easy one.

To create this linear gradient, we can use the x coordinate of uv() only, but on all 3 values of the vec3() so that we get a grayscale gradient:

// Pattern 2
material.colorNode = vec3(uv().x, uv().x, uv().x)

You can leave previous patterns without commenting them. Since we re-assign material.colorNode, they will be overridden.

And again, there is a shorter version. If we provide one value to a vector, this single value will be used as all properties of the vector:

material.colorNode = vec3(uv().x)

Pattern 3 05:13

Let’s try something harder.

Want to learn more?

That's the end of the free part of this lesson

WebGPU & TSL Course new $45 VAT incl.
  • 21 lessons · 24 hours of video
  • Quizzes · Certificate
  • Members-only Discord server · Future updates

How to use it 🤔

  • Download the Starter pack or Final project
  • Unzip it
  • Open your terminal and go to the unzip folder
  • Run npm install to install dependencies
    (if your terminal warns you about vulnerabilities, ignore it)
  • Run npm run dev to launch the local server
    (project should open on your default browser automatically)
  • Start coding
  • The JS is located in src/script.js
  • The HTML is located in src/index.html
  • The CSS is located in src/style.css

If you get stuck and need help, join the members-only Discord server:

Discord