-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
116 lines (106 loc) · 3.4 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
AmbientLight,
BoxGeometry,
Color,
DirectionalLight,
Mesh,
MeshLambertMaterial,
Object3D,
PerspectiveCamera,
Scene,
TextureLoader,
WebGLRenderer,
} from 'three';
import { Trail } from '../src';
import TrailParticle from '../src/TrailParticle';
import { CustomTrailMaterial } from './CustomTrailMaterial';
import trail from './textures/trail.png';
import particle from './textures/particle.png';
import { CustomTrailParticleMaterial } from './CustomTrailParticleMaterial';
async function main() {
const canvas = document.getElementById('canvas')!;
const textureLoader = new TextureLoader();
const trailTexture = await textureLoader.loadAsync(trail);
const particleTexture = await textureLoader.loadAsync(particle);
trailTexture.generateMipmaps = false;
particleTexture.generateMipmaps = false;
const scene = new Scene();
scene.background = new Color(0x123456);
const renderer = new WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(devicePixelRatio);
renderer.setSize(innerWidth, innerHeight);
const camera = new PerspectiveCamera(60, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 0, 20);
// const control = new OrbitControls(camera, canvas);
// control.enableDamping = true;
const YAxis = new Object3D();
const ZAxis = new Object3D();
const box = new Mesh(new BoxGeometry(1, 1), new MeshLambertMaterial({ color: 0xffffff }));
const trailBox = new Trail(undefined, new CustomTrailMaterial(trailTexture));
// 或者
// const trailBox = new Trail(
// undefined,
// new TrailMaterial({
// uniforms: { map: { value: trailTexture } },
// vertexShader: CustomTrailMaterial.VERT,
// fragmentShader: CustomTrailMaterial.FRAG,
// }),
// );
const trailLine = new Trail({ time: 0.5 }, new CustomTrailMaterial(trailTexture));
// const trailLine = new Trail({ time: 0.5 });
// trailLine.material.wireframe = true;
const trailParticle = new TrailParticle(
{ size: 1, velocity: 2 },
new CustomTrailParticleMaterial(particleTexture, new Color(0xffc107)),
);
box.position.x = 5;
trailBox.position.x = 5;
trailParticle.position.x = 5;
trailBox.scale.setScalar(0.25);
box.visible = false;
// trailBox.visible = false;
// trailParticle.visible = false;
// trailLine.visible = false;
ZAxis.add(box, trailBox, trailParticle);
YAxis.add(ZAxis);
scene.add(YAxis, camera, trailLine);
scene.add(new AmbientLight(0xffffff, 0.2));
camera.add(new DirectionalLight(0xffffff, 1));
window.addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
enum State {
Doing,
Pending,
}
const speed = 0.04;
let idleCount = 0;
let state = State.Doing;
const renderLoop = () => {
ZAxis.rotation.z += speed;
YAxis.rotation.y += speed * 0.35;
if (state === State.Doing) {
trailLine.position.y += 0.3;
if (trailLine.position.y > 10) {
state = State.Pending;
idleCount = 0;
trailLine.emitting = false;
}
} else {
idleCount++;
if (idleCount > 2000 / 16) {
trailLine.position.y = -10;
trailLine.reset();
state = State.Doing;
trailLine.emitting = true;
}
}
renderer.render(scene, camera);
requestAnimationFrame(renderLoop);
// setTimeout(renderLoop, 256);
};
renderLoop();
}
main();