lights_template.glsl 2.8 KB
Newer Older
1 2 3 4
//
// This is a template that can be used to light a material, it uses pluggable RenderEquations (RE)
//   for specific lighting scenarios.
//
5
// Instructions for use:
6 7
//  - Ensure that both Material_RE_DirectLight, Material_RE_IndirectDiffuseLight and Material_RE_IndirectSpecularLight are defined
//  - If you have defined a Material_RE_IndirectSpecularLight, you need to also provide a Material_LightProbeLOD.
8
//  - Create a material parameter that is to be passed as the third parameter to your lighting functions.
9 10 11 12 13 14
//
// TODO:
//  - Add area light support.
//  - Add sphere light support.
//  - Add diffuse light probe (irradiance cubemap) support.
//
15 16 17

GeometricContext geometry = GeometricContext( -vViewPosition, normalize( normal ), normalize(vViewPosition ) );

18
#if ( NUM_POINT_LIGHTS > 0 ) && defined( Material_RE_DirectLight )
19

20
	for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
21 22 23

		IncidentLight directLight = getPointDirectLight( pointLights[ i ], geometry );

W
WestLangley 已提交
24
		Material_RE_DirectLight( directLight, geometry, material, reflectedLight );
25 26 27 28 29

	}

#endif

30
#if ( NUM_SPOT_LIGHTS > 0 ) && defined( Material_RE_DirectLight )
31

32
	for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
33 34 35

		IncidentLight directLight = getSpotDirectLight( spotLights[ i ], geometry );

W
WestLangley 已提交
36
		Material_RE_DirectLight( directLight, geometry, material, reflectedLight );
37 38 39 40 41

	}

#endif

42
#if ( NUM_DIR_LIGHTS > 0 ) && defined( Material_RE_DirectLight )
43

44
	for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
45 46 47

		IncidentLight directLight = getDirectionalDirectLight( directionalLights[ i ], geometry );

W
WestLangley 已提交
48
		Material_RE_DirectLight( directLight, geometry, material, reflectedLight );
49

50 51 52 53
	}

#endif

54
#if defined( Material_RE_IndirectDiffuseLight )
55

56
	{
57

W
WestLangley 已提交
58
		vec3 indirectDiffuseIrradiance = getAmbientLightIrradiance( ambientLightColor );
59

60
#ifdef USE_LIGHTMAP
61

W
WestLangley 已提交
62
		indirectDiffuseIrradiance += PI * texture2D( lightMap, vUv2 ).xyz * lightMapIntensity; // factor of PI should not be present; included here to prevent breakage
63 64 65

#endif

66
#if ( NUM_HEMI_LIGHTS > 0 )
67

68
		for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
69

W
WestLangley 已提交
70
			indirectDiffuseIrradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
71

72 73
		}

B
Ben Houston 已提交
74 75
#endif

76
#if defined( USE_ENVMAP ) && defined( STANDARD )
B
Ben Houston 已提交
77 78

		// TODO, replace 8 with the real maxMIPLevel
79
		indirectDiffuseIrradiance += getLightProbeIndirectIrradiance( /*lightProbe,*/ geometry, 8 );
B
Ben Houston 已提交
80

81
#endif
82

W
WestLangley 已提交
83
		Material_RE_IndirectDiffuseLight( indirectDiffuseIrradiance, geometry, material, reflectedLight );
84 85 86 87

	}

#endif
88

89
#if defined( USE_ENVMAP ) && defined( Material_RE_IndirectSpecularLight )
90 91 92

	{

B
Ben Houston 已提交
93
		// TODO, replace 8 with the real maxMIPLevel
94
		vec3 indirectSpecularRadiance = getLightProbeIndirectRadiance( /*specularLightProbe,*/ geometry, Material_BlinnShininessExponent( material ), 8 );
95

W
WestLangley 已提交
96
		Material_RE_IndirectSpecularLight( indirectSpecularRadiance, geometry, material, reflectedLight );
97 98 99

    }

100
#endif