Threejs中使用A*算法寻路导航

news2024/12/24 11:26:10

 

<!DOCTYPE html>
<html>
	<head>
		<title>Threejs中使用A*算法寻路导航,Threejs室内室外地图导航</title>
		<script type="text/javascript" src="libs/three.js"></script>
		<script type="text/javascript" src="libs/OrbitControls.js"></script>
		<script type="text/javascript" charset="UTF-8" src="libs/other/Tween.min.js"></script>
		<script type="text/javascript" charset="UTF-8" src="libs/three/loaders/GLTFLoader.js"></script>
 
		<script type="text/javascript" src="libs/OBJLoader.js"></script>
		<script type="text/javascript" src="libs/MTLLoader.js"></script>
 
		<script type="text/javascript" src="libs/util.js"></script>
		<script type="text/javascript" src="libs/util/dat.gui.js"></script>
		<script type="text/javascript" src="libs/astar.js"></script>
		<style>
			body {
				margin: 0;
				overflow: hidden;
			}
		</style>
	</head>
	<body>
		<div id="webgl-output"></div>
		<script type="text/javascript">
			var camera;
			var renderer;
			var clock = new THREE.Clock();
			var mixer = new THREE.AnimationMixer();
			var clipAction;
			var animationClip;
			var length = 36;
			var ws = 2;
			var graph = []; // 记录地图
			var meshArr = new Array(); // 记录障碍物对象数据
			var meshxy = [{
					"x": 35,
					"y": 7
				},
				{
					"x": 35,
					"y": 7
				},
				{
					"x": 35,
					"y": 9
				},
				{
					"x": 35,
					"y": 11
				},
				{
					"x": 29,
					"y": 11
				},
				{
					"x": 29,
					"y": 13
				},
				{
					"x": 33,
					"y": 13
				},
				{
					"x": 33,
					"y": 11
				},
				{
					"x": 33,
					"y": 9
				},
				{
					"x": 33,
					"y": 5
				},
				{
					"x": 33,
					"y": 7
				},
				{
					"x": 29,
					"y": 5
				},
				{
					"x": 31,
					"y": 5
				},
				{
					"x": 31,
					"y": 11
				},
				{
					"x": 27,
					"y": 5
				},
				{
					"x": 25,
					"y": 5
				},
				{
					"x": 25,
					"y": 7
				},
				{
					"x": 25,
					"y": 9
				},
				{
					"x": 25,
					"y": 11
				},
				{
					"x": 25,
					"y": 13
				},
				{
					"x": 23,
					"y": 9
				},
				{
					"x": 21,
					"y": 9
				},
				{
					"x": 19,
					"y": 9
				},
				{
					"x": 19,
					"y": 11
				},
				{
					"x": 19,
					"y": 13
				},
				{
					"x": 15,
					"y": 9
				},
				{
					"x": 13,
					"y": 7
				},
				{
					"x": 13,
					"y": 9
				},
				{
					"x": 13,
					"y": 11
				},
				{
					"x": 11,
					"y": 7
				},
				{
					"x": 13,
					"y": 5
				},
				{
					"x": 11,
					"y": 5
				},
				{
					"x": 7,
					"y": 5
				},
				{
					"x": 5,
					"y": 5
				},
				{
					"x": 5,
					"y": 7
				},
				{
					"x": 7,
					"y": 7
				},
				{
					"x": 7,
					"y": 9
				},
				{
					"x": 7,
					"y": 11
				},
				{
					"x": 5,
					"y": 11
				},
				{
					"x": 5,
					"y": 9
				},
				{
					"x": 7,
					"y": 15
				},
				{
					"x": 7,
					"y": 17
				},
				{
					"x": 7,
					"y": 19
				},
				{
					"x": 11,
					"y": 23
				},
				{
					"x": 11,
					"y": 17
				},
				{
					"x": 11,
					"y": 21
				},
				{
					"x": 11,
					"y": 19
				},
				{
					"x": 17,
					"y": 25
				},
				{
					"x": 17,
					"y": 27
				},
				{
					"x": 17,
					"y": 29
				},
				{
					"x": 19,
					"y": 25
				},
				{
					"x": 21,
					"y": 25
				},
				{
					"x": 17,
					"y": 21
				},
				{
					"x": 17,
					"y": 23
				},
				{
					"x": 17,
					"y": 19
				},
				{
					"x": 15,
					"y": 19
				},
				{
					"x": 13,
					"y": 19
				},
				{
					"x": 7,
					"y": 29
				},
				{
					"x": 7,
					"y": 31
				},
				{
					"x": 7,
					"y": 33
				},
				{
					"x": 9,
					"y": 33
				},
				{
					"x": 13,
					"y": 33
				},
				{
					"x": 11,
					"y": 33
				},
				{
					"x": 13,
					"y": 31
				},
				{
					"x": 13,
					"y": 29
				},
				{
					"x": 15,
					"y": 33
				},
				{
					"x": 17,
					"y": 33
				},
				{
					"x": 19,
					"y": 33
				},
				{
					"x": 21,
					"y": 33
				},
				{
					"x": 25,
					"y": 29
				},
				{
					"x": 25,
					"y": 31
				},
				{
					"x": 25,
					"y": 33
				},
				{
					"x": 27,
					"y": 29
				},
				{
					"x": 29,
					"y": 29
				},
				{
					"x": 29,
					"y": 23
				},
				{
					"x": 27,
					"y": 23
				},
				{
					"x": 25,
					"y": 23
				},
				{
					"x": 29,
					"y": 21
				},
				{
					"x": 29,
					"y": 19
				},
				{
					"x": 33,
					"y": 19
				},
				{
					"x": 33,
					"y": 21
				},
				{
					"x": 33,
					"y": 23
				},
				{
					"x": 33,
					"y": 25
				},
				{
					"x": 33,
					"y": 27
				},
				{
					"x": 33,
					"y": 29
				},
				{
					"x": 33,
					"y": 31
				},
				{
					"x": 33,
					"y": 33
				},
				{
					"x": 31,
					"y": 35
				},
				{
					"x": 29,
					"y": 35
				},
				{
					"x": 27,
					"y": 35
				},
				{
					"x": 25,
					"y": 35
				},
				{
					"x": 7,
					"y": 23
				},
				{
					"x": 11,
					"y": 15
				},
				{
					"x": 9,
					"y": 15
				},
				{
					"x": 35,
					"y": 9
				},
				{
					"x": 35,
					"y": 11
				},
				{
					"x": 29,
					"y": 11
				},
				{
					"x": 29,
					"y": 13
				},
				{
					"x": 33,
					"y": 13
				},
				{
					"x": 33,
					"y": 11
				},
				{
					"x": 33,
					"y": 9
				},
				{
					"x": 33,
					"y": 5
				},
				{
					"x": 33,
					"y": 7
				},
				{
					"x": 29,
					"y": 5
				},
				{
					"x": 31,
					"y": 5
				},
				{
					"x": 31,
					"y": 11
				},
				{
					"x": 27,
					"y": 5
				},
				{
					"x": 25,
					"y": 5
				},
				{
					"x": 25,
					"y": 7
				},
				{
					"x": 25,
					"y": 9
				},
				{
					"x": 25,
					"y": 11
				},
				{
					"x": 25,
					"y": 13
				},
				{
					"x": 23,
					"y": 9
				},
				{
					"x": 21,
					"y": 9
				},
				{
					"x": 19,
					"y": 9
				},
				{
					"x": 19,
					"y": 11
				},
				{
					"x": 19,
					"y": 13
				},
				{
					"x": 15,
					"y": 9
				},
				{
					"x": 13,
					"y": 7
				},
				{
					"x": 13,
					"y": 9
				},
				{
					"x": 13,
					"y": 11
				},
				{
					"x": 11,
					"y": 7
				},
				{
					"x": 13,
					"y": 5
				},
				{
					"x": 11,
					"y": 5
				},
				{
					"x": 7,
					"y": 5
				},
				{
					"x": 5,
					"y": 5
				},
				{
					"x": 5,
					"y": 7
				},
				{
					"x": 7,
					"y": 7
				},
				{
					"x": 7,
					"y": 9
				},
				{
					"x": 7,
					"y": 11
				},
				{
					"x": 5,
					"y": 11
				},
				{
					"x": 5,
					"y": 9
				},
				{
					"x": 7,
					"y": 15
				},
				{
					"x": 7,
					"y": 17
				},
				{
					"x": 7,
					"y": 19
				},
				{
					"x": 11,
					"y": 23
				},
				{
					"x": 11,
					"y": 17
				},
				{
					"x": 11,
					"y": 21
				},
				{
					"x": 11,
					"y": 19
				},
				{
					"x": 17,
					"y": 25
				},
				{
					"x": 17,
					"y": 27
				},
				{
					"x": 17,
					"y": 29
				},
				{
					"x": 19,
					"y": 25
				},
				{
					"x": 21,
					"y": 25
				},
				{
					"x": 17,
					"y": 21
				},
				{
					"x": 17,
					"y": 23
				},
				{
					"x": 17,
					"y": 19
				},
				{
					"x": 15,
					"y": 19
				},
				{
					"x": 13,
					"y": 19
				},
				{
					"x": 7,
					"y": 29
				},
				{
					"x": 7,
					"y": 31
				},
				{
					"x": 7,
					"y": 33
				},
				{
					"x": 9,
					"y": 33
				},
				{
					"x": 13,
					"y": 33
				},
				{
					"x": 11,
					"y": 33
				},
				{
					"x": 13,
					"y": 31
				},
				{
					"x": 13,
					"y": 29
				},
				{
					"x": 15,
					"y": 33
				},
				{
					"x": 17,
					"y": 33
				},
				{
					"x": 19,
					"y": 33
				},
				{
					"x": 21,
					"y": 33
				},
				{
					"x": 25,
					"y": 29
				},
				{
					"x": 25,
					"y": 31
				},
				{
					"x": 25,
					"y": 33
				},
				{
					"x": 27,
					"y": 29
				},
				{
					"x": 29,
					"y": 29
				},
				{
					"x": 29,
					"y": 23
				},
				{
					"x": 27,
					"y": 23
				},
				{
					"x": 25,
					"y": 23
				},
				{
					"x": 29,
					"y": 21
				},
				{
					"x": 29,
					"y": 19
				},
				{
					"x": 33,
					"y": 19
				},
				{
					"x": 33,
					"y": 21
				},
				{
					"x": 33,
					"y": 23
				},
				{
					"x": 33,
					"y": 25
				},
				{
					"x": 33,
					"y": 27
				},
				{
					"x": 33,
					"y": 29
				},
				{
					"x": 33,
					"y": 31
				},
				{
					"x": 33,
					"y": 33
				},
				{
					"x": 31,
					"y": 35
				},
				{
					"x": 29,
					"y": 35
				},
				{
					"x": 27,
					"y": 35
				},
				{
					"x": 25,
					"y": 35
				},
				{
					"x": 7,
					"y": 23
				},
				{
					"x": 11,
					"y": 15
				},
				{
					"x": 9,
					"y": 15
				},
				{
					"x": 35,
					"y": 9
				},
				{
					"x": 35,
					"y": 11
				},
				{
					"x": 29,
					"y": 11
				},
				{
					"x": 29,
					"y": 13
				},
				{
					"x": 33,
					"y": 13
				},
				{
					"x": 33,
					"y": 11
				},
				{
					"x": 33,
					"y": 9
				},
				{
					"x": 33,
					"y": 5
				},
				{
					"x": 33,
					"y": 7
				},
				{
					"x": 29,
					"y": 5
				},
				{
					"x": 31,
					"y": 5
				},
				{
					"x": 31,
					"y": 11
				},
				{
					"x": 27,
					"y": 5
				},
				{
					"x": 25,
					"y": 5
				},
				{
					"x": 25,
					"y": 7
				},
				{
					"x": 25,
					"y": 9
				},
				{
					"x": 25,
					"y": 11
				},
				{
					"x": 25,
					"y": 13
				},
				{
					"x": 23,
					"y": 9
				},
				{
					"x": 21,
					"y": 9
				},
				{
					"x": 19,
					"y": 9
				},
				{
					"x": 19,
					"y": 11
				},
				{
					"x": 19,
					"y": 13
				},
				{
					"x": 15,
					"y": 9
				},
				{
					"x": 13,
					"y": 7
				},
				{
					"x": 13,
					"y": 9
				},
				{
					"x": 13,
					"y": 11
				},
				{
					"x": 11,
					"y": 7
				},
				{
					"x": 13,
					"y": 5
				},
				{
					"x": 11,
					"y": 5
				},
				{
					"x": 7,
					"y": 5
				},
				{
					"x": 5,
					"y": 5
				},
				{
					"x": 5,
					"y": 7
				},
				{
					"x": 7,
					"y": 7
				},
				{
					"x": 7,
					"y": 9
				},
				{
					"x": 7,
					"y": 11
				},
				{
					"x": 5,
					"y": 11
				},
				{
					"x": 5,
					"y": 9
				},
				{
					"x": 7,
					"y": 15
				},
				{
					"x": 7,
					"y": 17
				},
				{
					"x": 7,
					"y": 19
				},
				{
					"x": 11,
					"y": 23
				},
				{
					"x": 11,
					"y": 17
				},
				{
					"x": 11,
					"y": 21
				},
				{
					"x": 11,
					"y": 19
				},
				{
					"x": 17,
					"y": 25
				},
				{
					"x": 17,
					"y": 27
				},
				{
					"x": 17,
					"y": 29
				},
				{
					"x": 19,
					"y": 25
				},
				{
					"x": 21,
					"y": 25
				},
				{
					"x": 17,
					"y": 21
				},
				{
					"x": 17,
					"y": 23
				},
				{
					"x": 17,
					"y": 19
				},
				{
					"x": 15,
					"y": 19
				},
				{
					"x": 13,
					"y": 19
				},
				{
					"x": 7,
					"y": 29
				},
				{
					"x": 7,
					"y": 31
				},
				{
					"x": 7,
					"y": 33
				},
				{
					"x": 9,
					"y": 33
				},
				{
					"x": 13,
					"y": 33
				},
				{
					"x": 11,
					"y": 33
				},
				{
					"x": 13,
					"y": 31
				},
				{
					"x": 13,
					"y": 29
				},
				{
					"x": 15,
					"y": 33
				},
				{
					"x": 17,
					"y": 33
				},
				{
					"x": 19,
					"y": 33
				},
				{
					"x": 21,
					"y": 33
				},
				{
					"x": 25,
					"y": 29
				},
				{
					"x": 25,
					"y": 31
				},
				{
					"x": 25,
					"y": 33
				},
				{
					"x": 27,
					"y": 29
				},
				{
					"x": 29,
					"y": 29
				},
				{
					"x": 29,
					"y": 23
				},
				{
					"x": 27,
					"y": 23
				},
				{
					"x": 25,
					"y": 23
				},
				{
					"x": 29,
					"y": 21
				},
				{
					"x": 29,
					"y": 19
				},
				{
					"x": 33,
					"y": 19
				},
				{
					"x": 33,
					"y": 21
				},
				{
					"x": 33,
					"y": 23
				},
				{
					"x": 33,
					"y": 25
				},
				{
					"x": 33,
					"y": 27
				},
				{
					"x": 33,
					"y": 29
				},
				{
					"x": 33,
					"y": 31
				},
				{
					"x": 33,
					"y": 33
				},
				{
					"x": 31,
					"y": 35
				},
				{
					"x": 29,
					"y": 35
				},
				{
					"x": 27,
					"y": 35
				},
				{
					"x": 25,
					"y": 35
				},
				{
					"x": 7,
					"y": 23
				},
				{
					"x": 11,
					"y": 15
				},
				{
					"x": 9,
					"y": 15
				},
				{
					"x": 19,
					"y": 21
				},
				{
					"x": 21,
					"y": 21
				},
				{
					"x": 21,
					"y": 19
				},
				{
					"x": 29,
					"y": 27
				},
				{
					"x": 29,
					"y": 25
				}
			]; // 记录障碍坐标物数据
			var lineArr = new Array(); // 记录路网数据
			var meshbool = false; // 显示障碍物
			var linebool = false; // 显示路网
			var resultArray = new Array();
			var isCaculate = false;
 
			function init() {
				// 创建一个场景,它将包含我们所有的元素,如物体,相机和灯光。
				var scene = new THREE.Scene();
 
				var urls = [
					'assets/textures/cubemap/flowers/posx.jpg',
					'assets/textures/cubemap/flowers/negx.jpg',
					'assets/textures/cubemap/flowers/posy.jpg',
					'assets/textures/cubemap/flowers/negy.jpg',
					'assets/textures/cubemap/flowers/posz.jpg',
					'assets/textures/cubemap/flowers/negz.jpg'
				];
 
				var cubeLoader = new THREE.CubeTextureLoader();
				scene.background = cubeLoader.load(urls);
 
				// 创建一个摄像机,它定义了我们正在看的地方
				camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 1000);
				// 将摄像机对准场景的中心
				camera.position.x = 60;
				camera.position.y = 35;
				camera.position.z = 60;
				camera.lookAt({
					x: 0,
					y: 5,
					z: -20
				});
				var orbit = new THREE.OrbitControls(camera);
				orbit.target = camera.position;
				orbit.update();
 
				// 创建一个渲染器并设置大小,WebGLRenderer将会使用电脑显卡来渲染场景
				// initialize basic renderer
				renderer = initRenderer({
					antialias: true,
					logarithmicDepthBuffer: true,
				});
 
				// 将平面添加到场景中
				var plane = createPlaneGeometryBasicMaterial();
				scene.add(plane);
 
				// 在屏幕上显示坐标轴
				var axes = new THREE.AxesHelper(100);
				scene.add(axes);
 
				scene.add(new THREE.AmbientLight(0x666666));
				scene.add(new THREE.AmbientLight("#ffffff", 1));
 
				initModel();
				// initPeople();
 
				initGround();
				initGrid();
 
				var i = 0;
 
				function tweenComplete() {
					if (i < points.length) {
						switch (i) {
							case 0:
								aaa.rotateY(Math.PI);
								break;
							case 1:
							case 5:
							case 8:
							case 9:
								aaa.rotateY(-0.5 * Math.PI);
								break;
							case 2:
							case 3:
							case 4:
							case 6:
							case 7:
								aaa.rotateY(0.5 * Math.PI);
								break;
							case 10:
								mixer.stopAllAction();
								break;
 
						}
						tween = new TWEEN.Tween(points[i])
							.to(points[i + 1], 3000)
							.easing(TWEEN.Easing.Linear.None)
							.onUpdate(function() {
								aaa.position.set(this.x, this.y, this.z);
							})
							.onComplete(tweenComplete)
							.start();
						i++;
					}
				}
 
 
				// 使用GUI调试库
				var controls = new function() {
					this.clickBool = false;
 
					this.displayRoadGrid = function() {
						for (var i = 0; i < lineArr.length; i++) {
							if (linebool) {
								// 显示路网
								scene.add(lineArr[i].obj);
							} else {
								// 隐藏路网
								var obj = scene.getObjectByName(lineArr[i].name);
								scene.remove(obj);
							}
						}
						linebool = !linebool
					}
 
					this.displayObstacles = function() {
						for (var i = 0; i < meshArr.length; i++) {
							if (meshbool) {
								// 显示障碍物
								scene.add(meshArr[i].obj);
							} else {
								// 隐藏障碍物
								var obj = scene.getObjectByName(meshArr[i].name);
								scene.remove(obj);
							}
						}
						meshbool = !meshbool
					}
 
					this.outputObstacles = function() {
						for (var i = 0; i < meshArr.length; i++) {
							meshxy.push({
								x: meshArr[i].x,
								y: meshArr[i].y
							})
						}
						console.log(meshArr)
						console.log(lineArr)
						console.log(meshxy)
						localStorage.setItem('meshArr', JSON.stringify(meshArr));
						localStorage.setItem('lineArr', JSON.stringify(lineArr));
						localStorage.setItem('meshxy', JSON.stringify(meshxy));
					}
				}
				var gui = new dat.GUI();
				// 隐藏显示障路网
				gui.add(controls, 'displayRoadGrid');
				// 隐藏显示障碍物
				gui.add(controls, 'displayObstacles');
				// 输出障碍物对象
				gui.add(controls, 'outputObstacles');
				gui.add(controls, "clickBool");
 
				// 启动动画
				renderScene();
 
				// 添加模型
				function initModel() {
					var mtlLoader = new THREE.MTLLoader();
					mtlLoader.setPath("assets/models/obj_mtl/")
					mtlLoader.load('city.mtl', function(materials) {
						materials.preload();
 
						var objLoader = new THREE.OBJLoader();
						objLoader.setMaterials(materials);
						objLoader.load('assets/models/obj_mtl/city.obj', function(object) {
							var mesh = object;
							mesh.scale.set(3, 3, 3);
							mesh.position.set(18, 0, 18);
							scene.add(mesh);
						});
					});
				}
 
				// 添加人物模型
				function initPeople() {
					var loader = new THREE.GLTFLoader();
					loader.load('assets/models/CesiumMan/CesiumMan.gltf', function(result) {
						result.scene.scale.set(1, 1, 1);
						result.scene.translateY(0);
						aaa = result.scene;
						scene.add(result.scene);
 
						tweenComplete();
 
						mixer = new THREE.AnimationMixer(result.scene);
						animationClip = result.animations[0];
						clipAction = mixer.clipAction(animationClip).play();
						animationClip = clipAction.getClip();
					});
				}
 
				// 创建一个地面并设置草坪材质
				function createPlaneGeometryBasicMaterial() {
					var textureLoader = new THREE.TextureLoader();
					var cubeMaterial = new THREE.MeshStandardMaterial({
						map: textureLoader.load("assets/textures/stone/cd.jpg"),
					});
					cubeMaterial.map.wrapS = THREE.RepeatWrapping;
					cubeMaterial.map.wrapT = THREE.RepeatWrapping;
					cubeMaterial.map.repeat.set(18, 18)
					// 创建地平面并设置大小
					var planeGeometry = new THREE.PlaneGeometry(500, 500);
					var plane = new THREE.Mesh(planeGeometry, cubeMaterial);
 
					// 设置平面位置并旋转
					plane.rotation.x = -0.5 * Math.PI;
					plane.position.x = 0;
					plane.position.z = 0;
					return plane;
				}
 
				// 绘制线路
				function initLine(pArr) {
					var points = [];
					var geometry = new THREE.Geometry();
					for (var i = 0; i < pArr.length; i++) {
						var randomX = pArr[i].x;
						var randomY = pArr[i].y;
						var randomZ = pArr[i].z;
						var vector = new THREE.Vector3(randomX, randomY, randomZ);
						geometry.vertices.push(vector);
						points.push(vector);
					}
					var material = new THREE.LineBasicMaterial({
						color: 0x0000FF
					});
					var line = new THREE.Line(geometry, material);
					scene.add(line);
					return points;
				}
 
				// 绘制路网
				function initGround() {
					var geometry = new THREE.Geometry();
					geometry.vertices.push(new THREE.Vector3(0, 0, 0));
					geometry.vertices.push(new THREE.Vector3(length, 0, 0));
 
					for (var i = 0; i <= length / ws; i++) {
						var material = new THREE.LineBasicMaterial({
							color: 0x808080
						});
						var line = new THREE.Line(geometry, material);
						line.position.z = i * ws;
						line.name = "line_" + i;
						lineArr.push({
							name: "line_" + i,
							obj: line
						});
						scene.add(line);
 
						var line = new THREE.Line(geometry, material);
						line.position.x = i * ws;
						line.position.z = length;
						line.rotation.y = 90 * Math.PI / 180;
						line.name = "line_" + i + 180;
						lineArr.push({
							name: "line_" + i + 180,
							obj: line
						});
						scene.add(line);
					}
				}
 
				// 初始化障碍物随机
				function initGrid() {
					for (var i = 0; i < length / ws; i++) {
						var nodeRow = [];
						for (var j = 0; j < length / ws; j++) {
							nodeRow.push(1);
						}
						graph.push(nodeRow);
					}
					if (meshxy.length > 0) {
						for (var i = 0; i < meshxy.length; i++) {
							initObstacles(meshxy[i].x, meshxy[i].y);
						}
					} else {
						for (var i = 0; i < length / ws; i++) {
							var nodeRow = [];
							for (var j = 0; j < length / ws; j++) {
								var salt = Math.random() * 7;
								if (salt > 2) {
									nodeRow.push(1);
								} else {
									nodeRow.push(0);
								}
								if (salt <= 2) {
									var cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE
										.MeshBasicMaterial({
											color: 0xC0C0C0
										}));
									let x = ws * j + ws / 2;
									let z = ws * i + ws / 2;
									cube.position.set(x, 1.2, z);
									scene.add(cube);
								}
							}
							graph.push(nodeRow);
						}
 
					}
				}
 
				//计算路径
				function caculatePath(resultArray) {
					var maps = new Graph(graph); // 地图
					var startX = parseInt(resultArray[0].position.z / ws);
					var startY = parseInt(resultArray[0].position.x / ws);
					var endX = parseInt(resultArray[1].position.z / ws);
					var endY = parseInt(resultArray[1].position.x / ws);
					var start = maps.grid[startX][startY];
					var end = maps.grid[endX][endY];
					result = astar.search(maps, start, end);
					if (result.length == 0) {
						alert("无可到达路径");
						cleanSphere();
						return;
					}
 
					var nArr = [{
						x: resultArray[0].position.x,
						z: resultArray[0].position.z,
						y: 1.2
					}];
					for (var i = 0; i < result.length; i++) {
						let d = {
							x: result[i].y * ws + ws / 2,
							y: 1.2,
							z: result[i].x * ws + ws / 2,
						}
						nArr.push(d);
					}
					initLine(nArr);
				}
 
				//清除小球
				function cleanSphere() {
					let child = scene.children; //获取场景中的所有子对象
					for (var i = 0; i < child.length; i++) {
						if (child[i].geometry instanceof THREE.SphereGeometry) { //几何对象是球体几何
							scene.remove(child[i]); //从场景中移除
							i--;
						}
					}
					isCaculate = false;
				}
 
				//初始球体
				function initSphere(x, z) {
					if (isCaculate) {
						cleanSphere();
					}
					var geometry = new THREE.SphereGeometry(ws / 2, 30, 30); //球体几何
					var material = new THREE.MeshBasicMaterial({
						color: 0xffff00
					}); //网格基础材料
 
					if (resultArray.length == 0) {
						var sphere = new THREE.Mesh(geometry, material);
						sphere.position.x = x;
						sphere.position.y = 1;
						sphere.position.z = z;
						resultArray.push(sphere);
						scene.add(sphere);
					} else if (resultArray[0].position.x != x || resultArray[0].position.z != z) {
						var sphere = new THREE.Mesh(geometry, material);
						sphere.position.x = x;
						sphere.position.y = 1;
						sphere.position.z = z;
						resultArray.push(sphere);
						scene.add(sphere);
						caculatePath(resultArray);
						isCaculate = true;
						resultArray = new Array();
					}
				}
 
				// 绘制障碍物
				function initObstacles(x, z) {
					var name = "mesh_" + x + z;
					var obj = scene.getObjectByName(name);
					if (obj) {
						scene.remove(obj);
						for (var i = 0; i < meshArr.length; i++) {
							if (meshArr[i].name == name) {
								meshArr.splice(i, 1);
							}
						}
						graph[parseInt(z / ws)][parseInt(x / ws)] = 1;
					} else {
						var geometry = new THREE.BoxGeometry(ws, ws, ws); //球体几何
						var material = new THREE.MeshBasicMaterial({
							color: 0xff0000
						});
 
						var sphere = new THREE.Mesh(geometry, material);
						sphere.position.x = x;
						sphere.position.y = 1;
						sphere.position.z = z;
						sphere.name = name;
						scene.add(sphere);
						meshArr.push({
							name: name,
							obj: sphere,
							x: x,
							y: z,
						});
						graph[parseInt(z / ws)][parseInt(x / ws)] = 0;
					}
				}
 
				// 拾取对象
				function pickupObjects(event) {
					// 点击屏幕创建一个向量
					var raycaster = new THREE.Raycaster();
					var vector = new THREE.Vector2((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window
						.innerHeight) * 2 + 1);
					var fxl = new THREE.Vector3(0, 1, 0);
					var groundplane = new THREE.Plane(fxl, 0);
					raycaster.setFromCamera(vector, camera);
					var ray = raycaster.ray;
					let intersects = ray.intersectPlane(groundplane);
					let x = intersects.x;
					let z = intersects.z;
 
					if (x < 0 || z < 0 || length < z || length < x) {
						return;
					}
 
					var k, m;
					for (var i = 0; i < length; i += ws) {
						if (x >= i && x < i + ws) {
							k = i + ws / 2;
						}
					}
					for (var j = 0; j < length; j += ws) {
						if (z >= j && z < j + ws) {
							m = j + ws / 2;
						}
					}
 
 
					if (controls.clickBool) {
						initSphere(k, m); //初始化球体
					} else {
						initObstacles(k, m);
					}
				}
				document.addEventListener('click', pickupObjects, false); //监听单击拾取对象初始化球体
 
				// 动画渲染
				var step = 5;
 
				function renderScene() {
					TWEEN.update();
					var delta = clock.getDelta();
 
					orbit.update();
					mixer.update(delta);
 
					// 使用requestAnimationFrame函数进行渲染
					requestAnimationFrame(renderScene);
					renderer.render(scene, camera);
				}
 
				// 渲染的场景
				renderer.render(scene, camera);
 
				// 创建一个球形几何体
				function createSphereGeometryLambertMaterial(point) {
					// 创建一个球体
					var sphereGeometry = new THREE.SphereGeometry(0.2, 20, 20);
					var sphereMaterial = new THREE.MeshBasicMaterial({
						color: 0x7777ff,
						wireframe: true
					});
					var sphereMaterial = new THREE.MeshLambertMaterial({
						color: 0xff0000
					});
					var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
					// 设置该物体投射阴影
					sphere.castShadow = true;
 
					// 位置范围
					sphere.position.x = point.x;
					sphere.position.y = point.y;
					sphere.position.z = point.z;
					return sphere;
				}
			}
			window.onload = init;
 
			function onResize() {
				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();
				renderer.setSize(window.innerWidth, window.innerHeight);
			}
			// 监听调整大小事件
			window.addEventListener('resize', onResize, false);
		</script>
	</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2181275.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MySQL | union all 和union(去重和不去重)

题1&#xff1a; 现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据&#xff0c;请取出相应结果&#xff0c;结果不去重。 示例&#xff1a;user_profile 根据示例&#xff0c;你的查询应返回以下结果&#xff1a; UNION&#xff08…

【C/C++】 秋招常考面试题最全总结(让你有一种相见恨晚的感觉)

目录 1.C程序编译链接过程 2.浅拷贝和move有区别吗 3.深拷贝和浅拷贝的区别 4.空类的大小 5.类的继承有几种方式&#xff0c;区别是什么&#xff1f; 六、extern 关键字的作用 七、static关键字的作用 八、指针和引用的区别 九、C内存分配方式 十、结构体对齐…

manim中的桌面设计和实现转换

在这段代码中&#xff0c;我们实现了一系列富有创意和动态效果的数学表达式展示。这种展示方式不仅能够吸引观众的注意力&#xff0c;还能有效地传达复杂的数学概念。代码主要围绕一个点 dot 展开&#xff0c;它逐步转变为不同的数学公式和符号&#xff0c;每个转变过程都伴随着…

JUC高并发编程5:多线程锁

1 锁的八个问题演示 标准访问&#xff0c;先打印短信还是邮件 class Phone{public synchronized void sendSMS() throws InterruptedException {System.out.println("----------sendSMS");}public synchronized void sendEmail(){System.out.println("-------…

AndroidStudio导入so文件

点击app 右键依次选择New-Floder-JNI Floder 创建jni目录 将需要的so文件拷贝到jni目录 在app目录下&#xff0c;build.gradle文件的android{}中添加&#xff1a; sourceSets {main{jniLibs.srcDirs [src/main/jni]}}点击一下Sync Project with Gradle Files 然后编译生成AP…

第7届医联赛圆满举行,鱼跃医疗连续6年保障赛事安全

9月22日-26日&#xff0c;第7届医联赛戈壁群英会在甘肃瓜州举行&#xff0c;来自全国各地的3000名医药行业企业家代表齐聚一堂&#xff0c;开启为期3天的身心极限挑战——徒步穿越81公里的戈壁滩。鱼跃医疗连续6年全程保障参赛者的生命安全&#xff0c;助力赛事圆满完成。 鱼跃…

大模型初学者指南:笔记本电脑上用 ollama 运行大模型!

之前国外有一初学者小哥在Google Colab&#xff08;A100 40G&#xff09;利用DPO微调Mistral 7B&#xff0c;微调后的模型在基准测试中取得不错的成绩&#xff0c;从那时起&#xff0c;我觉得在笔记本电脑上运行/微调大模型并非遥不可及的。 对于初学者而言&#xff0c;如果能…

[论文精读]Multi-Channel Graph Neural Network for Entity Alignment

论文网址&#xff1a;Multi-Channel Graph Neural Network for Entity Alignment (aclanthology.org) 论文代码&#xff1a;https:// github.com/thunlp/MuGNN 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&a…

插上网线无法连接网络,控制面板以太网消失 | 如何重装网络驱动

如果你确定你的网线没问题&#xff0c;网线插口没问题&#xff0c;那你大概率就是驱动问题&#xff0c;可以试一下本方法。 0 以太网消失 事情是这样的&#xff0c;我工作时候需要接内网&#xff0c;插网线&#xff0c;摸鱼时候连外网&#xff0c;我就把网线关了。 每次插网线…

飞致云开源社区月度动态报告(2024年9月)

自2023年6月起&#xff0c;中国领先的开源软件公司FIT2CLOUD飞致云以月度为单位发布《飞致云开源社区月度动态报告》&#xff0c;旨在向广大社区用户同步飞致云旗下系列开源软件的发展情况&#xff0c;以及当月主要的产品新版本发布、社区运营成果等相关信息。 飞致云开源运营…

STM32F1+HAL库+FreeTOTS学习13——二值信号量

STM32F1HAL库FreeTOTS学习13——二值信号量 1. 信号量2. 二值信号量3. 相关API函数3.1 创建二值信号量3.2 获取信号量3.3 释放信号量3.4 删除信号量 4. 二值信号量操作实验1. 实验内容2. 代码实现&#xff1a;3. 运行结果 上一期我们学习了FreeRTOS中的队列&#xff0c;这一期我…

【Linux探索学习】第二弹——Linux的基础指令(中)——夯实基础第二篇

Linux基础指令&#xff08;上&#xff09;&#xff1a;【Linux探索学习】第一弹——Linux的基本指令&#xff08;上&#xff09;——开启Linux学习第一篇-CSDN博客 前言&#xff1a; 在前面我们已经讲解了一些常用的Linux的基础指令&#xff0c;那些当然是远远不够的&#xff…

舵机在无人机中的应用

一、舵机工作原理 舵机是一种位置&#xff08;角度&#xff09;伺服的驱动器&#xff0c;由电子控制与机械控制部分组成。当控制信号输入时&#xff0c;电子控制部分会根据控制器的指令调节直流电机输出的旋转角度和速度&#xff0c;由机械部分转化为控制面的位移以及相应的角…

基于SpringBoot+Vue的个人健康管理系统

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、Vue项目源码、SSM项目源码、微信小程序源码 精品专栏&#xff1a;…

基于Python可视化的学习系统的设计与实现(源码+文档+调试+答疑)

文章目录 一、项目介绍二、视频展示三、开发环境四、系统展示五、代码展示六、项目文档展示七、项目总结 大家可以帮忙点赞、收藏、关注、评论啦 &#x1f447;&#x1f3fb; 一、项目介绍 随着计算机技术发展&#xff0c;计算机系统的应用已延伸到社会的各个领域&#xff0c…

小程序原生-利用setData()对不同类型的数据进行增删改

1. 声明和绑定数据 wxml文件 <view> {{school}} </view> <view>{{obj.name}}</view> <view id"{{id}}" > 绑定属性值 </view> <checkbox checked"{{isChecked}}"/> <!--算数运算--> <view>{{ id …

TS系列(7):知识点汇总

你好&#xff0c;我是沐爸&#xff0c;欢迎点赞、收藏、评论和关注。 一、TS是什么&#xff1f; TypeScript 由微软开发&#xff0c;是基于 JavaScript 的一个扩展语言。TypeScript 包含 JavaScript 的所有内容&#xff0c;是 JavaScript 的超集。TypeScript 增加了静态类型检…

基于SSM的宠物领养管理系统的设计与实现 (含源码+sql+视频导入教程+文档+PPT)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1 、功能描述 基于SSM的宠物领养管理系统2拥有两种角色 管理员&#xff1a;宠物分类管理、领养管理、宠物商品管理、用户管理、寄存管理、丢失信息管理、订单管理等 用户&#xff1a;登录注册、收藏评…

算法:LCR 173. 点名 (原:剑指 offer:0~n-1 中缺失的数字 )

题目 链接&#xff1a;leetcode链接 思路分析&#xff08;一题多解&#xff09; 思路一&#xff1a;高斯求和公式 利用高斯求和公式求出0~n的和&#xff0c;然后减去nums数组中的每一个数&#xff0c;最后的结果就是缺失的数字 时间复杂度&#xff0c;O(N) 过于简单&#xff…

nginx:反向代理服务器——一个非常强大且灵活的工具

Nginx 是一个高性能的 HTTP 和反向代理服务器&#xff0c;常用于 Web 服务器、负载均衡器和反向代理。它以其高性能、稳定性、丰富的功能集、简单的配置文件和低资源消耗而著称。在部署 Web 应用程序时&#xff0c;Nginx 常被用来处理静态文件、反向代理动态内容、负载均衡等任…