【Three.js】十二、three.js摄像机控件

一、FirstPersonControls

第一人称控制器,类似fps射击类游戏视角。

控制方法:

操控效果
移动鼠标向四周看
上、下、左、右方向键向前、后、左、右移动视角
W向前移动
A向左移动
D向右移动
S向后移动
R向上移动
F向下移动

使用:

	import {FirstPersonControls} from '../../libs/contorls/FirstPersonControls'

	let controls = new FirstPersonControls(camera);
    controls.lookSpeed = 0.2; //鼠标移动查看的速度
    controls.movementSpeed = 30; //相机移动速度
    controls.noFly = true;
    controls.constrainVertical = true; //约束垂直
    controls.verticalMin = 1.0;
    controls.verticalMax = 2.0;
    controls.lon = 0; //进入初始视角x轴的角度
    controls.lat = 0; //初始视角进入后y轴的角度

示例

import '../../stylus/index.styl'
import * as THREE from 'three'
import * as dat from 'dat.gui'
import {FirstPersonControls} from '../../libs/contorls/FirstPersonControls'
import {initTrackballControls, initThree, initStats} from "../../util/util"

function init() {
    let stats = initStats();
    let {camera, scene, renderer} = initThree();
    camera.far = 5000;
    camera.position.set(0, 110, 0);

    let light = new THREE.SpotLight(0xffffff);
    light.shadow.mapSize.set(2048, 2048);
    light.castShadow = true;
    light.position.set(-700, 440, 730);
    scene.add(light);

    let ambientLight = new THREE.AmbientLight(0xdddddd, 0.4);
    scene.add(ambientLight);

    let planeGeometry = new THREE.PlaneBufferGeometry(14000, 10000);
    let planeMaterial = new THREE.MeshLambertMaterial({
        color: 0xffffff
    });
    let plane = new THREE.Mesh(planeGeometry, planeMaterial);
    plane.rotation.x = -0.5 * Math.PI;
    plane.receiveShadow = true;
    scene.add(plane);

    let getBoxes = () => {
        for (let i = 0; i < 300; i++) {
            let boxGeometry = new THREE.BoxBufferGeometry(20, Math.round(Math.random()*100 + 50), 20);
            let boxMaterial = new THREE.MeshLambertMaterial({
                color: new THREE.Color(Math.random(), Math.random(), Math.random()),
                transparent: true,
                opacity: 0.7
            });
            let box = new THREE.Mesh(boxGeometry, boxMaterial);
            box.position.set(Math.random()*-1390 + 690, box.geometry.parameters.height/2, Math.random()*-990 + 490);
            scene.add(box);
        }
    };
    getBoxes();

    let controls = new FirstPersonControls(camera, document.body);
    controls.lookSpeed = 0.2; //鼠标移动查看的速度
    controls.movementSpeed = 30; //相机移动速度
    controls.noFly = true;
    controls.constrainVertical = true; //约束垂直
    controls.verticalMin = 1.0;
    controls.verticalMax = 2.0;
    controls.lon = 0; //进入初始视角x轴的角度
    controls.lat = 0; //初始视角进入后y轴的角度

    let clock = new THREE.Clock();
    let render = () => {
        stats.update();
        controls.update(clock.getDelta());
        renderer.render(scene, camera);
        requestAnimationFrame(render);
    };
    window.addEventListener('resize', onWindowResize, false);

    function onWindowResize () {
        camera.aspect = window.innerWidth / window.innerHeight;
        renderer.setSize(window.innerWidth, window.innerHeight);
        camera.updateProjectionMatrix();
        stats.update();

    }
    render();
}

init();

在这里插入图片描述
完整示例(src/pages/three_FirstPersonContorls_demo):https://github.com/MAXLZ1/threejs_demo

二、FlyControls

飞行控制器

操作方法:

操控效果
按住鼠标左键和中键向前移动
按住鼠标右键向后移动
移动鼠标向周围看
W向前移动
A向左移动
S向后移动
D向右移动
R向上移动
F向下移动
Q向左翻滚
E向右翻滚
上、下、左、右键向上、下、左、右看

示例

import '../../stylus/index.styl'
import * as THREE from 'three'
import { initThree, initStats} from "../../util/util"
import {OBJLoader} from "../../libs/loaders/OBJLoader"
import {FlyControls} from "../../libs/contorls/FlyControls"

function init (){
    let stats = initStats();
    let {scene, camera, renderer} = initThree();
    camera.position.set(-100, 10, 90);

    let spotLight = new THREE.SpotLight(0xffffff);
    spotLight.position.set(-300, 300, 130);
    scene.add(spotLight);

    let loader = new OBJLoader();
    loader.load('../../static/modules/city/city.obj', (obj) => {
        setRandomColors(obj);
        scene.add(obj);
    });

    let controls = new FlyControls(camera, renderer.domElement);
    controls.rollSpeed = Math.PI / 12; // 翻滚速度
    controls.autoForward = true; //自动向前移动
    controls.dragToLook = false;
    controls.movementSpeed = 25;
    let clock = new THREE.Clock();

    let render = () => {
        stats.update();
        controls.update(clock.getDelta());
        renderer.render(scene,camera);
        requestAnimationFrame(render);
    };
    render();
}

function setRandomColors(object) {
    let children = object.children;
    if (children && children.length > 0) {
        children.forEach(function (e) {
            setRandomColors(e)
        });
    } else {
        if (object.type === 'Mesh') {
            if (object.material instanceof Array) {
                object.material.forEach(function(m) {
                    m.color = new THREE.Color(Math.random(),Math.random(),Math.random());
                    if (m.name.indexOf("building") == 0) {
                        m.emissive = new THREE.Color(0x444444);
                        m.transparent = true;
                        m.opacity = 0.8;
                    }
                });
            } else {
                object.material.color = new THREE.Color(Math.random(),Math.random(),Math.random());
                if (object.material.name.indexOf("building") == 0) {
                    object.material.emissive = new THREE.Color(0x444444);
                    object.material.transparent = true;
                    object.material.opacity = 0.8;
                }
            }
        }
    }
}

init();

在这里插入图片描述
完整示例(src/pages/three_FlyControls_demo):https://github.com/MAXLZ1/threejs_demo

三、TrackballControls

轨迹球控制器

操作方法:

操控效果
按住鼠标左键,拖动摄像机在场景中旋转和翻滚
转动鼠标滚轮放大和缩小

示例

import '../../stylus/index.styl'
import * as THREE from 'three'
import {initThree, initStats} from "../../util/util"
import {TrackballControls} from "../../libs/contorls/TrackballControls"

function init () {
    let stats = initStats();
    let {camera, renderer, scene} = initThree();
    camera.position.set(400,400,400);

    let ambientLight = new THREE.AmbientLight(0xffffff);
    scene.add(ambientLight);

    let addObjs = () => {
        for (let i = 0; i < 300; i++) {
            let boxGeometry = new THREE.BoxBufferGeometry(Math.random()*10+10, Math.random()*10 + 5,Math.random()*10 + 2);
            let boxMaterial = new THREE.MeshLambertMaterial({
                color: new THREE.Color(Math.random(), Math.random(), Math.random())
            });
            let box = new THREE.Mesh(boxGeometry, boxMaterial);
            box.position.set(Math.random()*300 - 150, Math.random()*300 - 150, Math.random()*300 - 150);
            box.castShadow = true;
            scene.add(box);
        }
    };

    addObjs();

    let controls = new TrackballControls(camera, renderer.domElement);
    // 旋转速度
    controls.rotateSpeed = 2.0;
    // 变焦速度
    controls.zoomSpeed = 2.2;
    // 平移速度
    controls.panSpeed = 1.8;
    // 是否不缩放
    controls.noZoom = false;
    // 是否不平移
    controls.noPan = false;
    // 是否开启移动惯性
    controls.staticMoving = true;
    // 动态阻尼系数 就是灵敏度
    controls.dynamicDampingFactor = 0.3;
    // 设置最大距离
    controls.maxDistance = 600;

    let render = () => {
        stats.update();
        controls.update();
        renderer.render(scene, camera);
        requestAnimationFrame(render);
    };
    render();
}

init();

在这里插入图片描述
完整示例(src/pages/three_TrackBallControls_demo):https://github.com/MAXLZ1/threejs_demo

四、OrbitControls

轨道控制器

操作方法:

操控效果
按住鼠标左键并移动摄像机围绕场景中心旋转
旋转鼠标滑轮放大或缩小
按住鼠标右键移动场景中平移
上、下、左、右方向键上、下、左、右平移

示例

	let controls = new OrbitControls(camera, renderer.domElement);
    // 设置最大距离
    controls.maxDistance = 600;
    // 设置自动旋转
    controls.autoRotate = true;
    // 设置自动旋转速度
    controls.autoRotateSpeed = 2.0;

在这里插入图片描述
完整示例(src/pages/three_OrbitControls_demo):https://github.com/MAXLZ1/threejs_demo

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MAXLZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值