gpt4 book ai didi

Javascript Three.JS 阴影贴图不工作

转载 作者:行者123 更新时间:2023-12-03 11:49:22 24 4
gpt4 key购买 nike

我对 Three.js 还算陌生,我很喜欢它,尽管我在阴影贴图方面遇到了问题,我已经阅读并到处查看,但我就是无法让它工作,有人可以指出我做错了什么吗?

    var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();

renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

var geometry = new THREE.BoxGeometry(1,1,1);
var cube = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( {color: 0x808080 } ));
cube.castShadow = true;
scene.add( cube );

camera.position.y = 2;

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

var geo2 = new THREE.BoxGeometry(5,0.1,5);

var floor = new THREE.Mesh(geo2, new THREE.MeshLambertMaterial( {color: 0x808080 } ));
floor.position.y = -1;
floor.receiveShadow = true;
scene.add(floor);

renderer.shadowMapEnabled = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;


var ambient = new THREE.AmbientLight( 0x111111 );
directionalLight.shadowCameraVisible = true;
scene.add( ambient );


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

function animate() {
camera.lookAt({x:0,y:0,z:0});
var timer = Date.now() * 0.0002;
camera.position.x = Math.cos(timer) * 5;
camera.position.z = Math.sin(timer) * 5;
requestAnimationFrame(animate);
render();
}

function render() {
cube.rotation.x += 0.1;
cube.rotation.y += 0.1;

renderer.render(scene, camera);
}
animate();

最佳答案

这三行:

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

应该在你的灯光上而不是渲染器上;所以

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

由于您的光线是定向的,您将需要:

directionalLight.shadowCameraLeft = -500;
directionalLight.shadowCameraRight = 500;
directionalLight.shadowCameraTop = 500;
directionalLight.shadowCameraBottom = -500;

您可能需要尝试使用这些值。

关于Javascript Three.JS 阴影贴图不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com