forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRangeNode.js
172 lines (122 loc) · 4.5 KB
/
RangeNode.js
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 Node from '../core/Node.js';
import { getValueType } from '../core/NodeUtils.js';
import { buffer } from '../accessors/BufferNode.js';
import { instancedBufferAttribute } from '../accessors/BufferAttributeNode.js';
import { instanceIndex } from '../core/IndexNode.js';
import { nodeProxy, float } from '../tsl/TSLBase.js';
import { Vector4 } from '../../math/Vector4.js';
import { MathUtils } from '../../math/MathUtils.js';
import { InstancedBufferAttribute } from '../../core/InstancedBufferAttribute.js';
let min = null;
let max = null;
/**
* `RangeNode` generates random instanced attribute data in a defined range.
* An exemplary use case for this utility node is to generate random per-instance
* colors:
* ```js
* const material = new MeshBasicNodeMaterial();
* material.colorNode = range( new Color( 0x000000 ), new Color( 0xFFFFFF ) );
* const mesh = new InstancedMesh( geometry, material, count );
* ```
* @augments Node
*/
class RangeNode extends Node {
static get type() {
return 'RangeNode';
}
/**
* Constructs a new range node.
*
* @param {Node<any>} [minNode=float()] - A node defining the lower bound of the range.
* @param {Node<any>} [maxNode=float()] - A node defining the upper bound of the range.
*/
constructor( minNode = float(), maxNode = float() ) {
super();
/**
* A node defining the lower bound of the range.
*
* @type {Node<any>}
* @default float()
*/
this.minNode = minNode;
/**
* A node defining the upper bound of the range.
*
* @type {Node<any>}
* @default float()
*/
this.maxNode = maxNode;
}
/**
* Returns the vector length which is computed based on the range definition.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {number} The vector length.
*/
getVectorLength( builder ) {
const minLength = builder.getTypeLength( getValueType( this.minNode.value ) );
const maxLength = builder.getTypeLength( getValueType( this.maxNode.value ) );
return minLength > maxLength ? minLength : maxLength;
}
/**
* This method is overwritten since the node type is inferred from range definition.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {string} The node type.
*/
getNodeType( builder ) {
return builder.object.count > 1 ? builder.getTypeFromLength( this.getVectorLength( builder ) ) : 'float';
}
setup( builder ) {
const object = builder.object;
let output = null;
if ( object.count > 1 ) {
const minValue = this.minNode.value;
const maxValue = this.maxNode.value;
const minLength = builder.getTypeLength( getValueType( minValue ) );
const maxLength = builder.getTypeLength( getValueType( maxValue ) );
min = min || new Vector4();
max = max || new Vector4();
min.setScalar( 0 );
max.setScalar( 0 );
if ( minLength === 1 ) min.setScalar( minValue );
else if ( minValue.isColor ) min.set( minValue.r, minValue.g, minValue.b, 1 );
else min.set( minValue.x, minValue.y, minValue.z || 0, minValue.w || 0 );
if ( maxLength === 1 ) max.setScalar( maxValue );
else if ( maxValue.isColor ) max.set( maxValue.r, maxValue.g, maxValue.b, 1 );
else max.set( maxValue.x, maxValue.y, maxValue.z || 0, maxValue.w || 0 );
const stride = 4;
const length = stride * object.count;
const array = new Float32Array( length );
for ( let i = 0; i < length; i ++ ) {
const index = i % stride;
const minElementValue = min.getComponent( index );
const maxElementValue = max.getComponent( index );
array[ i ] = MathUtils.lerp( minElementValue, maxElementValue, Math.random() );
}
const nodeType = this.getNodeType( builder );
if ( object.count <= 4096 ) {
output = buffer( array, 'vec4', object.count ).element( instanceIndex ).convert( nodeType );
} else {
// TODO: Improve anonymous buffer attribute creation removing this part
const bufferAttribute = new InstancedBufferAttribute( array, 4 );
builder.geometry.setAttribute( '__range' + this.id, bufferAttribute );
output = instancedBufferAttribute( bufferAttribute ).convert( nodeType );
}
} else {
output = float( 0 );
}
return output;
}
}
export default RangeNode;
/**
* TSL function for creating a range node.
*
* @tsl
* @function
* @param {Node<any>} [minNode=float()] - A node defining the lower bound of the range.
* @param {Node<any>} [maxNode=float()] - A node defining the upper bound of the range.
* @returns {RangeNode}
*/
export const range = /*@__PURE__*/ nodeProxy( RangeNode ).setParameterLength( 2 );