Set certain object to not receive light

Hi

So I have two entities and three lights in my scene, and both entities are located in the range of all three lights. I was wondering if there’s a way to instruct certain entity not to receive light or to have certain light eliminate certain object when rendering.

Thanks

You can use bit masks to associate objects with lights. It is currently only possible to do through scripting, but hopefully will be also accessible in UI.

The properties to look at are:

  • meshInstance.mask
  • light.mask

If you’re familiar with the concept of bit masks, you’ll get the idea. Object is lit if (meshInstance.mask & light.mask) > 0.

By default all meshes and lights have mask = 1.

In your particular situation you need to have the same bit set on the light and 2 objects, but don’t have it on the 3rd object.

If you have just a single light, it’s enough to set 3rd object’s mask to 0:

var meshes = this.entity.model.model.meshInstances;
for(var i=0; i<meshes.length; i++) meshes[i].mask = 0;

But if, for example, you want your 3rd object to be lit by some other Light2, which, in turn, doesn’t affect other 2 objects, you can do:
On 3rd object:

var meshes = this.entity.model.model.meshInstances;
for(var i=0; i&lt; meshes.length; i++) meshes[i].mask = 2;

On Light2:

this.entity.light.mask = 2;

And what if you want to add some Light3, which illuminates all 3 objects? Then you just write:

this.entity.light.mask = 1 | 2; // 3
1 Like

Is that valid JS? There’s 3 semicolons in the for loop header.

Edit, oh, that’s an HTML entity. It should be

for(var i=0; i < meshes.length; i++) meshes[i].mask = 2;

FYI anyone coming to this post in 2020 you can use layers to acheive this. No scripting required.

PlayCanvas mentions how to do this in the “Layers” developer doc here: https://developer.playcanvas.com/en/user-manual/graphics/layers/

Briefly, the workflow is as follows:

  1. In Settings -> Layers, add a new layer for each light that should only illuminate certain objects.
  2. In Settings -> Sublayers, add the newly created layers as sublayers too. (Opaque + Transparent for each)
  3. In your camera entity config, add all newly created layers to the camera.
  4. In each light entity that you created a special layer for, remove the world layer, then add the layer you created for that specific light.
  5. For the each entity that needs to receive light from the lights in step 4, add the layer(s) corresponding to the light(s) you want to illuminate that object.
8 Likes