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? 🖖

82%

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 01 — Fundamentals

72. Math

Difficulty: Easy20:13

Introduction 00:00

Let’s be honest, a huge part of building shaders is about math.

We calculate distances, angles, directions. We transform, we project, we normalize, and we regret not being more attentive in school.

TSL offers a wide range of mathematical nodes, from basic arithmetic to trigonometry, matrices, procedural functions, and much more.

Don’t worry, we’re not going to review them all. But we’ll try some, just to get the hang of it, know what can be done, and learn a few tricks along the way.

The lesson starts with a bit of theory, but you’ll get plenty of opportunities to practice those concepts in the following lessons.

Setup 00:59

The starter already contains the following:

  • OrbitControls to rotate around
  • A floor already using the MeshStandardNodeMaterial with the fade out
  • A torus knot knot already using the MeshStandardNodeMaterial
  • Running the WebGPURenderer
  • Some lighting with shadows

Arithmetic nodes 01:13

Until now, we’ve been doing basic arithmetic with add(), sub(), mul(), and div().

Most of the math nodes can be used as standalone functions, or chained:

const foo = float(0.25)
const bar = float(0.5).add(foo)

// Same as
const foo = float(0.25)
const bar = add(float(0.5), foo)

We are not going to test the following examples on a material.

And while we can use JavaScript numbers as the arguments, we can’t chain the number until it’s converted to a node (like float()):

// Works
const bar = add(0.25, 0.5)

// Doesn't work
const bar = 0.25.add(0.5)

Some nodes support more than 2 parameters:

const bar = add(0.25, 0.35, 0.4)

And TSL handles type conversion automatically, like multiplying a vector by a float:

const foo = vec3(0.25, 0.25, 0.25)
const bar = foo.mul(4)

But this automatic type conversion can lead to unexpected results:

const foo = int(1)
const bar = foo.add(float(0.3)) // <--- 1 (integer)

const foo = float(0.3)
const bar = foo.add(int(1)) // <--- 1.3 (float)

Again, there are many available nodes, like pow(), mod(), step(), min(), cos(), remap(), clamp(), etc. and we are not going to test them all now, but have a look at the list so that you know what’s available.

Constants 04:07

TSL provides classic mathematical constants that we often use such as

  • PI
  • TWO_PI
  • HALF_PI
  • EPSILON
  • INFINITY
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