1
- #define MaxDirectionalLight 5
2
- #define MaxPointLight 10
3
- #define MaxSpotLight 130
1
+ #define MaxLight 30
2
+ #define Directional 0
3
+ #define Point 1
4
+ #define Spot 2
5
+ #define Last 3
4
6
5
7
struct MaterialData
6
8
{
7
- float3 fresnelR0;
8
- float roughness;
9
- float3 ambient;
9
+ float3 fresnelR0;
10
+ float roughness;
11
+ float3 ambient;
10
12
};
11
13
12
14
cbuffer Material : register (b0, space1)
13
15
{
14
- MaterialData g_material[4 ];
16
+ MaterialData g_material[4 ];
15
17
};
16
18
17
- struct DirectionalLightData
19
+ struct LightData
18
20
{
19
- float3 strength;
20
- uint padding0;
21
- float3 direction;
22
- uint padding1;
23
- };
24
-
25
- struct PointLightData
26
- {
27
- float3 strength;
28
- float fallOffStart;
29
- float3 position;
30
- float fallOffEnd;
31
- };
32
-
33
- struct SpotLightData
34
- {
35
- float3 strength;
36
- float fallOffStart;
37
- float3 direction;
38
- float fallOffEnd;
39
- float3 position;
40
- float spotPower;
21
+ float3 strength;
22
+ float fallOffStart;
23
+ float3 direction;
24
+ float fallOffEnd;
25
+ float3 position;
26
+ float spotPower;
27
+ uint type;
28
+ float3 padding;
41
29
};
42
30
43
31
cbuffer Light : register (b0, space2)
44
32
{
45
- uint4 g_lightNum; // x : directional, y : point, z : spot
46
- DirectionalLightData g_directionalLights[MaxDirectionalLight];
47
- PointLightData g_pointLights[MaxPointLight];
48
- SpotLightData g_spotLights[MaxSpotLight];
33
+ LightData g_lights[MaxLight];
49
34
}
50
35
51
36
float CalcAttenuation (float d, float fallOffStart, float fallOffEnd)
52
37
{
53
- return saturate ((fallOffEnd - d) / (fallOffEnd - fallOffStart));
38
+ return saturate ((fallOffEnd - d) / (fallOffEnd - fallOffStart));
54
39
}
55
40
56
41
// r0 = ((n-1)/(n+1))^2, nÀº ±¼Àý Áö¼öÀÌ´Ù.
57
42
float3 SchlickFresnel (float3 r0, float3 normal, float3 lightVector)
58
43
{
59
- float cosIncidentAngle = saturate (dot (normal, lightVector));
60
-
61
- float f0 = 1.f - cosIncidentAngle;
62
- float3 reflectPercent = r0 + (1.f - r0) * (f0 * f0 * f0 * f0 * f0);
44
+ float cosIncidentAngle = saturate (dot (normal, lightVector));
45
+
46
+ float f0 = 1.f - cosIncidentAngle;
47
+ float3 reflectPercent = r0 + (1.f - r0) * (f0 * f0 * f0 * f0 * f0);
63
48
64
- return reflectPercent;
49
+ return reflectPercent;
65
50
}
66
51
67
52
float3 BlinnPhong (float3 lightStrength, float3 lightVector, float3 normal,
68
- float3 toEye, float3 diffuse, MaterialData material)
53
+ float3 toEye, float3 diffuse, MaterialData material)
69
54
{
70
- float shininess = 1.f - material.roughness;
71
- const float m = shininess * 256.f ;
72
- float3 halfVector = normalize (toEye + lightVector);
73
-
74
- float roughnessFactor = (m + 8.f ) * pow (max (dot (halfVector, normal), 0.f ), m) / 8.f ;
75
- float3 fresnelFactor = SchlickFresnel (material.fresnelR0, halfVector, lightVector);
55
+ float shininess = 1.f - material.roughness;
56
+ const float m = shininess * 256.f ;
57
+ float3 halfVector = normalize (toEye + lightVector);
76
58
77
- float3 specular = fresnelFactor * roughnessFactor;
59
+ float roughnessFactor = (m + 8.f ) * pow (max (dot (halfVector, normal), 0.f ), m) / 8.f ;
60
+ float3 fresnelFactor = SchlickFresnel (material.fresnelR0, halfVector, lightVector);
78
61
79
- specular = specular / (specular + 1.f );
62
+ float3 specular = fresnelFactor * roughnessFactor;
63
+ specular = specular / (specular + 1.f );
80
64
81
- return (diffuse + specular) * lightStrength;
65
+ return (diffuse + specular) * lightStrength;
82
66
}
83
67
84
- float3 ComputeDirectionalLight (DirectionalLightData light, float3 normal,
85
- float3 toEye, float3 diffuse, MaterialData material)
68
+ float3 ComputeDirectionalLight (LightData light, float3 normal,
69
+ float3 toEye, float3 diffuse, MaterialData material)
86
70
{
87
- float3 lightDirection = -light.direction;
71
+ float3 lightDirection = -light.direction;
88
72
89
- float ndotl = max (dot (lightDirection, normal), 0.0f );
90
- float3 lightStrength = light.strength * ndotl;
73
+ float ndotl = max (dot (lightDirection, normal), 0.0f );
74
+ float3 lightStrength = light.strength * ndotl;
91
75
92
- return BlinnPhong (lightStrength, lightDirection, normal, toEye, diffuse, material);
76
+ return BlinnPhong (lightStrength, lightDirection, normal, toEye, diffuse, material);
93
77
}
94
78
95
- float3 ComputePointLight (PointLightData light, float3 objectPosition, float3 normal,
96
- float3 toEye, float3 diffuse, MaterialData material)
79
+ float3 ComputePointLight (LightData light, float3 objectPosition, float3 normal,
80
+ float3 toEye, float3 diffuse, MaterialData material)
97
81
{
98
- float3 lightVector = light.position - objectPosition;
99
- float d = length (lightVector);
82
+ float3 lightVector = light.position - objectPosition;
83
+ float d = length (lightVector);
100
84
101
- if (d > light.fallOffEnd) return float3 (0.f , 0.f , 0.f );
85
+ if (d > light.fallOffEnd) return float3 (0.f , 0.f , 0.f );
102
86
103
- lightVector /= d;
104
- float ndotl = max (dot (lightVector, normal), 0.f );
105
- float3 lightStrength = light.strength * ndotl;
87
+ lightVector /= d;
88
+ float ndotl = max (dot (lightVector, normal), 0.f );
89
+ float3 lightStrength = light.strength * ndotl;
106
90
107
- float attenuation = CalcAttenuation (d, light.fallOffStart, light.fallOffEnd);
108
- lightStrength *= attenuation;
91
+ float attenuation = CalcAttenuation (d, light.fallOffStart, light.fallOffEnd);
92
+ lightStrength *= attenuation;
109
93
110
- return BlinnPhong (lightStrength, lightVector, normal, toEye, diffuse, material);
94
+ return BlinnPhong (lightStrength, lightVector, normal, toEye, diffuse, material);
111
95
}
112
96
113
- float3 ComputeSpotLight (SpotLightData light, float3 objectPosition, float3 normal,
114
- float3 toEye, float3 diffuse, MaterialData material)
97
+ float3 ComputeSpotLight (LightData light, float3 objectPosition, float3 normal,
98
+ float3 toEye, float3 diffuse, MaterialData material)
115
99
{
116
- float3 lightVector = light.position - objectPosition;
117
- float d = length (lightVector);
100
+ float3 lightVector = light.position - objectPosition;
101
+ float d = length (lightVector);
118
102
119
- if (d > light.fallOffEnd) return 0.0f ;
103
+ if (d > light.fallOffEnd) return 0.0f ;
120
104
121
- lightVector /= d;
122
- float ndotl = max (dot (lightVector, normal), 0.0f );
123
- float3 lightStrength = light.strength * ndotl;
105
+ lightVector /= d;
106
+ float ndotl = max (dot (lightVector, normal), 0.0f );
107
+ float3 lightStrength = light.strength * ndotl;
124
108
125
- float attenuation = CalcAttenuation (d, light.fallOffStart, light.fallOffEnd);
126
- lightStrength *= attenuation;
109
+ float attenuation = CalcAttenuation (d, light.fallOffStart, light.fallOffEnd);
110
+ lightStrength *= attenuation;
127
111
128
- float spotFactor = pow (max (dot (-lightVector, light.direction), 0.f ), light.spotPower);
129
- lightStrength *= spotFactor;
112
+ float spotFactor = pow (max (dot (-lightVector, light.direction), 0.f ), light.spotPower);
113
+ lightStrength *= spotFactor;
130
114
131
- return BlinnPhong (lightStrength, lightVector, normal, toEye, diffuse, material);
115
+ return BlinnPhong (lightStrength, lightVector, normal, toEye, diffuse, material);
132
116
}
133
117
134
118
float4 Lighting (float3 objectPosition, float3 normal,
135
- float3 toEye, float4 diffuse, MaterialData material)
119
+ float3 toEye, float4 diffuse, MaterialData material)
136
120
{
137
- float3 output = float3 (0.f , 0.f , 0.f );
138
-
139
- for (int i = 0 ; i < g_lightNum.x; ++i) {
140
- output += ComputeDirectionalLight (g_directionalLights[i], normal, toEye, diffuse.rgb, material);
141
- }
142
- for (int j = 0 ; j < g_lightNum.y; ++j) {
143
- output += ComputePointLight (g_pointLights[j], objectPosition, normal, toEye, diffuse.rgb, material);
144
- }
145
- for (int k = 0 ; k < g_lightNum.z; ++k) {
146
- output += ComputeSpotLight (g_spotLights[k], objectPosition, normal, toEye, diffuse.rgb, material);
147
- }
148
-
149
- float3 ambient = material.ambient * diffuse.rgb;
150
- output += ambient;
151
-
152
- return float4 (output, diffuse.a);
121
+ float3 output = float3 (0.f , 0.f , 0.f );
122
+
123
+ for (int i = 0 ; i < MaxLight; ++i)
124
+ {
125
+ if (g_lights[i].type == Last) break ;
126
+
127
+ switch (g_lights[i].type)
128
+ {
129
+ case Directional:
130
+ output += ComputeDirectionalLight (g_lights[i], normal, toEye, diffuse.rgb, material);
131
+ break ;
132
+ case Point:
133
+ output += ComputePointLight (g_lights[i], objectPosition, normal, toEye, diffuse.rgb, material);
134
+ break ;
135
+ case Spot:
136
+ output += ComputeSpotLight (g_lights[i], objectPosition, normal, toEye, diffuse.rgb, material);
137
+ break ;
138
+ }
139
+ }
140
+
141
+ float3 ambient = material.ambient * diffuse.rgb;
142
+ output += ambient;
143
+
144
+ return float4 (output, diffuse.a);
153
145
}
0 commit comments