Repository navigation
Expand file tree
/
Copy pathSampleNode.js
More file actions
97 lines (75 loc) · 2.02 KB
/
Copy pathSampleNode.js
File metadata and controls
97 lines (75 loc) · 2.02 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
import Node from '../core/Node.js';
import { uv } from '../accessors/UV.js';
import { nodeObject } from '../tsl/TSLCore.js';
/**
* Class representing a node that samples a value using a provided callback function.
*
* @extends Node
*/
class SampleNode extends Node {
/**
* Returns the type of the node.
*
* @type {string}
* @readonly
* @static
*/
static get type() {
return 'SampleNode';
}
/**
* Creates an instance of SampleNode.
*
* @param {Function} callback - The function to be called when sampling. Should accept a UV node and return a value.
* @param {?Node<vec2>} [uvNode=null] - The UV node to be used in the texture sampling.
*/
constructor( callback, uvNode = null ) {
super();
this.callback = callback;
/**
* Represents the texture coordinates.
*
* @type {?Node<vec2|vec3>}
* @default null
*/
this.uvNode = uvNode;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isSampleNode = true;
}
isCacheable( /*builder*/ ) {
return false;
}
/**
* Sets up the node by sampling with the default UV accessor.
*
* @returns {Node} The result of the callback function when called with the UV node.
*/
setup() {
return this.sample( uv() );
}
/**
* Calls the callback function with the provided UV node.
*
* @param {Node<vec2>} uv - The UV node or value to be passed to the callback.
* @returns {Node} The result of the callback function.
*/
sample( uv ) {
return this.callback( uv );
}
}
export default SampleNode;
/**
* Helper function to create a SampleNode wrapped as a node object.
*
* @function
* @param {Function} callback - The function to be called when sampling. Should accept a UV node and return a value.
* @param {?Node<vec2>} [uv=null] - The UV node to be used in the texture sampling.
* @returns {SampleNode} The created SampleNode instance wrapped as a node object.
*/
export const sample = ( callback, uv = null ) => new SampleNode( callback, nodeObject( uv ) );