bgfx/examples/31-rsm/vs_rsm_lbuffer.sc

61 lines
1.9 KiB
Python
Raw Normal View History

$input a_position
$output v_lightCenterScale, v_color0
/*
* Copyright 2016 Joseph Cherlin. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
uniform vec4 u_sphereInfo;
uniform mat4 u_invMvpShadow;
// Note texture binding starts at slot 2. Problem is that vert textures and frag textures are different.
SAMPLER2D(s_shadowMap, 2); // Used to reconstruct 3d position for lights
SAMPLER2D(s_rsm, 3); // Reflective shadow map, used to scale/color light
float toClipSpaceDepth(float _depthTextureZ)
{
2017-02-03 05:09:52 +03:00
#if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL
2016-07-25 02:07:09 +03:00
return _depthTextureZ;
#else
2016-07-25 02:07:09 +03:00
return _depthTextureZ * 2.0 - 1.0;
2017-02-03 05:09:52 +03:00
#endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL
}
vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos)
{
2016-07-25 02:07:09 +03:00
vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) );
return wpos.xyz / wpos.w;
}
void main()
{
2016-07-25 02:07:09 +03:00
// Calculate vertex position
vec3 objectSpacePos = a_position;
2016-07-25 02:07:09 +03:00
vec2 texCoord = u_sphereInfo.xy;
2016-07-25 02:07:09 +03:00
// Get world position using the shadow map
float deviceDepth = texture2DLod(s_shadowMap, texCoord, 0).x;
float depth = toClipSpaceDepth(deviceDepth);
vec3 clip = vec3(texCoord * 2.0 - 1.0, depth);
2017-02-03 05:09:52 +03:00
#if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL
2016-07-25 02:07:09 +03:00
clip.y = -clip.y;
2017-02-03 05:09:52 +03:00
#endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL
2016-07-25 02:07:09 +03:00
vec3 wPos = clipToWorld(u_invMvpShadow, clip);
wPos.y -= 0.001; // Would be much better to perturb in normal direction, but I didn't do that.
// Scale and color are already in the rsm
vec4 rsm = texture2DLod(s_rsm, texCoord, 0).xyzw;
float radScale = u_sphereInfo.z;
float rad = rsm.w * radScale;
gl_Position = mul(u_viewProj, vec4(wPos+objectSpacePos*rad, 1.0) );
v_lightCenterScale.xyz = wPos.xyz;
v_lightCenterScale.w = rad;
v_color0.xyz = rsm.xyz;
}