THREEJS案例(11)- 光照阴影
Posted 开源编程案例
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了THREEJS案例(11)- 光照阴影相关的知识,希望对你有一定的参考价值。
renderer.shadowMap.enabled = true;
var light = new THREE.DirectionalLight(0xdfebff, 1.75);
light.position.set(2, 8, 4);
// 开启阴影
light.castShadow = true;
// 阴影属性
light.shadow.mapSize.width = 1024; //定义阴影贴图的宽度
light.shadow.mapSize.height = 1024; //定义阴影贴图的高度
light.shadow.camera.near = 0.5; //产生阴影的近距离
light.shadow.camera.far = 20; //产生阴影的远距离
scene.add(light);
var groundMaterial = new THREE.MeshPhongMaterial({ color: 0x404040, specular: 0x111111 });
var mesh = new THREE.Mesh(new THREE.SphereBufferGeometry(1, 4, 1), groundMaterial);
mesh.position.y = 2;
mesh.castShadow = true; // 开启阴影
mesh.receiveShadow = true; // 是否接收阴影
scene.add(mesh);
var mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(20000, 20000), groundMaterial);
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true; // 接收阴影
scene.add(mesh);
function init() {
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.y = 5;
camera.position.z = 15;
scene.add(camera);
var light = new THREE.DirectionalLight(0xdfebff, 1.75);
light.position.set(2, 8, 4);
// 开启阴影
light.castShadow = true;
// 阴影属性
light.shadow.mapSize.width = 1024; //定义阴影贴图的宽度
light.shadow.mapSize.height = 1024; //定义阴影贴图的高度
light.shadow.camera.near = 0.5; //产生阴影的近距离
light.shadow.camera.far = 20; //产生阴影的远距离
scene.add(light);
var groundMaterial = new THREE.MeshPhongMaterial({ color: 0x404040, specular: 0x111111 });
var mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(20000, 20000), groundMaterial);
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true; // 接收阴影
scene.add(mesh);
var mesh = new THREE.Mesh(new THREE.SphereBufferGeometry(1, 4, 1), groundMaterial);
mesh.position.y = 2;
mesh.castShadow = true; // 开启阴影
mesh.receiveShadow = true; // 是否接收阴影
scene.add(mesh);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
renderer.shadowMap.enabled = true;
}
以上是关于THREEJS案例(11)- 光照阴影的主要内容,如果未能解决你的问题,请参考以下文章