-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
296 lines (246 loc) · 9.54 KB
/
index.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<html>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
background-color:cadetblue;
}
#DEBUG {
position: absolute;
z-index: 1;
width: 820px;
top: 10px;
left: 10px;
color: beige;
background-color:cadetblue;
}
textarea {
display: none;
}
#tbldbg {
width: 800px;
color: blue;
}
td {
width: 200px;
overflow-x:hidden;
}
</style>
</head>
<body>
<textarea id="draw_wgsl">
struct ViewParams {
modelViewProjectionMatrix : mat4x4<f32>,
};
struct MassSize {
mass: f32,
size: f32,
};
struct VertexOutput {
@builtin(position) Position : vec4<f32>,
@location(0) fragUV : vec2<f32>,
};
@binding(0) @group(0) var currentSampler: sampler;
@binding(1) @group(0) var currentTexture: texture_2d<f32>;
@binding(0) @group(1) var<uniform> viewParams : ViewParams;
@vertex
fn vert_main(
@location(0) a_force : vec3<f32>,
@location(1) a_position : vec3<f32>,
@location(2) a_velocity : vec3<f32>,
@location(3) a_mass_size : vec2<f32>,
@location(4) a_vert_pos : vec3<f32>,
@location(5) a_vert_uv : vec2<f32>) -> VertexOutput {
var output : VertexOutput;
output.Position = viewParams.modelViewProjectionMatrix * vec4<f32>((a_position + a_vert_pos * a_mass_size.y), 1.0);
output.fragUV = a_vert_uv;
return output;
}
@fragment
fn frag_main(@location(0) fragUV: vec2<f32>) -> @location(0) vec4<f32> {
let texColor = textureSample(currentTexture, currentSampler, fragUV);
return vec4<f32>(texColor.r, texColor.g, texColor.b, 1.0);
}
</textarea>
<textarea id="calc_wgsl">
struct SimParams {
time: f32,
};
struct MassSize {
mass: f32,
size: f32,
debug1: f32,
debug2: f32
};
struct Instance {
@size(16) force: vec3<f32>,
@size(16) position : vec3<f32>,
@size(16) velocity : vec3<f32>,
@size(16) mass_size: MassSize,
};
struct Instances {
instances : array<Instance>,
};
@binding(0) @group(0) var<storage, read> instancesIn : Instances;
@binding(1) @group(0) var<storage, read_write> instancesOut : Instances;
@binding(0) @group(1) var<uniform> params : SimParams;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) {
var G = 6.67408313131E-11;
var time = params.time;
var count : u32 = arrayLength(&instancesIn.instances);
var threadIndex = GlobalInvocationID.x;
if (threadIndex >= count)
{
return;
}
var first = instancesIn.instances[threadIndex];
var m1 = first.mass_size.mass;
if (m1 <= 0.0)
{
// no calculation required
// clone data, otherwise record in second buffer becomes dity (has stail values)
instancesOut.instances[threadIndex] = first;
return;
}
var forceVector = vec3<f32>(0.0, 0.0, 0.0);
// calculate result force on all objects/planets
for (var j : u32 = 0u; j < count; j=j+1u)
{
if (threadIndex == j)
{
continue;
}
var second = instancesIn.instances[j];
var m2 = second.mass_size.mass;
if (m2 <= 0.0)
{
// skip no mass object
continue;
}
var dist = distance(first.position, second.position);
var distanceSquared = dist * dist;
if (distanceSquared > 0.0)
{
var force = G * m1 * m2 / distanceSquared;
var directionVector = normalize((second.position - first.position));
forceVector = forceVector + (directionVector * force);
}
}
// calculate new position depends on force
var a = forceVector / m1;
var v = a * time;
var s = v * time / 2.0;
var newPosition = first.position + (first.velocity * time) + s;
instancesOut.instances[threadIndex].force = forceVector;
instancesOut.instances[threadIndex].mass_size = first.mass_size;
instancesOut.instances[threadIndex].mass_size.mass = m1;
instancesOut.instances[threadIndex].position = newPosition;
instancesOut.instances[threadIndex].velocity = first.velocity + v;
// calculate collisions
for(var j : u32 = 0u; j < count; j=j+1u)
{
if (threadIndex == j)
{
continue;
}
var second = instancesIn.instances[j];
var m2 = second.mass_size.mass;
if (m2 <= 0.0)
{
// skip no mass object
continue;
}
// calculate collision
var b = distance(first.position, second.position);
var c = distance(newPosition, second.position);
var radius = (first.mass_size.size + second.mass_size.size) / 2.0;
var id1 = threadIndex; //first.id;
var id2 = j; //second.id;
if (b < radius || c < radius) {
if (m1 > m2 || (m1 == m2 && id1 > id2)) {
// fix speed v = (m1v1 + m2v2) / (m1 + m2)
var v1 = first.velocity;
var v2 = second.velocity;
var vresult = (v1 * m1 + v2 * m2) / (m1 + m2);
var s1 = first.mass_size.size;
var s2 = second.mass_size.size;
var newR = pow((s1 * s1 * s1 + s2 * s2 * s2), 1.0 / 3.0);
// mark collision
instancesOut.instances[threadIndex].velocity = vresult;
instancesOut.instances[threadIndex].mass_size.mass = m1 + m2;
instancesOut.instances[threadIndex].mass_size.size = newR;
instancesOut.instances[threadIndex].mass_size.debug1 = -1.0;
instancesOut.instances[threadIndex].mass_size.debug2 = f32(j);
} else {
// collision from other side
instancesOut.instances[threadIndex].mass_size.mass = 0.0;
instancesOut.instances[threadIndex].mass_size.size = 0.0;
instancesOut.instances[threadIndex].velocity = vec3<f32>(0.0, 0.0, 0.0);
instancesOut.instances[threadIndex].force = vec3<f32>(0.0, 0.0, 0.0);
instancesOut.instances[threadIndex].mass_size.debug1 = -3.0;
instancesOut.instances[threadIndex].mass_size.debug2 = f32(j);
}
// no more calculations after collision
break;
}
}
}
</textarea>
<svg id="svg1" width="100" height="100" style="display:none;">
<circle cx="50" cy="50" r="45" stroke="red" stroke-width="4" fill="yellow" />
</svg>
<canvas id="renderCanvas"></canvas>
<div id="DEBUG"></div>
<script src="src/common.js"></script>
<script src="src/vector.js"></script>
<script src="src/matrix.js"></script>
<script src="src/data.js"></script>
<script src="src/mesh.js"></script>
<script src="src/scene.js"></script>
<script src="src/view.js"></script>
<script src="src/engine.js"></script>
<script src="src/custom/gravitymesh.js"></script>
<script>
class Runner {
constructor() {
this.engine = new Engine();
}
async run() {
await this.engine.init();
await this.#runLogic();
}
async #runLogic() {
// 300 000 - crash, 100 000 - very slow
// 35 000 speed is fine
//await this.#createScene(350);
await this.#createScene(1350);
this.engine.draw();
}
async #createScene(count) {
const scene = new Scene();
this.engine.setScene(scene);
const gravityMesh = new GravityMesh(this.engine, count);
gravityMesh.Shader = "draw_wgsl";
gravityMesh.ComputeShader = "calc_wgsl";
await gravityMesh.addSvg("svg1");
scene.addMesh(gravityMesh);
}
}
(async () => await new Runner().run())();
</script>
</body>
</html>