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.

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

[shaderproj.zip - Google Drive

](ShaderProject-Shadertoy.zip - Google Drive)

Inigo Quilez Youtube channel: https://www.youtube.com/channel/UCdmAhiG8HQDlz8uyekw4ENw
Inigo Quilez Shadertoy: Shader - Shadertoy BETA


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


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’ :


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:


PublicDependencyModuleNames.AddRange(new string] { "RHI", "ShaderCore", ...}

4.22+:


PublicDependencyModuleNames.AddRange(new string] { "RHI", "RenderCore", ...}

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


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

http://i.imgur.com/MPDbwxBh.jpg

 












From your project root folder:

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



```



#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 :



```


#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" );

```

4 Likes

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

Bump!!!

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 :




#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 :



#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" );

Little update for 4.22, You need to replace ShaderCore by RenderCore in
PublicDependencyModuleNames.AddRange(new string] { “RHI”, “ShaderCore”, …}

Thanks , updated the 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:D:\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,




class FCustomShaderModule : public FDefaultGameModuleImpl
{ virtual void StartupModule() override
{
  [INDENT=2]FString ShaderDirectory = FPaths::Combine(FPaths::ProjectDir(), TEXT("Shaders"));[/INDENT]
  [INDENT=2]AddShaderSourceDirectoryMapping("/Project", ShaderDirectory);[/INDENT]
  }

  virtual void ShutdownModule() override
{
  [INDENT=2]ResetAllShaderSourceDirectoryMappings();[/INDENT]
  }
 };
IMPLEMENT_PRIMARY_GAME_MODULE( FCustomShaderModule, ProjectName, "ProjectName" );



Glad you firgured it out!
Haven’t encountered this issue on my side however.

Thank you,saved my day!

Thank god, It worked !^^

I dont understand how this is not by default. So much trouble for such a simple thing

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?

Think i’ll make a small demo project and add it to the main post

Hey @MaximeDupart thanks for sharing your setup. Developing shaders in a game mode certainly has its benefits. I configured my environment to include custom shaders in plugins. You can read about my setup in this blog post.

Edit: Fixed link.

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!!

I don’t think it is possible to do this from Blueprints, as (to the best of my knowledge) the shader source directories must be initialized at module startup, and there is no way to modify an engine module from Blueprints. Additionally, there just isn’t a function to add a shader source directory in Blueprints.

Is there any particular reason that you are unable to add the shader paths via C++?

I downloaded Tommy Tran’s paint effect which looks fantastic in the 4.20 version but unfortunately doesn’t work in 4.24.

I followed @XenthorXi’s tutorial but it still doesn’t work.

Here are the errors :

The edited files :

And the “Shaders” folder in the project folder :

I’m a complete noob with C++ so if anyone could help me it would be much appreciated :slight_smile:

I wish i could have a name for Epic games Games, and a name for Epic Games forums. i end up having to change my name every time i wanna stream a multiplayer game from Epic game store, i’ll upload a demo project for you

Added downloadable Demo Project for 4.24+ :

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

[USER=“874847”]Odd Way[/USER] common errors are: external shader file is empty or the extension is actually wrong (your file is actually a .txt and you’ve hidden this extension).

I added a fully commented project with two external shader linked.

@MaximeDupart Sorry for the late answer i was busy these days.
Thank you very much for your solution i will look at your project. Nonetheless i succedeed to have it working just with a simple copy/paste in a “custom material node” :slight_smile: