S&box Wiki

Revision Difference

CustomPostProcessing#546069

<cat>Code.Shader</cat> <title>Custom Post-Processing</title> # Setting up your material and rendering it From the C# side you'll need to load a material with your custom shader and render it in the PostProcess event. ``` public CustomGame() { if(IsClient) { InitPostProcess(); } } private Material _postProcessMaterial; public void InitPostProcess() { _postProcessMaterial = Material.Load("materials/post_process_example.vmat"); } [Event("render.postprocess")] public void PostProcess() { if(_postProcessMaterial != null) { Render.CopyFrameBuffer(false); Render.Material = _postProcessMaterial; Render.DrawScreenQuad(); } } ``` This is a basic example using the post processing material `post_process_example.vmat`. We're loading the material on the client in the game's constructor. That's all we need to do from the C# side to get our post processing material to draw, now we just need to actually make the shader. # Basic Post Processing Shader ``` HEADER { CompileTargets = ( IS_SM_50 && ( PC || VULKAN ) ); Description = "Post Processing Shader"; } //========================================================================================================================= FEATURES { #include "common/features.hlsl" } //========================================================================================================================= MODES { VrForward(); Default(); } //========================================================================================================================= COMMON { #include "system.fxc" #include "common.fxc" } //========================================================================================================================= struct VertexInput { float3 vPositionOs : POSITION < Semantic( PosXyz ); >; float2 vTexCoord : TEXCOORD0 < Semantic( LowPrecisionUv ); >; }; //========================================================================================================================= struct PixelInput { float4 vPositionPs : SV_Position; float2 vTexCoord : TEXCOORD0; }; //========================================================================================================================= VS { PixelInput MainVs( VertexInput i ) { PixelInput o; o.vPositionPs = float4(i.vPositionOs.xyz, 1.0f); o.vTexCoord = i.vTexCoord; return o; } } //========================================================================================================================= PS { struct PixelOutput { float4 vColor : SV_Target0; }; // grab the frame buffer texture CreateTexture2D( g_FrameBuffer )< Attribute("FrameBufferCopyTexture"); SrgbRead( false ); >; PixelOutput MainPs( PixelInput i ) { PixelOutput o; float2 uv = i.vTexCoord.xy; // uv from 0,0 in the top left to 1,1 in the bottom right float4 color = Tex2D(g_FrameBuffer, uv.xy).rgba; // grab color from frame texture color = 1 - color; // invert color o.vColor = color; return o; } } ``` This is a basic example post processing shader that inverts the screen color. This is a basic example post processing shader that inverts the screen color.⤶ <upload src="3c1ff/8d9a61ea993d2f1.png" size="5219945" name="sbox-dev_oXb7vCc1Z3.png" />