Garry's Mod

Garry's Mod

1,160 ratings
GShader Library
13
5
7
9
4
4
2
2
7
4
3
2
2
2
   
Award
Favorite
Favorited
Unfavorite
Content Type: Addon
Addon Type: Tool
Addon Tags: Scenic
File Size
Posted
Updated
239.741 KB
5 Aug @ 6:08am
4 Oct @ 7:26pm
85 Change Notes ( view )

Subscribe to download
GShader Library

Description
GShader Library
— a shader library that serves as the foundation for creating Deferred Renderer (shading & lighting) shaders, acting as a convenient tool for creating advanced post-processing effects.

Brief list of textures in the addon:
  • _rt_WPDepth
  • _rt_NormalsTangents
  • _rt_Velocity
  • _rt_ResolvedFullFrameDepth
  • _rt_Bump
  • _rt_Fog

The addon includes:
  • Reconstruction of WorldPos, WorldNormals and Tangents from _rt_ResolvedFullFrameDepth.
  • Normal smoothing.
  • Increasing the bit depth of the depth buffer _rt_ResolvedFullFrameDepth.
  • View and projection matrices: View, Proj, ViewProj. For perspective and orthogonal projection.
  • Newly discovered texture formats that allow more flexible work with shaders.
  • Encoding Normals and Tangents into a single texture, packing WorldPos and Depth, which will fit within the 4-texture limit in screenspace_general.
  • Choice of normal reconstruction method[wickedengine.net]: Simple, 3 Tap, Improved, Accurate[atyuwen.github.io].
  • Function shaderlib.DrawScreenQuad() with Multiple Render Target support.
  • Function shaderlib.DrawVertexScreenQuad() with input data to vertex shader and MRT support. More info here: Example 6[github.com].
  • Velocity Buffer.

Encoding Normals and Tangents
Normals and Tangents are stored in the render target _rt_NormalsTangents, where:
  • .RG — Normals
  • .B — Tangents
  • .A — Sign: A value of 1 or -1. It will help you with lighting and creating Post-Process Parallax Mapping.
Normals are encoded using Octahedron normal vector encoding [knarkowicz.wordpress.com]
Normal decoding is done via the function:
float3 Decode(float2 f) { f = f * 2.0 - 1.0; // https://twitter.com/Stubbesaurus/status/937994790553227264 float3 n = float3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y)); float t = saturate(-n.z); n.xy += n.xy >= 0.0 ? -t : t; return normalize(n); }
Tangents are encoded using Diamond Encoding [www.jeremyong.com]
Tangent decoding is done via the function:
float2 decode_diamond(float p) { float2 v; // Remap p to the appropriate segment on the diamond float p_sign = sign(p - 0.5f); v.x = -p_sign * 4.f * p + 1.f + p_sign * 2.f; v.y = p_sign * (1.f - abs(v.x)); // Normalization extends the point on the diamond back to the unit circle return normalize(v); } float3 decode_tangent(float3 normal, float diamond_tangent) { // As in the encode step, find our canonical tangent basis span(t1, t2) float3 t1; if (abs(normal.y) > abs(normal.z)) { t1 = float3(normal.y, -normal.x, 0.f); } else { t1 = float3(normal.z, 0.f, -normal.x); } t1 = normalize(t1); float3 t2 = cross(t1, normal); // Recover the coordinates used with t1 and t2 float2 packed_tangent = decode_diamond(diamond_tangent); return packed_tangent.x * t1 + packed_tangent.y * t2; }

Example of working with _rt_NormalsTangents:
float4 normals_tangets = tex2D(NormalTangentBuffer,uv); float flipSign = normals_tangets.a; float3 worldNormal = Decode(normals_tangets.xy); float3 tangents = decode_tangent(worldNormal, normals_tangets.z); float3 binormals = normalize(cross(worldNormal,tangents))* flipSign; float3x3 TBN = float3x3(tangents, binormals, worldNormal);

Packing WorldPos and Depth
WorldPos and Depth are stored in the render target _rt_WPDepth, where:
  • .RGB1/WorldPos: This means that WorldPos is packed into values <1. To unpack, use float3 worldPos = 1/tex2D(WPDepthBuffer,uv).xyz; in the shader.
  • .A — Depth

NOTE:
The depth buffer does not write translucent objects, so you will most likely render shaders in the PreDrawTranslucentRenderables [wiki.facepunch.com] hook.

Special thanks to:
Meetric [github.com] — WorldPos reconstruction.
notunknowndude— the idea to improve the depth buffer.
puwada — the tip about texture format compatibility and DirectX.
LVutner [github.com] — implementation Velocity Buffer encoding of CryTeck method.

Links:

Originally posted by devonium:
SHADERS FOR EVERYONE!!!! i have no idea wtf is going on

P.S. Shader examples are not in the addon. Screenshots show what the GShader library allows you to do.
Popular Discussions View All (15)
34
16 Sep @ 8:05pm
Why is there a DMCA notice on this Addon?
CosmicInterloper
11
19 Sep @ 5:29am
can someone send a shader from this addon if there is one?
ModestShoducks
3
18 Sep @ 4:44pm
Black Sky when enabled (Help?)
LCOOLJ360
400 Comments
da ragin yajun 4 hours ago 
Hello Mr. Akabenko,

I'm currently having issues with GShader Library that prevent me from using it or any of its associated mods. Loading into a map greets me with this Lua error:

[GShader Library] lua/autorun/client/fogmask.lua:7: bad argument #8 to 'GetRenderTargetEx' (number expected, got nil)
1. GetRenderTargetEx - [C]:-1
2. unknown - lua/autorun/client/fogmask.lua:7
3. Run - lua/includes/modules/hook.lua:313
4. unknown - lua/autorun/client/proton_detect.lua:68

The GetRenderTargetEx value being nil is odd, and it seems to cause the viewport to break when using mods like the CSM shader someone else made using this library. I thought GWater 2 was to blame for this, as it seems to add additional graphics libraries to enable PhysX Flex water rendering. However, disabling and uninstalling that did nothing to fix it.

Any help would be greatly appreciated!
G. Possum 12 hours ago 
pretty much just breaks viewmodels by just copying the full model with practically no mods that majorly affect first person
cutmyheadoff 8 Oct @ 8:44pm 
Recently started causing some peculiar glitches with Vmanip. My viewmodel (c_hands with a weapon) start moving around the map when switching from an arc9/MWbase weapon to any other tool.
ste 8 Oct @ 2:31pm 
experiencing the same issue, except turning every setting off doesnt fix it for me
Designated Kitty 03-052 8 Oct @ 1:55pm 
if I turn every setting off it goes back to normal. Weird.
Designated Kitty 03-052 8 Oct @ 1:49pm 
This all the sudden started messing with vmanip. It just makes it disappear when looking straight horizontal but comes back if you look up or down. I don't know whats happening.
C1TY 6 Oct @ 3:03pm 
when using ARC9 weapon and aiming with a scope at the skybox the skybox wont show but the outside of the scope while aiming does show
da ragin yajun 3 Oct @ 7:09pm 
Check if you have Gmod Legs V3 installed, the way they simulated first-person legs was kinda janky and tended to break with certain mods. Uninstalling that fixed it for me, and the shader library wouldn't cause that sort of corruption.
Lusty Goblin 3 Oct @ 10:09am 
It's a shame, this mod makes me have some distorted copy of my body behind me for some reason, probably clashing with another mod but it's a pain.
Headcrab 28 Sep @ 5:31pm 
when I look at a npc or ground theres a weird flashlight thing