-
-
Notifications
You must be signed in to change notification settings - Fork 36.3k
Expand file tree
/
Copy pathNormal.js
More file actions
243 lines (170 loc) · 6.24 KB
/
Normal.js
File metadata and controls
243 lines (170 loc) · 6.24 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { attribute } from '../core/AttributeNode.js';
import { cameraViewMatrix, worldToViewRotation } from './Camera.js';
import { modelNormalMatrix, modelWorldMatrix } from './ModelNode.js';
import { mat3, vec3, Fn } from '../tsl/TSLBase.js';
import { positionView } from './Position.js';
import { directionToFaceDirection } from '../display/FrontFacingNode.js';
import { warn } from '../../utils.js';
/**
* TSL object that represents the normal attribute of the current rendered object in local space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalGeometry = /*@__PURE__*/ attribute( 'normal', 'vec3' );
/**
* TSL object that represents the vertex normal of the current rendered object in local space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalLocal = /*@__PURE__*/ ( Fn( ( builder ) => {
if ( builder.geometry.hasAttribute( 'normal' ) === false ) {
warn( 'TSL: Vertex attribute "normal" not found on geometry.' );
return vec3( 0, 1, 0 );
}
return normalGeometry;
}, 'vec3' ).once() )().toVar( 'normalLocal' );
/**
* TSL object that represents the flat vertex normal of the current rendered object in view space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalFlat = /*@__PURE__*/ positionView.dFdx().cross( positionView.dFdy() ).normalize().toVar( 'normalFlat' );
/**
* TSL object that represents the vertex normal of the current rendered object in view space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalViewGeometry = /*@__PURE__*/ ( Fn( ( builder ) => {
let node;
if ( builder.isFlatShading() ) {
node = normalFlat;
} else {
node = transformNormalToView( normalLocal ).toVarying( 'v_normalViewGeometry' ).normalize();
}
return node;
}, 'vec3' ).once() )().toVar( 'normalViewGeometry' );
/**
* TSL object that represents the vertex normal of the current rendered object in world space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalWorldGeometry = /*@__PURE__*/ ( Fn( ( builder ) => {
let normal = normalViewGeometry.transformDirection( cameraViewMatrix );
if ( builder.isFlatShading() !== true ) {
normal = normal.toVarying( 'v_normalWorldGeometry' );
}
return normal.normalize().toVar( 'normalWorldGeometry' );
}, 'vec3' ).once() )();
/**
* TSL object that represents the vertex normal of the current rendered object in view space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalView = /*@__PURE__*/ ( Fn( ( builder ) => {
let node;
if ( builder.subBuildFn === 'NORMAL' || builder.subBuildFn === 'VERTEX' ) {
node = normalViewGeometry;
if ( builder.isFlatShading() !== true ) {
node = directionToFaceDirection( node );
}
} else {
// Use custom context to avoid side effects from nodes overwriting getUV, getTextureLevel in the context (e.g. EnvironmentNode)
node = builder.context.setupNormal().context( { getUV: null, getTextureLevel: null } );
}
return node;
}, 'vec3' ).once( [ 'NORMAL', 'VERTEX' ] ) )().toVar( 'normalView' );
/**
* TSL object that represents the vertex normal of the current rendered object in world space.
*
* @tsl
* @type {Node<vec3>}
*/
export const normalWorld = /*@__PURE__*/ normalView.transformDirection( cameraViewMatrix ).toVar( 'normalWorld' );
/**
* TSL object that represents the clearcoat vertex normal of the current rendered object in view space.
*
* @tsl
* @type {Node<vec3>}
*/
export const clearcoatNormalView = /*@__PURE__*/ ( Fn( ( { subBuildFn, context } ) => {
let node;
if ( subBuildFn === 'NORMAL' || subBuildFn === 'VERTEX' ) {
node = normalView;
} else {
// Use custom context to avoid side effects from nodes overwriting getUV, getTextureLevel in the context (e.g. EnvironmentNode)
node = context.setupClearcoatNormal().context( { getUV: null, getTextureLevel: null } );
}
return node;
}, 'vec3' ).once( [ 'NORMAL', 'VERTEX' ] ) )().toVar( 'clearcoatNormalView' );
/**
* Transforms the normal with the given matrix.
*
* @tsl
* @function
* @param {Node<vec3>} normal - The normal.
* @param {Node<mat3>} [matrix=modelWorldMatrix] - The matrix.
* @return {Node<vec3>} The transformed normal.
*/
export const transformNormal = /*@__PURE__*/ Fn( ( [ normal, matrix = modelWorldMatrix ] ) => {
const m = mat3( matrix );
const transformedNormal = normal.div( vec3( m[ 0 ].dot( m[ 0 ] ), m[ 1 ].dot( m[ 1 ] ), m[ 2 ].dot( m[ 2 ] ) ) );
return m.mul( transformedNormal ).xyz;
} );
/**
* Transforms the given normal from local to view space.
*
* @tsl
* @function
* @param {Node<vec3>} normal - The normal.
* @param {NodeBuilder} builder - The current node builder.
* @return {Node<vec3>} The transformed normal.
*/
export const transformNormalToView = /*@__PURE__*/ Fn( ( [ normal ], builder ) => {
const modelNormalViewMatrix = builder.context.modelNormalViewMatrix;
if ( modelNormalViewMatrix ) {
return modelNormalViewMatrix.transformDirection( normal );
}
//
const transformedNormal = modelNormalMatrix.mul( normal );
return worldToViewRotation.mul( transformedNormal ).normalize();
} );
// Deprecated
/**
* TSL object that represents the transformed vertex normal of the current rendered object in view space.
*
* @tsl
* @type {Node<vec3>}
* @deprecated since r178. Use `normalView` instead.
*/
export const transformedNormalView = ( Fn( () => { // @deprecated, r177
warn( 'TSL: "transformedNormalView" is deprecated. Use "normalView" instead.' );
return normalView;
} ).once( [ 'NORMAL', 'VERTEX' ] ) )();
/**
* TSL object that represents the transformed vertex normal of the current rendered object in world space.
*
* @tsl
* @type {Node<vec3>}
* @deprecated since r178. Use `normalWorld` instead.
*/
export const transformedNormalWorld = ( Fn( () => { // @deprecated, r177
warn( 'TSL: "transformedNormalWorld" is deprecated. Use "normalWorld" instead.' );
return normalWorld;
} ).once( [ 'NORMAL', 'VERTEX' ] ) )();
/**
* TSL object that represents the transformed clearcoat vertex normal of the current rendered object in view space.
*
* @tsl
* @type {Node<vec3>}
* @deprecated since r178. Use `clearcoatNormalView` instead.
*/
export const transformedClearcoatNormalView = ( Fn( () => { // @deprecated, r177
warn( 'TSL: "transformedClearcoatNormalView" is deprecated. Use "clearcoatNormalView" instead.' );
return clearcoatNormalView;
} ).once( [ 'NORMAL', 'VERTEX' ] ) )();