简体   繁体   English

ThreeJS 没有投射阴影

[英]ThreeJS is not casting shadows

I am building a basic Three JS application.我正在构建一个基本的三个 JS 应用程序。 Basically, I am learning Three JS and so that I am experimenting with it.基本上,我正在学习三个 JS,所以我正在尝试它。 Now, I am building a scene where there is a floor, an object and a light.现在,我正在构建一个有地板、一个 object 和一盏灯的场景。 I want to see the shadow of the object on the floor due to light.我想看看object在地板上由于光线的阴影。 This is my code.这是我的代码。

import * as THREE from 'three';
const scene = new THREE.Scene();
const aspect_ratio = window.innerWidth / window.innerHeight;
const camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 500;
scene.add(camera);

const renderer = new THREE.WebGLRenderer();
renderer.shadowMapEnabled = true;
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.append(renderer.domElement);

var shape = new THREE.TorusGeometry(100, 50, 8, 20);
var cover = new THREE.MeshPhongMaterial();
cover.emissive.setRGB(0.8, 0.1, 0.1);
cover.specular.setRGB(0.9, 0.9, 0.9);
var donut = new THREE.Mesh(shape, cover); scene.add(donut);
donut.castShadow = true;

var sunlight = new THREE.DirectionalLight();
sunlight.intensity = 0.5;
sunlight.position.set(100, 100, 100);
scene.add(sunlight);
sunlight.castShadow = true;

var shape = new THREE.PlaneGeometry(1500, 1500);
var cover = new THREE.MeshBasicMaterial();
var ground = new THREE.Mesh(shape, cover);
scene.add(ground);
ground.position.set(0, -180, 0);
ground.rotation.set(-Math.PI/2, 0, 0);
ground.receiveShadow = true;

function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
animate();

I am supposed to see the shadow of the donut on the floor.我应该看到地板上甜甜圈的影子。 But this is what I see instead without shadow.但这是我看到的,没有阴影。

在此处输入图像描述

What is wrong with my code and how can I fix it?我的代码有什么问题,我该如何解决?

You have to make the ground to receive shadow你必须让地面接收阴影

to do this:去做这个:

ground.receiveShadow = true;

There are multiple issues in your code.您的代码中有多个问题。 The most important problem is that you do not configure your shadow frustum correctly.最重要的问题是您没有正确配置阴影截头体。 Besides, MeshBasicMaterial is not affected by light and thus can't receive shadows.此外, MeshBasicMaterial不受光线影响,因此无法接收阴影。 Try it like so:像这样尝试:

 const scene = new THREE.Scene(); const aspect_ratio = window.innerWidth / window.innerHeight; const camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000); camera.position.z = 500; scene.add(camera); const renderer = new THREE.WebGLRenderer(); renderer.shadowMap.enabled = true; renderer.setSize(window.innerWidth, window.innerHeight); document.body.append(renderer.domElement); const donutGeometry = new THREE.TorusGeometry(100, 50, 8, 20); const donutMaterial = new THREE.MeshPhongMaterial(); donutMaterial.emissive.setRGB(0.8, 0.1, 0.1); donutMaterial.specular.setRGB(0.9, 0.9, 0.9); const donut = new THREE.Mesh(donutGeometry, donutMaterial); scene.add(donut); donut.castShadow = true; var sunlight = new THREE.DirectionalLight(); sunlight.intensity = 0.5; sunlight.position.set(100, 100, 100); scene.add(sunlight); sunlight.castShadow = true; sunlight.shadow.camera.top = 200; sunlight.shadow.camera.bottom = - 200; sunlight.shadow.camera.left = - 200; sunlight.shadow.camera.right = 200; sunlight.shadow.camera.near = 1; sunlight.shadow.camera.far = 1000; //scene.add( new THREE.CameraHelper( sunlight.shadow.camera ) ); const groundGeometry = new THREE.PlaneGeometry(1500, 1500); const groundMaterial = new THREE.MeshPhongMaterial(); const ground = new THREE.Mesh(groundGeometry, groundMaterial); scene.add(ground); ground.position.set(0, -180, 0); ground.rotation.set(-Math.PI/2, 0, 0); ground.receiveShadow = true; function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate();
 body { margin: 0; }
 <script src="https://cdn.jsdelivr.net/npm/three@0.144/build/three.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM