forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridEffect.js
More file actions
172 lines (122 loc) · 2.75 KB
/
GridEffect.js
File metadata and controls
172 lines (122 loc) · 2.75 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
163
164
165
166
167
168
169
170
171
172
import { Uniform, Vector2 } from "three";
import { BlendFunction } from "../enums/index.js";
import { Effect } from "./Effect.js";
import fragmentShader from "./glsl/grid.frag";
/**
* A grid effect.
*/
export class GridEffect extends Effect {
/**
* Constructs a new grid effect.
*
* @param {Object} [options] - The options.
* @param {BlendFunction} [options.blendFunction=BlendFunction.OVERLAY] - The blend function of this effect.
* @param {Number} [options.scale=1.0] - The scale of the grid pattern.
* @param {Number} [options.lineWidth=0.0] - The line width of the grid pattern.
*/
constructor({ blendFunction = BlendFunction.OVERLAY, scale = 1.0, lineWidth = 0.0 } = {}) {
super("GridEffect", fragmentShader, {
blendFunction,
uniforms: new Map([
["scale", new Uniform(new Vector2())],
["lineWidth", new Uniform(lineWidth)]
])
});
/**
* The original resolution.
*
* @type {Vector2}
* @private
*/
this.resolution = new Vector2();
/**
* The grid scale, relative to the screen height.
*
* @type {Number}
* @private
*/
this.s = 0;
this.scale = scale;
/**
* The grid line width.
*
* @type {Number}
* @private
*/
this.l = 0;
this.lineWidth = lineWidth;
}
/**
* The scale.
*
* @type {Number}
*/
get scale() {
return this.s;
}
set scale(value) {
this.s = Math.max(value, 1e-6);
this.setSize(this.resolution.width, this.resolution.height);
}
/**
* Returns the current grid scale.
*
* @deprecated Use scale instead.
* @return {Number} The grid scale.
*/
getScale() {
return this.scale;
}
/**
* Sets the grid scale.
*
* @deprecated Use scale instead.
* @param {Number} value - The new grid scale.
*/
setScale(value) {
this.scale = value;
}
/**
* The line width.
*
* @type {Number}
*/
get lineWidth() {
return this.l;
}
set lineWidth(value) {
this.l = value;
this.setSize(this.resolution.width, this.resolution.height);
}
/**
* Returns the current grid line width.
*
* @deprecated Use lineWidth instead.
* @return {Number} The grid line width.
*/
getLineWidth() {
return this.lineWidth;
}
/**
* Sets the grid line width.
*
* @deprecated Use lineWidth instead.
* @param {Number} value - The new grid line width.
*/
setLineWidth(value) {
this.lineWidth = value;
}
/**
* Updates the size of this pass.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
this.resolution.set(width, height);
const aspect = width / height;
const scale = this.scale * (height * 0.125);
this.uniforms.get("scale").value.set(aspect * scale, scale);
this.uniforms.get("lineWidth").value = (scale / height) + this.lineWidth;
}
}