1.所需依赖
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import {TWEEN} from "three/examples/jsm/libs/tween.module.min";
2.基础功能:相机、场景、光源、渲染器、控制器
threejs: {
camera: null,
scene: null,
renderer: null,
controls: null,
animateId: null,
},
ControlsType:"3D",
initCamera() {
this.threejs.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000)
this.threejs.camera.position.set(40, 40, 40);
},
initScene() {
this.threejs.scene = new THREE.Scene()
this.threejs.scene.background = new THREE.Color("#353F47");
this.threejs.scene.add(this.threejs.camera)
},
initLight() {
const ambientLight = new THREE.AmbientLight("#ffffff", 1);
this.threejs.scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight("#555e66", 1);
directionalLight.position.set(20, 40, -40);
this.threejs.scene.add(directionalLight);
},
initRenderer() {
this.container = document.getElementById('container')
this.renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
this.renderer.setPixelRatio(this.container.devicePixelRatio);
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
this.container.appendChild(this.renderer.domElement)
this.threejs.renderer.shadowMap.enabled = true;
},
initControls() {
this.threejs.controls = new OrbitControls(this.threejs.camera, this.threejs.renderer.domElement);
this.threejs.controls.maxPolarAngle = 1.5;
this.threejs.controls.minPolarAngle = 0;
this.threejs.controls.enableZoom = true;
this.threejs.controls.enableDamping = true
},
3.animate循环动画
animate() {
this.animateId = requestAnimationFrame(this.animate);
this.threejs.renderer.render(this.threejs.scene, this.threejs.camera);
this.threejs.controls.update();
if (this.ControlsType === "3D") {
this.threejs.controls.mouseButtons = {
LEFT: THREE.MOUSE.ROTATE,
MIDDLE: THREE.MOUSE.ZOOM,
RIGHT: THREE.MOUSE.PAN
}
} else if (this.ControlsType === "2D") {
this.threejs.controls.mouseButtons = {
LEFT: THREE.MOUSE.PAN,
MIDDLE: THREE.MOUSE.DOLLY,
RIGHT: THREE.MOUSE.PAN
}
}
TWEEN.update();
},
4.切换时调用方法ChangeControl
ChangeControl(flag){
this.ControlsType = flag
if(this.ControlsType === "2D"){
this.tween2D({
x:0,y:0,z:0
})
}else{
this.threejs.controls.reset()
this.tween3D({
x:20,y:20,z:20
})
}
},
tween3D(Position) {
if(this.ControlsType === "3D"){
let that = this
this.threejs.camera.lookAt(new THREE.Vector3(0, 0, 0));
var p1 = {
x:this.threejs.camera.position.x / 2.2,
y:this.threejs.camera.position.y - 15,
z:this.threejs.camera.position.z / 2.2
}
var p2 = {
x: Position.x,
y: Position.y,
z: Position.z,
}
var tween = new TWEEN.Tween(p1).to(p2, 1200);
var update = function (object) {
that.threejs.camera.position.set(object.x * 2.2, object.y + 15, object.z * 2.2);
that.threejs.camera.lookAt(0, 0, 0);
that.threejs.controls.enabled = false;
that.threejs.controls.update();
};
tween.onUpdate(update);
tween.onComplete(() => {
that.threejs.controls.enabled = true;
});
tween.easing(TWEEN.Easing.Quadratic.InOut);
tween.start();
}
},
tween2D(Position) {
let that = this
this.threejs.camera.lookAt(new THREE.Vector3(0, 0, 0));
var p1 = {
x:this.threejs.camera.position.x,
y:this.threejs.camera.position.y - 40,
z:this.threejs.camera.position.z
}
var p2 = {
x: Position.x,
y: Position.y,
z: Position.z,
}
var tween = new TWEEN.Tween(p1).to(p2, 1200);
var update = function (object) {
that.threejs.camera.position.set(object.x, object.y + 40, object.z);
that.threejs.camera.lookAt(0, 0, 0);
that.threejs.controls.enabled = false;
that.threejs.controls.update();
};
tween.onUpdate(update);
tween.onComplete(() => {
that.threejs.controls.enabled = true;
});
tween.easing(TWEEN.Easing.Quadratic.InOut);
tween.start();
},
5.初始化方法
init() {
this.initRenderer()
this.initCamera()
this.initScene()
this.initLight()
this.initControls()
},
mounted() {
this.init()
this.animate()
},