-
Notifications
You must be signed in to change notification settings - Fork 12
/
simulation.ts
247 lines (185 loc) · 6.9 KB
/
simulation.ts
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
export const updateAlphaBeta = `
precision highp float;
uniform sampler2D material;
uniform float dt;
uniform float cellSize;
varying vec2 uv;
void main() {
// Get material values
vec3 mat = texture2D(material, uv).rgb;
float permeability = mat.x;
float permittivity = mat.y;
float conductivity = mat.z;
// Calculate alpha and beta for electric field
float cEl = conductivity * dt / (2.0 * permeability);
float dEl = 1.0 / (1.0 + cEl);
float alphaEl = (1.0 - cEl) * dEl;
float betaEl = dt / (permeability * cellSize) * dEl;
// Calculate alpha and beta for magnetic field
float cMag = conductivity * dt / (2.0 * permittivity);
float dMag = 1.0 / (1.0 + cMag);
float alphaMag = (1.0 - cMag) * dMag;
float betaMag = dt / (permittivity * cellSize) * dMag;
gl_FragColor = vec4(alphaEl, betaEl, alphaMag, betaMag);
}
`
export const updateElectric = `
precision highp float;
uniform sampler2D electricField;
uniform sampler2D magneticField;
uniform sampler2D alphaBetaField;
uniform vec2 relativeCellSize;
uniform bool reflectiveBoundary;
varying vec2 uv;
void main() {
if (!reflectiveBoundary) {
vec2 b = 2.0 * relativeCellSize;
float xAtMinBound = uv.x < b.x ? relativeCellSize.x : 0.0;
float xAtMaxBound = uv.x + b.x >= 1.0 ? -relativeCellSize.x : 0.0;
float yAtMinBound = uv.y < b.y ? relativeCellSize.y : 0.0;
float yAtMaxBound = uv.y + b.y >= 1.0 ? -relativeCellSize.y : 0.0;
if (xAtMinBound != 0.0 || xAtMaxBound != 0.0 || yAtMinBound != 0.0 || yAtMaxBound != 0.0) {
gl_FragColor = texture2D(electricField, vec2(
uv.x + xAtMinBound + xAtMaxBound,
uv.y + yAtMinBound + yAtMaxBound
));
return;
}
}
vec2 alphaBeta = texture2D(alphaBetaField, uv).rg;
vec3 el = texture2D(electricField, uv).rgb;
vec3 mag = texture2D(magneticField, uv).rgb;
vec3 magXN = texture2D(magneticField, uv - vec2(relativeCellSize.x, 0.0)).rgb;
vec3 magYN = texture2D(magneticField, uv - vec2(0.0, relativeCellSize.y)).rgb;
vec3 newEl = vec3(
// d_Y Z - d_Z Y, but d_Z = 0 in 2d
alphaBeta.x * el.x + alphaBeta.y * (mag.z - magYN.z),
// d_Z X - d_X Z, but d_Z = 0 in 2d
alphaBeta.x * el.y + alphaBeta.y * (magXN.z - mag.z),
// d_X Y - d_Y X
alphaBeta.x * el.z + alphaBeta.y * ((mag.y - magXN.y) - (mag.x - magYN.x))
);
gl_FragColor = vec4(newEl, 0.0);
}
`
export const updateMagnetic = `
precision highp float;
uniform sampler2D electricField;
uniform sampler2D magneticField;
uniform sampler2D alphaBetaField;
uniform vec2 relativeCellSize;
uniform bool reflectiveBoundary;
varying vec2 uv;
void main() {
if (!reflectiveBoundary) {
vec2 b = 2.0 * relativeCellSize;
float xAtMinBound = uv.x < b.x ? relativeCellSize.x : 0.0;
float xAtMaxBound = uv.x + b.x >= 1.0 ? -relativeCellSize.x : 0.0;
float yAtMinBound = uv.y < b.y ? relativeCellSize.y : 0.0;
float yAtMaxBound = uv.y + b.y >= 1.0 ? -relativeCellSize.y : 0.0;
if (xAtMinBound != 0.0 || xAtMaxBound != 0.0 || yAtMinBound != 0.0 || yAtMaxBound != 0.0) {
gl_FragColor = texture2D(magneticField, vec2(
uv.x + xAtMinBound + xAtMaxBound,
uv.y + yAtMinBound + yAtMaxBound
));
return;
}
}
vec2 alphaBeta = texture2D(alphaBetaField, uv).ba;
vec3 mag = texture2D(magneticField, uv).rgb;
vec3 el = texture2D(electricField, uv).rgb;
vec3 elXP = texture2D(electricField, uv + vec2(relativeCellSize.x, 0.0)).rgb;
vec3 elYP = texture2D(electricField, uv + vec2(0.0, relativeCellSize.y)).rgb;
vec3 newMag = vec3(
// d_Y Z - d_Z Y, but d_Z = 0 in 2d
alphaBeta.x * mag.x - alphaBeta.y * (elYP.z - el.z),
// d_Z X - d_X Z, but d_Z = 0 in 2d
alphaBeta.x * mag.y - alphaBeta.y * (el.z - elXP.z),
// d_X Y - d_Y X
alphaBeta.x * mag.z - alphaBeta.y * ((elXP.y - el.y) - (elYP.x - el.x))
);
gl_FragColor = vec4(newMag, 0.0);
}
`
export const injectSource = `
precision highp float;
uniform sampler2D sourceField;
uniform sampler2D field;
uniform float dt;
varying vec2 uv;
void main() {
vec4 source = texture2D(sourceField, uv);
vec4 field = texture2D(field, uv);
gl_FragColor = field + dt * source;
}
`
export const decaySource = `
precision highp float;
uniform sampler2D sourceField;
uniform float dt;
varying vec2 uv;
void main() {
vec4 source = texture2D(sourceField, uv);
vec4 decayedSource = source * pow(0.1, dt);
gl_FragColor = decayedSource;
}
`
export const drawSquare = `
precision highp float;
uniform sampler2D texture;
uniform vec2 pos;
uniform vec4 value;
uniform vec2 size;
uniform vec4 keep;
varying vec2 uv;
void main() {
vec2 d = abs(pos.xy - uv.xy);
vec4 oldValue = texture2D(texture, uv);
bool within = all(lessThanEqual(d, size));
gl_FragColor = within ? value + keep * oldValue : oldValue;
}
`
export const drawEllipse = `
precision highp float;
uniform sampler2D texture;
uniform vec2 pos;
uniform vec4 value;
uniform vec2 radius;
uniform vec4 keep;
varying vec2 uv;
void main() {
// Calculate distance squared
vec2 d = pos.xy - uv.xy;
// Check if distance is within ellipse
d = d / radius;
d = d * d;
bool within = d.x + d.y <= 1.0;
vec4 oldValue = texture2D(texture, uv);
gl_FragColor = within ? value + keep * oldValue : oldValue;
}
`
export const vert = `
precision highp float;
attribute vec2 position;
varying vec2 uv;
void main() {
uv = 0.5 * (position + 1.0);
gl_Position = vec4(position, 0, 1);
}
`
export const copyUint8ToFloat16 = `
precision highp float;
uniform sampler2D texture;
varying vec2 uv;
void main() {
gl_FragColor = (-128.0 + 255.0 * texture2D(texture, uv)) / 4.0;
}
`
export const copyFloat16ToUint8 = `
precision highp float;
uniform sampler2D texture;
varying vec2 uv;
void main() {
gl_FragColor = (128.0 + 4.0 * texture2D(texture, uv)) / 256.0;
}
`