-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemmodel002.html
217 lines (174 loc) · 6.06 KB
/
emmodel002.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
<body style="margin:0px">
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.6.1/gl-matrix-min.js"></script>
<script src="https://dglittle.github.io/cdn/utils001.js"></script>
<script>
var tau = Math.PI * 2
var n = 64
var zoom = 4
var c = document.createElement('canvas')
c.width = n
c.height = n
c.style.imageRendering = 'pixelated'
c.style.width = n * zoom
c.style.height = n * zoom
c.style.margin = '4px'
document.body.append(c)
var g = c.getContext('2d')
var cc = document.createElement('canvas')
cc.width = n
cc.height = n
cc.style.imageRendering = 'pixelated'
cc.style.width = n * zoom
cc.style.height = n * zoom
cc.style.margin = '4px'
document.body.append(cc)
var gg = cc.getContext('2d')
var ccc = document.createElement('canvas')
ccc.width = n
ccc.height = n
ccc.style.imageRendering = 'pixelated'
ccc.style.width = n * zoom
ccc.style.height = n * zoom
ccc.style.margin = '4px'
document.body.append(ccc)
var ggg = ccc.getContext('2d')
var img_data = g.createImageData(n, n)
var E = []
var M = []
for (var y = 0; y < n; y++) {
for (var x = 0; x < n; x++) {
E.push(vec3.fromValues(0, Math.sin(y / n * tau * 2) * 0.5, 0))
M.push(vec3.fromValues(0, 0, Math.cos(y / n * tau * 2) * 0.5))
// E.push(vec3.fromValues(0, Math.sin(y / n * tau * 2) * 0.5, 0))
// M.push(vec3.fromValues(Math.cos(y / n * tau * 2) * 0.5, 0, 0))
// E.push(vec3.fromValues(0, Math.sin(y / n * tau * 2) * 0.5, 0))
// M.push(vec3.fromValues(0, Math.sin(x / n * tau * 3) * 0.05, Math.sin(x / n * tau * 3) * 1))
// if (Math.random() < 0.998) {
// E.push(vec3.create())
// M.push(vec3.create())
// } else {
// var v = vec3.random(vec3.create())
// E.push(vec3.scale(vec3.create(), v, 5))
// M.push(vec3.create())
// }
// E.push(vec3.random(vec3.create()))
// M.push(vec3.random(vec3.create()))
}
}
// E[Math.floor(E.length / 2)][2] = 10
function get_index(x, y) {
return fmod(y, n) * n + fmod(x, n)
}
function step_physics() {
function curl_effects_other_field(F1, F2, factor) {
for (var y = 0; y < n; y++) {
for (var x = 0; x < n; x++) {
var c1 = vec3.cross(vec3.create(), F1[get_index(x, y - 1)], vec3.fromValues(0, 1, 0))
var c2 = vec3.cross(vec3.create(), F1[get_index(x, y + 1)], vec3.fromValues(0, -1, 0))
var c3 = vec3.cross(vec3.create(), F1[get_index(x - 1, y)], vec3.fromValues(1, 0, 0))
var c4 = vec3.cross(vec3.create(), F1[get_index(x + 1, y)], vec3.fromValues(-1, 0, 0))
var sq = Math.sqrt(0.5)
var c5 = vec3.cross(vec3.create(), F1[get_index(x - 1, y - 1)], vec3.fromValues(sq, sq, 0))
var c6 = vec3.cross(vec3.create(), F1[get_index(x - 1, y + 1)], vec3.fromValues(sq, -sq, 0))
var c7 = vec3.cross(vec3.create(), F1[get_index(x + 1, y - 1)], vec3.fromValues(-sq, sq, 0))
var c8 = vec3.cross(vec3.create(), F1[get_index(x + 1, y + 1)], vec3.fromValues(-sq, -sq, 0))
var total = vec3.create()
vec3.add(total, c1, c2)
vec3.add(total, total, c3)
vec3.add(total, total, c4)
var scale = 1 / Math.sqrt(2)
vec3.add(total, total, vec3.scale(vec3.create(), c5, scale))
vec3.add(total, total, vec3.scale(vec3.create(), c6, scale))
vec3.add(total, total, vec3.scale(vec3.create(), c7, scale))
vec3.add(total, total, vec3.scale(vec3.create(), c8, scale))
vec3.scale(total, total, factor)
vec3.add(F2[get_index(x, y)], F2[get_index(x, y)], total)
}
}
}
curl_effects_other_field(E, M, 0.03)
curl_effects_other_field(M, E, -0.03)
}
function get_color_for_vec3(v3) {
var a = Math.atan2(v3[1], v3[0])
var a = fmod(a, tau)
var R = 0
var G = 0
var B = 0
if (a < tau/3) {
R = lerp(0, 1, tau/3, 0, a)
G = lerp(0, 0, tau/3, 1, a)
} else if (a < tau*2/3) {
G = lerp(tau/3, 1, tau*2/3, 0, a)
B = lerp(tau/3, 0, tau*2/3, 1, a)
} else {
B = lerp(tau*2/3, 1, tau, 0, a)
R = lerp(tau*2/3, 0, tau, 1, a)
}
if (v3[2] >= 0) {
var fac = (1 - Math.abs(v3[2])) / 2
R = R * fac
G = G * fac
B = B * fac
} else {
var aa = lerp(-1, 0, 0, 1, v3[2])
R = R * aa + (1 - aa)
G = G * aa + (1 - aa)
B = B * aa + (1 - aa)
}
var len = vec3.len(v3)
if (len > 1) len = 1
R *= len
G *= len
B *= len
return [
Math.floor(255 * R),
Math.floor(255 * G),
Math.floor(255 * B),
]
}
function draw_everything() {
for (var i = 0; i < E.length; i++) {
var c = get_color_for_vec3(E[i])
img_data.data[i * 4 + 0] = c[0]
img_data.data[i * 4 + 1] = c[1]
img_data.data[i * 4 + 2] = c[2]
img_data.data[i * 4 + 3] = 255
}
g.putImageData(img_data, 0, 0)
for (var i = 0; i < M.length; i++) {
var c = get_color_for_vec3(M[i])
img_data.data[i * 4 + 0] = c[0]
img_data.data[i * 4 + 1] = c[1]
img_data.data[i * 4 + 2] = c[2]
img_data.data[i * 4 + 3] = 255
}
gg.putImageData(img_data, 0, 0)
for (var i = 0; i < E.length; i++) {
var c = Math.min(255, Math.floor(vec3.length(E[i]) / 3 * 256))
img_data.data[i * 4 + 0] = c
img_data.data[i * 4 + 1] = c
img_data.data[i * 4 + 2] = c
img_data.data[i * 4 + 3] = 255
}
ggg.putImageData(img_data, 0, 0)
}
var stop = false
function loop() {
if (stop) { return }
step_physics()
draw_everything()
setTimeout(loop, 30)
}
loop()
document.addEventListener('keydown', function (e) {
console.log('stop!')
stop = true
})
function fmod(a, b) {
var m = a % b
if (m < 0) return m + b
return m
}
</script>
</body>