| 1 | #version 100 |
|---|
| 2 | |
|---|
| 3 | precision mediump int; |
|---|
| 4 | precision mediump float; |
|---|
| 5 | |
|---|
| 6 | float shadowPCF(sampler2D shadowMap, vec4 shadowMapPos, vec2 offset) |
|---|
| 7 | { |
|---|
| 8 | shadowMapPos = shadowMapPos / shadowMapPos.w; |
|---|
| 9 | vec2 uv = shadowMapPos.xy; |
|---|
| 10 | vec3 o = vec3(offset, -offset.x) * 0.3; |
|---|
| 11 | |
|---|
| 12 | // Note: We using 2x2 PCF. Good enough and is a lot faster. |
|---|
| 13 | float c = (shadowMapPos.z <= texture2D(shadowMap, uv.xy - o.xy).r) ? 1.0 : 0.0; // top left |
|---|
| 14 | c += (shadowMapPos.z <= texture2D(shadowMap, uv.xy + o.xy).r) ? 1.0 : 0.0; // bottom right |
|---|
| 15 | c += (shadowMapPos.z <= texture2D(shadowMap, uv.xy + o.zy).r) ? 1.0 : 0.0; // bottom left |
|---|
| 16 | c += (shadowMapPos.z <= texture2D(shadowMap, uv.xy - o.zy).r) ? 1.0 : 0.0; // top right |
|---|
| 17 | |
|---|
| 18 | return c / 4.0; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | uniform vec4 lightPosition; // object space |
|---|
| 22 | uniform vec3 eyePosition; // object space |
|---|
| 23 | uniform mat4 worldViewProjMatrix; |
|---|
| 24 | uniform mat4 texWorldViewProjMatrix0; |
|---|
| 25 | uniform mat4 texWorldViewProjMatrix1; |
|---|
| 26 | uniform mat4 texWorldViewProjMatrix2; |
|---|
| 27 | |
|---|
| 28 | varying vec4 oUv0; |
|---|
| 29 | varying vec3 oLightDir; |
|---|
| 30 | varying vec3 oHalfAngle; |
|---|
| 31 | varying vec4 oLightPosition0; |
|---|
| 32 | varying vec4 oLightPosition1; |
|---|
| 33 | varying vec4 oLightPosition2; |
|---|
| 34 | varying vec3 oNormal; |
|---|
| 35 | |
|---|
| 36 | attribute vec4 position; |
|---|
| 37 | attribute vec3 normal; |
|---|
| 38 | attribute vec4 uv0; |
|---|
| 39 | |
|---|
| 40 | void main() |
|---|
| 41 | { |
|---|
| 42 | // Calculate output position |
|---|
| 43 | gl_Position = worldViewProjMatrix * position; |
|---|
| 44 | |
|---|
| 45 | // Pass the main uvs straight through unchanged |
|---|
| 46 | oUv0.xy = uv0.xy; |
|---|
| 47 | oUv0.z = gl_Position.z; |
|---|
| 48 | |
|---|
| 49 | // Calculate tangent space light vector |
|---|
| 50 | // Get object space light direction |
|---|
| 51 | oLightDir = normalize(lightPosition.xyz - (position * lightPosition.w).xyz); |
|---|
| 52 | |
|---|
| 53 | // Calculate half-angle in tangent space |
|---|
| 54 | vec3 eyeDir = normalize(eyePosition - position.xyz); |
|---|
| 55 | oHalfAngle = normalize(eyeDir + oLightDir); |
|---|
| 56 | |
|---|
| 57 | // Calculate the position of vertex in light space |
|---|
| 58 | oLightPosition0 = texWorldViewProjMatrix0 * position; |
|---|
| 59 | oLightPosition1 = texWorldViewProjMatrix1 * position; |
|---|
| 60 | oLightPosition2 = texWorldViewProjMatrix2 * position; |
|---|
| 61 | |
|---|
| 62 | oNormal = normal; |
|---|
| 63 | } |
|---|