-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
145 lines (145 loc) · 6.05 KB
/
app.js
File metadata and controls
145 lines (145 loc) · 6.05 KB
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
///<reference path="./node_modules/@types/three/index.d.ts"/>
///<reference path="./node_modules/@types/dat-gui/index.d.ts"/>
class ThreeJSTest {
constructor() {
this.screenWidth = 640;
this.screenHeight = 480;
this.num = 10000;
this.theta = new Array(this.num);
this.rand = new Array(this.num);
this.createRenderer();
this.createScene();
}
createRenderer() {
// dat.GUI
var gui = new dat.GUI({ autoPlace: false, width: 256 });
var guielement = document.createElement("div");
guielement.id = "dat-gui";
guielement.appendChild(gui.domElement);
document.getElementById("viewport").appendChild(guielement);
this.controls = new GuiControl();
// var gui = new dat.GUI();
gui.add(this.controls, 'transparent').onChange((e) => {
this.material.transparent = e;
});
gui.add(this.controls, 'opacity', 0, 1).onChange((e) => {
this.material.opacity = e;
});
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(this.screenWidth, this.screenHeight);
this.renderer.setClearColor(new THREE.Color(0x000000));
document.getElementById("viewport").appendChild(this.renderer.domElement);
}
createScene() {
this.scene = new THREE.Scene();
for (var i = 0; i < this.theta.length; i++) {
this.theta[i] = this.random(0.1, 0.5);
this.rand[i] = this.random(0.01, 0.05);
}
this.geometry = new THREE.BoxGeometry(1, 1, 1);
this.cube = new THREE.Mesh(this.geometry, this.material);
//this.scene.add(this.cube);
this.camera = new THREE.PerspectiveCamera(75, this.screenWidth /
this.screenHeight, 0.1, 1000);
this.camera.position.x = 30;
this.camera.position.y = 30;
this.camera.position.z = 30;
this.camera.lookAt(new THREE.Vector3(0, 0, 0));
this.orbitControl = new THREE.OrbitControls(this.camera, this.renderer.domElement);
this.light = new THREE.DirectionalLight(0xffffff);
var lvec = new THREE.Vector3(1, 1, 1).normalize();
this.light.position.set(lvec.x, lvec.y, lvec.z);
this.scene.add(this.light);
this.createParticle();
}
random(min, max) {
return Math.random() * (max + 1 - min) + min;
}
createParticle() {
var textureLoader = new THREE.TextureLoader();
var texture = textureLoader.load('particles.png');
//Geometry
var geo = new THREE.Geometry();
//Material
this.material = new THREE.PointsMaterial({ size: 10, map: texture, blending: THREE.AdditiveBlending, color: 0xffffff,
depthWrite: false, transparent: true });
//particle
var max = 200;
var min = -200;
this.pvelocity = [];
for (var x = 0; x < this.num; x++) {
var X = Math.random() * (max + 1 - min) + min;
X = Math.random() * (25 + 1 + 25) - 25;
var Y = Math.cos(Math.random() * (max + 1 - min) + min);
var Z = Math.sin(Math.random() * (max + 1 - min) + min);
Z = Math.random() * (25 + 1 + 25) - 25;
if (Math.pow(X, 2) + Math.pow(Z, 2) < 25 * 25) {
this.pvelocity.push(new THREE.Vector3(this.random(0.1, 0.3), this.random(0.1, 0.3), this.random(0.1, 0.3)));
var particle = new THREE.Vector3(X, Y, Z);
geo.vertices.push(particle);
geo.colors.push(new THREE.Color(Math.random() * 0x00ffff));
}
/*
this.pvelocity.push(new THREE.Vector3(this.random(0.1,0.3),this.random(0.1,0.3),this.random(0.1,0.3)));
var particle = new THREE.Vector3(X,Y,Z);
geo.vertices.push(particle);
geo.colors.push(new THREE.Color(Math.random() * 0x00ffff));
*/
}
//THREE.points
this.cloud = new THREE.Points(geo, this.material);
//Scene
this.scene.add(this.cloud);
}
reCreateParticle() {
var geo = new THREE.Geometry();
var particleNum = this.controls.particleNum, transparent = this.controls.transparent, opacity = this.controls.opacity;
this.material = new THREE.PointsMaterial({ size: 4, vertexColors: THREE.VertexColors, opacity: opacity, transparent: transparent });
var n = Math.sqrt(particleNum);
var max = 100;
var min = -100;
for (var x = 0; x < n; x++) {
for (var y = 0; y < n; y++) {
var X = Math.random() * (max + 1 - min) + min;
var Y = Math.random() * (max + 1 - min) + min;
var Z = Math.random() * (max + 1 - min) + min;
var particle = new THREE.Vector3(X, Y, Z);
geo.vertices.push(particle);
geo.colors.push(new THREE.Color(Math.random() * 0x00ffff));
}
}
this.cloud = new THREE.Points(geo, this.material);
this.scene.add(this.cloud);
}
render() {
var geo = this.cloud.geometry;
var vertices = geo.vertices;
for (var i = 0; i < vertices.length; i++) {
if (200 < vertices[i].y) {
vertices[i].y = 0;
}
else {
vertices[i].y = vertices[i].y + this.pvelocity[i].y;
vertices[i].x += 2 * Math.cos(this.theta[i]);
vertices[i].z += -2 * Math.sin(this.theta[i]);
this.theta[i] += this.rand[i];
}
}
geo.verticesNeedUpdate = true;
this.orbitControl.update();
this.renderer.render(this.scene, this.camera);
requestAnimationFrame(this.render.bind(this));
}
}
class GuiControl {
constructor() {
this.transparent = false;
this.opacity = 1;
this.particleNum = 100;
}
}
window.onload = () => {
var threeJSTest = new ThreeJSTest();
threeJSTest.render();
};
//# sourceMappingURL=app.js.map