-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathTransformTangentNormal.osl
47 lines (38 loc) · 1.73 KB
/
TransformTangentNormal.osl
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
// Transform Tangent Normal Shader - applies a matrix transform or a rotation to a tangent space normal map
/*
Copyright 2023 Edward Darby Edelen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
shader TransformTangentNormal(
color normal_input = color(0,0,1)
[[string label = "Normal Texture"]],
vector tangent_in = 0
[[string label="Tangent Input"]],
float tangent_rotation = 0
[[string label = "Tangent Rotation", string page="Transform", float slidermin = -180, float slidermax = 180]],
matrix tangent_space = matrix(1)
[[string label = "Tangent Space", string page="Transform"]],
output normal Transformed_Normal = normal(0,0,1)
)
{
vector T = select(normalize(dPdu), tangent_in, isconnected(tangent_in));
vector B = cross(T, Ng);
matrix TBN = matrix(
T.x, T.y, T.z, 0,
B.x, B.y, B.z, 0,
Ng.x, Ng.y, Ng.z, 0,
0, 0, 0, 1
);
vector unbiased_normal = normalize(normal_input * vector(2) - vector(1));
vector result = rotate(unbiased_normal, radians(tangent_rotation), vector(0,0,-1));
result = isconnected(tangent_space) ? normalize(transform(tangent_space / TBN, result)) : result;
Transformed_Normal = (result + vector(1)) * vector(0.5);
}