From cgwiki
Revision as of 18:11, 25 January 2023 by MattEstela (talk | contribs) (→‎useHelper)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Or the more honest title. 'React Three Fiber, and how little Javascript can you get away with learning'.


I'd avoided JavaScript for literally decades, buying all the internet hate that that it was messy, slow, best avoided.

Well it's not going away, and with my current job being more web focused, took the plunge. It's nowhere near as bad as I expected, just a couple of new symbols and idioms to learn. If you know enough vex to write a wrangle or two, JavaScript is fine.

My particular focus with it is on React Three Fiber, a React wrapper around Threejs. Seemed scary at first, but hasn't taken long to get mildly productive with it. React introduces a bunch of unfamiliar terms like useRef and useEffect and other things, but for the most part just pulling apart examples from other people is a good way to get started.

Codesandbox was the big revelation for me in terms of learning. It's a combined text editor like VScode, a web development framework like npm, and a browser-in-a-browser. There's several similar offerings available (jsfiddle, shadertoy, codepen etc ), but is the one favoured by the R3F scene. While its possible to use tools like npm and vscode natively on your machine to do dev (and as you get deeper into this stuff its a natural progression), but for just starting out, codesandbox is a no-brainer; find an example of something you want to do, fork it, code away.

Language tricks

I started this as a way to remember how to port my python knowledge over to javascript, but I've since realised it's just a summary of the great react docs at . I'm going on the asssumption that people skimming this are looking to get into react three fiber, which means learning react, which means you may as well just jump over there and read their docs!

List comprehension is a map

My python coding is 99% list comprehensions, its the one bmx trick I know, I use it everywhere:

mylist = [1,2,3,4]
[ x*2 for x in mylist ] 
# returns [2,4,6,8]

Javascript uses a similar thing, map.

mylist = [1,2,3,4] x => x*2) 
// returns [2,4,6,8]

This combines a couple of ideas into typically terse but practical js. Map lets you call a function on the elements of an array. The full form would look like this:

function doubleIt(x) {
    return x*2

mylist = [1,2,3,4]

// returns [2,4,6,8]

If the function is short, you can can use the arrow => operator to define an in-line function. This is a pretty standard thing in react. You can write a longer function, you just have to remember to use curly braces and a return. Watch where the regular braces match (after map, end of the function), and the curly braces (after the arrow, before the final brace):

mylist = [1,2,3,4] => {
    n += 1
    n *= 2
    n = n + ' is the magic number'
    return n
// returns ['4 is the magic number', '6 is the magic number', '8 is the magic number', '10 is the magic number']

List comprehension is a filter

My other standard trick with list comprehensions is to add in some filter logic:

mylist = [1,2,3,4]
[ x * 2 for x in mylist if x>=3]
# returns [6,8]

In javascript you run the filter and map as separate operations:

let mylist = [1,2,3,4]
mylist = mylist.filter(i => i>=3)
mylist = => x*2 )
// returns [6,8]

It's possible to concatenate these operations together, but readability suffers:

mylist = [1,2,3,4].filter(x => x>=3).map(x => x*2 )
// mylist is [6,8]

I've seen it written like this, which is a little better? I guess? Rising tone at end of sentence??

mylist = [1,2,3,4]
    .filter(x => x>=3)
    .map(x => x*2 )
// mylist is [6,8]

Map a dictionary or object

R3F will load glb's as an object of key:value pairs. These can't be directly mapped, but you can get a list of the keys, then map on that.

This is one way to do that, codesandbox here:

// get a list of keys
const keys = Object.keys(nodes) 

// from here you could do a simple map of just return the keys: k => k)

//or use this key to retrieve the matching values: k => nodes[k])

// you can also get back an index if needed: (k,i) => 'index of '+k+' is '+i)

// Here's a full show off thing that builds a list of 
// transformed nodes in a glb with a purple material:
const list = (k,i) => {
return (
  <group key={i} position-x={i*0.05}>
    <primitive name={k} object={nodes[k]}>
      <meshStandardMaterial color={'mediumpurple'} />

Spread operator or the 3 dots thing

I thought it was code snippets online implying 'and you fill in the rest', but no, its a thing. Several JS examples online will have function calls that look like


What on earth does that mean? Well imagine you had a particularly stupid function called add4, that took 4 explicit arguments:

function add4(a,b,c,d) {
  return a + b + c + d;

// returns 13

Now say you had an array with 4 numbers, and you wanted to send that to add4. If you're being a good person you'd declare the arguments explicitly:

myvals = [1,2,3,4]
// returns 10

But what if you were lazy, and put trust in javascript's generally pretty loose and forgiving behaviour? You'd get this:

// returns '1,2,3,4undefinedundefinedundefined'

It its trying to add the array [1,2,3,4] to 3 undefined variables, its not happy. The ... syntax does the implicit unpacking for you, 'spreading' the array elements into the matching slots. Very handy.

// returns 10


React Three Fiber is a pretty amazing React wrapper around ThreeJS. Lets you write web 3d almost as easy as writing html, with a ton of other nice things. That first link there goes straight to their collection of amazing examples, click on any of them to be taken to a codesandbox setup you can immediately customise, super cool, or scroll down a little further to the basic examples if you're after cleaner templates.

The Three JS Journey tutorial site is a great way to get started, well worth the money.

R3F terminology

The terms can be a little confusing at first, here's a glossary:

  • Threejs - a javascript library for doing 3d on the web. Its great, but a little low level for my tastes (like people who wanna vex everything)
  • React - a popular framework for the web, originally developed by facebook, now open source. One of its key aims is to simplify complex javascript development and be able to write clean easily accessed components, so that stuff that would otherwise be arcane javascript boilerplate can almost be treated like html. So in the way you use <b>bold</b> and <i>italic</tags>, you could have an image carousel reduced to a single <Carousel> call, or a twitch stream window just <Twitch>.
  • React Three Fiber - a React wrapper on Three. Combining the two above ideas, rather than using javascript box = new box() style coding, you write 3d in a html style way, so <Box>, and <camera> and <directionalLight> . Seem obtuse at first, but it actually makes a lot of creating 3d on the web much lighter and more fluid.
  • Drei - like Sidefx labs for R3F. The maintainers of R3F have strong ideas about what should be in the core R3F system, but are also aware there's lots of helpers and nice-to-haves that people want. All those things are pushed into the Drei library (drei is german for 'three', geddit?), and there's an incredible amount of cool stuff in it. You want sparkles? <Sparkles />. You want easy motion trails? <Trails />. You want to load a gltf? Environment maps? A premade presentation stage with soft lighting and an orbitable camera? It's all there.
  • Pmdrs - the artist and programmer collective behind both R3F and Drei.

Make colours match across Figma and R3F

This may be specific to our pipeline, but we had issues where 3d geometry that was derived from figma looked different in R3f:

  • textures looked desaturated and pale
  • colours thats should be identical in textures and vertex colours looked very different.

Turns out the vertex colours on our end required gamma correction, so running them through a gamma of 0.45 made them match the textures.

The bigger issue was the deafult tonemapper used by R3F. While it's filmic and great, my gut feeling is the shoulder of the tonemapper is too aggressive, and starts desaturating and softening off colours well before they should start getting that filmic overexposure vibe.

To turn it off and revert back to classic crappy srgb, add the flat={true} prop to the canvas:

<Canvas  flat={true}>

This will cause colours to oversaturate and highlights to go weird, but diffuse surfaces will match their colour much better to the unlit textures, very important for logo work. In my case when using the Stage helper, I could bring everything down to a balanced level by adjusting the stage intensity:

<Stage intensity={0.5}  >

Modifying glb materials

To debug the colour stuff, I needed to swap between standard and unlit materials ( meshStandardMaterial and meshBasicMaterial in threejs terms).

I was using the gltfjsx converter to get a tag representation of the glb, to tweak this to my requirements was easy enough; comment the material prop, make the mesh tag have open and close sections, and insert a material tag in the middle. So at a simplified level, this:



 <meshStandardMaterial color={'green'} />

To make it use vertex colours from the mesh, swap color={} for 'vertexColors':

<mesh geometry={} >
 <meshStandardMaterial vertexColors />

Textures are assigned to materials via the map={} prop. I console logged out the material, expanded out its tree to get the map, and directly referred to it:

<mesh geometry={nodes.logo.geometry} >
 <meshStandardMaterial map={}/>

finally if the image had an alpha channel, that can be enabled with the transparent prop:

<mesh geometry={nodes.logo.geometry} >
   <meshStandardMaterial transparent={true} map={}/>

Transparent images

Make sure to add


To any materials using texture with alpha, otherwise you'll get odd colours bleeding over where the alpha is meant to be 0.

Except you'd probably assume this would show the image where the alpha is 1, and reveal the plain untextured material where the alpha is 0 right. No. It will make the entire object transparent where the alpha is 0.

I was surprised that there's no built in mechanism to just treat the image like a decal. This leaves you with a few options:

  • Make a duplicate of your mesh that just has a plain untextured material. This is the easiest, but its inefficient. If you're only showing a handful of lightweight meshes, this isn't such a big deal.
  • Use Drei's Decal function, however this is for projecting a texture, not for working in the uv space of an image (and under the hood its also duplicating your mesh anyway)
  • Write your own shader to do the required alpha compositing. This is what people in native threejs are doing.
  • Use a layered shader builder like Lamina

As I said, I'm just duplicating meshes for now.

Occlusion Roughness Metalness

Codesandbox example:

ORM maps appear to be hardcoded in threejs for MeshStandardMaterial, so don't try and be cute with material networks in Houdini, Three will ignore you.

So if you want to use occ/rough/metal, pack it so that

Red = Occlusion
Green = Roughness
Blue = Metalness
  • Occlusion assumes it's being treated as a lightmap bake, so wil only work if you have a second uv set. Otherwise it's ignored.
  • The map values will multiply against the roughness and metalness properties.
  • If roughness and metalness aren't specified then they go to a default of roughness=1 (fine) and metalness=0 (not fine, your map will never be seen!)
  • This means you can adjust the strength of your roughness/metalness by tweaking the matching property as an overall multiplier, handy.
  • You can use seperate occ/rough/metal maps, thats fine, but its a waste of resources if three is just loading a single channel from each map anyway.

Here's how it looks if you're defining by hand:

 const orm = useTexture('./test_orm.png')
 const col = useTexture('./col.png')
 return (   
     <boxGeometry args={[1, 1, 1]} />

Flipped UVs on GLB meshes

Annoyingly for some reason uv's will be upside down for glbs in R3F. I was going back to Houdini and flipping uv's there, only to find after the fact that there's a built in threejs option, which you can access from R3F. Just add map-flipY={true} :

<meshStandardMaterial map={texture} map-flipY={true} />

Load and playback a GLB

I always forget the basic version, here's a template:

import  {useGLTF, useAnimations } from '@react-three/drei'

export const MyGlb = () => {
  const url = '/mycool.glb'
  const { scene, animations } = useGLTF(url)
  const { actions, names } = useAnimations(animations)
   useEffect(() => {    
    const anim = actions[names[0]]
    return (
     <primitive object={scene}  />

// and then over in your canvas element
<MyGlb />

Control GLB playback timing from a time source

Clever Mike Lyndon to thank here. Normally you'd just go and let it run its own thing, but what if you need the playback locked to an external source, like the global frame variable provided by remotion?

useEffect(() => {    
    const anim = actions[names[0]] = true
    anim.time = 0.25+(frame-10) / 25.0 

Clone a glb with animation

R3F doesn't like when you refer to a glb twice. The drei Clone function lets you do what it says and clone whatever you need, but when you do this, you lose the connection to the original animation.

Paul from pmndrs pointed to the glb example they provide that has a replicated glb playing different animations, I slimmed it down a little to just the bits I needed. Some tricky stuff involving three functions and mystery helpers, but as I understand it the core of it is you have to make a deep clone of the full scene early on, and be able to identify the copies with ref's.

The other esoterica here is the skinnedMesh call. I'm using a houdini generated glb, where I had a top level joint called 'root', and a skinnedmesh called 'mesh'. Lots of head scratching while looking at the other codesandbox reference, in the end you need to point to use both a <primitive> call that points to 'nodes.root', and in skinnedmesh refer to 'nodes.mesh.geometry' and 'nodes.mesh.skeleton':

import { useGLTF, useAnimations } from '@react-three/drei'
import { useEffect, useMemo} from 'react'
import { useGraph } from "@react-three/fiber"
import { useCurrentFrame } from 'remotion'
import { SkeletonUtils } from "three-stdlib"

export const MultiGLB = () => {
  const url = '/theglb.glb'
  const { scene, animations } = useGLTF(url)
  const clone = useMemo(() => SkeletonUtils.clone(scene), [scene])
  const { nodes } = useGraph(clone)
  const { ref, actions, names } = useAnimations(animations)

  useEffect(() => {    
    const anim = actions[names[0]]

  return (
    <group ref={ref}  >
        <primitive object={nodes.root}  />


edit: well dur, this is the first thing covered on the hooks documentation for R3f.

Three has the abilty to show gizmos on lights/cameras/objects like a 3d app would, R3F wraps that up with the userHelper hooks. I struggled with this, each time I tried to use it I'd have R3F shout at me that I was using it in the wrong place.

I think what I was doing wrong is that it needs to be called from within the canvas component. I kept calling it from the function that was returning a canvas component, but that seems to be one level too far away.

The fix is to wrap whatever you're wanting a useHelper for in a component, then call that in your canvas. Ie, this won't work:

import { useHelper } from "@react-three/drei"
import * as THREE from 'three'

export const App = () => {
  const camRef = useRef()  
  useHelper(camRef, THREE.CameraHelper, 1) // outside the canvas, dur
  return (
      <orthographicCamera ref={camRef}  /> 

but if you create a component that wraps both the camera and the helper, and call that in the canvas, its happy:

import { useHelper } from "@react-three/drei";
import * as THREE from 'three'

const CamWithHelper = () => { // wrap camera and helper together
  const camRef = useRef()  
  useHelper(camRef, THREE.CameraHelper, 1)
  return (
    <orthographicCamera ref={camRef}  /> 

export  const App = () => {
  return (
    <Canvas >
        <CamWithHelper /> // helper and camera in the correct scope, woo


Template with javascript

Remotion is an amazing react system that lets you control other react components from a timeline, including r3f. It defaults to typescript, but there's a hello world js template available in the demo pack. Run

npm init video

as per usual, choose the second demo.