如何在Three.JS中创建方向性光影? [英] How to create directional light shadow in Three.JS?

查看:300
本文介绍了如何在Three.JS中创建方向性光影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 DirectionalLight 创建阴影?

如果我使用 SpotLight 然后我看到一个阴影,但如果我使用 DirectionalLight 它不起作用。

If I use SpotLight then I see a shadow, but if I use DirectionalLight it doesn't work.

推荐答案

是的,你绝对可以 使用定向灯来投射阴影。您需要确保不使用 MeshBasicMaterial ,因为它们不支持阴影。请改用 MeshLambertMaterial MeshPhongMaterial

Yes, you most definitely can use directional lights to cast shadows. You need to make sure you are not using MeshBasicMaterial as they don't support shadows. Use MeshLambertMaterial or MeshPhongMaterial instead.

你需要使用以下内容为渲染器启用阴影:

You need to enable shadows for the renderer with something along these lines:

renderer.shadowMapEnabled = 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 then you must enable shadow casting and shadow receiving per object and per light so you would have

dirLight.castShadow = true;
object.castShadow = true;
otherObject.receiveShadow = true;

然后,如果灯光和物体放置在适当的位置。 dirLight 将导致 object 的阴影投射到 otherObject

Then, if the light and objects are placed at appropriate positions. dirLight will cause the shadow of object to be cast against otherObject.

这是 工作演示

: Here is a working demo for anyone looking to do something similar.

这篇关于如何在Three.JS中创建方向性光影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆