-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathFrontFacingNode.js
More file actions
127 lines (93 loc) · 2.94 KB
/
Copy pathFrontFacingNode.js
File metadata and controls
127 lines (93 loc) · 2.94 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
import Node from '../core/Node.js';
import { nodeImmutable, float, Fn } from '../tsl/TSLBase.js';
import { warnOnce } from '../../utils.js';
import { BackSide, DoubleSide } from '../../constants.js';
/**
* This node can be used to evaluate whether a primitive is front or back facing.
*
* @augments Node
*/
class FrontFacingNode extends Node {
static get type() {
return 'FrontFacingNode';
}
/**
* Constructs a new front facing node.
*/
constructor() {
super( 'bool' );
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isFrontFacingNode = true;
}
generate( builder ) {
if ( builder.shaderStage !== 'fragment' ) return 'true';
//
if ( builder.materialSide === BackSide ) {
return 'false';
}
return builder.getFrontFacing();
}
}
export default FrontFacingNode;
/**
* TSL object that represents whether a primitive is front or back facing
*
* @tsl
* @type {FrontFacingNode<bool>}
*/
export const frontFacing = /*@__PURE__*/ nodeImmutable( FrontFacingNode );
/**
* TSL object that represents the front facing status as a number instead of a bool.
* `1` means front facing, `-1` means back facing.
*
* @tsl
* @type {Node<float>}
*/
export const faceDirection = /*@__PURE__*/ float( frontFacing ).mul( 2.0 ).sub( 1.0 );
/**
* Negates a vector if the rendering occurs on the back side of a face,
* based on the material's side configuration.
*
* - If the material's side is `BackSide`, the vector is inverted (negated).
* - If the material's side is `DoubleSide`, the vector is multiplied by `faceDirection`
* (negated only for back-facing fragments).
* - If the material's side is `FrontSide` (default), the vector remains unchanged.
*
* @tsl
* @function
* @param {Node<vec3>} vector - The vector to process.
* @returns {Node<vec3>} The processed vector.
*/
export const negateOnBackSide = /*@__PURE__*/ Fn( ( [ vector ], { materialSide } ) => {
if ( materialSide === BackSide ) {
vector = vector.mul( - 1.0 );
} else if ( materialSide === DoubleSide ) {
vector = vector.mul( faceDirection );
}
return vector;
} );
/**
* Negates a vector if the rendering occurs on the back side of a face,
* based on the material's side configuration.
*
* - If the material's side is `BackSide`, the vector is inverted (negated).
* - If the material's side is `DoubleSide`, the vector is multiplied by `faceDirection`
* (negated only for back-facing fragments).
* - If the material's side is `FrontSide` (default), the vector remains unchanged.
*
* @tsl
* @function
* @deprecated since r185. Use {@link negateOnBackSide} instead.
* @param {Node<vec3>} vector - The vector to convert.
* @returns {Node<vec3>} The converted vector.
*/
export const directionToFaceDirection = ( vector ) => {
warnOnce( 'TSL: "directionToFaceDirection()" has been renamed to "negateOnBackSide()".' ); // @deprecated r185
return negateOnBackSide( vector );
};