UE4 – HLSL texture sample quick tip

Software:
Unreal Engine 4.26

When sampling textures using an HLSL custom node,
The UE4 TextureObject input name, will automatically have a sampler object generated named:

<your TextureObject name>sampler

For example, if you named your TextureObject input “tex_in”, the available sampler will be named “tex_insampler”.
So the code for sampling the texture will be:

Texture2DSample(tex_in, tex_inSampler, coords);

The following is an example of a simple u-blur custom node code, with 4 node inputs:
1. tex_in – TextureObject
2. coords – float2
3. size – float
4. sample – float

int blur_samples = int(samples * 0.5f);
float3 col = 0.0f;
float2 sample_coords = 0.0f;
for (int i = -blur_samples; i < blur_samples; i ++)
{	
	sample_coords.x = i * (size / blur_samples * 2);
	col += Texture2DSample(tex_in, tex_inSampler, coords + sample_coords ) / (blur_samples * 2);
}
return col;

The above code can typed directly in the Custom node’s Code field. or loaded from an external .usf file.

See also:
Loading HLSL shaders from the project folder

UE4 – Replace the Camera component icon

Software:
Unreal Engine 4.26

Note:
This seems like an awkward workaround..
So if I missed something here, and there’s a better method to do this,
I’ll be very grateful is you share it in the comments.
Also,
The following tip is only relevant for the CineCameraActor and won’t work with a regular CameraActor as it has a different built in offset (hopefully, I’ll have time to add this to the post later..)

Replacing the camera icon:
Its fairly simple to replace the Camera component’s mesh icon,
Just select the component and replace it’s Camera Mesh Static Mesh component with a different static mesh object:

So what’s the problem?
The problem is that the default mesh used for the camera icon doesn’t have its natural pivot at the focal point of the camera, but at its bottom somewhere,
And there is a hardcoded transform offset that compensates for that and places the icon mesh in a way that has the Icon lens roughly at the actual Camera actor pivot / focal point:

* I haven’t found any exposed transform parameter that allows moving the icon itself without moving the camera.
So in-order to replace the camera mesh with an alternative icon mesh, and have it be aligned properly to the camera’s pivot / focal point (without changing engine code and building it) the built-in offset must be negatively pre-added to the new mesh model:

In this example in Blender, a new icon is modeled facing positive Y, with pre-built offset to compensate for the hardcoded offset in UE.

The Camera actor with the alternative Icon:

Note:
In this example, I’ve replaced the camera icon with a much smaller model, intentionally, to suite a tiny scale project,
You can also scale the icon without replacing the model.

UE4 – Loading shaders from within the project folder

Software:
Unreal Engine 4.25

*Also tested on Unreal Engine 5.0.1

Disclaimer:
I’m probably the 10th guy that’s documenting these steps on the web,
I didn’t come up with this solution myself, I learned it from the sources listed below.
The reason I’m documenting this (again) myself is to have a clear source I can come back to for this issue because I’m absolutely incapable of remembering subjects like this…… :-\
If you find inaccuracies in the steps I’m detailing or my explanation, I’ll be very grateful if you share a comment.

  1. https://forums.unrealengine.com/development-discussion/rendering/1562454-virtual-shader-source-path-link-custom-shaders-shadertoy-demo-download
  2. https://dinwy.github.io/study/2019/july/customShaderInUE4/
  3. https://forums.unrealengine.com/community/community-content-tools-and-tutorials/1710373-using-external-shader-files-ush-usf-and-getting-the-most-of-the-custom-node

In short:
AFAIK since version 4.21 UE doesn’t load custom node shader code from your project/Shaders folder by default anymore, but only from the shaders folder in the engine installation, which makes it less practical for developing shaders for specific projects.


Steps for setting the UE project to load shaders from the project folder in UE 4.22:

> The examples here are for a project named: “Tech_Lab”

A. The project must be a C++ project:

So either create a new project, define as such or just create a new C++ class and compile the project with it to convert it to a C++ project.
Notes:
a. You may need to right click the .uproject file icon and and Generate Visual Studio Project Files for the project to load correctly into Visual Studio and compile.
b. You can delete the unneeded C++ class you added after the new settings took place.

B. Create a folder for the shader files:

Typically, it will be called “Shaders” and will be placed in the project root folder.

C. Add the RenderCore module to the project:

This is done by adding string “RenderCore” to array of public dependency modules in the <project>.build.cs file:

PublicDependencyModuleNames.AddRange(new string[] { "RenderCore", "Core", "CoreUObject", "Engine", "InputCore" });
(see image)

Notes:
a. In UE 4.21 it should be ShaderCore.
b. This addition is needed in-order to compile a new primary project module (next step).

D. Define a custom primary module for your project:

In <project_name>.h file add a new module named F<project_name>Module, with a StartupModule function overrides.
Notes:
a. We have add an include statement for “Modules/ModuleManager”.
b. The <project_name>.h file is located in the /Source/<project_name> folder.
c. Some sources state that you also have to override the ShutdownModule function, with an empty override, it works for me without this (maybe its just a mistake..)

E. Implement the function override,
and set the custom module as the project primary module:

In <project_name>.cpp file, add the StartupModule override,
With the definition of the added shaders path:
FString ShaderDirectory = FPaths::Combine(FPaths::ProjectDir(), TEXT("Shaders"));

and mapping this new path as “/Project” for conveniently including it:
AddShaderSourceDirectoryMapping("/Project", ShaderDirectory);

Last thing to do is to replace “FDefaultGameModuleImpl” with our custom module name in the IMPLEMENT_PRIMARY_GAME_MODULE macro:
IMPLEMENT_PRIMARY_GAME_MODULE(FTech_LabModule, Tech_Lab, "Tech_Lab" );

Notes:
a. We must include “Misc/Paths”
b. Note that the addition of this folder mapping is restricted to versions 4.22 and higher via a compiler directive condition. for version 4.21, you should state “ENGINE_MINOR_VERSION >= 21:

Note for UE5:
Unreal Engine 5 supports this from the get go so this compiler directive condition should be deleted for this to work.

F. Wrapping up:

After taking these steps and compiling the project.
You should be able to include .ush and .usf files stored in <your_ue_project>/Shaders with the “Project” path mapping:
include "/Project/test.usf"

That’s it! 🙂

I hope you found this helpful,
And if you encountered errors, or inaccuracies,
I’ll be grateful if you’ll take the time to comment.


Related:

  1. UE4 – Cyber enhancement shader
  2. UE4 – Fog post process effect

UE4 – Triplanar projection mapping setup

Software:
Unreal Engine 4.25

Triplanar Projection Mapping can be an effective texture mapping solution for cases where the model doesn’t have naturally flowing continuous UV coordinates, or there is a need to have the texture projected independently of UV channels, with minimally visible stretching and other mapping artifacts.
Classic use cases for Triplanar Projection Mapping are terrains and organic materials. provided that the image being used is a seamless texture, no seams will be visible because this projection type isn’t affected by UV coordinates.
Triplanar Projection Mapping can also be used in world space to create a continuous texture between separate meshes, allowing the meshes to be indestructibly transformed and edited.

How does Triplanar Projection Mapping work?
Triplanar Projection Mapping is a linear blend between 3 orthogonal 2D planar texture projections, typically each aligned to a natural world or object axis.
The more the surface faces an axis, the higher the weight of this axis projection in the final blend.

UE4 local (object) space Triplanar Projection Mapping material setup:
* It’s usually more efficient to create this setup as a Material Function

  1. Local shading coordinates are multiplied by by a “density” parameter to allow convenient scaling of the projected.
  2. The scaled coordinates vector is separated to its components who are combined to 3 pairs of planar coordinates XY, XZ and YZ, and fed as the sample coordinates to the 3 Texture Sample nodes.
  3. The Vertex Normal input vector is transformed to local space, converted to absolute value (absolute orientation in the positive axes octant) and separated to its X, Y and Z components so they can serve as blend weights in the mix.
  4. Each of the 3 planar axis projections is multiplied by the blend factors, and the resulting values are added to the raw mix.
  5. A value of 1.0 is divided by the Normal vector component values to obtain the factor needed to normalize the blend result to a value of 1.0.
  6. The raw blend value is multiplied by the normalizing factor so the blend resulting color will be normalized.
    * The blend weights should add to a value of 1.0, but a unit vector’s values add up to more than 1 in diagonal directions. for this reason, without this final step, the color of the texture in point on the surface that are diagonal to the projection axes will appear brighter than in points on the surface that face a projection axis.

An example of Triplanar Projection Mapping in world space:

A bunch of Blender monkeys (Suzanne) continuously textured using world space Triplanar Projection Mapping:

Related posts:
UE4 – Material Functions
UE4 – Material Instances
UE4 – Bump mapping
UE4 – Procedural bump mapping

UE4 – Procedural 3D noise bump setups

Software:
Unreal Engine 4.25

Yet another case where I develop my own costly solution only to find out afterwards that there’s actually a much more efficient built-in solution.. 😀

In this case the subject is deriving a bump normal from a procedural or non-uv projected height map/texture (like noise, or tri-planar mapping for example).

The built-in way:
Using the pre-made material functions, PreparePerturbNormalHQ and PerturbNormalHQ, the first of which uses the low level Direct3D functions DDX and DDY to derive the two extra surface adjacent values needed to derive a bump normal, and the last uses the 3 values to generate a world-space bump normal:

240 instructions
  1. Noise coordinates are obtained by multiplying the surface shading point local position by a value to set the pattern density.
  2. The Noise output value is multiplied by a factor to set the resulting bump intensity.
  3. The PreparePerturbNormalHQ function is used to derive the 2 extra values needed to derive a bump normal.
  4. The PerturbNormalHQ function is used to derive the World-Space bump normal.
  5. Note:
    Using this method, the material’s normal input must be set to world-space by unchecking Tangent Space Normal in the material properties.

The method I’m using:
This method is significantly more expensive in the number of shader instructions, but in my opinion, generates a better quality bump.
Sampling 3 Noise nodes at 3 adjacent locations in tangent-space to derive the 3 input values necessary for the NormalFromFunction material function:

412 instructions
  1. Noise coordinates are obtained by multiplying the surface shading point local position by a value to set the pattern density.
  2. Crossing the vertex normal with the vertex tangent vectors to derive the bitangent (sometimes called “binormal”).
  3. Multiplying the vertex-tangent and bitangent vectors by a bump-offset* factor to create the increment steps to the additional sampled Noise values.
    * This factor should be parameter for easy tuning, since it determines the distance between the height samples in tangent space.
  4. The increment vectors are added to the local-position to get the final height samples positions.
  5. The NormalFromFunction material function is used to derive a tangent-space normal from the 3 supplied height samples.

Note:
From my experience, even though the UV1, UV2 and UV3 inputs of the NormalFromFunction are annotated as V3, the function will only work is the inputs are a scalar value and not a vector/color.

Related:
UE4 – Material Functions
UE4 – Bump map
UE4 – fix an inverted normal map
UE4 – Triplanar mapping

UE4 – Quick fix for normal map encoding

Software:
Unreal Engine 4.25 | Photoshop 2020

Quick UE normal map tip:
If you load a normal map into a UE material and the result appears inverted, i.e. holes instead of bumps or the other way:

The quick fix:

  1. In the texture settings, check the Flip Green Channel option and save it:

Annotation 2020-09-06 160838

The deep fix:
* This can be performed an automated action on multiple files

  1. Open the normal map in Photoshop
  2. In the Channels panel, select the Green channel
  3. Press Ctrl + I,
    Or Select Image > Adjustments > Invert
    To invert the green channel.
  4. Save the texture and reload into Unreal Engine.
    Annotation 2020-07-06 233156

Inverted normal map:Annotation 2020-07-06 232554

Fixed normal map:Annotation 2020-07-06 233036

Related:
UE4 Bump map
UE4 – Procedural Bump Normals

UE4 – Technical model visualization tips

Software:
Unreal Engine 4.25

This post is a summary of the tips given by Epic Games technical-artist Min Oh in his GDC 2017 lecture about improving photo-realism in product visualization, more specifically, how to render high quality surfaces.
I recommend watching the full lecture:

  1. Render sharper reflections by increasing the Cubemap resolution of reflection captures:
    Project Settings > Engine > Rendering > Reflection > Reflection Capture Resolution
    * use powers of 2 values i.e. 256, 512, 1024….
    Annotation 2020-07-06 195120
  2. Improve the accuracy of environment lighting by increasing the Cubemap resolution of the Skylight:
    * use powers of 2 values i.e. 256, 512, 1024….
    Annotation 2020-07-06 202415
  3. Improve screen space effects accuracy like screen space reflections by setting the engine to compute high precision normals for the GBuffer:
    Set Project Settings > Engine > Rendering > Optimizations > GBuffer Format to:
    High Precision Normals
    Annotation 2020-07-06 204022
  4. Use a high degree of tessellation (subdivision) for the models pre-import.
    Simpy put:
    Use high quality models.
  5. Improve the surfaces tangent space accuracy, and as a result also the shading/reflection accuracy by setting the model’s static mesh components to encode high precision tangent basis:
    Static Mesh Editor > Details > LOD 0 > Build Settings > Use High Precision Tangent basis
    Annotation 2020-07-06 210030
  6. Creating materials with rich dual specular layers by enabling material clear coat separate normal input:
    Project Settings > Engine > Rendering > Materials > Clear Coat Enable Second Normal
    Annotation 2020-07-06 211152Set the material Shading Model to Clear Coat and use a ClearCoatBottomNormal input node to add a normal map for the underlying layer:
    Annotation 2020-07-06 221027

 

Related:

  1. UE4 – Lighting calculation tips
  2. UE4 HDRI lighting
  3. UE4 – Enable DXR ray-traced reflections 

UE4 – Enable DXR Raytracing

Software:
Unreal Engine 4.25

Steps for activating DXR Ray-tracing in a UE4 project:

  1. Project Settings:
    Platforms > Windows > Targeted RHIs:
    Set Default RHI to DirectX 12
    * RHI = Rendering Hardware Interface
  2. Project Settings:
    Engine > Rendering > Ray Tracing:
    Check Ray Tracing
    * Requires restarting the editor, and may take a while to load the project afterwards..
    * I’m actually not sure if the reason for delay in re-launching the project is a full re-build of the lighting or compiling shaders..
  3. Post Process Volume > Rendering Features > Reflections:
    Set Type to: Ray Tracing
  4. Post Process Volume > Rendering Features > Ray Tracing Reflections:
    Set Max Bounces to more than 1 if needed

No DXR Reflections:
Annotation 2020-07-05 011317

DXR Reflections on a GTX 1070 GPU:
Annotation 2020-07-05 020433

 

Related posts:

  1. UE4 Light calculation tips
  2. UE4 HDRI lighting
  3. UE4 – Technical model quality tips

UE4 – Lighting calculation tips for Archviz

Software:
Unreal Engine 4.25

The Static Lighting calculation in UE4 is performed by the Lightmass module (UE4’s integrated GI* engine), and the result of this calculation is stored in each object’s Lightmap, an extra texture map used for storing static light and shadow information.
This post provides a list of useful tips and techniques for improving your UE4 scene setup for an efficient light calculation.

Annotation 2020-07-04 223338

Notes:

  1. The following tips are aimed at achieving a good lighting calculation/solution but they don’t include optimization methods for high performance projects.
    Namely, we don’t get into manual Lightmap UV optimizations here.
  2. The following tips don’t take into account the now real-time ray-tracing options that have become available with Nvidia Geforce RTX / DirectX DXR.

 

Scene Setup:

  1. Delete unseen polygons from your mesh, so they wont waste Lightmap resolution.
    * For example, in an interior Archviz project, delete the outer polygons of the walls.
  2. Set the architectural surfaces to cast shadows from both sides:
    Details > Lighting > Shadow Two Sided
    Annotation 2020-07-04 231158
  3. Place “light blockers” around the structure to avoid light licks.
    * Wrap the structure on all sides with scaled cubes that have an absolute black material:
    Annotation 2020-07-04 194647
  4. Set the “light blockers” to be invisible in rendering:
    Annotation 2020-07-04 194713
  5. Scale the Lightmass Importance Volume fit around the structure tightly.
    Annotation 2020-07-04 185623

 

Lightmap Resolution:

  1. Optimize the architectural surfaces (static meshes) Light map resolution.
    A higher resolution will allow the Light Map to store more detailed lighting.
    The Static Mesh resolution setting is found in:
    Static Mesh Edior > Details > General Settings > Light Map Resolution:
    * This setting can also be overriden at the actor settings by selecting the actor in the map/level and activating:
    Details > Lighting > Override Lightmap Res
    Annotation 2020-07-04 214810
  2. Use the Lightmap Density optimization display mode to inspect the actual Lightmap texel density.
    The Lightmap Density display mode also color codes the display to indicate the efficiency of the Lightmap resolution per object (green color being optimal, and warm colors being too dense)
    * Note that in many cases of Archviz you may want a higher density than the editor displays as optimal.
    Annotation 2020-07-04 195909

 

Lighmass Settings:
The Lightmass setting are found in:
World Settings > Lightmass

  1. Decrease the Volumetric Lightmap Detail Cell Size to increase the light calculation accuracy:
    * This will increase the calculation time
    Annotation 2020-07-04 214102
  2. Decrease the Indirect Lighting smoothness to get more detailed shadows:
    Annotation 2020-07-04 232028
  3. Disable Compress Lightmaps to avoid banding artifacts in the shadow gradient:
    Annotation 2020-07-04 214102b
  4. Use the Lighting Only display mode to evaluate the lighting solution:
    Annotation 2020-07-04 200645
  5. For final quality, set the Light Quality to Production:
    Build menu > Lighting Quality > Production
    Annotation 2020-07-04 222201

 

* GI – “Global Illumination” is a term referring to indirect light simulation, namely a calculation of how light reflects and bounces between surfaces.

 

Related posts:

  1. 3ds max & V-Ray to UE4 using Datasmith
  2. “Cleaning” the UE4 FPS template for Archviz
  3. UE4 – HDRI Lighting
  4. UE4 – Activate DXR ray-traced reflections

UE4 – HDRI Environment & Lighting

Software:
Unreal Engine 4.25

Annotation 2020-05-20 162811

Creating HDRI environment backgruond and lighting* in UE4:
Note:
Lighting using a panoramic HDRI background is also referred to as IBL – Image Based Lighting.

* The example HDRIs in this post are from www.hdrihaven.com

  1. Import HDRI environment file.
    Note:
    The file must be saved as a *.hdr file and not *.exr because AFAIK that’s the only way UE4 will recognize it as an HDRI environment and encode it as a Texture Cube (cube map)
  2. Enable the HDRIBackdrop plugin:
    Go to Edit > Plugins
    Type “HDRI” in the search field to locate HDRIBackdrop and enable it.
    * You’l have to restart the UE Editor before using the plugin
    Annotation 2020-05-20 153925
  3. Drag a Lights > HDRI Backdrop object to your level:
    Annotation 2020-05-20 154657
  4. In the HDRIBackdrop details, select the wanted Cubemap:
    Annotation 2020-05-20 155212
  5. > Set the HDRIBackdrop‘s Intensity (self explanatory..).
    > Rotate the HDRIBackdrop around its Z axis to set the environment’s direction.
    > Set the HDRIBackdrop‘s Size.
    * Make it larger than your whole scene,
    And if Use Camera Projection is unchecked make it also large enough so that noticeable objects in the HDRI image will be distant enough as to not move incorrectly when you strife.
    * When Use Camera Projection is activated the Size property has no effect.
    > If Use Camera Projection is unchecked, set the Projection Center Z value to define the background image height below which it is projected as a flat ground.
    > Lighting Distance Factor defines ground projection area that will appear to receive shadows from your scene objects.
    * Set this attribute to 0 in-order to turn off the ground projection shadow.
    > Use Camera Projection:
    Activate this option to get a traditional infinitely far background with no flat ground surface projection.Annotation 2020-05-20 160338

 

Related:

  1. Sun & Sky link
  2. UE4 Architectural Glazing
  3. 3ds max & V-Ray to UE4 Datasmith workflow
  4. Preparing an FPS project for archviz
  5. UE4 – Archviz Light calculaion tips