Announcement

Collapse
No announcement yet.

Virtual Shader Source Path - Link custom Shaders - Shadertoy Demo download

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

    Virtual Shader Source Path - Link custom Shaders - Shadertoy Demo download

    ShaderToy Demo - Shader by Inigo Quilez

    Built on top of the empty demo project below in a couple minutes.
    https://github.com/MaximeDup/UnrealEngine_ShaderLinking

    You just need to convert GLSL to HLSL:
    https://docs.microsoft.com/en-us/win...hlsl-reference

    https://drive.google.com/open?id=1Vxn2zzCUTMJfFda67YoPOVEyg1rsTZvj



    Inigo Quilez Youtube channel: https://www.youtube.com/channel/UCdmAhiG8HQDlz8uyekw4ENw
    Inigo Quilez Shadertoy: https://www.shadertoy.com/view/3lsSzf


    ________________________________________


    DEMO - empty project Download for 4.24+


    https://drive.google.com/open?id=1gz..._v00gJB1nH7nIA


    Features fully commented code - Setup two shader files : library/final output

    Click image for larger version  Name:	demoo.png Views:	0 Size:	320.3 KB ID:	1742153




    _______________________________________________________________



    Full code example at end of post











    Click image for larger version  Name:	iliektrain.png Views:	0 Size:	32.2 KB ID:	1697728

    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
    /* only IModuleInterface necessary if not hosting gamemode in this module */
    : public FDefaultGameModuleImpl
    {
    public:
        virtual void StartupModule() override;
        virtual void ShutdownModule() override;
    };
    ILikeTrains/Source/ILikeTrains/Private/ILikeTrains.cpp :

    Code:
    #include "ILikeTrains.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-18-2020, 08:39 AM.
    LinkedIn | Link custom Shaders | Atmosphere Modelisation & Procedural Planets | 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
    Check 3d tips! POLY DNA

    Comment


      #3
      Bump!!!!!!!!!!
      Check 3d tips! POLY DNA

      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 | Atmosphere Modelisation & Procedural Planets | 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 | Atmosphere Modelisation & Procedural Planets | Distance Matching Locomotion | Nvidia GameWorks builds - 4.19.2 : VXGI2.0, Blast, HairWorks, Flow - Plugins: VictoryBP

            Comment


              #7
              Originally posted by MaximeDupart View Post
              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" );
              Hi, I have followed the same way to create my own Shaders directory using you method. However, everytime when I recompile, I get an assertion

              [2019.08.05-13.35.12:949][399]LogWindows: Error: Assertion failed: !GShaderSourceDirectoryMappings.Contains(VirtualShaderDirectory) [File:\Build\++UE4\Sync\Engine\Source\Runtime\RenderCore\Private\ShaderCore.cpp] [Line: 1091]

              Please help, I'll appreciate any help I can get.

              Comment


                #8
                Originally posted by XynanXDB View Post

                Hi, I have followed the same way to create my own Shaders directory using you method. However, everytime when I recompile, I get an assertion

                [2019.08.05-13.35.12:949][399]LogWindows: Error: Assertion failed: !GShaderSourceDirectoryMappings.Contains(VirtualShaderDirectory) [File:\Build\++UE4\Sync\Engine\Source\Runtime\RenderCore\Private\ShaderCore.cpp] [Line: 1091]

                Please help, I'll appreciate any help I can get.
                I have solved it. The assertion point was hit because GShaderSourceDirectoryMappings stored the directory we are trying to set. To resolve this, we just need to ResetAllShaderSourceDirectoryMappings() at ShutdownModule();

                In ProjectName.cpp,
                Code:
                class FCustomShaderModule : public FDefaultGameModuleImpl
                {
                virtual void StartupModule() override {
                FString ShaderDirectory = FPaths::Combine(FPaths::ProjectDir(), TEXT("Shaders"));
                AddShaderSourceDirectoryMapping("/Project", ShaderDirectory);
                }
                virtual void ShutdownModule() override {
                ResetAllShaderSourceDirectoryMappings();
                }
                }; IMPLEMENT_PRIMARY_GAME_MODULE( FCustomShaderModule, ProjectName, "ProjectName" );

                Comment


                  #9
                  Glad you firgured it out!
                  Haven't encountered this issue on my side however.
                  LinkedIn | Link custom Shaders | Atmosphere Modelisation & Procedural Planets | Distance Matching Locomotion | Nvidia GameWorks builds - 4.19.2 : VXGI2.0, Blast, HairWorks, Flow - Plugins: VictoryBP

                  Comment


                    #10
                    Thank you,saved my day!

                    Comment


                      #11
                      Thank god, It worked !^^

                      I dont understand how this is not by default. So much trouble for such a simple thing
                      Check 3d tips! POLY DNA

                      Comment


                        #12
                        This only compiles for me when I comment out //#include "Interfaces/IPluginManager.h"

                        Once compiled and run I still can't load from the Project/Shaders folder

                        Any ideas?

                        Comment


                          #13
                          Think i'll make a small demo project and add it to the main post
                          LinkedIn | Link custom Shaders | Atmosphere Modelisation & Procedural Planets | Distance Matching Locomotion | Nvidia GameWorks builds - 4.19.2 : VXGI2.0, Blast, HairWorks, Flow - Plugins: VictoryBP

                          Comment


                            #14
                            Hey MaximeDupart thanks for sharing your setup. Developing shaders in a game mode certainly has its benefits. We configured our environment to include custom shaders in plugins. Though might I share the findings here too. You can read about our setup in this blog post.
                            Company Website
                            Marketplace Seller Page

                            Comment


                              #15
                              Any thought on how to achieve this from Blueprints? Many thanks... Have been trying for a while to have my custom shaders local to the Project. (Currently they have to reside under Program Files\Epic...\UE2.24\Engine\Shaders which is a GIT nightmare)

                              Thanks!!

                              Comment

                              Working...
                              X