-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
177 lines (146 loc) · 4.65 KB
/
index.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import "file-loader?name=[name].[ext]!./src/html/index.html";
import {
Scene,
OrthographicCamera,
WebGLRenderer,
Mesh,
DataTexture,
RGBAFormat,
FloatType,
PlaneBufferGeometry,
ShaderMaterial,
Vector2,
} from "three";
import PingpongRenderTarget from "./src/PingpongRenderTarget";
import RenderTarget from "./src/RenderTarget";
import dat from "dat.gui";
import Controls from "./src/Controls";
// 0 configure scene
//////////////////////////////////////
let w = window.innerWidth;
let h = window.innerHeight;
const renderer = new WebGLRenderer({
alpha: true,
});
document.body.appendChild(renderer.domElement);
renderer.setSize(w, h);
const scene = new Scene();
const camera = new OrthographicCamera(-w / 2, w / 2, h / 2, -h / 2, 0.1, 100);
camera.position.z = 1;
// 1 init buffers
//////////////////////////////////////
let size = 512; // particles amount = ( size ^ 2 )
let count = size * size;
let pos = new Float32Array(count * 3);
let uvs = new Float32Array(count * 2);
let ptexdata = new Float32Array(count * 4);
let id = 0,
u,
v;
for (let i = 0; i < count; i++) {
//point cloud vertex
id = i * 3;
pos[id++] = pos[id++] = pos[id++] = 0;
//computes the uvs
u = (i % size) / size;
v = ~~(i / size) / size;
id = i * 2;
uvs[id++] = u;
uvs[id] = v;
//particle texture values (agents)
id = i * 4;
ptexdata[id++] = Math.random(); // normalized pos x
ptexdata[id++] = Math.random(); // normalized pos y
ptexdata[id++] = Math.random(); // normalized angle
ptexdata[id++] = 1;
}
// 2 data & trails
//////////////////////////////////////
//performs the diffusion and decay
let diffuse_decay = new ShaderMaterial({
uniforms: {
points: { value: null },
decay: { value: 0.9 },
},
vertexShader: require("./src/glsl/quad_vs.glsl"),
fragmentShader: require("./src/glsl/diffuse_decay_fs.glsl"),
});
let trails = new PingpongRenderTarget(w, h, diffuse_decay);
// 3 agents
//////////////////////////////////////
//moves agents around
let update_agents = new ShaderMaterial({
uniforms: {
data: { value: null },
sa: { value: 2 },
ra: { value: 4 },
so: { value: 12 },
ss: { value: 1.1 },
},
vertexShader: require("./src/glsl/quad_vs.glsl"),
fragmentShader: require("./src/glsl/update_agents_fs.glsl"),
});
let agents = new PingpongRenderTarget(size, size, update_agents, ptexdata);
// 4 point cloud
//////////////////////////////////////
//renders the updated agents as red dots
let render_agents = new ShaderMaterial({
vertexShader: require("./src/glsl/render_agents_vs.glsl"),
fragmentShader: require("./src/glsl/render_agents_fs.glsl"),
});
let render = new RenderTarget(w, h, render_agents, pos, uvs);
// 5 post process
//////////////////////////////////////
//post process the result of the trails (render the trails as greyscale)
let postprocess = new ShaderMaterial({
uniforms: {
data: {
value: null,
},
},
vertexShader: require("./src/glsl/quad_vs.glsl"),
fragmentShader: require("./src/glsl/postprocess_fs.glsl"),
});
let postprocess_mesh = new Mesh(new PlaneBufferGeometry(), postprocess);
postprocess_mesh.scale.set(w, h, 1);
scene.add(postprocess_mesh);
// 6 interactive controls
//////////////////////////////////////
let controls = new Controls(renderer, agents);
controls.count = ~~(size * size * 0.05);
// animation loop
//////////////////////////////////////
function raf() {
requestAnimationFrame(raf);
time = (Date.now() - start) * 0.001;
trails.material.uniforms.points.value = render.texture;
trails.render(renderer, time);
agents.material.uniforms.data.value = trails.texture;
agents.render(renderer, time);
render.material.uniforms.input_texture.value = agents.texture;
render.render(renderer, time);
postprocess_mesh.material.uniforms.data.value = trails.texture;
renderer.setSize(w, h);
renderer.clear();
renderer.render(scene, camera);
}
//////////////////////////////////////////////////
let materials = [diffuse_decay, update_agents, render_agents];
let resolution = new Vector2(w, h);
materials.forEach((mat) => {
mat.uniforms.resolution.value = resolution;
});
let start = Date.now();
let time = 0;
raf();
// settings
//////////////////////////////////////////////////
let gui = new dat.GUI();
gui.add(diffuse_decay.uniforms.decay, "value", 0.01, 0.99, 0.01).name("decay");
gui.add(update_agents.uniforms.sa, "value", 1, 90, 0.1).name("sa");
gui.add(update_agents.uniforms.ra, "value", 1, 90, 0.1).name("ra");
gui.add(update_agents.uniforms.so, "value", 1, 90, 0.1).name("so");
gui.add(update_agents.uniforms.ss, "value", 0.1, 10, 0.1).name("ss");
gui.add(controls, "random");
gui.add(controls, "radius", 0.001, 0.25);
gui.add(controls, "count", 1, size * size, 1);