| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <!--
- Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
- All rights reserved.
- Code licensed under the BSD License.
- http://www.anki3d.org/LICENSE
- -->
- <!-- Classic deferred lighting shader -->
- <shaderProgram>
- <mutators>
- <mutator name="LIGHT_TYPE" values="0 1"/>
- </mutators>
- <shaders>
- <shader type="vert">
- <source><![CDATA[
- #include "shaders/Common.glsl"
- layout(location = 0) in vec3 in_position;
- out gl_PerVertex
- {
- vec4 gl_Position;
- };
- layout(ANKI_UBO_BINDING(0, 0), row_major) uniform u0_
- {
- mat4 u_mvp;
- };
- void main()
- {
- gl_Position = u_mvp * vec4(in_position, 1.0);
- }
- ]]></source>
- </shader>
- <shader type="frag">
- <inputs>
- <input name="FB_SIZE" type="uvec2" const="1"/>
- </inputs>
- <source><![CDATA[
- #include "shaders/Pack.glsl"
- #include "shaders/LightFunctions.glsl"
- #define POINT_LIGHT_TYPE 0
- #define SPOT_LIGHT_TYPE 1
- layout(location = 0) out vec3 out_color;
- // Point light
- struct PointLight
- {
- vec4 posRadius; // xyz: Light pos in world space. w: The -1/radius
- vec4 diffuseColorPad1; // xyz: diff color
- };
- // Spot light
- struct SpotLight
- {
- vec4 posRadius; // xyz: Light pos in world space. w: The -1/radius
- vec4 diffuseColorOuterCos; // xyz: diff color, w: outer cosine of spot
- vec4 lightDirInnerCos; // xyz: light dir, w: inner cosine of spot
- };
- layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
- layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
- layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
- layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
- layout(ANKI_UBO_BINDING(0, 1), row_major) uniform u1_
- {
- vec4 u_inputTexUvScaleAndOffset; // Use this to get the correct face UVs
- mat4 u_invViewProjMat;
- vec4 u_camPosPad1;
- #if LIGHT_TYPE == POINT_LIGHT_TYPE
- PointLight u_light;
- #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
- SpotLight u_light;
- #else
- # error See file
- #endif
- };
- #if LIGHT_TYPE == POINT_LIGHT_TYPE
- # define u_ldiff u_light.diffuseColorPad1.xyz
- #else
- # define u_ldiff u_light.diffuseColorOuterCos.xyz
- # define u_lightDir u_light.lightDirInnerCos.xyz
- # define u_outerCos u_light.diffuseColorOuterCos.w
- # define u_innerCos u_light.lightDirInnerCos.w
- # define u_lightDir u_light.lightDirInnerCos.xyz
- #endif
- #define u_camPos u_camPosPad1.xyz
- void main()
- {
- // Compute UV coordinates
- vec2 uv = vec2(gl_FragCoord.xy) / vec2(FB_SIZE.x, FB_SIZE.y);
- vec2 uvToRead = fma(uv, u_inputTexUvScaleAndOffset.xy, u_inputTexUvScaleAndOffset.zw);
- // Do manual depth test
- float depth = texture(u_msDepthRt, uvToRead).r;
- if(gl_FragCoord.z < depth)
- {
- discard;
- }
- // Decode and process gbuffer
- GbufferInfo gbuffer;
- readGBuffer(u_msRt0, u_msRt1, u_msRt2, uvToRead, 0.0, gbuffer);
- vec4 worldPos4 = u_invViewProjMat * vec4(UV_TO_NDC(uv), depth, 1.0);
- vec3 worldPos = worldPos4.xyz / worldPos4.w;
- // Calculate the light color
- vec3 viewDir = normalize(u_camPos - worldPos);
- vec3 frag2Light = u_light.posRadius.xyz - worldPos;
- vec3 l = normalize(frag2Light);
- float nol = max(0.0, dot(gbuffer.normal, l));
- vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l);
- vec3 diffC = diffuseLambert(gbuffer.diffuse);
- float att = computeAttenuationFactor(u_light.posRadius.w, frag2Light);
- float lambert = nol;
- #if LIGHT_TYPE == POINT_LIGHT_TYPE
- out_color = (specC + diffC) * u_ldiff * (att * max(lambert, gbuffer.subsurface));
- #else
- float spot = computeSpotFactor(l, u_outerCos, u_innerCos, u_lightDir);
- out_color = (diffC + specC) * u_ldiff * (att * spot * max(lambert, gbuffer.subsurface));
- #endif
- }
- ]]></source>
- </shader>
- </shaders>
- </shaderProgram>
|