ThreeJS 官方案例学习(webgl_geometries)
1.效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7fb007155fd94247b7d5bbb6c5d32fb1.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';
export default {
data() {
return {
container: null,
height: 0,
width: 0,
scene: null,
camera: null,
renderer: null,
controller: null,
stats: null,
mixer: null,
model: null,
clock: new THREE.Clock(),
};
},
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.model = null
},
methods: {
init() {
this.container = document.getElementById('container')
this.width = this.container.clientWidth
this.height = this.container.clientHeight
this.setScene()
this.setCamera()
this.setRenderer()
this.setController()
this.addHelper()
this.setPMREMGenerator()
this.setLight()
this.setMesh()
this.addStatus()
},
setScene() {
this.scene = new THREE.Scene()
},
setCamera() {
this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 2000)
this.camera.position.set(0, 400, 800)
this.camera.aspect = this.width / this.height;
this.camera.updateProjectionMatrix();
this.camera.lookAt(new THREE.Vector3(0, 0, 0))
this.scene.add(this.camera)
},
setRenderer() {
this.renderer = new THREE.WebGLRenderer({
antialias: true,
logarithmicDepthBuffer: true,
})
this.renderer.setSize(this.width, this.height);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.container.appendChild(this.renderer.domElement);
},
setController() {
this.controller = new OrbitControls(this.camera, this.renderer.domElement);
this.controller.enableDamping = true;
this.controller.dampingFactor = 0.04;
this.controller.target.set(0, 0, 0);
},
addHelper() {
let helper = new THREE.CameraHelper(this.camera);
let axisHelper = new THREE.AxesHelper(450);
this.scene.add(axisHelper)
let gridHelper = new THREE.GridHelper(400, 30, 0x2C2C2C, 0x888888);
this.scene.add(gridHelper);
},
setPMREMGenerator() {
const pmremGenerator = new THREE.PMREMGenerator(this.renderer);
this.scene.environment = pmremGenerator.fromScene(new RoomEnvironment(this.renderer), 0.04).texture;
},
setLight() {
const ambientLight = new THREE.AmbientLight(0xcccccc, 1.5);
this.scene.add(ambientLight);
const PointLight = new THREE.PointLight(0xffffff, 2.5, 0, 0);
this.scene.add(PointLight);
},
addStatus() {
this.stats = new Stats();
this.container.appendChild(this.stats.dom);
},
setMesh() {
let object = null
const map = new THREE.TextureLoader().load('/textures/uv_grid_opengl.jpg');
map.wrapS = map.wrapT = THREE.RepeatWrapping;
map.anisotropy = 16;
map.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.MeshPhongMaterial({ map: map, side: THREE.DoubleSide });
object = new THREE.Mesh(new THREE.SphereGeometry(75, 20, 10), material);
object.position.set(- 300, 0, 200);
this.scene.add(object);
object = new THREE.Mesh(new THREE.IcosahedronGeometry(75, 1), material);
object.position.set(- 100, 0, 200);
this.scene.add(object);
object = new THREE.Mesh(new THREE.OctahedronGeometry(75, 2), material);
object.position.set(100, 0, 200);
this.scene.add(object);
object = new THREE.Mesh(new THREE.TetrahedronGeometry(75, 0), material);
object.position.set(300, 0, 200);
this.scene.add(object);
object = new THREE.Mesh(new THREE.PlaneGeometry(100, 100, 4, 4), material);
object.position.set(- 300, 0, 0);
this.scene.add(object);
object = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100, 4, 4, 4), material);
object.position.set(- 100, 0, 0);
this.scene.add(object);
object = new THREE.Mesh(new THREE.CircleGeometry(50, 20, 0, Math.PI * 2), material);
object.position.set(100, 0, 0);
this.scene.add(object);
object = new THREE.Mesh(new THREE.RingGeometry(10, 50, 20, 5, 0, Math.PI * 2), material);
object.position.set(300, 0, 0);
this.scene.add(object);
object = new THREE.Mesh(new THREE.CylinderGeometry(25, 75, 100, 40, 5), material);
object.position.set(- 300, 0, - 200);
this.scene.add(object);
const points = [];
for (let i = 0; i < 50; i++) {
points.push(new THREE.Vector2(Math.sin(i * 0.2) * Math.sin(i * 0.1) * 15 + 50, (i - 5) * 2));
}
object = new THREE.Mesh(new THREE.LatheGeometry(points, 20), material);
object.position.set(- 100, 0, - 200);
this.scene.add(object);
object = new THREE.Mesh(new THREE.TorusGeometry(50, 20, 20, 20), material);
object.position.set(100, 0, - 200);
this.scene.add(object);
object = new THREE.Mesh(new THREE.TorusKnotGeometry(50, 10, 50, 20), material);
object.position.set(300, 0, - 200);
console.log('object :>> ', object);
this.scene.add(object);
},
onWindowSize() {
this.width = this.container.clientWidth
this.height = this.container.clientHeight
this.camera.aspect = this.width / this.height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(this.width, this.height);
this.renderer.setPixelRatio(window.devicePixelRatio)
},
animate() {
const timer = Date.now() * 0.0001;
this.camera.position.x = Math.cos(timer) * 800;
this.camera.position.z = Math.sin(timer) * 800;
this.camera.lookAt(this.scene.position);
const delta = this.clock.getDelta();
if (this.mixer) {
this.mixer.update(delta);
}
requestAnimationFrame(this.animate);
this.controller.update(delta);
this.stats.update();
this.renderer.render(this.scene, this.camera);
},
},
};
</script>
<style>
#container {
position: absolute;
width: 100%;
height: 100%;
}
</style>