| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #ifdef OPENGL_ES
- precision highp float;
- #endif
- // Uniforms
- uniform vec3 u_lightColor; // Light color
- uniform vec3 u_ambientColor; // Ambient color
- uniform sampler2D u_diffuseTexture; // Diffuse texture.
- // Inputs
- varying vec3 v_normalVector; // NormalVector in view space.
- varying vec2 v_texCoord; // Texture coordinate (u, v).
- // Global variables
- vec4 _baseColor; // Base color
- vec3 _ambientColor; // Ambient Color
- vec3 _diffuseColor; // Diffuse Color
- void lighting(vec3 normalVector, vec3 lightDirection, float attenuation)
- {
- // Ambient
- _ambientColor = _baseColor.rgb * u_ambientColor;
- // Diffuse
- float diffuseIntensity = attenuation * max(0.0, dot(normalVector, lightDirection));
- diffuseIntensity = max(0.0, diffuseIntensity);
- _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
- }
- #if defined(POINT_LIGHT)
- varying vec4 v_vertexToPointLightDirection; // Light direction w.r.t current vertex.
- void applyLight()
- {
- // Normalize the vectors.
- vec3 normalVector = normalize(v_normalVector);
-
- vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
-
- // Fetch point light attenuation.
- float pointLightAttenuation = v_vertexToPointLightDirection.w;
- lighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
- }
- #elif defined(SPOT_LIGHT)
- uniform vec3 u_spotLightDirection; // Direction of the spot light.
- uniform float u_spotLightInnerAngleCos; // The bright spot [0.0 - 1.0]
- uniform float u_spotLightOuterAngleCos; // The soft outer part [0.0 - 1.0]
- varying vec3 v_vertexToSpotLightDirection; // Light direction w.r.t current vertex.
- varying float v_spotLightAttenuation; // Attenuation of spot light.
- float lerpstep( float lower, float upper, float s)
- {
- return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
- }
- void applyLight()
- {
- // Normalize the vectors.
- vec3 normalVector = normalize(v_normalVector);
- vec3 spotLightDirection =normalize(u_spotLightDirection);
- vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
- // "-lightDirection" is used because light direction points in opposite direction to
- // to spot direction.
- // Calculate spot light effect.
- float spotCurrentAngleCos = max(0.0, dot(spotLightDirection, -vertexToSpotLightDirection));
- // Intensity of spot depends on the spot light attenuation and the
- // part of the cone vertexToSpotLightDirection points to (inner or outer).
- float spotLightAttenuation = clamp(v_spotLightAttenuation, 0.0, 1.0);
- spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
- lighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
- }
- #else
- uniform vec3 u_lightDirection; // Light direction
- void applyLight()
- {
- vec3 normalVector = normalize(v_normalVector);
- vec3 lightDirection = normalize(u_lightDirection);
- lighting(normalVector, -lightDirection, 1.0);
- }
- #endif
- void main()
- {
- // Fetch diffuse color from texture.
- _baseColor = texture2D(u_diffuseTexture, v_texCoord);
- // Apply light
- applyLight();
- // Light the pixel
- gl_FragColor.a = _baseColor.a;
- gl_FragColor.rgb = _ambientColor + _diffuseColor;
- }
|