1 | //--------------------------------------------------------------------------- |
---|
2 | //These materials/shaders are part of the NEW InstanceManager implementation |
---|
3 | //Written by Matias N. Goldberg ("dark_sylinc") |
---|
4 | //--------------------------------------------------------------------------- |
---|
5 | |
---|
6 | #version 300 es |
---|
7 | |
---|
8 | precision mediump int; |
---|
9 | precision mediump float; |
---|
10 | |
---|
11 | uniform sampler2D diffuseMap; |
---|
12 | |
---|
13 | uniform vec4 lightPosition; |
---|
14 | uniform vec3 cameraPosition; |
---|
15 | uniform vec3 lightAmbient; |
---|
16 | uniform vec3 lightDiffuse; |
---|
17 | uniform vec3 lightSpecular; |
---|
18 | uniform vec4 lightAttenuation; |
---|
19 | uniform float lightGloss; |
---|
20 | |
---|
21 | #if DEPTH_SHADOWRECEIVER |
---|
22 | uniform float invShadowMapSize; |
---|
23 | uniform lowp sampler2DShadow shadowMap; |
---|
24 | |
---|
25 | //declare external function |
---|
26 | |
---|
27 | // Simple PCF |
---|
28 | // Number of samples in one dimension (square for total samples) |
---|
29 | #define NUM_SHADOW_SAMPLES_1D 2.0 |
---|
30 | #define SHADOW_FILTER_SCALE 1.0 |
---|
31 | |
---|
32 | #define SHADOW_SAMPLES NUM_SHADOW_SAMPLES_1D*NUM_SHADOW_SAMPLES_1D |
---|
33 | |
---|
34 | vec4 offsetSample(vec4 uv, vec2 offset, float invMapSize) |
---|
35 | { |
---|
36 | return vec4(uv.xy + offset * invMapSize * uv.w, uv.z, uv.w); |
---|
37 | } |
---|
38 | |
---|
39 | float calcDepthShadow(lowp sampler2DShadow shadowMap, vec4 uv, float invShadowMapSize) |
---|
40 | { |
---|
41 | // 4-sample PCF |
---|
42 | |
---|
43 | float shadow = 0.0; |
---|
44 | float offset = (NUM_SHADOW_SAMPLES_1D/2.0 - 0.5) * SHADOW_FILTER_SCALE; |
---|
45 | for (float y = -offset; y <= offset; y += SHADOW_FILTER_SCALE) |
---|
46 | for (float x = -offset; x <= offset; x += SHADOW_FILTER_SCALE) |
---|
47 | { |
---|
48 | float depth = textureProj(shadowMap, offsetSample(uv, vec2(x, y), invShadowMapSize)); |
---|
49 | if (depth >= 1.0 || depth >= uv.z) |
---|
50 | shadow += 1.0; |
---|
51 | } |
---|
52 | |
---|
53 | shadow /= SHADOW_SAMPLES; |
---|
54 | |
---|
55 | return shadow; |
---|
56 | } |
---|
57 | #endif |
---|
58 | |
---|
59 | in vec2 _uv0; |
---|
60 | in vec3 oNormal; |
---|
61 | in vec3 oVPos; |
---|
62 | #if DEPTH_SHADOWRECEIVER |
---|
63 | in vec4 oLightSpacePos; |
---|
64 | #endif |
---|
65 | out vec4 fragColour; |
---|
66 | |
---|
67 | //--------------------------------------------- |
---|
68 | //Main Pixel Shader |
---|
69 | //--------------------------------------------- |
---|
70 | void main(void) |
---|
71 | { |
---|
72 | vec4 color = texture( diffuseMap, _uv0 ); |
---|
73 | |
---|
74 | float fShadow = 1.0; |
---|
75 | #if DEPTH_SHADOWRECEIVER |
---|
76 | fShadow = calcDepthShadow( shadowMap, oLightSpacePos, invShadowMapSize ); |
---|
77 | #endif |
---|
78 | |
---|
79 | vec4 baseColour = texture( diffuseMap, _uv0 ); |
---|
80 | |
---|
81 | //Blinn-Phong lighting |
---|
82 | vec3 normal = normalize( oNormal ); |
---|
83 | vec3 lightDir = lightPosition.xyz - oVPos * lightPosition.w; |
---|
84 | vec3 eyeDir = normalize( cameraPosition - oVPos ); |
---|
85 | |
---|
86 | float fLength = length( lightDir ); |
---|
87 | lightDir = normalize( lightDir ); |
---|
88 | |
---|
89 | float NdotL = max( 0.0, dot( normal, lightDir ) ); |
---|
90 | vec3 halfVector = normalize(lightDir + eyeDir); |
---|
91 | float HdotN = max( 0.0, dot( halfVector, normal ) ); |
---|
92 | |
---|
93 | vec3 ambient = lightAmbient * baseColour.xyz; |
---|
94 | vec3 diffuse = lightDiffuse * NdotL * baseColour.xyz; |
---|
95 | vec3 specular = lightSpecular * pow( HdotN, lightGloss ); |
---|
96 | |
---|
97 | vec3 directLighting = (diffuse + specular) * fShadow; |
---|
98 | |
---|
99 | fragColour = vec4( directLighting + ambient, baseColour.a ); |
---|
100 | //fragColour = baseColour; |
---|
101 | } |
---|