-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.hlsl
234 lines (186 loc) · 5.11 KB
/
shader.hlsl
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
//*****************************************************************************
// 定数バッファ
//*****************************************************************************
// マトリクスバッファ
cbuffer WorldBuffer : register( b0 )
{
matrix World;
}
cbuffer ViewBuffer : register( b1 )
{
matrix View;
}
cbuffer ProjectionBuffer : register( b2 )
{
matrix Projection;
}
// マテリアルバッファ
struct MATERIAL
{
float4 Ambient;
float4 Diffuse;
float4 Specular;
float4 Emission;
float Shininess;
int noTexSampling;
float Dummy[2];//16byte境界用
};
cbuffer MaterialBuffer : register( b3 )
{
MATERIAL Material;
}
// ライト用バッファ
struct LIGHT
{
float4 Direction[10];
float4 Position[10];
float4 Diffuse[10];
float4 Ambient[10];
float4 Attenuation[10];
int4 Flags[10];
int Enable;
int Dummy[3];//16byte境界用
};
cbuffer LightBuffer : register( b4 )
{
LIGHT Light;
}
struct FOG
{
float4 Distance;
float4 FogColor;
int Enable;
float Dummy[3];//16byte境界用
};
// フォグ用バッファ
cbuffer FogBuffer : register( b5 )
{
FOG Fog;
};
// 縁取り用バッファ
cbuffer Fuchi : register(b6)
{
int fuchi;
int fill[3];
};
cbuffer CameraBuffer : register(b7)
{
float4 Camera;
}
//=============================================================================
// 頂点シェーダ
//=============================================================================
void VertexShaderPolygon( in float4 inPosition : POSITION0,
in float4 inNormal : NORMAL0,
in float4 inDiffuse : COLOR0,
in float2 inTexCoord : TEXCOORD0,
out float4 outPosition : SV_POSITION,
out float4 outNormal : NORMAL0,
out float2 outTexCoord : TEXCOORD0,
out float4 outDiffuse : COLOR0,
out float4 outWorldPosition : POSITION0
)
{
matrix wvp;
wvp = mul(World, View);
wvp = mul(wvp, Projection);
outPosition = mul(inPosition, wvp);
outNormal = normalize(mul(float4(inNormal.xyz, 0.0f), World));
outTexCoord = inTexCoord;
outWorldPosition = mul(inPosition, World);
if (Light.Enable == 0)
{
outDiffuse = inDiffuse * Material.Diffuse;
}
else
{
float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 outColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
for (int i = 0; i < 10; i++)
{
if (Light.Flags[i].x == 1)
{
float4 worldNormal, normal;
normal = float4(inNormal.xyz, 0.0);
worldNormal = mul(normal, World);
worldNormal = normalize(worldNormal);
float light = 0.5 - 0.5 * dot(Light.Direction[i].xyz, worldNormal.xyz);
color = inDiffuse * Material.Diffuse * light * Light.Diffuse[i];
color += inDiffuse * Material.Ambient * Light.Ambient[i];
}
else if (Light.Flags[i].x == 2)
{
float4 worldNormal, normal;
normal = float4(inNormal.xyz, 0.0);
worldNormal = mul(normal, World);
worldNormal = normalize(worldNormal);
float3 posWorld = mul(inPosition, World);
float3 lightDir;
lightDir = normalize(posWorld - Light.Position[i]);
float light = -1.0 * dot(lightDir.xyz, worldNormal.xyz);
color = inDiffuse * Material.Diffuse * light * Light.Diffuse[i];
color += inDiffuse * Material.Ambient * Light.Ambient[i];
float distance = length(posWorld - Light.Position[i]);
float att = saturate((Light.Attenuation[i].x - distance) / Light.Attenuation[i].x);
color *= att;
}
else
{
color = float4(0.0f, 0.0f, 0.0f, 0.0f);
}
outColor += color;
}
outColor += Material.Emission;
outDiffuse = saturate(outColor);
outDiffuse.a = inDiffuse.a * Material.Diffuse.a;
}
}
//*****************************************************************************
// グローバル変数
//*****************************************************************************
Texture2D g_Texture : register( t0 );
SamplerState g_SamplerState : register( s0 );
//=============================================================================
// ピクセルシェーダ
//=============================================================================
void PixelShaderPolygon( in float4 inPosition : SV_POSITION,
in float4 inNormal : NORMAL0,
in float2 inTexCoord : TEXCOORD0,
in float4 inDiffuse : COLOR0,
in float4 inWorldPosition : POSITION0,
out float4 outDiffuse : SV_Target )
{
float4 color;
if (Material.noTexSampling == 0)
{
color = g_Texture.Sample(g_SamplerState, inTexCoord);
color *= inDiffuse;
}
else
{
color = inDiffuse;
}
if (Fog.Enable == 1)
{
float z = inPosition.z*inPosition.w;
float f = (Fog.Distance.y - z) / (Fog.Distance.y - Fog.Distance.x);
f = saturate(f);
outDiffuse = f * color + (1 - f)*Fog.FogColor;
outDiffuse.a = color.a;
}
else
{
outDiffuse = color;
}
//縁取り
if (fuchi == 1)
{
float angle = dot(normalize(inWorldPosition.xyz - Camera.xyz), normalize(inNormal));
//if ((angle < 0.5f)&&(angle > -0.5f))
if (angle > -0.3f)
{
outDiffuse.rb = 1.0f;
outDiffuse.g = 0.0f;
}
}
}