THREE.MapControls()
地图控件,如果查看类似地图模型或者不希望用户对模型进行反转的时候可以使用这个控件,这个控件是three.js
r94版本徐新添加的。
1、示例
http://ithanmang.com/threeJs/home/201809/20180904/01-MapControls.html
当screenSpacePanning
属性为true
的时候,可以拖动旋转模型。
2、使用控件
2.1、引入js文件
<script src="../../libs/examples/js/controls/MapControls.js"></script>
MapControls.js
在版本**r94**
以及以上可以找到。
2.3、创建实例
/* 地图控件 */
controls = new THREE.MapControls(camera, renderer.domElement);
2.4、设置参数
/* 属性参数 */
controls.enableDamping = true;// 启用动态阻尼时需要一个动画循环
controls.dampingFactor = 0.30;
controls.screenSpacePanning = false;// 若为 true 则可以平移
controls.maxDistance = 400;
controls.minDistance = 100;
controls.maxPolarAngle = Math.PI / 2.2;
controls.autoRotate = false;
如果打开controls.enableDamping = true
动态阻尼的话。
需要在循环动画中,调用controls.update()
方法,来更新数据。
完整的参数列表可以查看源码获得
2.5、操控方法
操控 | 动作 |
---|---|
鼠标左键 | 平移 |
鼠标右键 | 旋转和改变极地角度 |
鼠标滑轮 | 缩放 |
3、示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="../../../three.png">
<title>MapControls 地图控件</title>
<style>
body {
margin: 0;
overflow: hidden; /* 溢出隐藏 */
}
#loading {
position: fixed;
top: 50%;
left: 50%;
color: #FFFFFF;
font-size: 20px;
margin-top: -30px;
margin-left: -40px;
}
</style>
<script src="../../libs/build/three-r93.js"></script>
<script src="../../libs/examples/js/Detector.js"></script>
<script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
<script src="../../libs/examples/js/libs/stats.min.js"></script>
<script src="../../libs/examples/js/loaders/MTLLoader.js"></script>
<script src="../../libs/examples/js/loaders/OBJLoader.js"></script>
<script src="../../libs/chroma/chroma.js"></script>
<script src="../../libs/examples/js/controls/MapControls.js"></script>
</head>
<body>
<p id="loading">loading......</p>
<script>
let scene, camera, renderer, controls, guiControls;
let stats = initStats();
/* 场景 */
function initScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x050505);
scene.fog = new THREE.FogExp2(0x050505, 0.005);
}
/* 相机 */
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(0, 90, 220);
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
/* 渲染器 */
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
/* 灯光 */
function initLight() {
scene.add(new THREE.AmbientLight(0x0c0c0c));
let spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-400, -400, -400);
let spotLight2 = new THREE.SpotLight(0xffffff);
spotLight2.position.set(400, 800, 400);
scene.add(spotLight);
scene.add(spotLight2);
}
/* 控制器 */
function initControls() {
/* 地图控件 */
controls = new THREE.MapControls(camera, renderer.domElement);
/* 属性参数 */
controls.enableDamping = true;// 启用动态阻尼时需要一个动画循环
controls.dampingFactor = 0.30;
controls.screenSpacePanning = false;// 若为 true 则可以平移
controls.maxDistance = 400;
controls.minDistance = 100;
controls.maxPolarAngle = Math.PI / 2.2;
controls.autoRotate = false;
}
/* 调试插件 */
function initGui() {
guiControls = new function () {
this.screenSpacePanning = controls.screenSpacePanning;
this.autoRotate = controls.autoRotate;
this.reset = function () {
controls.reset();
};
this.getPolarAngle = function () {
alert('极坐标:'+controls.getPolarAngle());
};
this.getAzimuthalAngle = function () {
alert('方位角:'+controls.getAzimuthalAngle());
};
};
let gui = new dat.GUI();
gui.add(guiControls,'screenSpacePanning').onChange(function (e) {
controls.screenSpacePanning = e;
});
gui.add(guiControls,'autoRotate').onChange(function (e) {
controls.autoRotate = e;
});
gui.add(guiControls,'reset');
gui.add(guiControls,'getPolarAngle');
gui.add(guiControls,'getAzimuthalAngle');
}
/* 场景中的内容 */
function initContent() {
let mtlLoader = new THREE.MTLLoader();
mtlLoader.load('../../models/obj/city.mtl', function (materials) {
let objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load('../../models/obj/city.obj', function (object) {
let scale = chroma.scale(['red', 'green', 'blue']);
setRandomColor(object, scale);
console.log(object);
scene.add(object);
document.getElementById('loading').style.display = 'none';
});
});
}
function setRandomColor(object, scale) {
let children = object.children;
if (children && children.length > 0) {
children.forEach(function (e) {
setRandomColor(e, scale);
});
} else {
if (object instanceof THREE.Mesh) {
for (let i = 0; i < object.material.length; i++) {
let material = object.material;
if (material[i].name.indexOf('building') === 0) {
material[i].emissive = new THREE.Color(scale(Math.random()).hex());
material[i].transparent = true;
material[i].opacity = 0.7;
}
}
}
}
}
/* 性能插件 */
function initStats() {
let stats = new Stats();
document.body.appendChild(stats.domElement);
return stats;
}
/* 窗口变动触发 */
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
let clock = new THREE.Clock();
/* 数据更新 */
function update() {
stats.update();
controls.update(clock.getDelta());
}
/* 初始化 */
function init() {
initScene();
initCamera();
initRender();
initLight();
initControls();
initContent();
initGui();
/* 监听事件 */
window.addEventListener('resize', onWindowResize, false);
}
/* 循环渲染 */
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
update();
}
/* 初始加载 */
(function () {
console.log("three init start...");
init();
animate();
console.log("three init end...");
})();
</script>
</body>
</html>