/
painted.js
230 lines (192 loc) · 6.87 KB
/
painted.js
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
import THREE from '../third_party/three.js';
import orthoVertexShader from '../shaders/ortho.js';
import vignette from '../shaders/vignette.js';
import fxaa from '../shaders/fxaa-pixel-shift.js';
import grayscale from '../shaders/grayscale.js';
import sobel from '../shaders/sobel.js';
import overlay from '../shaders/overlay.js';
import softLight from '../shaders/soft-light.js';
import lighten from '../shaders/lighten.js';
import { levelRange } from '../shaders/levels.js';
import { blur5 } from '../shaders/fast-separable-gaussian-blur.js';
import highPass from '../shaders/high-pass.js';
import ShaderPass from '../modules/shader-pass.js';
const antialiasFragmentShader =
`
precision highp float;
uniform vec2 resolution;
uniform sampler2D inputTexture;
uniform float minLevel;
uniform float maxLevel;
varying vec2 vUv;
${fxaa}
${levelRange}
void main() {
gl_FragColor = vec4(levelRange(fxaa(inputTexture, vUv ).rgb, vec3(minLevel), vec3(maxLevel)),1.);
}
`;
const grayscaleFragmentShader =
`
precision highp float;
uniform vec2 resolution;
uniform sampler2D inputTexture;
uniform vec2 direction;
varying vec2 vUv;
${grayscale}
${blur5}
void main() {
vec4 color = blur5(inputTexture, vUv, resolution, direction);
gl_FragColor = vec4(vec3(grayscale(color)),1.);
}
`;
const edgesFragmentShader =
`
precision highp float;
uniform vec2 resolution;
uniform sampler2D inputTexture;
varying vec2 vUv;
${sobel}
void main() {
gl_FragColor = vec4(sobel(inputTexture, vUv, resolution.x/800.),1.);
}
`;
const fragmentShader =
`
precision highp float;
uniform vec2 resolution;
uniform sampler2D inputTexture;
uniform sampler2D edgesTexture;
uniform sampler2D grayscaleTexture;
uniform float vignetteBoost;
uniform float vignetteReduction;
uniform float lightenPass;
varying vec2 vUv;
${vignette}
${fxaa}
${sobel}
${overlay}
${softLight}
${lighten}
${highPass}
void main() {
vec4 color = texture2D(inputTexture, vUv);
vec4 edges = texture2D(edgesTexture, vUv);
vec4 grayEdges = 1.-texture2D(grayscaleTexture, vUv);
color = overlay(color, edges, .4);
vec4 hp = highPass(inputTexture, vUv);
color = softLight(color, hp);
if(lightenPass==1.) {
color = lighten(color, grayEdges);
}
color = softLight(color, vec4(vec3(vignette(vUv, vignetteBoost, vignetteReduction)),1.));
gl_FragColor = color;
}
`;
function Painted(renderer, params = {}) {
let w = 1;
let h = 1;
const colorFBO = new THREE.WebGLRenderTarget(w, h, {
wrapS: THREE.ClampToEdgeWrapping,
wrapT: THREE.ClampToEdgeWrapping,
minFilter: THREE.LinearFilter,
format: THREE.RGBAFormat,
stencilBuffer: false,
depthBuffer: true
});
const antialiasShader = new THREE.RawShaderMaterial({
uniforms: {
resolution: { value: new THREE.Vector2(w, h) },
inputTexture: { value: colorFBO.texture },
minLevel: { value: 0 },
maxLevel: { value: 1 }
},
vertexShader: orthoVertexShader,
fragmentShader: antialiasFragmentShader,
});
const antialiasPass = new ShaderPass(renderer, antialiasShader, w, h, THREE.RGBAFormat, THREE.UnsignedByteType, THREE
.LinearFilter, THREE.LinearFilter, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping);
const edgesShader = new THREE.RawShaderMaterial({
uniforms: {
resolution: { value: new THREE.Vector2(w, h) },
inputTexture: { value: colorFBO.texture }
},
vertexShader: orthoVertexShader,
fragmentShader: edgesFragmentShader,
});
const edgesPass = new ShaderPass(renderer, edgesShader, w, h, THREE.RGBAFormat, THREE.UnsignedByteType, THREE.LinearFilter,
THREE.LinearFilter, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping);
const blurShader = new THREE.RawShaderMaterial({
uniforms: {
resolution: { value: new THREE.Vector2(w, h) },
direction: { value: new THREE.Vector2(1, 0) },
inputTexture: { value: edgesPass.fbo.texture }
},
vertexShader: orthoVertexShader,
fragmentShader: grayscaleFragmentShader,
});
const blurHPass = new ShaderPass(renderer, blurShader, w, h, THREE.RGBAFormat, THREE.UnsignedByteType, THREE.LinearFilter,
THREE.LinearFilter, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping);
const blurVPass = new ShaderPass(renderer, blurShader, w, h, THREE.RGBAFormat, THREE.UnsignedByteType, THREE.LinearFilter,
THREE.LinearFilter, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping);
const shader = new THREE.RawShaderMaterial({
uniforms: {
resolution: { value: new THREE.Vector2(w, h) },
vignetteBoost: { value: .5 },
vignetteReduction: { value: .5 },
inputTexture: { value: antialiasPass.fbo.texture },
edgesTexture: { value: edgesPass.fbo.texture },
grayscaleTexture: { value: blurVPass.fbo.texture },
lightenPass: { value: params.lightenPass !== undefined ? params.lightenPass : 1 }
},
vertexShader: orthoVertexShader,
fragmentShader: fragmentShader,
});
const pass = new ShaderPass(renderer, shader, w, h, THREE.RGBAFormat, THREE.UnsignedByteType, THREE.LinearFilter,
THREE.LinearFilter, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping);
function render(scene, camera) {
const size = renderer.getSize();
if (size.width !== w || size.height !== h) {
console.log(`Resize ${size.width}, ${size.height}`);
w = size.width;
h = size.height;
colorFBO.setSize(w, h);
antialiasPass.setSize(w, h);
antialiasShader.uniforms.resolution.value.set(w, h);
edgesPass.setSize(w, h);
edgesShader.uniforms.resolution.value.set(w, h);
blurHPass.setSize(w, h);
blurShader.uniforms.resolution.value.set(w, h);
blurVPass.setSize(w, h);
pass.setSize(w, h);
shader.uniforms.resolution.value.set(w, h);
}
renderer.render(scene, camera, colorFBO);
antialiasPass.shader.uniforms.inputTexture.value = colorFBO.texture;
antialiasPass.shader.uniforms.minLevel.value = params.minLevel || .2;
antialiasPass.shader.uniforms.maxLevel.value = params.maxLevel || 1;
antialiasPass.render();
edgesPass.render();
const d = 2 * w / 800;
blurHPass.shader.uniforms.inputTexture.value = edgesPass.fbo.texture;
blurHPass.shader.uniforms.direction.value.set(d, 0);
blurHPass.render();
blurVPass.shader.uniforms.inputTexture.value = blurHPass.fbo.texture;
blurVPass.shader.uniforms.direction.value.set(0, d);
blurVPass.render();
blurHPass.shader.uniforms.inputTexture.value = blurVPass.fbo.texture;
blurHPass.shader.uniforms.direction.value.set(d, 0);
blurHPass.render();
blurVPass.shader.uniforms.inputTexture.value = blurHPass.fbo.texture;
blurVPass.shader.uniforms.direction.value.set(0, d);
blurVPass.render();
pass.render();
antialiasPass.shader.uniforms.minLevel.value = 0;
antialiasPass.shader.uniforms.maxLevel.value = 1;
antialiasPass.shader.uniforms.inputTexture.value = pass.fbo.texture;
antialiasPass.render(true);
}
return {
render
}
}
export default Painted;