0%

切线空间和对象空间的转换

摘要

因为一时找不到工具用来转换模型的切线空间和对象空间.

整了一个Substance Painter 可以方便直接处理.


原理

由于Substance的引擎可以直接读取TBN信息.

通过构建TBN矩阵的转置与否,直接转换.

相当简单.

存储成 .glsl 后缀格式,直接拖进Substance Painter

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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
This Shader was made for Substance Painter
help converting Tangent Space into Obeject/World Space
or converting ObjectSpace to Tangent Space
*/


#define BLACK_COLOR vec4(0.0f,0.0f,0.0f,1.0f)
#define WHITE_COLOR vec4(1.0f,1.0f,1.0f,1.0f)


import lib-utils.glsl
import lib-env.glsl
import lib-normal.glsl
import lib-alpha.glsl
import lib-vectors.glsl
import lib-sparse.glsl
import lib-sampler.glsl


/*
//--------texure Sampler ---------------------------------------------------//
*/

//: param auto channel_user0
uniform SamplerSparse InputSpaceTexSampler;

/*
//-------- About ----------------------------------------------------//
*/

//: param custom { "default": true, "label": "MineClever's Space Converter", "group": "About" }
uniform bool tAuthor;

//: param custom { "default": true, "label": "Version 0.0.3", "group": "About" }
uniform bool tAuthor_version;

/*
//-------- Space ----------------------------------------------------//
*/
//: param custom { "default": false, "label": "Tangent Space", "group": "Convert Space" }

uniform bool bSwapSpace;

//: param custom {
//: "default": 0,
//: "label": "Remap Input XYZ",
//: "widget": "combobox",
//: "group": "Convert Space",
//: "values": {
//: "XYZ": 0,
//: "XZY": 1,
//: "YXZ": 2,
//: "YZX": 3,
//: "ZXY": 4,
//: "ZYX": 5
//: }
//: }
uniform int XYZOrderType;

//: param custom { "default": false, "label": "-X", "group": "Convert Space" }
uniform bool Xnegative;

//: param custom { "default": false, "label": "-Y", "group": "Convert Space" }
uniform bool Ynegative;

//: param custom { "default": false, "label": "-Z", "group": "Convert Space" }
uniform bool Znegative;

/*
//-------- Tools ----------------------------------------------------//
*/

//: param custom { "default": true, "label": "unsigned Input Image", "group": "Input" }
uniform bool bPacked;

//: param custom { "default": true, "label": "Compute Binormal Direction", "group": "Input" }
uniform bool bInputTBN;

//: param custom { "default": false, "label": "Decode Input Gamma", "group": "Input" }
uniform bool bInputGamma;

//: param custom {
//: "default": 0,
//: "label": "Input Gamma Type",
//: "widget": "combobox",
//: "group": "Input",
//: "values": {
//: "sRGB": 0,
//: "Gamma 2.2 (Tga)": 1
//: }
//: }
uniform int EnumGammaType;


//: param custom { "default": false, "label": "Flip Y", "group": "Output" }
uniform bool bFlipY;

//: param custom { "default": false, "label": "Use Output Gamma", "group": "Output" }
uniform bool bOutputGamma;

//: param custom {
//: "default": 0,
//: "label": "Output Gamma Type",
//: "widget": "combobox",
//: "group": "Output",
//: "values": {
//: "sRGB": 0,
//: "Gamma 2.2 (Tga)": 1
//: }
//: }
uniform int EnumOutputGamma;

//: param custom { "default": false, "label": "Use Signed Float(Exr Only)", "group": "Output" }
uniform bool bOutputSigned;

//: param custom { "default": false, "label": "Normalize Output", "group": "Output" }
uniform bool bOutputNormalized;

/*
//-------- Debug ----------------------------------------------------//
*/

//: param custom {
//: "default": 0,
//: "label": "Viewport",
//: "widget": "combobox",
//: "group": "Debug",
//: "values": {
//: "Output Map": 0,
//: "Input Map": 1
//: }
//: }
uniform int EnumDebugView;


/*
// HLSL function help to port/cast
*/

float saturate(float v)
{
return clamp(v, 0.0, 1.0);
};

vec2 saturate(vec2 v)
{
return clamp(v, 0.0, 1.0);
};

vec3 saturate(vec3 v)
{
return clamp(v, 0.0, 1.0);
};

vec4 saturate(vec4 v)
{
return clamp(v, 0.0, 1.0);
};


//custom Substance engine function
float sampleWithDefault(SamplerSparse sampler, SparseCoord coord, float defaultValue)
{
vec2 value = textureSparse(sampler, coord).rg;
return value.r + defaultValue * (1.0 - value.g);
};

vec3 sampleWithColor(SamplerSparse sampler, SparseCoord coord,vec3 defaultColor)
{
vec4 Color = textureSparse(sampler, coord).rgba;
Color.rgb = mix(defaultColor.rgb, Color.rgb, Color.a);
return Color.rgb;
};

/* build TNB Matrix InputValue pixel shader*/
mat3 buildTBN (vec3 T, vec3 B, vec3 N)
{
return mat3 (T,B,N);
};


/* easily switching the range between [0,1] and [-1,1]*/

vec3 unpackNormalMap (vec3 InputValue)
{
vec3 OUT;
//OUT = (InputValue - 0.5f) * 2.0f;
OUT = 2 * InputValue - 1;
return OUT;
};

vec4 unpackNormalMap (vec4 InputValue)
{
vec4 OUT;
OUT.xyz = unpackNormalMap(InputValue.xyz);
OUT.w = InputValue.w;
return OUT;
};

vec3 packNormalMap (vec3 InputValue)
{
vec3 OUT;
//OUT = ( InputValue + 1.0f ) * 0.5f;
OUT = 0.5 * InputValue + 0.5;
return OUT;
};

float rcp (float InputValue)
{
return 1.0f/InputValue;
};

/*Gamma Function designed for user*/
vec3 deGamma (vec3 InputValue, int gammaType = 0)
{
OUT = (gammaType == 0) ? linear2sRGB(InputValue) : pow(InputValue,vec3(rcp(2.2)));
return OUT;
};

vec3 enGamma (vec3 InputValue, int gammaType = 0)
{
OUT = (gammaType == 0) ? sRGB2linear(InputValue) : pow(InputValue,vec3(2.2));
return OUT;
};

/* This function was desigined to make swizzle xyz by user */
vec3 remapXYZorder (vec3 InputValue, int orderType)
{
vec3 OUT;
switch(orderType)
{
case 1 :
OUT.xyz = InputValue.xzy;
break;
case 2 :
OUT.xyz = InputValue.yxz;
break;
case 3 :
OUT.xyz = InputValue.yzx;
break;
case 4 :
OUT.xyz = InputValue.zxy;
break;
case 5 :
OUT.xyz = InputValue.zyx;
break;
default:
OUT.xyz = InputValue.xyz;
};
return OUT;
};

/* fragmentShader Here (surface shader) */
void shade(V2F inputs) {

vec3 T,B,N;
/* use engine function to get correct TBN */
//LocalVectors vectors = computeLocalFrame(inputs);
// N = vectors.normal;
// B = vectors.bitangent;
// T = vectors.tangent;

/* Compute by ourself */
N = normalize(inputs.normal);
T = normalize(inputs.tangent);
B = cross(N,T);
float binormalDirection = (dot(B, inputs.bitangent) < 0.0) && (bInputTBN) ? -1 : 1;
B *= binormalDirection;

mat3 TBN = buildTBN(T, B, N);

TBN = bSwapSpace ? transpose(TBN) : TBN;

/* mapping [0,1] to [-1,1]*/
vec3 XYZnegative = 1 - vec3(Xnegative, Ynegative , Znegative) * 2 ;

/* sample input map with filpper */
vec3 originInputSpaceColor;
originInputSpaceColor = sampleWithColor(InputSpaceTexSampler, inputs.sparse_coord, N);

originInputSpaceColor = bPacked ? unpackNormalMap(originInputSpaceColor) : originInputSpaceColor;

originInputSpaceColor = remapXYZorder(originInputSpaceColor,XYZOrderType) * XYZnegative;
vec3 inputSpaceColor = originInputSpaceColor;

inputSpaceColor = bInputGamma ? deGamma(inputSpaceColor, EnumGammaType) : inputSpaceColor;

/* converting Space by TBN Matrix */
vec3 outputSpaceColor = inputSpaceColor.rgb * TBN;

outputSpaceColor = bOutputNormalized ? normalize(outputSpaceColor) : outputSpaceColor;

outputSpaceColor.y = bFlipY ? -outputSpaceColor.y : outputSpaceColor.y;

outputSpaceColor = bOutputSigned ? outputSpaceColor : packNormalMap( outputSpaceColor );

outputSpaceColor = bOutputGamma ? enGamma(outputSpaceColor, EnumOutputGamma) : outputSpaceColor;

outputSpaceColor = mix(outputSpaceColor,inputSpaceColor,EnumDebugView * 1.0);

diffuseShadingOutput(outputSpaceColor.rgb);//sv_target::COLOR
};

歡迎關注我的其它發布渠道