babylonjs

Sprites and light


Is there a way to have a BABYLON.Sprite affected by lights just the way meshes are?

The Sprite API has access to the RGB values, so indirectly it can be done, but I'm not sure there is a better way to do this.


Solution

  • Not sure if this is valid for an answer, but it worked for me.

    const size = 10;
    
    // sprites is an array of the sprites on my scene
    sprites.forEach((sprite) => {
      const ratio = Vector3.Distance(sprite.position, light.position) / (size * 10);
      const val = light.intensity - ratio;
      sprite.color.set(val, val, val, 1);
    });
    

    Since the sprite affected by light is not supported by default, I think this workaround should do the trick(as a matter of fact, it does on my game).