-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.life.html
234 lines (195 loc) · 6.57 KB
/
index.life.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
<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 LifeInfo {
size: f32,
dummy1: f32,
dummy2: f32,
dummy3: 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_position : vec3<f32>,
@location(1) a_life_info : vec4<f32>,
@location(2) a_vert_pos : vec3<f32>,
@location(3) a_vert_uv : vec2<f32>) -> VertexOutput {
var output : VertexOutput;
output.Position = viewParams.modelViewProjectionMatrix * vec4<f32>((a_position + a_vert_pos * a_life_info.x), 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,
width: f32,
size: f32,
};
struct LifeInfo {
size: f32,
dummy1: f32,
dummy2: f32,
dummy3: f32,
};
struct Instance {
@size(16) position : vec3<f32>,
@size(16) life_info: LifeInfo,
};
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;
fn getLife(pos : u32, count : u32) -> u32 {
if (pos < 0) {
return 0;
}
if (pos >= count) {
return 0;
}
var second = instancesIn.instances[pos];
if (second.life_info.size > 0.0)
{
return 1;
}
return 0;
}
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) {
var time = params.time;
var count : u32 = arrayLength(&instancesIn.instances);
var threadIndex = GlobalInvocationID.x;
if (threadIndex >= count)
{
return;
}
var width : u32 = u32(params.width);
var first = instancesIn.instances[threadIndex];
instancesOut.instances[threadIndex] = first;
var countLifes : u32 = 0u;
countLifes += getLife(threadIndex - 1 - width, count);
countLifes += getLife(threadIndex - 1 , count);
countLifes += getLife(threadIndex - 1 + width, count);
countLifes += getLife(threadIndex + width, count);
countLifes += getLife(threadIndex + 1 + width, count);
countLifes += getLife(threadIndex + 1 , count);
countLifes += getLife(threadIndex + 1 - width, count);
countLifes += getLife(threadIndex - width, count);
if (countLifes <= 1)
{
// die
instancesOut.instances[threadIndex].life_info.size = 0.0;
}
else if (countLifes >= 4)
{
// die of overcrowding
instancesOut.instances[threadIndex].life_info.size = 0.0;
}
else if (countLifes == 3)
{
// new life
instancesOut.instances[threadIndex].life_info.size = params.size;
}
}
</textarea>
<svg id="svg1" width="100" height="100" style="display:none;">
<rect x="0" y="0" width="100" height="100" rx="20" ry="20" stroke-width="1" stroke="green" 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/lifemesh.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(400);
this.engine.draw();
}
async #createScene(count) {
const scene = new Scene();
this.engine.setScene(scene);
const lifeMesh = new LifeMesh(this.engine, count);
lifeMesh.Shader = "draw_wgsl";
lifeMesh.ComputeShader = "calc_wgsl";
await lifeMesh.addSvg("svg1");
scene.addMesh(lifeMesh);
}
}
(async () => await new Runner().run())();
</script>
</body>
</html>