1 Star 0 Fork 0

mmc/threeJs-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
光照阴影计算.html 3.34 KB
一键复制 编辑 原始数据 按行查看 历史
mmc 提交于 2022-06-30 10:52 . 提交使用three.js写的一些小案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script src="./three.js/three.js"></script>
<script src="./three.js/OrbitControls.js"></script>
<script>
var scene = new THREE.Scene();
var geometry = new THREE.BoxGeometry(40, 100, 40);
var material = new THREE.MeshLambertMaterial({
color: 0x0000ff,
});
var mesh = new THREE.Mesh(geometry, material);
// mesh.position.set(0, 0, 0);
scene.add(mesh);
// 设置产生投影的网格模型
mesh.castShadow = true;
//创建一个平面几何体作为投影面
var planeGeometry = new THREE.PlaneGeometry(500, 400);
var planeMaterial = new THREE.MeshLambertMaterial({
color: 0x999999,
});
// 平面网格模型作为投影面
var planeMesh = new THREE.Mesh(planeGeometry, planeMaterial);
scene.add(planeMesh); //网格模型添加到场景中
planeMesh.rotateX(-Math.PI / 2); //旋转网格模型
planeMesh.position.y = -50; //设置网格模型y坐标
// 设置接收阴影的投影面
planeMesh.receiveShadow = true;
// 方向光
var directionalLight = new THREE.DirectionalLight(0xffffff, 1);
// 设置光源位置
directionalLight.position.set(60, 100, 40);
directionalLight.castShadow = true;
scene.add(directionalLight);
// 设置用于计算阴影的光源对象
// 设置计算阴影的区域,最好刚好紧密包围在对象周围
// 计算阴影的区域过大:模糊 过小:看不到或显示不完整
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 300;
directionalLight.shadow.camera.left = -50;
directionalLight.shadow.camera.right = 50;
directionalLight.shadow.camera.top = 200;
directionalLight.shadow.camera.bottom = -100;
// 设置mapSize属性可以使阴影更清晰,不那么模糊
// directionalLight.shadow.mapSize.set(1024, 1024);
console.log(directionalLight.shadow.camera);
var ambientLight = new THREE.AmbientLight("#333");
scene.add(ambientLight);
// 4,创建正交相机
var width = window.innerWidth;
var height = window.innerHeight;
var k = width / height;
var s = 200;
// var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
var camera = new THREE.PerspectiveCamera(45, width / height, 1, 2000);
camera.position.set(400, 200, 300);
camera.lookAt(scene.position);
// 添加坐标轴
var axis_helper = new THREE.AxesHelper(500);
scene.add(axis_helper);
// 5,创建渲染器对象
var renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
function render() {
renderer.render(scene, camera);
}
render();
// 添加鼠标控制对象
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener("change", render);
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mimanchi680/three-js-demo.git
[email protected]:mimanchi680/three-js-demo.git
mimanchi680
three-js-demo
threeJs-demo
master

搜索帮助