Announcement

Collapse
No announcement yet.

Virtual Shader Source Path - Link custom Shaders 4.21/4.22

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Virtual Shader Source Path - Link custom Shaders 4.21/4.22

    4/10/2019 Update: Full code example at end of post

    Hello developers community,

    I used to write custom shaders and store them locally in my game projects root/ "Shaders" directory, which used to be defined as a virtual shader source path by default until 4.21.

    If you're working in C++ an easy way to add it back:


    While initiating your C++ project, you'll have a file implementing your project as a 'game module' :

    Code:
    IMPLEMENT_PRIMARY_GAME_MODULE(FDefaultGameModuleImpl, MyUE4Project, "MyUE4Project");

    Instead of using 'FDefaultGameModuleImpl', you can create your own simple game module and override "StartupModule" and "ShutdownModule" (check ShooterGame project for reference).




    If UE 4.21: Add "ShaderCore "as a dependency in your project "xxx.build.cs", otherwise for UE 4.22+ add "RenderCore" :

    4.21:
    Code:
    PublicDependencyModuleNames.AddRange(new string[] { "RHI", "ShaderCore", ...}
    4.22+:
    Code:
    PublicDependencyModuleNames.AddRange(new string[] { "RHI", "RenderCore", ...}


    And add back the project "shaders" directory as a potential source for shader files:

    Code:
    void FMyUE4ProjectModule::StartupModule()
    {
    
        FString ShaderDirectory = FPaths::Combine(FPaths::ProjectDir(), TEXT("Shaders"));
        AddShaderSourceDirectoryMapping("/Project", ShaderDirectory);
    
    }

    Now you can create a "Shaders" folder at the root of your game project and store your shaders in there !

    Tutorial for custom HLSL shaders: https://www.raywenderlich.com/57-unr...aders-tutorial




    ~~~~~~


    Full example:

    From your project root folder:

    ILikeTrains/Source/ILikeTrains/Public/ILikeTrains.h :

    Code:
    #pragma once
    
    #include "CoreMinimal.h"
    #include "ModuleManager.h"
    
    
    class FILikeTrainsModule
        : public IModuleInterface
    {
    public:
        virtual void StartupModule() override;
        virtual void ShutdownModule() override;
    };
    ILikeTrains/Source/ILikeTrains/Private/ILikeTrains.cpp :

    Code:
    #include "ILikeTrains.h"
    #include "Modules/ModuleManager.h"
    #include "Interfaces/IPluginManager.h"
    #include "Logging/LogMacros.h"
    #include "Misc/Paths.h"
    
    //#define LOCTEXT_NAMESPACE "FILikeTrainsModule"
    
    void FILikeTrainsModule::StartupModule()
    {
    
    #if (ENGINE_MINOR_VERSION >= 21)    
        FString ShaderDirectory = FPaths::Combine(FPaths::ProjectDir(), TEXT("Shaders"));
        AddShaderSourceDirectoryMapping("/Project", ShaderDirectory);
    #endif
    
    }
    
    void FILikeTrainsModule::ShutdownModule()
    {
    }
    
    IMPLEMENT_PRIMARY_GAME_MODULE(FILikeTrainsModule, ILikeTrains, "ILikeTrains" );
    Last edited by MaximeDupart; 05-22-2019, 08:45 AM.
    LinkedIn | Link custom Shaders in 4.21 | Atmosphere Modelisation | Distance Matching Locomotion | Nvidia GameWorks builds - 4.19.2 : VXGI2.0, Blast, HairWorks, Flow - Plugins: VictoryBP

    #2
    Can you please explain a little more how to add the virtual shader source? I cant found info about implementing a game module in the way you wrote (if not any documentation at all like with everithing else in the engine) .

    First time implementing a module, and Im not sure where to insert the code since the shooter game and the default cs files looks very different from what you posted.
    Only similarity is "PublicDependencyModuleNames.AddRange" function.



    DearEpic:

    God, why does ue4 ALLWAYS have to make EVERITHING so overcomplicated and delete the features that work? T_T

    Comment


      #3
      Bump!!!!!!!!!!

      Comment


        #4
        Oh dear, thought i answered, my bad.

        Let's say your project is named ILikeTrains, make it a c++ project by at least adding a c++ class to your game from the engine.

        From your project root folder:

        ILikeTrains/Source/ILikeTrains/Public/ILikeTrains.h :

        Code:
        #pragma once
        
        #include "CoreMinimal.h"
        #include "ModuleManager.h"
        
        
        class FILikeTrainsModule
            : public IModuleInterface
        {
        public:
            virtual void StartupModule() override;
            virtual void ShutdownModule() override;
        };
        ILikeTrains/Source/ILikeTrains/Private/ILikeTrains.cpp :

        Code:
        #include "ILikeTrains.h"
        #include "Modules/ModuleManager.h"
        #include "Interfaces/IPluginManager.h"
        #include "Logging/LogMacros.h"
        #include "Misc/Paths.h"
        
        //#define LOCTEXT_NAMESPACE "FILikeTrainsModule"
        
        void FILikeTrainsModule::StartupModule()
        {
        
        #if (ENGINE_MINOR_VERSION >= 21)    
            FString ShaderDirectory = FPaths::Combine(FPaths::ProjectDir(), TEXT("Shaders"));
            AddShaderSourceDirectoryMapping("/Project", ShaderDirectory);
        #endif
        
        }
        
        void FILikeTrainsModule::ShutdownModule()
        {
        }
        
        IMPLEMENT_PRIMARY_GAME_MODULE(FILikeTrainsModule, ILikeTrains, "ILikeTrains" );
        LinkedIn | Link custom Shaders in 4.21 | Atmosphere Modelisation | Distance Matching Locomotion | Nvidia GameWorks builds - 4.19.2 : VXGI2.0, Blast, HairWorks, Flow - Plugins: VictoryBP

        Comment


          #5
          Little update for 4.22, You need to replace ShaderCore by RenderCore in
          PublicDependencyModuleNames.AddRange(new string[] { "RHI", "ShaderCore", ...}

          Comment


            #6
            Originally posted by LRI_Kevin View Post
            Little update for 4.22, You need to replace ShaderCore by RenderCore in
            PublicDependencyModuleNames.AddRange(new string[] { "RHI", "ShaderCore", ...}
            Thanks , updated the post.
            LinkedIn | Link custom Shaders in 4.21 | Atmosphere Modelisation | Distance Matching Locomotion | Nvidia GameWorks builds - 4.19.2 : VXGI2.0, Blast, HairWorks, Flow - Plugins: VictoryBP

            Comment

            Working...
            X