Read about the types of lighting options and highly performant renderer WebGLRenderer available in Three.js, a JS graphics library.

Three.js is a JS graphics library that is used for rendering 3D graphics in browsers. Major graphics organizations use Three.js for creating and rendering 3D scenes for movies, anime, advertisements, and games.

Three.js uses the WebGL engine in the browser for rendering scenes. The API is based on OpenGL (GL stands for graphics library), a desktop graphics API. In this post, we’ll look at types of lights and WebGLRenderer in Three.js.

Lights

First, let’s learn how to use lights in Three.js so we know the types of lighting that can be used and the effect each of them presents.

Three.js offers several different types of light. All lights in Three.js are instances of THREE.Light. They offer us illumination so that we can light up objects in a scene.

Not all material texture in objects in Three.js is affected by lighting. Only objects with MeshLambertMaterial and MeshPhongMaterial material texture can be affected by lighting.

Here are the type of lights available in Three.js:

Ambient

Ambient light is used for the illumination of objects in a scene. It does not point or direct light toward a direction, so it cannot cast shadows.

THREE.AmbientLight(color) 

The objects are illuminated by the color in the AmbientLight. Ambient light affects all lit objects in the scene equally, and is essentially a light that’s color is added to the color of an object’s material.

var cubeGeometry = new THREE.CubeGeometry(20, 20, 20);
var basicMaterial = new THREE.MeshLambertMaterial({
    color: 0x0095DD
});

var cubeMesh = new THREE.Mesh(cubeGeometry, basicMaterial);

var light = new THREE.AmbientLight(0x404040);
scene.add(light);

scene.add(cubeMesh)

For example, this illuminates the cube object with soft white light:

threejs-light-remderer

var light = new THREE.AmbientLight(0xf6e86d);

This code will illuminate the cube object with a green color.

threejs light renderer example

#javascript #three #web-development #programming #developer

How to use Lighting and WebGLRenderer in Three.js
6.80 GEEK