1- import { Uniform , Vector3 } from "three" ;
1+ import { Uniform , Vector2 } from "three" ;
22import { HalftoneShape } from "../enums/HalftoneShape.js" ;
3- import { LinearDodgeBlendFunction } from "./blending/blend-functions/LinearDodgeBlendFunction .js" ;
3+ import { ColorDodgeBlendFunction } from "./blending/blend-functions/ColorDodgeBlendFunction .js" ;
44import { Effect } from "./Effect.js" ;
55
66import fragmentShader from "./shaders/halftone.frag" ;
@@ -30,63 +30,52 @@ export interface HalftoneEffectOptions {
3030 radius ?: number ;
3131
3232 /**
33- * The grid rotation for all color channels in radians.
33+ * The grid rotation in radians.
3434 *
35- * This setting yields better performance compared to individual rotations per channel.
36- *
37- * @defaultValue 0
35+ * @defaultValue 0.7513
3836 */
3937
4038 rotation ?: number ;
4139
4240 /**
43- * The grid rotation for the red channel in radians .
41+ * The sample count .
4442 *
45- * @defaultValue { @link rotation }
43+ * @defaultValue 6
4644 */
4745
48- rotationR ?: number ;
46+ samples ?: number ;
4947
5048 /**
51- * The grid rotation for the green channel in radians .
49+ * A threshold bias .
5250 *
53- * @defaultValue { @link rotationR }
51+ * @defaultValue 0.0
5452 */
5553
56- rotationG ?: number ;
54+ bias ?: number ;
5755
5856 /**
59- * The grid rotation for the blue channel in radians .
57+ * Whether the halftone pattern should be premultiplied with the input color .
6058 *
61- * @defaultValue { @link rotationG }
59+ * @defaultValue true
6260 */
6361
64- rotationB ?: number ;
62+ premultiply ?: boolean ;
6563
6664 /**
67- * The halftone scatter factor .
65+ * Whether the halftone thresholding should be inverted .
6866 *
69- * @defaultValue 0
67+ * @defaultValue false
7068 */
7169
72- scatterFactor ?: number ;
73-
74- /**
75- * The sample count.
76- *
77- * @defaultValue 8
78- */
79-
80- samples ?: number ;
70+ inverted ?: boolean ;
8171
8272}
8373
8474/**
8575 * A halftone effect.
8676 *
8777 * @category Effects
88- * @see https://github.com/meatbags/after-effects-plugins/blob/master/Halftone/HalftoneSampler.hpp
89- * @see https://github.com/mrdoob/three.js/blob/0bf3908b73b2cf73d7361cce17cfc8b816cb2a00/examples/jsm/postprocessing/HalftonePass.js
78+ * @see https://github.com/mrange/shader-advent-2024/blob/main/day-22/README.md
9079 */
9180
9281export class HalftoneEffect extends Effect implements HalftoneEffectOptions {
@@ -97,6 +86,12 @@ export class HalftoneEffect extends Effect implements HalftoneEffectOptions {
9786
9887 private _radius : number ;
9988
89+ /**
90+ * @see {@link rotation }
91+ */
92+
93+ private _rotation : number ;
94+
10095 /**
10196 * Constructs a new halftone effect.
10297 *
@@ -106,29 +101,31 @@ export class HalftoneEffect extends Effect implements HalftoneEffectOptions {
106101 constructor ( {
107102 shape = HalftoneShape . DOT ,
108103 radius = 6.0 ,
109- rotation = 0 ,
110- rotationR = rotation ,
111- rotationG = rotationR ,
112- rotationB = rotationG ,
113- scatterFactor = 0 ,
114- samples = 8
104+ rotation = 0.7513 ,
105+ samples = 6 ,
106+ bias = 0.0 ,
107+ premultiply = false ,
108+ inverted = false
115109 } : HalftoneEffectOptions = { } ) {
116110
117111 super ( "HalftoneEffect" ) ;
118112
119113 this . fragmentShader = fragmentShader ;
120- this . blendMode . blendFunction = new LinearDodgeBlendFunction ( ) ;
114+ this . blendMode . blendFunction = new ColorDodgeBlendFunction ( ) ;
121115
122116 const uniforms = this . input . uniforms ;
123- uniforms . set ( "radius " , new Uniform ( 1.0 ) ) ;
124- uniforms . set ( "rotationRGB " , new Uniform ( new Vector3 ( rotationR , rotationG , rotationB ) ) ) ;
125- uniforms . set ( "scatterFactor " , new Uniform ( scatterFactor ) ) ;
117+ uniforms . set ( "invRadius " , new Uniform ( 1.0 ) ) ;
118+ uniforms . set ( "rotation " , new Uniform ( new Vector2 ( ) ) ) ;
119+ uniforms . set ( "bias " , new Uniform ( bias ) ) ;
126120
127121 this . _radius = radius ;
122+ this . _rotation = rotation ;
123+
128124 this . shape = shape ;
125+ this . rotation = rotation ;
129126 this . samples = samples ;
130-
131- this . updateRGBRotation ( ) ;
127+ this . premultiply = premultiply ;
128+ this . inverted = inverted ;
132129
133130 }
134131
@@ -157,7 +154,7 @@ export class HalftoneEffect extends Effect implements HalftoneEffectOptions {
157154
158155 this . input . defines . set ( "SAMPLES" , value ) ;
159156 this . input . defines . set ( "INV_SAMPLES" , ( 1.0 / value ) . toFixed ( 9 ) ) ;
160- this . input . defines . set ( "INV_SAMPLES_PLUS_ONE " , ( 1.0 / ( value + 1.0 ) ) . toFixed ( 9 ) ) ;
157+ this . input . defines . set ( "INV_SAMPLES_SQ " , ( 1.0 / ( value * value ) ) . toFixed ( 9 ) ) ;
161158 this . setChanged ( ) ;
162159
163160 }
@@ -175,96 +172,77 @@ export class HalftoneEffect extends Effect implements HalftoneEffectOptions {
175172
176173 }
177174
178- get scatterFactor ( ) {
175+ get rotation ( ) {
179176
180- return this . input . uniforms . get ( "scatterFactor" ) ! . value as number ;
181-
182- }
183-
184- set scatterFactor ( value : number ) {
185-
186- this . input . uniforms . get ( "scatterFactor" ) ! . value = value ;
177+ return this . _rotation ;
187178
188179 }
189180
190181 set rotation ( value : number ) {
191182
192- const rotationRGB = this . input . uniforms . get ( "rotationRGB " ) ! . value as Vector3 ;
193- rotationRGB . setScalar ( value ) ;
194- this . updateRGBRotation ( ) ;
183+ const rotation = this . input . uniforms . get ( "rotation " ) ! . value as Vector2 ;
184+ rotation . set ( Math . sin ( value ) , Math . cos ( value ) ) ;
185+ this . _rotation = value ;
195186
196187 }
197188
198- get rotationR ( ) {
189+ get bias ( ) {
199190
200- const rotationRGB = this . input . uniforms . get ( "rotationRGB" ) ! . value as Vector3 ;
201- return rotationRGB . x ;
191+ return this . input . uniforms . get ( "bias" ) ! . value as number ;
202192
203193 }
204194
205- set rotationR ( value : number ) {
195+ set bias ( value : number ) {
206196
207- const rotationRGB = this . input . uniforms . get ( "rotationRGB" ) ! . value as Vector3 ;
208- rotationRGB . x = value ;
209- this . updateRGBRotation ( ) ;
197+ this . input . uniforms . get ( "bias" ) ! . value = value ;
210198
211199 }
212200
213- get rotationG ( ) {
201+ get premultiply ( ) : boolean {
214202
215- const rotationRGB = this . input . uniforms . get ( "rotationRGB" ) ! . value as Vector3 ;
216- return rotationRGB . y ;
203+ return this . input . defines . has ( "PREMULTIPLY" ) ;
217204
218205 }
219206
220- set rotationG ( value : number ) {
207+ set premultiply ( value : boolean ) {
221208
222- const rotationRGB = this . input . uniforms . get ( "rotationRGB" ) ! . value as Vector3 ;
223- rotationRGB . y = value ;
224- this . updateRGBRotation ( ) ;
209+ if ( this . premultiply !== value ) {
225210
226- }
211+ if ( value ) {
227212
228- get rotationB ( ) {
213+ this . input . defines . set ( "PREMULTIPLY" , true ) ;
229214
230- const rotationRGB = this . input . uniforms . get ( "rotationRGB" ) ! . value as Vector3 ;
231- return rotationRGB . z ;
215+ } else {
232216
233- }
217+ this . input . defines . delete ( "PREMULTIPLY" ) ;
234218
235- set rotationB ( value : number ) {
219+ }
236220
237- const rotationRGB = this . input . uniforms . get ( "rotationRGB" ) ! . value as Vector3 ;
238- rotationRGB . z = value ;
239- this . updateRGBRotation ( ) ;
221+ this . setChanged ( ) ;
222+
223+ }
240224
241225 }
242226
243- /**
244- * Enables or disables RGB rotation based on the current rotation settings.
245- */
227+ get inverted ( ) : boolean {
246228
247- private updateRGBRotation ( ) {
229+ return this . input . defines . has ( "INVERTED" ) ;
248230
249- const currentlyEnabled = this . input . defines . has ( "RGB_ROTATION" ) ;
231+ }
250232
251- const shouldBeEnabled = (
252- this . rotationR !== this . rotationG ||
253- this . rotationR !== this . rotationB ||
254- this . rotationG !== this . rotationB
255- ) ;
233+ set inverted ( value : boolean ) {
256234
257- if ( shouldBeEnabled ) {
235+ if ( this . inverted !== value ) {
258236
259- this . input . defines . set ( "RGB_ROTATION" , true ) ;
237+ if ( value ) {
260238
261- } else {
239+ this . input . defines . set ( "INVERTED" , true ) ;
262240
263- this . input . defines . delete ( "RGB_ROTATION" ) ;
241+ } else {
264242
265- }
243+ this . input . defines . delete ( "INVERTED" ) ;
266244
267- if ( currentlyEnabled !== shouldBeEnabled ) {
245+ }
268246
269247 this . setChanged ( ) ;
270248
@@ -275,7 +253,7 @@ export class HalftoneEffect extends Effect implements HalftoneEffectOptions {
275253 protected override onResolutionChange ( ) : void {
276254
277255 const r = this . radius * this . resolution . scaledPixelRatio ;
278- this . input . uniforms . get ( "radius " ) ! . value = Math . max ( r , 1e-9 ) ;
256+ this . input . uniforms . get ( "invRadius " ) ! . value = 1.0 / Math . max ( r , 1e-9 ) ;
279257
280258 }
281259
0 commit comments