File size: 2,230 Bytes
a28eca3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { TempNode } from 'three/webgpu';
import { rand, Fn, fract, time, uv, clamp, mix, vec4, nodeProxy } from 'three/tsl';

/** @module FilmNode **/

/**
 * Post processing node for creating a film grain effect.
 *
 * @augments TempNode
 */
class FilmNode extends TempNode {

	static get type() {

		return 'FilmNode';

	}

	/**
	 * Constructs a new film node.
	 *
	 * @param {Node} inputNode - The node that represents the input of the effect.
	 * @param {Node<float>?} [intensityNode=null] - A node that represents the effect's intensity.
	 * @param {Node<vec2>?} [uvNode=null] - A node that allows to pass custom (e.g. animated) uv data.
	 */
	constructor( inputNode, intensityNode = null, uvNode = null ) {

		super( 'vec4' );

		/**
		 * The node that represents the input of the effect.
		 *
		 * @type {Node}
		 */
		this.inputNode = inputNode;

		/**
		 * A node that represents the effect's intensity.
		 *
		 * @type {Node<float>}
		 */
		this.intensityNode = intensityNode;

		/**
		 * A node that allows to pass custom (e.g. animated) uv data.
		 *
		 * @type {Node<vec2>}
		 */
		this.uvNode = uvNode;

	}

	/**
	 * This method is used to setup the effect's TSL code.
	 *
	 * @param {NodeBuilder} builder - The current node builder.
	 * @return {ShaderCallNodeInternal}
	 */
	setup( /* builder */ ) {

		const uvNode = this.uvNode || uv();

		const film = Fn( () => {

			const base = this.inputNode.rgb;
			const noise = rand( fract( uvNode.add( time ) ) );

			let color = base.add( base.mul( clamp( noise.add( 0.1 ), 0, 1 ) ) );

			if ( this.intensityNode !== null ) {

				color = mix( base, color, this.intensityNode );

			}

			return vec4( color, this.inputNode.a );

		} );

		const outputNode = film();

		return outputNode;

	}

}

export default FilmNode;

/**
 * TSL function for creating a film node for post processing.
 *
 * @function
 * @param {Node<vec4>} inputNode - The node that represents the input of the effect.
 * @param {Node<float>?} [intensityNode=null] - A node that represents the effect's intensity.
 * @param {Node<vec2>?} [uvNode=null] - A node that allows to pass custom (e.g. animated) uv data.
 * @returns {FilmNode}
 */
export const film = /*@__PURE__*/ nodeProxy( FilmNode );