Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realistic lighting (sunlight) with Three.js?

Tags:

I'm attempting to create a small 1st-person game using Three.js, but I'm having trouble with the lighting. Basically I want to simulate the sun and have it rotate around casting light on everything. I'm using THREE.DirectionalLight at the moment and it only lights up the one direction so sides of cubes remain black/dark.

Do I have to use multiple lights so everything is lit up? Or could I somehow reflect light off the ground/objects?

like image 673
Joey Morani Avatar asked Mar 18 '13 13:03

Joey Morani


People also ask

How do I add fog to my 3js?

Just add the following line of code after you've defined your scene: scene. fog = new THREE. Fog( 0xffffff, 0.015, 100 );

What is directional light?

Directional Lights A directional light mimics the lighting that you would get from the sun. Directional lights emit parallel light rays in a single direction but the light reaches out into infinity.


1 Answers

Yes, you'll have to use multiple lights to achieve this, faking the reflected light. Computing real reflected light isn't built in (and computationally very complex/expensive). You have a variety of options.

A second directional light that could always be in the opposite position and direction of your sun.

A hemisphere light that remains constant. Hemisphere lighting gets a sky color and a ground color and intensity and adds a nice extra bit of depth to your lighting.

//                                    sky color ground color intensity  hemiLight = new THREE.HemisphereLight( 0x0000ff, 0x00ff00, 0.6 );  

here's a working example https://threejs.org/examples/#webgl_lights_hemisphere

you could use any combination of different lights but be careful there's a performance trade off.

It's also worth mentioning half-lambert shading which is enabled by setting the wrapAround property to true in your material. This gives a nicer falloff to black that results in less harsh lighting. More mid tones and less blacks.

like image 179
Andrew Berg Avatar answered Sep 17 '22 19:09

Andrew Berg