-
Notifications
You must be signed in to change notification settings - Fork 12
/
rendering.ts
264 lines (234 loc) · 7.57 KB
/
rendering.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import * as k from "./kernels/rendering"
import REGL, { Regl, Framebuffer2D } from 'regl'
export function createReglFromCanvas(canvas: HTMLCanvasElement) {
return REGL({
canvas: canvas,
extensions: [
"OES_texture_half_float",
],
optionalExtensions: [
"OES_texture_half_float_linear",
// Shows a warning without this. However making it
// non-optional would make it crash on iPad 6 even
// though it works.
"EXT_color_buffer_half_float"
]
})
}
export function makeRenderSimulatorCanvas(regl: Regl, canvasSize: [number, number], gridSize: [number, number]) {
const filter = regl.hasExtension("OES_texture_half_float_linear") ? "linear" : "nearest"
const frameBuffers: Framebuffer2D[] = []
const makeFrameBuffer = () => {
// Create half-precision texture at canvas size
const fbo = regl.framebuffer({
color: regl.texture({
width: canvasSize[0],
height: canvasSize[1],
wrap: "clamp",
type: "float16",
format: "rgba",
min: filter,
mag: filter
}),
depthStencil: false
})
// Keep track of all frame buffers so we can
// resize them when the canvas size changes.
frameBuffers.push(fbo)
return fbo
}
const fbos = [makeFrameBuffer(), makeFrameBuffer()]
const energyFbo = makeFrameBuffer()
const backgroundFbo = makeFrameBuffer()
type RenderBackgroundUniforms = {
gridSize: [number, number]
}
const renderBackground = regl<RenderBackgroundUniforms, {}, RenderBackgroundUniforms>({
frag: k.renderBackground,
framebuffer: backgroundFbo,
uniforms: {
gridSize: (_, { gridSize }) => gridSize
},
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vertDraw,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
type RenderEnergyUniforms = {
brightness: number
electricField: Framebuffer2D
magneticField: Framebuffer2D
material: Framebuffer2D
electricEnergyFactor: number
magneticEnergyFactor: number
}
type RenderEnergyAttributes = {
position: number[][]
}
const renderEnergy = regl<RenderEnergyUniforms, RenderEnergyAttributes, RenderEnergyUniforms>({
frag: k.renderEnergy,
framebuffer: energyFbo,
uniforms: {
brightness: (_, { brightness }) => brightness,
electricField: (_, { electricField }) => electricField,
magneticField: (_, { magneticField }) => magneticField,
material: (_, { material }) => material,
electricEnergyFactor: (_, { electricEnergyFactor }) => electricEnergyFactor,
magneticEnergyFactor: (_, { magneticEnergyFactor }) => magneticEnergyFactor,
},
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vertDraw,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
type BloomExtractUniforms = {
texture: Framebuffer2D
threshold: number
}
const bloomExtract = regl<BloomExtractUniforms, {}, BloomExtractUniforms>({
frag: k.bloomExtract,
framebuffer: fbos[0],
uniforms: {
texture: () => energyFbo,
threshold: (_, { threshold }) => threshold,
},
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vertDraw,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
const blurVert = regl<{ texture: Framebuffer2D, direction: [number, number] }, {}, { texture: Framebuffer2D }>({
frag: k.blurDirectional,
framebuffer: fbos[1],
uniforms: {
texture: (_, { texture }) => texture,
direction: ctx => [1 / ctx.drawingBufferHeight, 0]
},
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vertDraw,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
const blurHor = regl({
frag: k.blurDirectional,
framebuffer: fbos[0],
uniforms: {
texture: fbos[1],
direction: ctx => [0, 1 / ctx.drawingBufferHeight]
},
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vertDraw,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
type DrawUniforms = {
energyTexture: Framebuffer2D
bloomTexture: Framebuffer2D
materialTexture: Framebuffer2D
backgroundTexture: Framebuffer2D
}
const draw = regl<DrawUniforms, {}, DrawUniforms>({
frag: k.draw,
uniforms: {
energyTexture: (_, { energyTexture }) => energyTexture,
bloomTexture: (_, { bloomTexture }) => bloomTexture,
materialTexture: (_, { materialTexture }) => materialTexture,
backgroundTexture: (_, { backgroundTexture }) => backgroundTexture,
},
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vertDraw,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
function render(electricField: Framebuffer2D, magneticField: Framebuffer2D,
material: Framebuffer2D, cellSize: number, gridSize: [number, number],
showElectric: boolean, showMagnetic: boolean) {
renderEnergy({
brightness: 0.02 * 0.02 / (cellSize * cellSize),
electricField: electricField,
magneticField: magneticField,
material: material,
electricEnergyFactor: showElectric ? 1 : 0,
magneticEnergyFactor: showMagnetic ? 1 : 0
})
bloomExtract({
threshold: 1
})
const blurCount = 3
for (let i = 0; i < blurCount; i++) {
blurVert({
texture: i === 0 ? energyFbo : fbos[0]
})
blurHor()
}
draw({
energyTexture: energyFbo,
bloomTexture: fbos[0],
materialTexture: material,
backgroundTexture: backgroundFbo
})
}
renderBackground({
gridSize: gridSize
})
return {
render: render,
adjustCanvasSize: (size: [number, number]) => {
frameBuffers.forEach(fbo => fbo.resize(size[0], size[1]))
regl.poll()
},
adjustGridSize: (gridSize: [number, number]) => {
renderBackground({
gridSize: gridSize
})
}
}
}