Cesium-Examples/examples/threeEx/1.1.43、模型运动阴影.html
2025-03-11 17:51:04 +08:00

141 lines
5.0 KiB
HTML

<!--********************************************************************
* by jiawanlong
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
* {
margin: 0;
padding: 0;
overflow: hidden;
}
body,
#box {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="importmap">
{
"imports": {
"three": "./../../three/build/three.module.js",
"three/addons/": "./../../three/examples/jsm/"
}
}
</script>
<script src="./tween.umd.js"></script>
<script type="module">
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'
const box = document.getElementById('box')
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(); //相机
camera.position.set(100, 200, 200); //相机位置
camera.lookAt(150, 0, 10); //相机位置
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })
scene.add(new THREE.AmbientLight(0xffffff, 1))
scene.add(new THREE.AxesHelper(1000))
// --------------------renderer启用阴影--------------------
renderer.shadowMap.enabled = true; // 启用阴影映射
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 使用软阴影
// --------------------renderer启用阴影--------------------
// --------------------平行光开始--------------------
// 创建一个平行光
const directionalLight = new THREE.DirectionalLight(0xffffff, 5);
directionalLight.position.set(500, 300, 500).normalize(); // 设置平行光的方向
directionalLight.castShadow = true; // 启用平行光的阴影
// 配置平行光的阴影贴图
directionalLight.shadow.mapSize.width = 1024; // 阴影贴图的宽度
directionalLight.shadow.mapSize.height = 1024; // 阴影贴图的高度
directionalLight.shadow.camera.near = 0.5; // 阴影摄像机的近裁剪面
directionalLight.shadow.camera.far = 5000; // 阴影摄像机的远裁剪面
directionalLight.shadow.camera.left = -500;
directionalLight.shadow.camera.right = 500;
directionalLight.shadow.camera.top = 500;
directionalLight.shadow.camera.bottom = -500;
directionalLight.position.set(200 * 2, 500 * 2, 200 * 2);
// 将平行光添加到场景中
scene.add(directionalLight);
// --------------------平行光结束--------------------
// --------------------地面开始--------------------
const planeGeometry = new THREE.PlaneGeometry(500, 500);
const planeMaterial = new THREE.MeshStandardMaterial({ color: 0x808080, side: THREE.DoubleSide });
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true; // 启用接收阴影
plane.rotation.x = -Math.PI / 2; // 使平面水平
scene.add(plane);
// --------------------地面结束--------------------
// 加载模型 gltf/ glb
const loader = new GLTFLoader()
loader.load('./zhanji.glb', (gltf) => {
scene.add(gltf.scene)
gltf.scene.scale.set(0.005, 0.005, 0.005)
//绕x轴旋转π/4
gltf.scene.rotateY(Math.PI / 4);
//创建一段平移的动画
const tween = new TWEEN.Tween(gltf.scene.position);
tween.to({ x: 100, y: 50, z: 100 }, 5000).onComplete(function (obj) {
// 跳转方向,连续动画
gltf.scene.rotateY(Math.PI/1.5);
const tween3 = new TWEEN.Tween(gltf.scene.position);
tween3.to({ x: 100, y: 50, z: 0 }, 5000).start()
});
//创建一段缩放的动画
const tween2 = new TWEEN.Tween(gltf.scene.scale);
tween2.to({ x: 0.01, y: 0.01, z: 0.01 }, 10000);
tween.start();
tween2.start();
// ---------确保模型的每个网格都启用投射阴影-----------
gltf.scene.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.castShadow = true; // 启用投射阴影
}
});
})
renderer.setSize(box.clientWidth, box.clientHeight)
box.appendChild(renderer.domElement)
new OrbitControls(camera, renderer.domElement)
animate()
function animate() {
TWEEN.update();
requestAnimationFrame(animate)
renderer.render(scene, camera)
}
</script>
</body>
</html>