-
Notifications
You must be signed in to change notification settings - Fork 1
/
emmodel001.html
179 lines (148 loc) · 4.28 KB
/
emmodel001.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
<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 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.random(vec3.create()))
M.push(vec3.random(vec3.create()))
}
}
// E[E.length / 2][2] = 1
function get_index(x, y) {
return fmod(y, n) * n + fmod(x, n)
}
function step_physics() {
var dt = 1
function field_div(F) {
var F_div = []
for (var y = 0; y < n; y++) {
for (var x = 0; x < n; x++) {
var sum = 0
sum += vec3.dot(F[get_index(x, y - 1)], vec3.fromValues(0, -1, 0))
sum += vec3.dot(F[get_index(x + 1, y)], vec3.fromValues(1, 0, 0))
sum += vec3.dot(F[get_index(x, y + 1)], vec3.fromValues(0, 1, 0))
sum += vec3.dot(F[get_index(x - 1, y)], vec3.fromValues(-1, 0, 0))
F_div[get_index(x, y)] = sum / 4
}
}
return F_div
}
var f = -dt
var E_div = field_div(E)
for (var y = 0; y < n; y++) {
for (var x = 0; x < n; x++) {
var div = E_div[get_index(x, y)]
vec3.add(E[get_index(x, y - 1)], E[get_index(x, y - 1)], vec3.fromValues(0, div * f, 0))
vec3.add(E[get_index(x + 1, y)], E[get_index(x + 1, y)], vec3.fromValues(div * -f, 0, 0))
vec3.add(E[get_index(x, y + 1)], E[get_index(x, y + 1)], vec3.fromValues(0, div * -f, 0))
vec3.add(E[get_index(x - 1, y)], E[get_index(x - 1, y)], vec3.fromValues(div * f, 0, 0))
}
}
// var f = dt
// for (var y = 0; y < n; y++) {
// for (var x = 0; x < n; x++) {
// vec3.scale(E[get_index(x, y)], E[get_index(x, y)], 0.9)
// }
// }
}
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() {
console.log('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)
}
var stop = false
function loop() {
if (stop) { return }
step_physics()
draw_everything()
setTimeout(loop, 100)
}
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>