-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.html
147 lines (123 loc) · 4.87 KB
/
test.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body style="margin: 0;">
<div id="fps">0</div>
<div id="logo">izLab</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// fps
const fps = document.getElementById('fps');
// 创建场景
const scene = new THREE.Scene();
// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建星星粒子
const starGeometry = new THREE.BufferGeometry();
const starMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 1, transparent: true, opacity: 0.8 });
// 初始化星星位置
const starVertices = [];
const starOpacity = [];
for (let i = 0; i < 80000; i++) {
const x = THREE.MathUtils.randFloatSpread(4000);
const y = THREE.MathUtils.randFloatSpread(3000);
const z = THREE.MathUtils.randFloatSpread(6000);
starVertices.push(x, y, z);
starOpacity.push(Math.random()); // 随机初始透明度
}
starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
starGeometry.setAttribute('opacity', new THREE.Float32BufferAttribute(starOpacity, 1));
const stars = new THREE.Points(starGeometry, starMaterial);
scene.add(stars);
let render_fps = 0;
let rev = false;
let moving = false;
function animate() {
const positions = starGeometry.attributes.position.array;
const opacities = starGeometry.attributes.opacity.array;
requestAnimationFrame(animate);
if(moving) return;
render_fps ++;
if (camera.position.z < -1000)
rev = true;
if (camera.position.z > 0)
rev = false;
for (let i = 0; i < opacities.length; i++) {
opacities[i] = Math.sin(performance.now() / 500 + positions[i * 3] * 0.01) * 0.5 + 0.5;
}
starGeometry.attributes.opacity.needsUpdate = true;
camera.position.z += rev ? 1 : -1;
stars.rotation.x += rev ? 0.001 : -0.001;
stars.rotation.y += rev ? 0.002 : -0.002;
renderer.render(scene, camera);
}
setInterval(function(){
fps.innerHTML = render_fps;
render_fps = 0;
}, 1000);
// 调整窗口大小时更新渲染器和相机
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
// 开始动画
animate();
// 监测鼠标事件
document.addEventListener('pointerdown', function(e){
function handleMove(e){
moving = true; render_fps ++;
stars.rotation.x += (e.clientX - window.innerWidth / 2) * 0.0001;
stars.rotation.y += (e.clientY - window.innerHeight / 2) * 0.0001;
renderer.render(scene, camera);
}
document.addEventListener('pointermove', handleMove);
document.addEventListener('pointerup', () => {
document.removeEventListener('pointermove', handleMove);
moving = false
});
});
document.addEventListener('wheel', e => stars.rotation.z += e.deltaY * 0.0001)
</script>
<style>
canvas{
width: 100%;
height: 100%;
z-index: -1;
}
#fps{
font-size: 20px;
position: fixed;
top: 1rem;
left: 1rem;
color: white;
}
#fps::after{
content: ' fps';
color: gray;
}
#logo{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1;
color: transparent;
font-size: 60px;
letter-spacing: 5px;
background-image: linear-gradient(45deg, #dedede96, #e6ffe8, #bebebe82);
background-clip: text;
font-weight: 600;
}
</style>
</body>
</html>