2

Finally, i need a normal shadow. But using Spot / Directional lights with Lambert / Phong materials i get no proper result:

picture with examples

When i use Spot Light with Lambert Material, this material don't react to light (pic. 1, 2). When i use Spot Light with Phong Material, i get shadow, like pattern, not smooth (pic. 3, 4). When i use Directional Light with Lambert / Phong material, i get smooth, but not proper shadow (pic. 5 - 8).

I use this preferences for shadows:

renderer.shadowMap.enabled = true;
renderer.shadowMapSoft = true;

renderer.shadowCameraNear = 3;
renderer.shadowCameraFar = camera.far;
renderer.shadowCameraFov = 50;

renderer.shadowMapBias = 0.0039;
renderer.shadowMapDarkness = 0.5;
renderer.shadowMapWidth = 1024;
renderer.shadowMapHeight = 1024;

And this for lights:

var ambientLight =new THREE.AmbientLight( 0x555555 );
scene.add(ambientLight);

and

var spotLight = new THREE.SpotLight( 0xffffff);
spotLight.position.set( 12, 22, -25 );
spotLight.castShadow = true;
scene.add(spotLight );

and

var directionalLight=new THREE.DirectionalLight( 0xffffff, 0.5 );
directionalLight.position.set( 12, 22, -25 );
directionalLight.castShadow = true;
scene.add(directionalLight);

Also, i use the same castShadow and receiveShadow propertyes for all of this examples.

If it needing, other code can be watched as sourcecode of this page:

Spot Light, Lambert Material

This code the same for all of my examples, excluding light - material combinations.

gman
  • 100,619
  • 31
  • 269
  • 393
  • Now i check my examples on another machine, and found that the example with the Spot Light / Lambert Material is working the same way as a Spot Light / Phong Material. Could it depends of video card? But other problems are still the same: not smooth shadow with Spot Light and not proper shadow with Directional Light in combinations with both types of materials. [new example](http://i.imgur.com/U2GjzxR.png) – Алиса Фамина Jun 04 '16 at 21:40
  • [THIS](http://stackoverflow.com/questions/37581812) question may be related – 2pha Jun 05 '16 at 03:58
  • It is. @2pha, thank You. It solves my recent problems, but I have a new one. All not lighted faces have no color: [example](https://jsfiddle.net/alisa23a/b3Lquwuw/3/). If I should create a new question, or extend this one? – Алиса Фамина Jun 05 '16 at 15:27

1 Answers1

2

Realtime shadows in Three.js are tricky in general. Here are some basics to follow to improve your example.

Limit the shadow.camera-frustum:

spotLight.shadow.camera.near = 25;
spotLight.shadow.camera.far = 50;
spotLight.shadow.camera.fov = 30;

Increase the shadow.mapSize:

spotLight.shadow.mapSize.width = 2048;
spotLight.shadow.mapSize.height = 2048;

Use shadowBias to reduce artefacts:

spotLight.shadowBias = -0.003;

The result isnt perfect because now light seams inside the room are showing up. It requires more tweaking and trade-ofs, but maybe its good enough for your needs: https://jsfiddle.net/wbrj8uak/8/


Just leaving a comment here, regarding 2pha´s updated example and why im restoring it:

setting the camera position results in a disappearing shadow inside the room. This is sure confusing for the poster who wants to have a shadow inside, thats why i just left his code the way he supplied it.

Falk Thiele
  • 4,424
  • 2
  • 17
  • 44
  • Thank You, it is helpful. But can You also answer one more question? Why only lighted faces have color, and why a material is semi-opaque for light in this [example](https://jsfiddle.net/alisa23a/b3Lquwuw/4/)? – Алиса Фамина Jun 05 '16 at 17:53
  • The grey color you see on faces that are not hit by the spot/directional light are just reflecting the ambient light (#555555). By semi-opaque you are talking about the bright triangle spot at the shadow side of the wall? I dont know where that is coming from... – Falk Thiele Jun 05 '16 at 18:16
  • Thank You for the explanation about ambient light. So, how to make a realistic color of the faces, which not turned to a directional light? Is it necessary to add more directional light sources with a different directions? A bright triangle is a light from window. It passes through the wall and the floor: [picture](http://i.imgur.com/XPjAAaK.png). A dark area around that bright spot, is a shadow from the ceiling and other walls. Therefore, I say that the material is semi-opaque (or semi-transparent?), if it transmits light. Do You know, how to set zero transparency to material? – Алиса Фамина Jun 05 '16 at 19:39
  • 1
    The main reason I edited the fiddle in the first place is that OrbitControls does not load properly and results in an error making the fiddle just display a black screen, at least it does for me in Chrome. The new fiddle (version 7) does this also. – 2pha Jun 06 '16 at 02:17
  • @2pha, that's exactly what I wanted from the beginning [https://jsfiddle.net/alisa23a/b3Lquwuw/7/](https://jsfiddle.net/alisa23a/b3Lquwuw/7/). Except that the light passes through the material and is visible from the other side: [picture](http://i.imgur.com/ZXOnzms.png). How do I close this issue to set the new, about the transparency of the material in my example? – Алиса Фамина Jun 06 '16 at 13:28