Notes to Self

These are notes written to myself. You could generously call it a blog. That's probably reaching.

TLDR; Fully Metallic materials in ThreeJS don’t respond to non-directional lights - eg. Ambient or Hemisphere Lights. Adjust the material to be non-metallic, or add Directional lights to the scene

The MeshStandardMaterial in ThreeJS (and all materials that inherit from it - MeshPhysicalMaterial etc.) have a metallness attribute.

The metallness attribute defines how metalic the material should appear. It’s a float, with values from 0 to 1.0. 0 being not metallic at all, 1.0 being fully metallic.

Often when working with scenes, you may see a material showing up as fully black, even when there are lights in the scene. This metallness value might be the cause of that.

In ThreeJs fully metallic materials only respond to non-directional lights. If you’ve only got AmbientLight or HemisphereLight lights lighting your scene, anything with a metallness value of 1.0 will be fully black.

Fully Metallic Black Material A test prop with a material set to be fully metalic, and only AmbientLight lights lighting the scene, leaving a fully black output material

There’s a handful of different ways to fix this, depending on the scene, materials and effect you need.

1. Turn off the metallness on the material

  • Pros It’s simpler to test
  • Cons With only non-direction lights you’ll have a flat looking scene

Often the metallness value might have been mis-set in the original scene. Simply setting it to 0 will allow the material to be lit1:

mesh.material.metallness = 0
mesh.material.needsUpdate = true

Non Metallic with Ambient Lighting Materials adjusted to be non-metalic, with only an AmbientLight lighting the scene

2. Add directional lights to the scene

  • Pros Properly setup lighting will give good looking shadows
  • Cons It’s more involved and expensive on the render

This depends on what your scene needs to look like, but just adding a directional light will show up the material properly. You’ll probably want a handful of lights at different intensities and positions around the center point2.

var light = new THREE.DirectionalLight('#ffe6ae', 5)
light.position.set(7.5, 5, 10)

Adding Directional Lighting The same scene, with the materials set to non-metalic, but with 3 DirectionalLight lights added around the object, and shadow maps enabled

  1. Be sure to set needsUpdate to true when modifying any material values otherwise you won’t see the changes. material.needsUpdate = true [return]
  2. Directional Lights default to a target of [0,0,0] unless you set an alternative. [return]

In a ThreeJs scene you might want to lock down certain camera motions.

Using the standard OrbitControls controls system, you’d have something like this setup for your controls in the scene:

controls = new OrbitControls(camera, container)

Controls have 3 basic movements available - zoom, pan, and rotate.

It’s easy to disable each of these directly like so:

controls.enableZoom = false 
controls.enablePan = false 
controls.enableRotate = false 

Disabling all three of the movement types effectively locks the camera in place, which is great if you want to take control of the view for something specific.

Bonus - Disabling controls unbinds any associated input listeners!

The most notable benefit of this - normally with Zoom enabled, the canvas will capture any scroll events and interpret them as zoom in/out motion. For users on a page with a viewer in the content area, this can easily disrupt the scrolling down.

Setting controls.enableZoom = false unbinds the scroll input listeners, so scrolling down a page behaves unaffected by the viewer. Super!

Within Previz we use a custom fragment and vertex shader to handle multiple UV mapping support.

These shaders run in a language called GLSL, which is similar to C/C++, and runs directly on the GPU, and run very close to the metal of the machine with have very limited outputs - they effectively control what the GPU is drawing to the individual pixels and nothing else. Because of that - debugging them is pretty hard. console.log is just a fever dream in this context.

In looking for ways to debug, I ran into an amazing workaround option - instead of trying to output debugging context back to the original context (ie. a browser console or debug hook) overlay debug info on top of the rendered stream.

It uses 2 parts - first a new texture available on the vertex shader which is a character set texture, and secondly a debug function that offsets that texture and draws a debug output onto the output.

Shader Glyfs image

Shader Debug Output

All credit goes go Spektre on this Stack Overflow thread.

The main application running on the kiosks is a VueJS web app, installed as a PWA on the individual tablets. This pairs up to web-views running on a media server, which outputs to a large LED wall.

In this case there are multiple tablets on site, and they will be hot swapped out during the event.

During the actual event - the only visible UI on the tablets is the kiosk application, so we needed an easy way to pro-actively debug and check the state of all the devices without having to physically check each one by one.

For that we built up a quick fleet management system within the app. Each instance of the web app has a built in heartbeat that’s emitted via a web-socket to our management system, and exposes a few helper utility functions.

Instances send a heartbeat every 5 seconds with a payload that looked something like:

    msg: 'heartbeat',
    id: '{instance_id}',
    state: '{current_view}'
    meta: { ... }