-
Notifications
You must be signed in to change notification settings - Fork 0
/
blurEffect.sj
111 lines (92 loc) · 4.17 KB
/
blurEffect.sj
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
blurHorizontalShader : shader("shaders/v3f-t2f.vert", "shaders/blur-horizontal.frag")
blurVerticalShader : shader("shaders/v3f-t2f.vert", "shaders/blur-vertical.frag")
blurEffect #effect (
radius := 0.0f
_rect := rect()
_vertexBuffer1 := empty'boxVertexBuffer
_vertexBuffer2 := empty'boxVertexBuffer
_scenebuffer1 := empty'scenebuffer
_scenebuffer2 := empty'scenebuffer
_innerScene : scene2d()
getRect() { _rect }
setRect(rect_ : 'rect, cb : '(:rect)void) {
if _rect != rect_ {
_rect = rect_
ifValid t1 : _scenebuffer1 {
if t1.size != size(_rect.w, _rect.h) {
_scenebuffer1 = empty'scenebuffer
_vertexBuffer1 = empty'boxVertexBuffer
}
}
ifValid t2 : _scenebuffer2 {
if t2.size != size(_rect.w, _rect.h) {
_scenebuffer2 = empty'scenebuffer
}
}
_vertexBuffer2 = empty'boxVertexBuffer
}
if radius == 0.0f {
cb(_rect)
} else {
cb(rect(0, 0, _rect.w, _rect.h))
}
void
}
render(scene : 'scene2d, cb : '(:scene2d)void) {
if radius == 0.0f {
cb(scene)
} else {
if isEmpty(_vertexBuffer1) {
_vertexBuffer1 = valid(boxVertexBuffer(
rect : rect(0, 0, _rect.w, _rect.h)
))
void
}
if isEmpty(_vertexBuffer2) {
_vertexBuffer2 = valid(boxVertexBuffer(
rect : _rect
))
void
}
if isEmpty(_scenebuffer1) {
_scenebuffer1 = valid(scenebuffer(size(_rect.w, _rect.h)))
}
if isEmpty(_scenebuffer2) {
_scenebuffer2 = valid(scenebuffer(size(_rect.w, _rect.h)))
}
ifValid f1 : _scenebuffer1, f2 : _scenebuffer2, v1 : _vertexBuffer1, v2 : _vertexBuffer2 {
glPushFramebuffer(f1.framebuffer)
_innerScene.setSize(f1.size)
_innerScene.start()
cb(_innerScene)
_innerScene.end()
glPopFramebuffer(f1.framebuffer)
glPushFramebuffer(f2.framebuffer)
_innerScene.setSize(f2.size)
_innerScene.start()
glBindTexture(glTexture.GL_TEXTURE_2D, f1.texture)
glUseProgram(blurVerticalShader)
glBlendFunc(glBlendFuncType.GL_ONE, glBlendFuncType.GL_ONE_MINUS_SRC_ALPHA)
glUniformI32(glGetUniformLocation(blurVerticalShader, "texture"), 0)
glUniformF32(glGetUniformLocation(blurVerticalShader, "sigma"), radius)
glUniformF32(glGetUniformLocation(blurVerticalShader, "blurSize"), 1.0f / f1.size.h as f32)
glUniformMat4(glGetUniformLocation(blurVerticalShader, "model"), _innerScene.model)
glUniformMat4(glGetUniformLocation(blurVerticalShader, "view"), _innerScene.view)
glUniformMat4(glGetUniformLocation(blurVerticalShader, "projection"), _innerScene.projection)
v1.render(_innerScene)
_innerScene.end()
glPopFramebuffer(f2.framebuffer)
glBindTexture(glTexture.GL_TEXTURE_2D, f2.texture)
glUseProgram(blurHorizontalShader)
glBlendFunc(glBlendFuncType.GL_ONE, glBlendFuncType.GL_ONE_MINUS_SRC_ALPHA)
glUniformI32(glGetUniformLocation(blurHorizontalShader, "texture"), 0)
glUniformF32(glGetUniformLocation(blurHorizontalShader, "sigma"), radius)
glUniformF32(glGetUniformLocation(blurHorizontalShader, "blurSize"), 1.0f / f2.size.w as f32)
glUniformMat4(glGetUniformLocation(blurHorizontalShader, "model"), scene.model)
glUniformMat4(glGetUniformLocation(blurHorizontalShader, "view"), scene.view)
glUniformMat4(glGetUniformLocation(blurHorizontalShader, "projection"), scene.projection)
v2.render(scene)
}
}
}
) { this }