forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussianBlurMaterial.js
More file actions
162 lines (116 loc) · 2.99 KB
/
GaussianBlurMaterial.js
File metadata and controls
162 lines (116 loc) · 2.99 KB
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
import { NoBlending, ShaderMaterial, Uniform, Vector2 } from "three";
import { GaussKernel } from "../core/index.js";
import { updateFragmentShader } from "../utils/index.js";
import fragmentShader from "./glsl/convolution.gaussian.frag";
import vertexShader from "./glsl/convolution.gaussian.vert";
/**
* An optimized Gaussian convolution shader material.
*
* References:
*
* Filip Strugar, Intel, 2014: [An investigation of fast real-time GPU-based image blur algorithms](
* https://www.intel.com/content/www/us/en/developer/articles/technical/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms.html)
*
* @implements {Resizable}
*/
export class GaussianBlurMaterial extends ShaderMaterial {
/**
* Constructs a new convolution material.
*
* @param {Object} [options] - The options.
* @param {Number} [options.kernelSize=35] - The kernel size.
*/
constructor({ kernelSize = 35 } = {}) {
super({
name: "GaussianBlurMaterial",
uniforms: {
inputBuffer: new Uniform(null),
texelSize: new Uniform(new Vector2()),
direction: new Uniform(new Vector2()),
kernel: new Uniform(null),
scale: new Uniform(1.0)
},
blending: NoBlending,
toneMapped: false,
depthWrite: false,
depthTest: false,
fragmentShader,
vertexShader
});
this.fragmentShader = updateFragmentShader(this.fragmentShader);
/**
* @see {@link kernelSize}
* @type {Number}
* @private
*/
this._kernelSize = 0;
this.kernelSize = kernelSize;
}
/**
* The input buffer.
*
* @type {Texture}
*/
set inputBuffer(value) {
this.uniforms.inputBuffer.value = value;
}
/**
* The kernel size.
*
* @type {Number}
*/
get kernelSize() {
return this._kernelSize;
}
set kernelSize(value) {
this._kernelSize = value;
this.generateKernel(value);
}
/**
* The blur direction.
*
* @type {Vector2}
*/
get direction() {
return this.uniforms.direction.value;
}
/**
* The blur kernel scale. Values greater than 1.0 may introduce artifacts.
*
* @type {Number}
*/
get scale() {
return this.uniforms.scale.value;
}
set scale(value) {
this.uniforms.scale.value = value;
}
/**
* Generates the Gauss kernel.
*
* @param {KernelSize} kernelSize - The kernel size. Should be an odd number.
* @private
*/
generateKernel(kernelSize) {
const kernel = new GaussKernel(kernelSize);
const steps = kernel.linearSteps;
// Store offsets and weights as vec2 instances to minimize the uniform count.
const kernelData = new Float64Array(steps * 2);
for(let i = 0, j = 0; i < steps; ++i) {
kernelData[j++] = kernel.linearOffsets[i];
kernelData[j++] = kernel.linearWeights[i];
}
this.uniforms.kernel.value = kernelData;
this.defines.STEPS = steps.toFixed(0);
this.needsUpdate = true;
}
/**
* Sets the size of this object.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
this.uniforms.texelSize.value.set(1.0 / width, 1.0 / height);
}
}