gpt4 book ai didi

three.js - ThreeJS阴影无法渲染

转载 作者:行者123 更新时间:2023-12-04 05:39:45 24 4
gpt4 key购买 nike

我浏览了其他一些S.O.问题,遵循所有建议,但对于为何无法在这个非常基本的场景上渲染阴影,我仍然一无所知。

http://jsfiddle.net/4Txgp/

[更新]代码:

var SCREEN_WIDTH = window.innerWidth - 25;
var SCREEN_HEIGHT = window.innerHeight - 25;

var camera, scene;
var canvasRenderer, webglRenderer;

var container, mesh, geometry, plane;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();

function init() {

container = document.createElement('div');
document.body.appendChild(container);

camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.x = 1200;
camera.position.y = 1000;
camera.lookAt({
x: 0,
y: 0,
z: 0
});

scene = new THREE.Scene();

var groundMaterial = new THREE.MeshLambertMaterial({
color: 0x6C6C6C
});
plane = new THREE.Mesh(new THREE.PlaneGeometry(10000, 10000, 100, 100), groundMaterial);
plane.rotation.x = -Math.PI / 2;
plane.receiveShadow = true;

scene.add(plane);

// LIGHTS
// scene.add(new THREE.AmbientLight(0x666666));

/*
var light;

light = new THREE.DirectionalLight(0xdfebff, 1.75);
light.position.set(600, 800, 100);
//light.position.multiplyScalar(1.3);

light.castShadow = true;
light.shadowCameraVisible = true;

light.shadowMapWidth = light.shadowMapHeight = 2048;

var d = 50;

light.shadowCameraLeft = -d;
light.shadowCameraRight = d;
light.shadowCameraTop = d;
light.shadowCameraBottom = -d;

light.shadowCameraFar = 500;
light.shadowDarkness = 0.5;

scene.add(light);
*/

var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set( 700, 1000, 100 );

spotLight.castShadow = true;
spotLight.shadowCameraVisible = true;

spotLight.shadowMapWidth = 2048;
spotLight.shadowMapHeight = 2048;

spotLight.shadowCameraNear = 100;
spotLight.shadowCameraFar = 2000;
spotLight.shadowCameraFov = 30;

scene.add( spotLight );

var boxgeometry = new THREE.CubeGeometry(100, 200, 100);
var boxmaterial = new THREE.MeshLambertMaterial({
color: 0x0aeedf
});
var cube = new THREE.Mesh(boxgeometry, boxmaterial);

cube.position.x = 0;
cube.position.y = 100;
cube.position.z = 0;

scene.add(cube);

// RENDERER
webglRenderer = new THREE.WebGLRenderer();
webglRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
//webglRenderer.domElement.style.position = "relative";
webglRenderer.shadowMapEnabled = true;
webglRenderer.shadowMapSoft = true;

container.appendChild(webglRenderer.domElement);
}

function animate() {
var timer = Date.now() * 0.0002;
camera.position.x = Math.cos(timer) * 1000;
camera.position.z = Math.sin(timer) * 1000;
requestAnimationFrame(animate);
render();
}

function render() {
camera.lookAt(scene.position);
webglRenderer.render(scene, camera);
}

我有一个场景,其中包括飞机,对象(立方体),聚光灯(出于测试目的直接从 http://threejs.org/docs/58/#Reference/Lights/SpotLight复制)和照相机。除了立方体没有在“地面”(平面)上转换阴影,而且阴影看起来一切都已在基本 Material 中完成之外,它的渲染效果很好。我正在使用Phongs和Lamberts的组合。

我的定向光设置为castShadow = true ;,我的平面设置为receiveShadow = true,同时设置了阴影贴图。渲染器本身具有shadowMapEnabled = true。

我已经尝试了各种解决方案,我记得以前的ThreeJS版本会根据您想做什么进行外部库调用,但是我也看到了JSFiddle上的其他示例,它们只是单独调用ThreeJS,还有来自官方网站,表示工作正常。

关于忽略简单和小的事物的任何提示/信息/ build 性言论吗?

最佳答案

你需要设置

cube.castShadow = true;

并确保阴影摄像机远平面到达立方体。

fiddle : http://jsfiddle.net/4Txgp/13/

three.js r.58

关于three.js - ThreeJS阴影无法渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17536903/

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