//最近没有时间整理代码,就这样吧
<template>
<div>
<div id="map"></div>
</div>
</template>
<script>
// import * as Three from '../../node_modules/three/build/three.module.js';
import "maptalks/dist/maptalks.css";
import * as maptalks from "maptalks";
import * as Three from "three";
import { ThreeLayer } from "maptalks.three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { PointerLockControls } from "three/examples/jsm/controls/PointerLockControls.js";
import { PCDLoader } from "three/examples/jsm/loaders/PCDLoader";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
export default {
name: "ThreeTest",
data() {
return {
map: null,
threeLayer: null,
mapConfig: {
center: [568318.74296, 4314626.47854],
zoom: 12.364821148501335,
pitch: 71.6000000000002,
bearing: -52.200000000000045,
},
camera: null,
scene: null,
renderer: null,
controls: "",
intersections: null,
objects: [],
clock: "",
moveForward: false,
moveLeft: false,
moveBackward: false,
moveRighta: false,
//----车辆参数----------
car: null, //汽车对象
speed: 0,
rSpeed: 0,
run: false,
acceleration: 0.005, //car 转弯半径大小,越小越转弯越陡
deceleration: 0.02, //car溜车长短,越小溜车越久
// maxSpeed: 2,
// lock: -1,
// isBrake: false,
realRotation: -1.6, // 车辆自身真实的旋转度
dirRotation: 0, // 方向上的旋转
addRotation: 0, // 累计的旋转角度
//--------------
direction: new Three.Vector3(),
velocity: new Three.Vector3(),
prevTime: performance.now(),
};
},
methods: {
init() {
var that = this;
this.map = new maptalks.Map("map", {
center: this.mapConfig.center,
zoom: this.mapConfig.zoom,
seamlessZoom: false,
hitDetect: false, // 是否为此地图上的光标样式启用图层命中检测,请禁用它以提高性能。
zoomControl: false,
scaleControl: false,
overviewControl: false,
attribution: false,
pitch: this.mapConfig.pitch,
bearing: this.mapConfig.bearing,
spatialReference: {
projection: "identity",
},
});
//3D图层
this.threeLayer = new ThreeLayer("car", {
forceRenderOnMoving: true,
forceRenderOnRotating: true,
animation: true,
});
this.threeLayer.setZIndex(10);
this.threeLayer.prepareToDraw = function (gl, scene, camera) {
// this.camera = camera;
that.initScene(); //场景对象
that.initPlane(); //地板
that.initCamera(); //相机
that.initWebGLRenderer(); //渲染器
// this.initAxisHelper(); //辅助坐标
that.render();
// this.createControls(); //控件对象
that.loadPointCloud();
that.Car();
that.initControls(); //相机视角
that.initMobile(); //移动
};
this.threeLayer.addTo(this.map);
},
//鼠标控制移动相机视角*****
initControls() {
let that = this;
that.controls = new PointerLockControls(this.camera, document.body);
// var container = document.getElementById("container");
// container.addEventListener("click", function () {
// that.controls.lock();
// });
this.scene.add(that.controls.getObject());
},
initMobile() {
let that = this;
// console.log(this.controls);
var onKeyDown = function (event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
that.run = true;
break;
case 37: // left
case 65: // a
that.rSpeed = 0.02;
break;
case 40: // down
case 83: // s
that.run = false;
break;
case 39: // right
case 68: // d
that.rSpeed = -0.02;
break;
}
};
var onKeyUp = function (event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
that.run = false;
break;
case 37: // left
case 65: // a
that.rSpeed = 0;
break;
case 40: // down
case 83: // s
that.run = false;
break;
case 39: // right
case 68: // d
that.rSpeed = 0;
break;
}
};
document.addEventListener("keydown", onKeyDown, false);
document.addEventListener("keyup", onKeyUp, false);
},
// 创建场景对象Scene
initScene() {
this.scene = new Three.Scene();
},
// 相机
initCamera() {
// let container = document.getElementById("map");
this.camera = new Three.PerspectiveCamera(
90,
window.innerWidth / window.innerHeight,
0.1,
10000
);
this.camera.speed = {
z: 0,
x: 0,
};
this.camera.position.set(2, 7, 5); //设置相机位置
// this.camera.lookAt(this.camera.position); //设置相机方向(指向的场景对象)
this.camera.add(new Three.PointLight("#fff", 3)); //设置灯光
},
loadPointCloud() {
var that = this;
// instantiate a loader
var loader = new PCDLoader();
// load a resource
loader.load(
// resource URL
"./trunk.pcd",
// called when the resource is loaded
function (mesh) {
console.log(mesh);
// scene.add(mesh);
mesh.scale.set(1.32, 1.32, 5);
// mesh.position.copy(
// that.threeLayer.coordinateToVector3([567403.0, 4315210.0])
// );
that.threeLayer.addMesh(mesh);
},
// called when loading is in progresses
function (xhr) {
// console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
// called when loading has errors
function (error) {
console.log("An error happened");
}
);
},
Car() {
var that = this;
var loader = new GLTFLoader();
let url = "./car.glb";
loader.load(url, function (gltf) {
console.log(gltf);
var model = gltf.scene;
model.name = "car";
// model.rotation.x = 0;
// model.rotation.z =0;
model.rotation.y = -1.5; //car自身旋转度
model.scale.set(0.5, 0.5, 0.5);
// 矫正
model.position.z = -15;
model.position.y = 0;
model.position.x = 0;
that.car = model;
that.scene.add(that.car);
});
},
//地板
initPlane() {
var planeGeometry = new Three.PlaneGeometry(1000, 1000);
//平面使用颜色为0xcccccc的基本材质
// var planeMaterial = new Three.MeshBasicMaterial({ color: 0xcccccc });
this.scene.add(new Three.AmbientLight(0xffffff)); //添加灯光显示地板图片
// ground 添加地面
const loader = new Three.TextureLoader();
const groundTexture = loader.load(require('../../public/Cad1.png')); //图片
// const groundTexture = loader.load(
// require("../assets/grasslight-big.jpeg")
// ); //绿色草地
groundTexture.wrapS = groundTexture.wrapT = Three.RepeatWrapping;
// groundTexture.repeat.set(100, 100);
groundTexture.anisotropy = 16;
groundTexture.encoding = Three.sRGBEncoding;
const groundMaterial = new Three.MeshLambertMaterial({
map: groundTexture,
});
var plane = new Three.Mesh(planeGeometry, groundMaterial);
//设置屏幕的位置和旋转角度
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 0;
plane.position.y = 0;
plane.position.z = 0;
//将平面添加场景中
this.scene.add(plane);
},
//创建渲染器对象
initWebGLRenderer() {
var container = document.getElementById("map");
this.renderer = new Three.WebGLRenderer({ antialias: true });
this.renderer.setSize(container.clientWidth, container.clientHeight); //设置渲染区域尺寸
this.renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
container.appendChild(this.renderer.domElement); //body元素中插入canvas对象
},
//辅助三维坐标系AxisHelper
initAxisHelper() {
this.axisHelper = new Three.AxisHelper(1000);
this.scene.add(this.axisHelper);
},
//处理车辆方向键逻辑及算法
runCarTick() {
if (this.run) {
this.speed += this.acceleration;
// if (this.speed > this.maxSpeed) {
// this.speed = this.maxSpeed;
// }
} else {
this.speed -= this.deceleration;
if (this.speed < 0) {
this.speed = 0;
}
}
var speed = -this.speed;
if (speed === 0) {
return;
}
var time = Date.now();
this.dirRotation += this.rSpeed;
this.realRotation += this.rSpeed;
var rotation = this.dirRotation;
var speedX = Math.sin(rotation) * speed;
var speedZ = Math.cos(rotation) * speed;
var tempX = this.car.position.x + speedX;
var tempZ = this.car.position.z + speedZ;
var tempA = -this.car.rotation.y;
this.car.rotation.y = this.realRotation;
this.car.position.z += speedZ;
this.car.position.x += speedX;
this.camera.rotation.y = rotation;
this.camera.position.x = this.car.position.x + Math.sin(rotation) * 20;
this.camera.position.z = this.car.position.z + Math.cos(rotation) * 20;
},
render: function () {
this.runCarTick();
requestAnimationFrame(this.render); //请求再次执行渲染函数render
this.renderer.render(this.scene, this.camera); //执行渲染操作
},
// 创建控件对象
createControls() {
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
//是否开启右键拖拽
// this.controls.enablePan = false;
//设置自动旋转
// this.controls.autoRotate = true;
// 禁止鼠标操作
this.controls.enabled = false;
},
},
mounted() {
this.init();
},
};
</script>
<style scoped>
#map {
height: 800px;
}
</style>
- 关于java设计模式与极品飞车游戏的思考
------- android培训.java培训.期待与您交流! ---------- 对像我一样正在学习java的人来讲,对设计模式的学习是个很重要的环节.而我们在学习设计模式时,不仅仅应该知道它们 ...
- 添加 Windows 8.1 无虚拟机启动项 解决极品飞车的不支持虚拟机报错
在Windows 8.1 64位环境下,安装完极品飞车17后,运行程序会出现错误对话框: Sorry, this application cannot run under a Virual Machi ...
- 如何用纯 CSS 创作一个极品飞车 loader
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MBbEMo 可交互视频 ...
- 前端每日实战:84# 视频演示如何用纯 CSS 创作一个极品飞车 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MBbEMo 可交互视频 此视频是可 ...
- Online Game Development in C++ 第五部分总结
I. 教程案例框架描述 该套教程做了一个简单的汽车控制系统,没有用到物理模拟.用油门和方向控制汽车的加速度和转向,同时还有一些空气阻力和滚动摩擦力的设置增加了真实感.汽车的位置是通过加速度和时间等计算 ...
- Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux
我用过的编辑器不少,真不少- 但却没有哪款让我特别心仪的,直到我遇到了 Sublime Text 2 !如果说“神器”是我能给予一款软件最高的评价,那么我很乐意为它封上这么一个称号.它小巧绿色且速度非 ...
- Andorid-15k+的面试题。
andorid开发也做了3年有余了,也面试很多加企业,借此机会分享一下,我们中遇到过的问题以及解决方案吧,希望能够对正在找工作的andoird程序员有一定的帮助. 特别献上整理过的50道面试题目 1. ...
- 19、android面试题整理(自己给自己充充电吧)
(转载,出处丢失,请原作者原谅,如有意见,私信我我会尽快删除本文) JAVA 1.GC是什么? 为什么要有GC?GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问 ...
- Java设计模式11:常用设计模式之代理模式(结构型模式)
1. Java之代理模式(Proxy Pattern) (1)概述: 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问. 在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象 ...
随机推荐
- php 和微信小程序 解构赋值
php 解构赋值只能解构索引数组 js:
- jq 全选 全不选
- 题解 CF17E 【Palisection】
卡空间PAM,2010没有PAM,所以都是马拉车 众所周知,PAM拥有十分优秀的时间复杂度,但空间复杂度lj得不行 但这题卡空间,所以得用到邻接链表PAM 先讲思路 题目要求相交的回文子串对,这很难做 ...
- CF1386C口胡
自己在物理课上编了一道题,大概就是这题把删除区间的边改为保留区间的边...都不觉得判断短路和判断二分图有点儿像吗 题意:给定一张无向图,每次暂时删除一个区间内的边,问删除后这个区间是否为二分图. 首先 ...
- 9.Flink实时项目之订单宽表
1.需求分析 订单是统计分析的重要的对象,围绕订单有很多的维度统计需求,比如用户.地区.商品.品类.品牌等等.为了之后统计计算更加方便,减少大表之间的关联,所以在实时计算过程中将围绕订单的相关数据整合 ...
- Mysqldump 命令只导出表结构或数据或数据和结构的方法
1.备份数据库 #mysqldump --opt -d 数据库名 -u root -p > xxx.sql #mysqldump DB_NAME >数据库备份名 #mysqldump -A ...
- web自动化之定位
UI自动化必不可少的操作--元素定位 8大基础定位 driver.find_element_by_id() # id定位 driver.find_element_by_name() # name定位 ...
- 12.19 file 与io流
12.19 file与io流 File 1. 新建文件或者文件夹 新建文件 File F = new File(pathname:"路径+文件名");Boolean flag = ...
- Spring bean 和单例bean的线程安全
Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).prototype(原型).request.session和global session,5种作用域说 ...
- BASH和DOS之间的基本区别是什么?
BASH和DOS控制台之间的主要区别在于3个方面:1. BASH命令区分大小写,而DOS命令则不区分;2. 在BASH下,/ character是目录分隔符,\作为转义字符.在DOS下,/用作命令参数 ...