-
Notifications
You must be signed in to change notification settings - Fork 2
/
WF_Object_MMM.fxsub
478 lines (402 loc) · 19.7 KB
/
WF_Object_MMM.fxsub
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
////////////////////////////////////////////////////////////////////////////////////////////////
//
// WF_Object_MMM.fxsub モデルを地面に対して鏡像描画
// ( MikuMikuMoving対応版, WorkingFloor2.fx から呼び出されます.オフスクリーン描画用)
// 作成: 針金P( MoggP氏のSampleBase.fxm改変 )
//
////////////////////////////////////////////////////////////////////////////////////////////////
// アクセに組み込む場合はここを適宜変更してください.
float3 MirrorPos = float3( 0.0, 0.0, 0.0 ); // ローカル座標系における鏡面上の任意の座標(アクセ頂点座標の一点)
float3 MirrorNormal = float3( 0.0, 1.0, 0.0 ); // ローカル座標系における鏡面の法線ベクトル
///////////////////////////////////////////////////////////////////////////////////////////////
// 鏡面座標変換パラメータ
float4x4 MirrorWorldMatrix: CONTROLOBJECT < string Name = "(OffscreenOwner)"; string item = "センター"; >; // 鏡面アクセのワールド変換行列
// ワールド座標系における鏡像位置への変換
static float3 WldMirrorPos = mul( float4(MirrorPos, 1.0f), MirrorWorldMatrix ).xyz;
static float3 WldMirrorNormal = normalize( mul( MirrorNormal, (float3x3)MirrorWorldMatrix ) );
// 座標の鏡像変換
float4 TransMirrorPos( float4 Pos )
{
Pos.xyz -= WldMirrorNormal * 2.0f * dot(WldMirrorNormal, Pos.xyz - WldMirrorPos);
return Pos;
}
// ベクトルの鏡像変換
float3 TransMirrorVec( float3 Vec )
{
Vec -= WldMirrorNormal * 2.0f * dot(WldMirrorNormal, Vec);
return Vec;
}
//カメラ位置
float3 CameraPosition : POSITION < string Object = "Camera"; >;
// 鏡面表裏判定(座標とカメラが鏡面の同じ側にあれば+)
float IsFace( float4 Pos )
{
return dot(Pos.xyz-WldMirrorPos, WldMirrorNormal) * dot(CameraPosition-WldMirrorPos, WldMirrorNormal);
}
///////////////////////////////////////////////////////////////////////////////////////////////
// Obj座標変換行列
float4x4 WorldViewProjMatrix : WORLDVIEWPROJECTION;
float4x4 ViewProjMatrix : VIEWPROJECTION;
float4x4 WorldMatrix : WORLD;
float4x4 ViewMatrix : VIEW;
float4x4 ProjMatrix : PROJECTION;
//ライト関連
bool LightEnables[MMM_LightCount] : LIGHTENABLES; // 有効フラグ
float4x4 LightWVPMatrices[MMM_LightCount] : LIGHTWVPMATRICES; // 座標変換行列
float3 LightDirection[MMM_LightCount] : LIGHTDIRECTIONS; // 方向
float3 LightPositions[MMM_LightCount] : LIGHTPOSITIONS; // ライト位置
float LightZFars[MMM_LightCount] : LIGHTZFARS; // ライトzFar値
//材質モーフ関連
float4 AddingTexture : ADDINGTEXTURE; // 材質モーフ加算Texture値
float4 AddingSphere : ADDINGSPHERE; // 材質モーフ加算SphereTexture値
float4 MultiplyTexture : MULTIPLYINGTEXTURE; // 材質モーフ乗算Texture値
float4 MultiplySphere : MULTIPLYINGSPHERE; // 材質モーフ乗算SphereTexture値
// マテリアル色
float4 MaterialDiffuse : DIFFUSE < string Object = "Geometry"; >;
float3 MaterialAmbient : AMBIENT < string Object = "Geometry"; >;
float3 MaterialEmmisive : EMISSIVE < string Object = "Geometry"; >;
float3 MaterialSpecular : SPECULAR < string Object = "Geometry"; >;
float SpecularPower : SPECULARPOWER < string Object = "Geometry"; >;
float4 MaterialToon : TOONCOLOR;
float4 EdgeColor : EDGECOLOR;
float EdgeWidth : EDGEWIDTH;
float4 GroundShadowColor : GROUNDSHADOWCOLOR;
bool spadd; // スフィアマップ加算合成フラグ
bool usetoontexturemap; // Toonテクスチャフラグ
// ライト色
float3 LightDiffuses[MMM_LightCount] : LIGHTDIFFUSECOLORS;
float3 LightAmbients[MMM_LightCount] : LIGHTAMBIENTCOLORS;
float3 LightSpeculars[MMM_LightCount] : LIGHTSPECULARCOLORS;
// ライト色
static float4 DiffuseColor[3] = { MaterialDiffuse * float4(LightDiffuses[0], 1.0f),
MaterialDiffuse * float4(LightDiffuses[1], 1.0f),
MaterialDiffuse * float4(LightDiffuses[2], 1.0f) };
static float3 AmbientColor[3] = { saturate(MaterialAmbient * LightAmbients[0]) + MaterialEmmisive,
saturate(MaterialAmbient * LightAmbients[1]) + MaterialEmmisive,
saturate(MaterialAmbient * LightAmbients[2]) + MaterialEmmisive };
static float3 SpecularColor[3] = { MaterialSpecular * LightSpeculars[0],
MaterialSpecular * LightSpeculars[1],
MaterialSpecular * LightSpeculars[2] };
// オブジェクトのテクスチャ
texture ObjectTexture: MATERIALTEXTURE;
sampler ObjTexSampler = sampler_state {
texture = <ObjectTexture>;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};
// スフィアマップのテクスチャ
texture ObjectSphereMap: MATERIALSPHEREMAP;
sampler ObjSphareSampler = sampler_state {
texture = <ObjectSphereMap>;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};
///////////////////////////////////////////////////////////////////////////////////////////////
// オブジェクト描画
struct VS_OUTPUT {
float4 Pos : POSITION; // 射影変換座標
float2 Tex : TEXCOORD0; // テクスチャ
float4 SubTex : TEXCOORD1; // サブテクスチャ/スフィアマップテクスチャ座標
float3 Normal : TEXCOORD2; // 法線
float3 Eye : TEXCOORD3; // カメラとの相対位置
float4 SS_UV1 : TEXCOORD4; // セルフシャドウテクスチャ座標
float4 SS_UV2 : TEXCOORD5; // セルフシャドウテクスチャ座標
float4 SS_UV3 : TEXCOORD6; // セルフシャドウテクスチャ座標
float4 WPos : TEXCOORD7; // 鏡像元モデルのワールド座標
float4 Color : COLOR0; // ライト0による色
};
//==============================================
// 頂点シェーダ
// MikuMikuMoving独自の頂点シェーダ入力(MMM_SKINNING_INPUT)
//==============================================
VS_OUTPUT Basic_VS(MMM_SKINNING_INPUT IN, uniform bool useTexture, uniform bool useSphereMap, uniform bool useToon, uniform bool useSelfShadow)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
//================================================================================
//MikuMikuMoving独自のスキニング関数(MMM_SkinnedPositionNormal)。座標と法線を取得する。
//================================================================================
MMM_SKINNING_OUTPUT SkinOut = MMM_SkinnedPositionNormal(IN.Pos, IN.Normal, IN.BlendWeight, IN.BlendIndices, IN.SdefC, IN.SdefR0, IN.SdefR1);
// ワールド座標変換
float4 Pos = mul( SkinOut.Position, WorldMatrix );
Out.WPos = Pos; // ワールド座標
// カメラとの相対位置(光源も鏡像化されていることを考慮)
Out.Eye = CameraPosition - Pos.xyz;
// 鏡像位置への座標変換
Pos = TransMirrorPos( Pos ); // 鏡像変換
// 頂点法線
Out.Normal = normalize( mul( SkinOut.Normal, (float3x3)WorldMatrix ) );
// 頂点座標
if (MMM_IsDinamicProjection)
{
float4x4 vpmat = mul(ViewMatrix, MMM_DynamicFov(ProjMatrix, length(CameraPosition - Pos.xyz)));
Out.Pos = mul( Pos, vpmat );
}
else
{
Out.Pos = mul( Pos, ViewProjMatrix );
}
Out.Pos.x = -Out.Pos.x; // ポリゴンが裏返らないように左右反転にして描画
// ディフューズ色+アンビエント色 計算
float3 color = float3(0, 0, 0);
float3 ambient = float3(0, 0, 0);
float count = 0;
for (int i = 0; i < 3; i++) {
if (LightEnables[i]) {
color += (float3(1,1,1) - color) * (max(0, DiffuseColor[i].rgb * dot(Out.Normal, -LightDirection[i])));
ambient += AmbientColor[i];
count = count + 1.0;
}
}
Out.Color.rgb = saturate(ambient / count + color);
Out.Color.a = MaterialDiffuse.a;
// テクスチャ座標
Out.Tex = IN.Tex;
Out.SubTex.xy = IN.AddUV1.xy;
if ( useSphereMap ) {
// スフィアマップテクスチャ座標(外縁が見えやすくなるので少し補正)
float2 NormalWV = mul( Out.Normal, (float3x3)ViewMatrix ).xy * 0.99f;
Out.SubTex.z = NormalWV.x * 0.5f + 0.5f;
Out.SubTex.w = NormalWV.y * -0.5f + 0.5f;
}
if (useSelfShadow) {
float4 dpos = mul(SkinOut.Position, WorldMatrix);
//デプスマップテクスチャ座標
Out.SS_UV1 = mul(dpos, LightWVPMatrices[0]);
Out.SS_UV2 = mul(dpos, LightWVPMatrices[1]);
Out.SS_UV3 = mul(dpos, LightWVPMatrices[2]);
Out.SS_UV1.y = -Out.SS_UV1.y;
Out.SS_UV2.y = -Out.SS_UV2.y;
Out.SS_UV3.y = -Out.SS_UV3.y;
Out.SS_UV1.z = (length(LightPositions[0] - SkinOut.Position.xyz) / LightZFars[0]);
Out.SS_UV2.z = (length(LightPositions[1] - SkinOut.Position.xyz) / LightZFars[1]);
Out.SS_UV3.z = (length(LightPositions[2] - SkinOut.Position.xyz) / LightZFars[2]);
}
return Out;
}
//==============================================
// ピクセルシェーダ
// 入力は特に独自形式なし
//==============================================
float4 Basic_PS(VS_OUTPUT IN, uniform bool useTexture, uniform bool useSphereMap, uniform bool useToon, uniform bool useSelfShadow) : COLOR0
{
// 鏡面の裏側にある部位は鏡像表示しない
clip( IsFace( IN.WPos ) );
float4 Color = IN.Color;
float4 texColor = float4(1,1,1,1);
float texAlpha = MultiplyTexture.a + AddingTexture.a;
//スペキュラ色計算
float3 HalfVector;
float3 Specular = 0;
for (int i = 0; i < 3; i++) {
if (LightEnables[i]) {
HalfVector = normalize( normalize(IN.Eye) + -LightDirection[i] );
Specular += pow( max(0,dot( HalfVector, normalize(IN.Normal) )), SpecularPower ) * SpecularColor[i];
}
}
// テクスチャ適用
if (useTexture) {
texColor = tex2D(ObjTexSampler, IN.Tex);
texColor.rgb = (texColor.rgb * MultiplyTexture.rgb + AddingTexture.rgb) * texAlpha + (1.0 - texAlpha);
}
Color.rgb *= texColor.rgb;
// スフィアマップ適用
if ( useSphereMap ) {
// スフィアマップ適用
if(spadd) Color.rgb = Color.rgb + (tex2D(ObjSphareSampler,IN.SubTex.zw).rgb * MultiplySphere.rgb + AddingSphere.rgb);
else Color.rgb = Color.rgb * (tex2D(ObjSphareSampler,IN.SubTex.zw).rgb * MultiplySphere.rgb + AddingSphere.rgb);
}
// アルファ適用
Color.a = IN.Color.a * texColor.a;
// セルフシャドウなしのトゥーン適用
float3 color;
if (!useSelfShadow && useToon && usetoontexturemap ) {
//================================================================================
// MikuMikuMovingデフォルトのトゥーン色を取得する(MMM_GetToonColor)
//================================================================================
color = MMM_GetToonColor(MaterialToon, IN.Normal, LightDirection[0], LightDirection[1], LightDirection[2]);
Color.rgb *= color;
}
// セルフシャドウ
if (useSelfShadow) {
if (useToon && usetoontexturemap) {
//================================================================================
// MikuMikuMovingデフォルトのセルフシャドウ色を取得する(MMM_GetSelfShadowToonColor)
//================================================================================
float3 shadow = MMM_GetToonColor(MaterialToon, IN.Normal, LightDirection[0], LightDirection[1], LightDirection[2]);
color = MMM_GetSelfShadowToonColor(MaterialToon, IN.Normal, IN.SS_UV1, IN.SS_UV2, IN.SS_UV3, false, useToon);
Color.rgb *= min(shadow, color);
}
else {
Color.rgb *= MMM_GetSelfShadowToonColor(MaterialToon, IN.Normal, IN.SS_UV1, IN.SS_UV2, IN.SS_UV3, false, useToon);
}
}
// スペキュラ適用
Color.rgb += Specular;
return Color;
}
//==============================================
// オブジェクト描画テクニック
// UseSelfShadowが独自に追加されています。
//==============================================
technique MainTec0 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = false; bool UseToon = false; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, false, false, false);
PixelShader = compile ps_3_0 Basic_PS(false, false, false, false);
}
}
technique MainTec1 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = false; bool UseToon = false; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, false, false, false);
PixelShader = compile ps_3_0 Basic_PS(true, false, false, false);
}
}
technique MainTec2 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = true; bool UseToon = false; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, true, false, false);
PixelShader = compile ps_3_0 Basic_PS(false, true, false, false);
}
}
technique MainTec3 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = true; bool UseToon = false; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, true, false, false);
PixelShader = compile ps_3_0 Basic_PS(true, true, false, false);
}
}
technique MainTec4 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = false; bool UseToon = true; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, false, true, false);
PixelShader = compile ps_3_0 Basic_PS(false, false, true, false);
}
}
technique MainTec5 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = false; bool UseToon = true; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, false, true, false);
PixelShader = compile ps_3_0 Basic_PS(true, false, true, false);
}
}
technique MainTec6 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = true; bool UseToon = true; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, true, true, false);
PixelShader = compile ps_3_0 Basic_PS(false, true, true, false);
}
}
technique MainTec7 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = true; bool UseToon = true; bool UseSelfShadow = false; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, true, true, false);
PixelShader = compile ps_3_0 Basic_PS(true, true, true, false);
}
}
technique MainTec8 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = false; bool UseToon = false; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, false, false, true);
PixelShader = compile ps_3_0 Basic_PS(false, false, false, true);
}
}
technique MainTec9 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = false; bool UseToon = false; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, false, false, true);
PixelShader = compile ps_3_0 Basic_PS(true, false, false, true);
}
}
technique MainTec10 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = true; bool UseToon = false; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, true, false, true);
PixelShader = compile ps_3_0 Basic_PS(false, true, false, true);
}
}
technique MainTec11 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = true; bool UseToon = false; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, true, false, true);
PixelShader = compile ps_3_0 Basic_PS(true, true, false, true);
}
}
technique MainTec12 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = false; bool UseToon = true; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, false, true, true);
PixelShader = compile ps_3_0 Basic_PS(false, false, true, true);
}
}
technique MainTec13 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = false; bool UseToon = true; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, false, true, true);
PixelShader = compile ps_3_0 Basic_PS(true, false, true, true);
}
}
technique MainTec14 < string MMDPass = "object"; bool UseTexture = false; bool UseSphereMap = true; bool UseToon = true; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(false, true, true, true);
PixelShader = compile ps_3_0 Basic_PS(false, true, true, true);
}
}
technique MainTec15 < string MMDPass = "object"; bool UseTexture = true; bool UseSphereMap = true; bool UseToon = true; bool UseSelfShadow = true; > {
pass DrawObject {
VertexShader = compile vs_3_0 Basic_VS(true, true, true, true);
PixelShader = compile ps_3_0 Basic_PS(true, true, true, true);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
// 輪郭描画
struct VS_OUTPUT2 {
float4 Pos : POSITION; // 射影変換座標
float4 WPos : TEXCOORD1; // 鏡像元モデルのワールド座標
};
//==============================================
// 頂点シェーダ
//==============================================
VS_OUTPUT2 Edge_VS(MMM_SKINNING_INPUT IN)
{
VS_OUTPUT2 Out = (VS_OUTPUT2)0;
//================================================================================
//MikuMikuMoving独自のスキニング関数(MMM_SkinnedPosition)。座標を取得する。
//================================================================================
MMM_SKINNING_OUTPUT SkinOut = MMM_SkinnedPositionNormal(IN.Pos, IN.Normal, IN.BlendWeight, IN.BlendIndices, IN.SdefC, IN.SdefR0, IN.SdefR1);
// ワールド座標変換
float4 Pos = mul( SkinOut.Position, WorldMatrix );
Out.WPos = Pos; // ワールド座標
// 鏡像位置への座標変換
Pos = TransMirrorPos( Pos ); // 鏡像変換
// 頂点法線
float3 Normal = normalize( mul( SkinOut.Normal, (float3x3)WorldMatrix ) );
// 法線の鏡像変換
Normal = TransMirrorVec( Normal );
// 頂点座標
if (MMM_IsDinamicProjection)
{
float dist = length(CameraPosition - Pos.xyz);
float4x4 vpmat = mul(ViewMatrix, MMM_DynamicFov(ProjMatrix, dist));
Pos += float4(Normal, 0) * IN.EdgeWeight * EdgeWidth * distance(Pos.xyz, CameraPosition) * MMM_GetDynamicFovEdgeRate(dist);
Out.Pos = mul( Pos, vpmat );
}
else
{
Pos += float4(Normal, 0) * IN.EdgeWeight * EdgeWidth * distance(Pos.xyz, CameraPosition);
Out.Pos = mul( Pos, ViewProjMatrix );
}
Out.Pos.x = -Out.Pos.x; // ポリゴンが裏返らないように左右反転にして描画
return Out;
}
//==============================================
// ピクセルシェーダ
//==============================================
float4 Edge_PS(VS_OUTPUT2 IN) : COLOR
{
// 鏡面の裏側にある部位は鏡像表示しない
clip( IsFace( IN.WPos ) );
// 輪郭色で塗りつぶし
return EdgeColor;
}
//==============================================
// 輪郭描画テクニック
//==============================================
technique EdgeTec < string MMDPass = "edge"; > {
pass DrawEdge {
VertexShader = compile vs_2_0 Edge_VS();
PixelShader = compile ps_2_0 Edge_PS();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
// 影(非セルフシャドウ)描画
technique ShadowTec < string MMDPass = "shadow"; > {
// 非セルフシャドウの地面影は表示しない
}
///////////////////////////////////////////////////////////////////////////////////////////////