text
stringlengths
0
17.2k
Test this in your codebase by clearing a console variable that can be toggled at runtime. Use the following code to do this:
Unreal Shader Files
Example Global Shader
Additional Notes
Creating and Adding a New Shader
Class Declaration
Registering a Shader Type
Declaring the Pixel Shader
Writing a Simple Function
Code Example:
// Simple pass-through vertex shader void MainVS( in float4 InPosition : ATTRIBUTE0, out float4 Output : SV_POSITION ) { Output = InPosition; } // Simple solid color pixel shader float4 MyColor; float4 MainPS() : SV_Target0 { return MyColor; }
Code Example:
#include "GlobalShader.h" // This can go on a header or cpp file class FMyTestVS : public FGlobalShader { DECLARE_EXPORTED_SHADER_TYPE(FMyTestVS, Global, /*MYMODULE_API*/); FMyTestVS() { } FMyTestVS(const ShaderMetaType::CompiledShaderInitializerType& Initializer) : FGlobalShader(Initializer) { } static bool ShouldCache(EShaderPlatform Platform) { return true; } };
Code Example:
// This needs to go on a cpp file IMPLEMENT_SHADER_TYPE(, FMyTestVS, TEXT("MyTest"), TEXT("MainVS"), SF_Vertex);
Code Example:
> `Shader type was loaded after engine init. Use `ELoadingPhase::PostConfigInit` on your module to cause it to load earlier.`
Code Example:
class FMyTestPS : public FGlobalShader { DECLARE_EXPORTED_SHADER_TYPE(FMyTestPS, Global, /*MYMODULE_API*/); FShaderParameter MyColorParameter; FMyTestPS() { } FMyTestPS(const ShaderMetaType::CompiledShaderInitializerType& Initializer) : FGlobalShader(Initializer) { MyColorParameter.Bind(Initializer.ParameterMap, TEXT("MyColor"), SPF_Mandatory); } static void ModifyCompilationEnvironment(EShaderPlatform Platform, FShaderCompilerEnvironment& OutEnvironment) { FGlobalShader::ModifyCompilationEnvironment(Platform, OutEnvironment); // Add your own defines for the shader code OutEnvironment.SetDefine(TEXT("MY_DEFINE"), 1); } static bool ShouldCache(EShaderPlatform Platform) { // Could skip compiling for Platform == SP_METAL for example return true; } // FShader interface. virtual bool Serialize(FArchive& Ar) override { bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar); Ar << MyColorParameter; return bShaderHasOutdatedParameters; } void SetColor(FRHICommandList& RHICmdList, const FLinearColor& Color) { SetShaderValue(RHICmdList, GetPixelShader(), MyColorParameter, Color); } }; // Same source file as before, different entry point IMPLEMENT_SHADER_TYPE(, FMyTestPS, TEXT("MyTest"), TEXT("MainPS"), SF_Pixel);
Code Example:
void RenderMyTest(FRHICommandList& RHICmdList, ERHIFeatureLevel::Type FeatureLevel, const FLinearColor& Color) { // Get the collection of Global Shaders auto ShaderMap = GetGlobalShaderMap(FeatureLevel); // Get the actual shader instances off the ShaderMap TShaderMapRef MyVS(ShaderMap); TShaderMapRef MyPS(ShaderMap); // Declare a bound shader state using those shaders and apply it to the command list static FGlobalBoundShaderState MyTestBoundShaderState; SetGlobalBoundShaderState(RHICmdList, FeatureLevel, MyTestBoundShaderState, GetVertexDeclarationFVector4(), *MyVS, *MyPS); // Call our function to set up parameters MyPS->SetColor(RHICmdList, Color); // Setup the GPU in prep for drawing a solid quad RHICmdList.SetRasterizerState(TStaticRasterizerState::GetRHI()); RHICmdList.SetBlendState(TStaticBlendState<>::GetRHI()); RHICmdList.SetDepthStencilState(TStaticDepthStencilState::GetRHI(), 0); // Setup the vertices FVector4 Vertices[4]; Vertices[0].Set(-1.0f, 1.0f, 0, 1.0f); Vertices[1].Set(1.0f, 1.0f, 0, 1.0f); Vertices[2].Set(-1.0f, -1.0f, 0, 1.0f); Vertices[3].Set(1.0f, -1.0f, 0, 1.0f); // Draw the quad DrawPrimitiveUP(RHICmdList, PT_TriangleStrip, 2, Vertices, sizeof(Vertices[0])); }
Code Example:
static TAutoConsoleVariable CVarMyTest( TEXT("r.MyTest"), 0, TEXT("Test My Global Shader, set it to 0 to disable, or to 1, 2 or 3 for fun!"), ECVF_RenderThreadSafe ); void FDeferredShadingSceneRenderer::RenderFinish(FRHICommandListImmediate& RHICmdList) { [...] // *** // Inserted code, just before finishing rendering, so we can overwrite the screen's contents! int32 MyTestValue = CVarMyTest.GetValueOnAnyThread(); if (MyTestValue != 0) { FLinearColor Color(MyTestValue == 1, MyTestValue == 2, MyTestValue == 3, 1); RenderMyTest(RHICmdList, FeatureLevel, Color); } // End Inserted code // *** FSceneRenderer::RenderFinish(RHICmdList); [...] }
Remarks
Test the functionality of your added console variable by running your project and opening the console window using the tilde (~) key. Then type in the any one of the following commands to set the variable:
Animation Sharing Plugin
When creating animation systems for large crowds of characters, you can use the Animation Sharing Plugin to significantly reduce your projects performance cost. Rather than performing individual evaluations of an Animation Blueprint for each character in your level, you can share animations from a single evaluation across many characters, resulting in a system where the difference between 100 characters and 1000 characters results in a minimum increase in performance cost.
Utalizing the Leader Pose Component system, Animation Sharing utilizes a set of animation state buckets for which animation instances are evaluated. The resulting poses are then transferred to all child components that are part of the bucket. You can then implement blending and playback position randomization techniques to give your crowd a more diverse animation playback.
The following diagram provides a high-level breakdown of how the system works with a numbered key explaining the included components:
Individual Crowd Actors (AActor)
UAnimationStateProcessor instance that is part of the Animation Sharing Setup
Runtime manager that is initialized using the Animation Sharing Setup
States (enum named), which the user has set up
Runtime representation of the state that takes the form of a Skeletal Mesh Component
Master Pose Component system used to share the animation with the child Skeletal Mesh Components
A crowd Skeletal Mesh character.
A set of animations to play on the character.
After installing the Animation Sharing plugin, create an Animation Sharing Setup asset. To create a new asset, use (+) Add in the Content Browser and navigate to Animation > Advanced > Animation Sharing Setup.
The Animation Sharing Setup asset contains all the information that will be shared across the specified Actors.
Open the Animation Sharing Setup asset to access its properties in the Detail panel.
Using the animation Sharing Setup asset's Skeletal Setups property, you can assign multiple Skeletons along with their corresponding properties to define the meshes that will share animation systems. This is useful when you have multiple Skeletons and Skeletal Meshes that will be driven by animations during animation sharing. You can add an array using (+) Add in the Skeleton Setups property.
Here you can reference a list of the available properties you can access after adding a Skeleton Setups array to the asset, and a description of their functionalities:
Define the skeleton and skeletal mesh assets you wish to share animations across using the respective Skeleton and Skeletal Mesh properties.
In order to select poses for the crowd character, you will need to create an Enumeration asset that will select Animation States in the Animation Sharing Setup asset. Create an Enumeration asset by navigating in the Content Browser to (+) Add, and selecting Blueprints > Enumeration.
Name and open the Enumeration asset.
Then assign Enums for each of your crowds animation states using the (+) Add Enumerator button and typing the definitions in the Display Name properties. In this workflow two Enums are created for an Idle and Run animation.
Create a character blueprint that will represent your crowd-character. Navigate in the content browser to (+) Add and select Blueprint Class > Character.
Name and open your Crowd Character Blueprint.
Assign the Character's Mesh using the drop-down in the Skeletal Mesh Asset Property.
In the Character Blueprint's Event Graph, you can build the logic that will set the Enumeration Asset's state based on a variable or gameplay state. In the workflow example a key press function, Keyboard Event 5, is used to change the Enum state.