Skip to content

Commit de0f9c8

Browse files
committed
Added Effects.
Counts towards #82.
1 parent 3e43f10 commit de0f9c8

31 files changed

Lines changed: 2522 additions & 2 deletions

‎src/effects/BloomEffect.js‎

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
import {
2+
LinearFilter,
3+
RGBFormat,
4+
Uniform,
5+
Vector2,
6+
WebGLRenderTarget
7+
} from "three";
8+
9+
import { KernelSize, LuminanceMaterial } from "../materials";
10+
import { BlurPass, ShaderPass } from "../passes";
11+
import { BlendFunction } from "./blending/BlendFunction.js";
12+
import { Effect, EffectType } from "./Effect.js";
13+
14+
import fragment from "./glsl/texture/shader.frag";
15+
16+
/**
17+
* A bloom pass.
18+
*
19+
* This pass uses the fast Kawase convolution technique and a luminance filter
20+
* to blur bright highlights.
21+
*/
22+
23+
export class BloomEffect extends Effect {
24+
25+
/**
26+
* Constructs a new bloom pass.
27+
*
28+
* @param {Object} [options] - The options.
29+
* @param {BlendFunction} [options.blendFunction=BlendFunction.SCREEN] - The blend function of this effect.
30+
* @param {Number} [options.resolutionScale=0.5] - The render texture resolution scale, relative to the screen render size.
31+
* @param {Number} [options.kernelSize=KernelSize.LARGE] - The blur kernel size.
32+
* @param {Number} [options.distinction=1.0] - The luminance distinction factor. Raise this value to bring out the brighter elements in the scene.
33+
*/
34+
35+
constructor(options = {}) {
36+
37+
const settings = Object.assign({
38+
blendFunction: BlendFunction.SCREEN,
39+
resolutionScale: 0.5,
40+
kernelSize: KernelSize.LARGE,
41+
distinction: 1.0
42+
}, options);
43+
44+
super("BloomEffect", fragment, {
45+
46+
type: EffectType.CONVOLUTION,
47+
blendFunction: settings.blendFunction,
48+
49+
uniforms: new Map([
50+
["texture", new Uniform(null)]
51+
])
52+
53+
});
54+
55+
/**
56+
* A render target.
57+
*
58+
* @type {WebGLRenderTarget}
59+
* @private
60+
*/
61+
62+
this.renderTarget = new WebGLRenderTarget(1, 1, {
63+
minFilter: LinearFilter,
64+
magFilter: LinearFilter,
65+
stencilBuffer: false,
66+
depthBuffer: false
67+
});
68+
69+
this.renderTarget.texture.name = "Bloom.Target";
70+
this.renderTarget.texture.generateMipmaps = false;
71+
72+
this.uniforms.get("texture").value = this.renderTarget.texture;
73+
74+
/**
75+
* The original resolution.
76+
*
77+
* @type {Vector2}
78+
* @private
79+
*/
80+
81+
this.resolution = new Vector2();
82+
83+
/**
84+
* A blur pass.
85+
*
86+
* @type {BlurPass}
87+
* @private
88+
*/
89+
90+
this.blurPass = new BlurPass(settings);
91+
92+
/**
93+
* A luminance shader pass.
94+
*
95+
* @type {ShaderPass}
96+
* @private
97+
*/
98+
99+
this.luminancePass = new ShaderPass(new LuminanceMaterial(true));
100+
101+
this.distinction = settings.distinction;
102+
this.kernelSize = settings.kernelSize;
103+
104+
}
105+
106+
/**
107+
* Indicates whether dithering is enabled.
108+
*
109+
* @type {Boolean}
110+
*/
111+
112+
get dithering() {
113+
114+
return this.blurPass.dithering;
115+
116+
}
117+
118+
/**
119+
* Enables or disables dithering.
120+
*
121+
* @type {Boolean}
122+
*/
123+
124+
set dithering(value) {
125+
126+
this.blurPass.dithering = value;
127+
128+
}
129+
130+
/**
131+
* The blur kernel size.
132+
*
133+
* @type {KernelSize}
134+
*/
135+
136+
get kernelSize() {
137+
138+
return this.blurPass.kernelSize;
139+
140+
}
141+
142+
/**
143+
* @type {KernelSize}
144+
*/
145+
146+
set kernelSize(value = KernelSize.LARGE) {
147+
148+
this.blurPass.kernelSize = value;
149+
150+
}
151+
152+
/**
153+
* The luminance distinction factor.
154+
*
155+
* @type {Number}
156+
*/
157+
158+
get distinction() {
159+
160+
return this.luminancePass.getFullscreenMaterial().uniforms.distinction.value;
161+
162+
}
163+
164+
/**
165+
* @type {Number}
166+
*/
167+
168+
set distinction(value = 1.0) {
169+
170+
this.luminancePass.getFullscreenMaterial().uniforms.distinction.value = value;
171+
172+
}
173+
174+
/**
175+
* A texture that contains the intermediate result of this effect.
176+
*
177+
* This texture will be applied to the scene colors unless the blend function
178+
* is set to `SKIP`.
179+
*
180+
* @type {Texture}
181+
*/
182+
183+
get texture() {
184+
185+
return this.renderTarget.texture;
186+
187+
}
188+
189+
/**
190+
* Returns the current resolution scale.
191+
*
192+
* @return {Number} The resolution scale.
193+
*/
194+
195+
getResolutionScale() {
196+
197+
return this.blurPass.getResolutionScale();
198+
199+
}
200+
201+
/**
202+
* Sets the resolution scale.
203+
*
204+
* @param {Number} scale - The new resolution scale.
205+
*/
206+
207+
setResolutionScale(scale) {
208+
209+
this.blurPass.setResolutionScale(scale);
210+
this.setSize(this.resolution.x, this.resolution.y);
211+
212+
}
213+
214+
/**
215+
* Updates this effect.
216+
*
217+
* @param {WebGLRenderer} renderer - The renderer.
218+
* @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass.
219+
* @param {Number} [delta] - The time between the last frame and the current one in seconds.
220+
*/
221+
222+
update(renderer, inputBuffer, delta) {
223+
224+
const renderTarget = this.renderTarget;
225+
226+
this.luminancePass.render(renderer, inputBuffer, renderTarget);
227+
this.blurPass.render(renderer, renderTarget, renderTarget);
228+
229+
}
230+
231+
/**
232+
* Updates the size of internal render targets.
233+
*
234+
* @param {Number} width - The width.
235+
* @param {Number} height - The height.
236+
*/
237+
238+
setSize(width, height) {
239+
240+
// Remember the original resolution.
241+
this.resolution.set(width, height);
242+
243+
this.blurPass.setSize(width, height);
244+
245+
// The blur pass applies the resolution scale.
246+
width = this.blurPass.width;
247+
height = this.blurPass.height;
248+
249+
this.renderTarget.setSize(width, height);
250+
251+
}
252+
253+
/**
254+
* Performs initialization tasks.
255+
*
256+
* @param {WebGLRenderer} renderer - The renderer.
257+
* @param {Boolean} alpha - Whether the renderer uses the alpha channel or not.
258+
*/
259+
260+
initialize(renderer, alpha) {
261+
262+
this.blurPass.initialize(renderer, alpha);
263+
264+
if(!alpha) {
265+
266+
this.renderTarget.texture.format = RGBFormat;
267+
268+
}
269+
270+
}
271+
272+
}

‎src/effects/BokehEffect.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Uniform } from "three";
2+
import { Effect, EffectType } from "./Effect.js";
3+
import { BlendFunction } from "./blending/BlendFunction.js";
4+
5+
import fragment from "./glsl/bokeh/shader.frag";
6+
7+
/**
8+
* A depth of field (bokeh) shader effect.
9+
*
10+
* Original shader code by Martins Upitis:
11+
* http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
12+
*/
13+
14+
export class BokehEffect extends Effect {
15+
16+
/**
17+
* Constructs a new bokeh effect.
18+
*
19+
* @param {PerspectiveCamera} [camera] - A camera.
20+
* @param {Object} [options] - The options.
21+
* @param {Number} [options.focus=1.0] - The focus distance, corresponds directly with the scene depth.
22+
* @param {Number} [options.dof=0.02] - Depth of field. An area in front of and behind the focus point that still appears sharp.
23+
* @param {Number} [options.aperture=0.025] - Camera aperture scale. Bigger values for stronger blur and shallower depth of field.
24+
* @param {Number} [options.maxBlur=1.0] - The maximum blur strength.
25+
*/
26+
27+
constructor(camera, options = {}) {
28+
29+
const settings = Object.assign({
30+
focus: 1.0,
31+
dof: 0.02,
32+
aperture: 0.025,
33+
maxBlur: 1.0
34+
}, options);
35+
36+
super("BokehEffect", fragment, {
37+
38+
type: EffectType.CONVOLUTION,
39+
blendFunction: BlendFunction.NORMAL,
40+
41+
uniforms: new Map([
42+
["focus", new Uniform(settings.focus)],
43+
["dof", new Uniform(settings.dof)],
44+
["aperture", new Uniform(settings.aperture)],
45+
["maxBlur", new Uniform(settings.maxBlur)]
46+
])
47+
48+
});
49+
50+
}
51+
52+
}

‎src/effects/ColorAverageEffect.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { BlendFunction } from "./blending/BlendFunction.js";
2+
import { Effect } from "./Effect.js";
3+
4+
import fragment from "./glsl/color-average/shader.frag";
5+
6+
/**
7+
* A color average effect.
8+
*/
9+
10+
export class ColorAverageEffect extends Effect {
11+
12+
/**
13+
* Constructs a new color average effect.
14+
*
15+
* @param {BlendFunction} [blendFunction=BlendFunction.NORMAL] - The blend function of this effect.
16+
*/
17+
18+
constructor(blendFunction = BlendFunction.NORMAL) {
19+
20+
super("ColorAverageEffect", fragment, { blendFunction });
21+
22+
}
23+
24+
}

0 commit comments

Comments
 (0)