Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Example/GLSLES/Instancing.frag @ 12091

Last change on this file since 12091 was 12091, checked in by wiesep, 6 years ago

Updated programs and adjusted Material to work with GLSL>150

File size: 2.7 KB
Line 
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
8precision mediump int;
9precision mediump float;
10
11uniform sampler2D diffuseMap;
12
13uniform vec4    lightPosition;
14uniform vec3    cameraPosition;
15uniform vec3    lightAmbient;
16uniform vec3    lightDiffuse;
17uniform vec3    lightSpecular;
18uniform vec4    lightAttenuation;
19uniform float   lightGloss;
20
21#if DEPTH_SHADOWRECEIVER
22uniform float invShadowMapSize;
23uniform 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
34vec4 offsetSample(vec4 uv, vec2 offset, float invMapSize)
35{
36        return vec4(uv.xy + offset * invMapSize * uv.w, uv.z, uv.w);
37}
38
39float 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
59in vec2 _uv0;
60in vec3 oNormal;
61in vec3 oVPos;
62#if DEPTH_SHADOWRECEIVER
63        in vec4 oLightSpacePos;
64#endif
65out vec4 fragColour;
66
67//---------------------------------------------
68//Main Pixel Shader
69//---------------------------------------------
70void 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}
Note: See TracBrowser for help on using the repository browser.