lights_pars.glsl 6.0 KB
Newer Older
1
uniform vec3 ambientLightColor;
2

W
WestLangley 已提交
3 4 5 6 7 8
vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {


	return PI * ambientLightColor;

}
9 10 11 12 13 14 15 16 17 18

#if MAX_DIR_LIGHTS > 0

	struct DirectionalLight {
	  vec3 direction;
	  vec3 color;
	};

	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];

M
Mr.doob 已提交
19
	IncidentLight getDirectionalDirectLight( const in DirectionalLight directionalLight, const in GeometricContext geometry ) {
B
Ben Houston 已提交
20 21

		IncidentLight directLight;
M
Mr.doob 已提交
22

23
		directLight.color = directionalLight.color;
M
Mr.doob 已提交
24
		directLight.direction = directionalLight.direction;
25

B
Ben Houston 已提交
26
		return directLight;
27 28 29 30
	}

#endif

B
Ben Houston 已提交
31

32 33 34 35 36 37 38 39 40 41 42
#if MAX_POINT_LIGHTS > 0

	struct PointLight {
	  vec3 position;
	  vec3 color;
	  float distance;
	  float decay;
	};

	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];

M
Mr.doob 已提交
43 44
	IncidentLight getPointDirectLight( const in PointLight pointLight, const in GeometricContext geometry ) {

B
Ben Houston 已提交
45
		IncidentLight directLight;
M
Mr.doob 已提交
46 47 48 49 50 51 52

		vec3 lVector = pointLight.position - geometry.position;
		directLight.direction = normalize( lVector );

		directLight.color = pointLight.color;
		directLight.color *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay );

B
Ben Houston 已提交
53
		return directLight;
54 55 56 57
	}

#endif

B
Ben Houston 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72
#if MAX_SPOT_LIGHTS > 0

	struct SpotLight {
	  vec3 position;
	  vec3 direction;
	  vec3 color;
	  float distance;
	  float decay;
	  float angleCos;
	  float exponent;
	};

	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];

B
Ben Houston 已提交
73
	IncidentLight getSpotDirectLight( const in SpotLight spotLight, const in GeometricContext geometry ) {
M
Mr.doob 已提交
74

B
Ben Houston 已提交
75 76
		IncidentLight directLight;

B
Ben Houston 已提交
77
		vec3 lVector = spotLight.position - geometry.position;
78
		directLight.direction = normalize( lVector );
M
Mr.doob 已提交
79

B
Ben Houston 已提交
80 81 82 83 84 85
		float spotEffect = dot( directLight.direction, spotLight.direction );

		if ( spotEffect > spotLight.angleCos ) {

			float spotEffect = dot( spotLight.direction, directLight.direction );
			spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );
M
Mr.doob 已提交
86

B
Ben Houston 已提交
87 88 89 90 91 92 93 94
			directLight.color = spotLight.color;
			directLight.color *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );

		}
		else {

			directLight.color = vec3( 0.0 );
		}
95

B
Ben Houston 已提交
96
		return directLight;
B
Ben Houston 已提交
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	}

#endif


#if MAX_HEMI_LIGHTS > 0

	struct HemisphereLight {
	  vec3 direction;
	  vec3 skyColor;
	  vec3 groundColor;
	};

	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];

W
WestLangley 已提交
113
	vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
M
Mr.doob 已提交
114

115 116
		float dotNL = dot( geometry.normal, hemiLight.direction );
		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
117

W
WestLangley 已提交
118
		return PI * mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
119 120 121 122

	}

#endif
123 124


125
#if defined( USE_ENVMAP ) && defined( PHYSICAL )
126

127
	vec3 getLightProbeIndirectIrradiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in int maxMIPLevel ) {
B
Ben Houston 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

		#ifdef DOUBLE_SIDED

			float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );

		#else

			float flipNormal = 1.0;

		#endif

		vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );

		#ifdef ENVMAP_TYPE_CUBE

B
Ben Houston 已提交
143
			vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
B
Ben Houston 已提交
144

145 146 147
			// TODO: replace with properly filtered cubemaps and access the irradiance LOD level, be it the last LOD level
			// of a specular cubemap, or just the default level of a specially created irradiance cubemap.

M
Mr.doob 已提交
148
			#if defined( TEXTURE_CUBE_LOD_EXT )
B
Ben Houston 已提交
149 150 151 152 153

				vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );

			#else

M
Mr.doob 已提交
154
				// force the bias high to get the last LOD level as it is the most blurred.
B
Ben Houston 已提交
155 156 157 158 159
				vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );

			#endif
		#else

M
Mr.doob 已提交
160
			vec3 envMapColor = vec3( 0.0 );
B
Ben Houston 已提交
161 162 163 164 165

		#endif

		envMapColor.rgb = inputToLinear( envMapColor.rgb );

W
WestLangley 已提交
166
		return PI * envMapColor.rgb;
B
Ben Houston 已提交
167 168 169

	}

170
	// taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
171
	float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
172

173
		//float envMapWidth = pow( 2.0, maxMIPLevelScalar );
174
		//float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( square( blinnShininessExponent ) + 1.0 );
175 176 177

		float maxMIPLevelScalar = float( maxMIPLevel );
		float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( square( blinnShininessExponent ) + 1.0 );
M
Mr.doob 已提交
178

179
		// clamp to allowable LOD ranges.
180
		return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
181 182 183

	}

184
	vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
M
Mr.doob 已提交
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		#ifdef ENVMAP_MODE_REFLECTION

			vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );

		#else

			vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );

		#endif

		#ifdef DOUBLE_SIDED

			float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );

		#else

			float flipNormal = 1.0;

		#endif

206
		reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
207 208 209

		#ifdef ENVMAP_TYPE_CUBE

210
			vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
211

M
Mr.doob 已提交
212
			#if defined( TEXTURE_CUBE_LOD_EXT )
213

214 215
				float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
				vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
216 217 218

			#else

219
				vec4 envMapColor = textureCube( envMap, queryReflectVec );
220 221

			#endif
M
Mr.doob 已提交
222

223 224 225 226 227 228

		#elif defined( ENVMAP_TYPE_EQUIREC )

			vec2 sampleUV;
			sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
			sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
229
			vec4 envMapColor = texture2D( envMap, sampleUV );
230 231 232 233

		#elif defined( ENVMAP_TYPE_SPHERE )

			vec3 reflectView = flipNormal * normalize((viewMatrix * vec4( reflectVec, 0.0 )).xyz + vec3(0.0,0.0,1.0));
234
			vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
235 236 237 238 239

		#endif

		envMapColor.rgb = inputToLinear( envMapColor.rgb );

240
		return envMapColor.rgb;
241 242 243 244

	}

#endif