forked from vixorien/D3D11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light.hlsli
56 lines (48 loc) · 1.19 KB
/
light.hlsli
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
#ifndef __GGP_LIGHT__
#define __GGP_LIGHT__
/*
Everything here must be matched by Light.h
*/
#define MAX_SPECULAR_EXPONENT (256.0f)
#define LIGHT_TYPE_DIRECTIONAL (0)
#define LIGHT_TYPE_POINT (1)
#define LIGHT_TYPE_SPOT (2)
#define MAX_LIGHTS (5)
struct Light
{
int type;
float3 direction;
float range;
float3 position;
float intensity;
float3 color;
float spotInnerAngleRadians;
float spotOuterAngleRadians;
int isShadowCaster;
float _padding;
float4x4 shadowView;
float4x4 shadowProjection;
};
float3 LightDirection(Light light, float3 worldPosition)
{
switch (light.type)
{
case LIGHT_TYPE_DIRECTIONAL:
return -light.direction;
default:
return normalize(light.position - worldPosition);
}
}
float LightAttenuation(Light light, float3 worldPosition)
{
if (light.type == LIGHT_TYPE_DIRECTIONAL)
{
return 1.f;
}
float attenuationFactor = 0.f;
float dist = distance(light.position, worldPosition);
attenuationFactor = saturate(1.0f - (dist * dist) / (light.range * light.range));
attenuationFactor *= attenuationFactor;
return attenuationFactor;
}
#endif