forked from miluluyo/cute-cnblogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.js
104 lines (83 loc) · 2.37 KB
/
star.js
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
var scene,
camera,
renderer,
container,
aspect,
fov,
plane,
far,
mouseX,
mouseY,
windowHalfX,
windowHalfY,
geometry,
starStuff,
materialOptions,
stars;
init();
animate();
$("#showstar").find("canvas").attr("style","width:100%;height:40vh;position:absolute;top:0;left:0;z-index:1")
function init() {
container = document.createElement(`div`);
container.setAttribute("id","showstar");
document.body.appendChild(container);
mouseX = 0;
mouseY = 0;
aspect = window.innerWidth / window.innerHeight;
fov = 40;
plane = 1;
far = 800;
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera = new THREE.PerspectiveCamera(
fov,
aspect,
plane,
far
);
camera.position.z = far / 2;
scene = new THREE.Scene({ antialias: true });
scene.fog = new THREE.FogExp2(0x1b1b1b, 0.0001);
starForge();
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio((window.devicePixelRatio) ? window.devicePixelRatio : 1);
renderer.setSize(window.innerWidth, parseFloat($("#blogTitle").css("height")));
renderer.autoClear = false;
renderer.setClearColor(0x000000, 0.0);
container.appendChild(renderer.domElement);
document.addEventListener('mousemove', onMouseMove, false);
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
camera.position.x += (mouseX - camera.position.x) * 0.005;
camera.position.y += (-mouseY - camera.position.y) * 0.005;
camera.lookAt(scene.position);
renderer.render(scene, camera);
}
function starForge() {
var amount = 45000;
geometry = new THREE.SphereGeometry(1000, 100, 50);
materialOptions = {
color: new THREE.Color(0xffffff),
size: 1.1,
transparency: true,
opacity: 0.8
};
starStuff = new THREE.PointsMaterial(materialOptions);
for (var i = 0; i < amount; i++) {
var item = new THREE.Vector3();
item.x = Math.random() * 2000 - 1000;
item.y = Math.random() * 2000 - 1000;
item.z = Math.random() * 2000 - 1000;
geometry.vertices.push(item);
}
stars = new THREE.PointCloud(geometry, starStuff);
scene.add(stars);
}
function onMouseMove(e) {
mouseX = e.clientX - windowHalfX;
mouseY = e.clientY - windowHalfY;
}