Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lightView =
- Matrix4.LookAt(light.lightPosition, light.lightPosition - Vector3.UnitZ, Vector3.UnitY);
- #version 120
- uniform mat4 worldView;
- uniform float direction;
- attribute vec3 position; //usage:position
- attribute vec3 normal; //usage:normal
- varying highp vec2 depth;
- varying float clip;
- const float near = 0.1f;
- const float far = 1000.0f;
- void main()
- {
- highp vec4 pos4 = worldView * vec4(position, 1);
- pos4 = pos4 / pos4.w;
- pos4.z = pos4.z * direction;
- highp float L = length(pos4.xyz);
- pos4 = pos4 / L;
- clip = pos4.z;
- pos4.z = pos4.z + 1;
- pos4.x = pos4.x / pos4.z;
- pos4.y = pos4.y / pos4.z;
- pos4.z = (L - near) / (far - near);
- pos4.w = 1;
- depth = pos4.zw;
- gl_Position = pos4;
- }
- #version 120
- varying highp vec2 depth;
- varying float clip;
- vec4 mapDepthToARGB32(float value)
- {
- const vec4 bitSh = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
- const vec4 mask = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
- highp vec4 res = fract(value * bitSh);
- res -= res.xxyz * mask;
- return res;
- }
- void main()
- {
- if(clip < 0)
- discard;
- float finalDepth = depth.x / depth.y;
- gl_FragColor = mapDepthToARGB32(finalDepth); //vec4(depth.y, depth.y, depth.y, 1.0f);
- }
- #version 120
- uniform highp vec3 lightPosition;
- uniform mat4 worldViewProjection;
- uniform mat4 world;
- attribute vec3 position; //usage:position
- attribute vec2 uv; //usage:uv
- attribute vec3 normal; //usage:normal
- varying highp vec3 faceNormal;
- varying highp vec3 lightNormal;
- varying highp vec2 finalUv;
- varying highp vec3 pos;
- void main()
- {
- vec4 pos4 = vec4(position, 1);
- pos = (world * pos4).xyz;
- gl_Position = worldViewProjection * pos4;
- lightNormal = lightPosition - pos;
- faceNormal = (world * vec4(normal , 0)).xyz;
- finalUv = uv;
- }
- #version 120
- uniform highp vec3 lightColor;
- uniform highp vec3 ambientColor;
- uniform mat4 lightView;
- uniform highp float uvMultiplier;
- uniform sampler2D colorTexture1;
- uniform sampler2D colorTexture2;
- uniform sampler2D colorTexture3;
- uniform sampler2D weightTexture;
- uniform sampler2D frontShadowMap;
- uniform sampler2D backShadowMap;
- varying highp vec3 faceNormal;
- varying highp vec3 lightNormal;
- varying highp vec2 finalUv;
- varying highp vec3 pos;
- const float SHADOW_EPSILON = 0.00005f;
- float getDepthFromARGB32(vec4 value)
- {
- const vec4 bitSh = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
- return(dot(value, bitSh));
- }
- const float near = 0.1f;
- const float far = 1000;
- void main()
- {
- highp float factor = clamp(dot(normalize(lightNormal), normalize(faceNormal)), 0.0, 1.0);
- highp vec3 finalColor = vec3(0,0,0);
- lowp vec4 weights = texture2D(weightTexture, finalUv);
- finalColor += texture2D(colorTexture1, finalUv * uvMultiplier).xyz * weights.x;
- finalColor += texture2D(colorTexture2, finalUv * uvMultiplier).xyz * weights.y;
- finalColor += texture2D(colorTexture3, finalUv * uvMultiplier).xyz * weights.z;
- finalColor = (finalColor * lightColor * factor) + (finalColor * ambientColor);
- highp vec3 vPosDP = (vec4(pos, 1) * lightView).xyz;
- float fLength = length(vPosDP);
- vPosDP /= fLength;
- float fDPDepth;
- float fSceneDepth;
- if(vPosDP.z >= 0.0f)
- {
- vec2 vTexFront;
- vTexFront.x = (vPosDP.x / (1.0f + vPosDP.z)) * 0.5f + 0.5f;
- vTexFront.y = 1.0f - ((vPosDP.y / (1.0f + vPosDP.z)) * 0.5f + 0.5f);
- fSceneDepth = (fLength - near) / (far - near);
- fDPDepth = getDepthFromARGB32(texture2D(frontShadowMap, vTexFront));
- }
- else
- {
- // for the back the z has to be inverted
- vec2 vTexBack;
- vTexBack.x = (vPosDP.x / (1.0f - vPosDP.z)) * 0.5f + 0.5f;
- vTexBack.y = 1.0f - ((vPosDP.y / (1.0f - vPosDP.z)) * 0.5f + 0.5f);
- fSceneDepth = (fLength - near) / (far - near);
- fDPDepth = getDepthFromARGB32(texture2D(backShadowMap, vTexBack));
- }
- if((fDPDepth + SHADOW_EPSILON) < fSceneDepth)
- finalColor = finalColor * 0.3f;
- gl_FragColor = vec4(finalColor, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement