ThreeJS 官方案例学习(webgl_effects_ascii)
1.效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e49455224625490ba56a1b05ba56e62e.jpeg#pic_center)
2.源码
<template>
<div>
<div id="container"></div>
</div>
</template>
<script>
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { RoomEnvironment } from 'three/examples/jsm/environments/RoomEnvironment.js';
import Stats from 'three/examples/jsm/libs/stats.module.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
import gsap from 'gsap';
import { AsciiEffect } from 'three/examples/jsm/effects/AsciiEffect.js';
import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls.js';
export default {
data() {
return {
container: null,
scene: null,
camera: null,
renderer: null,
controller: null,
stats: null,
mixer: null,
start: Date.now(),
effect: null,
sphere: null,
};
},
mounted() {
this.init()
this.animate()
window.addEventListener("resize", this.onWindowSize)
},
beforeUnmount() {
console.log('beforeUnmount===============');
this.container = null
this.scene = null
this.camera = null
this.renderer = null
this.controller = null
this.stats = null
this.mixer = null
this.sphere = null
},
methods: {
init() {
this.container = document.getElementById('container')
this.setScene()
this.setCamera()
this.setRenderer()
this.setEffect()
this.setController()
this.addHelper()
this.setLight()
this.setMeah()
this.addStatus()
},
setScene() {
this.scene = new THREE.Scene()
this.scene.background = new THREE.Color(0, 0, 0);
},
setCamera() {
this.camera = new THREE.PerspectiveCamera(70, this.container.clientWidth / this.container.clientHeight, 1, 1000)
this.camera.position.set(0, 150, 500)
this.camera.aspect = this.container.clientWidth / this.container.clientHeight;
this.camera.updateProjectionMatrix();
this.camera.lookAt(new THREE.Vector3(0, 0, 0))
this.scene.add(this.camera)
},
setRenderer() {
this.renderer = new THREE.WebGLRenderer()
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
},
setEffect() {
this.effect = new AsciiEffect(this.renderer, ' .:-+*=%@#', { invert: true })
this.effect.setSize(this.container.clientWidth, this.container.clientHeight);
this.effect.domElement.style.color = 'white';
this.effect.domElement.style.backgroundColor = 'black';
this.container.appendChild(this.effect.domElement);
},
setController() {
this.controller = new TrackballControls(this.camera, this.effect.domElement);
this.controller.enableDamping = true;
this.controller.dampingFactor = 0.04;
},
addHelper() {
let helper = new THREE.CameraHelper(this.camera);
let axisHelper = new THREE.AxesHelper(150);
let gridHelper = new THREE.GridHelper(100, 30, 0x2C2C2C, 0x888888);
},
setPMREMGenerator() {
const pmremGenerator = new THREE.PMREMGenerator(this.renderer);
this.scene.environment = pmremGenerator.fromScene(new RoomEnvironment(this.renderer), 0.04).texture;
},
setLight() {
const pointLight1 = new THREE.DirectionalLight(0xffffff, 3, 0, 0);
pointLight1.position.set(500, 500, 500);
this.scene.add(pointLight1);
const pointLight2 = new THREE.DirectionalLight(0xffffff, 1, 0, 0);
pointLight2.position.set(- 500, - 500, - 500);
this.scene.add(pointLight2);
},
addStatus() {
this.stats = new Stats();
this.container.appendChild(this.stats.dom);
},
setMeah() {
this.sphere = new THREE.Mesh(new THREE.SphereGeometry(200, 20, 10), new THREE.MeshPhongMaterial({ flatShading: true }))
this.scene.add(this.sphere)
const plane = new THREE.Mesh(new THREE.PlaneGeometry(400, 400), new THREE.MeshBasicMaterial({ color: 0xe0e0e0 }));
plane.position.y = - 200;
plane.rotation.x = - Math.PI / 2;
this.scene.add(plane)
},
onWindowSize() {
this.camera.aspect = this.container.clientWidth / this.container.clientHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
this.effect.setSize(this.container.clientWidth, this.container.clientHeight);
},
animate() {
const timer = Date.now() - this.start;
this.sphere.position.y = Math.abs(Math.sin(timer * 0.002)) * 150;
this.sphere.rotation.x = timer * 0.0003;
this.sphere.rotation.z = timer * 0.0002;
requestAnimationFrame(this.animate);
this.controller.update();
this.stats.update();
this.effect.render(this.scene, this.camera);
},
},
};
</script>
<style>
#container {
position: absolute;
width: 100%;
height: 100%;
}
</style>
tats.update();
this.effect.render(this.scene, this.camera);
},
},
};
</script>
<style>
#container {
position: absolute;
width: 100%;
height: 100%;
}
</style>