Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Add Lighting Using Light Components in React VR

    15 - 16

    In order to illuminate a scene containing 3D objects a lighting setup is required. In this lesson we'll walk through the available lighting components and create a common outdoor lighting setup.

    This includes the components: <AmbientLight/>, which affects all objects in the scene equally and from all directions; <DirectionalLight/>, which illuminates all objects equally from a given direction; <PointLight/>, which spreads outward in all directions from one point; and finally <SpotLight/>, which spreads outwards in the form of a cone.

    A handy demo for <SpotLight/> can be found here.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    When rendering 3D scenes, we can define a lighting center. For demonstration purposes, our scene already includes a couple of objects positioned in front of us, and the property lit is set to true, so they're affected by the lights.

    In order to create a lighting center, we can leverage a handful of lighting sources that ship with React VR out of the box -- AmbientLight, DirectionalLight, PointLight, and SpotLight. Let's get started with the AmbientLight.

    When we add an AmbientLight, we can see how it affects the scene. The AmbientLight itself is a light that affects all objects in the scene equally from all directions. Upon rendering, all objects in the scene are illuminated equally with the specified intensity and color.

    First, we're going to decrease it to 05. Then we're going to increase it to two, but changing the color can also impact the scene's general coloring.

    We remove the AmbientLight and move on to the DirectionalLight. It's a light that gets emitted in a specific direction. It will behave as it would be infinitely far away, and the rays will all come in parallel. Same as AmbientLight, it accepts intensity as well as color.

    When you position it, keep in mind that it shines towards the target's position, which in React VR's case is us. DirectionalLight is often used to simulate daylight, since the sun is far enough away that its position can be considered infinite.

    The third source we're going to explore is PointLight. We position a PointLight right in between our objects. As you can see, it gets emitted from a single point and spreads outwards in all directions.

    A common use case for this is to replicate the behavior of a light bulb, but compared to the light sources before, this one offers additional properties. For once the distance, which describes the distance from the light to where the intensity goes to zero.

    By default, this distance is set to zero, and when set to zero, the light will never stop. In our case, we set it to two, which means in a radius of two meters the light dims to an intensity of zero.

    Now this intensity itself, the case can be manipulated by the property decay. Its default is set to one, and in our case we set it to two, which leads to physically realistic light falloff.

    Last but not least, we explore the SpotLight. Similar to the PointLight, it originates from a single point, but compared to PointLight, it spreads outwards in only one direction in the form of a cone.

    It also supports distance and decay, but also two more -- penumbra and angle. They're a bit hard to explain and demonstrate, so best way to explore how these parameters affect each other is to play with the demo on the free JS documentation.

    Here you can see how distance affects how far the light shines. Angle means how wide or small the cone is. Penumbra is how sharp the light is, and decay is the effect of how the intensity actually decays.