HLSL Material for Unreal Engine

Related tags

Game HLSLMaterial
Overview

HLSL Material for Unreal Engine

Ever wanted to write complex material functions directly in HLSL? Now you can!

Unreal Engine 4.26, 4.27 and 5.0 are supported. 4.25 cannot be supported as custom nodes have a single output in that version.

Demo: https://twitter.com/phyronnaz/status/1452553917204733953

Features

  • HLSL support: write all your functions in a single hlsl file and use any of them in regular materials
  • Team-friendly: regular material functions are generated, so your team members don't need the plugin to use them!
  • Live updates: material functions & opened material editors are refreshed when saving the hlsl file (Windows only)
  • Comment support: comments are parsed & pin tooltips are set accordingly

Installing from source

Download the repo as a zip or download a release with prebuilt binaries, and extract it under your project Plugins folder so you have YourProject/Plugins/HLSLMaterial/HLSLMaterial.uplugin

Visual Studio will be required if you don't download a prebuilt release.

How to

  • Create a new HLSL Material Function Library (right click Content Browser -> Material & Textures). This asset will be the link between your hlsl file and all the generated material functions.
  • Set the File on it to point to your HLSL file
  • Add functions to the file
  • Material functions will be created when you save the HLSL file
  • You can also disable the automatic updates and manually right click the asset -> Update from HLSL

Syntax

  • All return types must be void to ensure the pins are all properly named
  • To mark a parameter as an output, use out: eg, out float3 MyOutput
  • Comments must use the // syntax, /* is not supported
  • @param in comments will be parsed & put into the pin tooltips
// Ray-sphere intersection
// @param   RayOrigin       The origin of the ray
// @param   RayDirection    The direction of the ray
// @param   SphereCenter    The center of the sphere
// @param   SphereRadius    The radius of the sphere
// @param   Distance        The distance from the ray origin to the hit on the sphere
void RaySphereIntersect(float3 RayOrigin, float3 RayDirection, float3 SphereCenter, float SphereRadius, out float Distance) 
{
    float a = dot(RayDirection, RayDirection);
    float3 SphereCenterToRayOrigin = RayOrigin - SphereCenter;
    float b = 2.0 * dot(RayDirection, SphereCenterToRayOrigin);
    float c = dot(SphereCenterToRayOrigin, SphereCenterToRayOrigin) - (SphereRadius * SphereRadius);
    float Discriminant = b * b - 4.0 * a* c;

    if (Discriminant < 0.0) 
    {
        Distance = -1.0;
    }
    else
    {
      Distance = (-b - sqrt(Discriminant)) / 2 * a;
    }
}

How it works

The plugin manually parses the functions in the HLSL file. From there, it creates new material functions with a Custom node holding the function body.

The logic is pretty simple & straightforward, so it should be relatively robust.

Project management

Moving functions

Material functions are generated next to your library asset, under YourFunctionLibraryAsset_Generated/. Once they are generated, you should be able to move them anywhere - the library keeps a ref to them.

Renaming functions

If you rename a function in your HLSL file, you need to manually rename the corresponding material function in Unreal, ideally before saving the HLSL. Otherwise, a new one will be created with the new name.

Deleting functions

The plugin will never delete any asset. If you remove a function from the HLSL, it is up to you to remove it from Unreal.

Source Control

You don't need to check in the HLSL file or the library - simply checking in the generated functions should be enough. However, if your teammates are also using the plugin it might be best to check in everything.

Comments
  • Add IncludeFilePaths and AdditionalDefines properties for custom node

    Add IncludeFilePaths and AdditionalDefines properties for custom node

    Unreal custom node in which our HLSL code is placed has useful properties for defines and include paths.

    This allows for more possibilities when creating hlsl material functions including usage of .ush files.

    This pull request adds ability to set those properties for all functions in library as well as update function on changes to included files.

    As just adding those properties was very easy most of the code changes are related to watching for changes in includes and supporting that properly.

    opened by adam-wolski 8
  • Fix compilation on UE5.0 release branch

    Fix compilation on UE5.0 release branch

    Those changes fix compilation on UE5.0 release branch.

    Pretty minor but had to fully disable permutation window feature and do a little hack in 8b86ea6335547965f558ebe9064ca59b8da88fc5 to keep support for UE5 Early Access.

    opened by adam-wolski 2
  • Feature Request: Composability

    Feature Request: Composability

    One of the main limitations of the custom node is that it doesn't allow for multiple functions to be defined/used in a single node. A workaround exists where functions can be defined within a struct.

    It would be awesome if the plugin could perform this process automatically, such that functions get automatically wrapped into a struct and calls to functions are swapped for calls to the function within the struct. Or alternatively, there may be a better workaround.

    opened by kriNon 1
  • Added support for structs.

    Added support for structs.

    At the current state adding structs to the code breaks parsing, especially structs with functions which is the only easy way of adding non-material function "libraries" to the code node in UE4, i.e:

    struct Functions 
    {
        float4 SomeLocalFunction(float4 A, float4 B)
        {
            return lerp(A, B, 0.25);
        } 
    };
    
    
    void MaterialFunc(
        float4 A,
        float4 B
        out float4 Color)
    {
        Functions fun;
    
        Color = fun.SomeLocalFunction(A, B);
    }
    

    If file contains multiple material functions, structs will be added to all of the code nodes.

    opened by qwe321 1
  • Fix relative paths not working

    Fix relative paths not working

    When UHLSLMaterialFunctionLibrary::OnDirectoryChanged is called array of FFileChangeData has paths that are always full, not relative.

    Filepath in HLSLMaterialFunctionLibrary is stored as relative filepath when possible. Which is good as this allowes for having shared hlsl directory in the project for all project users.

    But when OnDirectoryChanged is called with those full paths it fails to find matching files.

    With this change GetFilePath always returns full, not relative path

    opened by adam-wolski 1
  • How can I use #include to add external file into the current .hlsl file?

    How can I use #include to add external file into the current .hlsl file?

    I met these matters:

    • when using include, UE5 only supports .usf or .ush
    • simply using #include "basic.usf" does not work, how to write the correct path so UE can find it?
    opened by ChaelKenway 0
  • Includes are added even if they are guarded by #ifdef-s

    Includes are added even if they are guarded by #ifdef-s

    When parsing includes in hlsl file all of them are added even if they are guarded by failed #ifdef

    #include "header_a.ush"
    
    #ifndef SOMEDEF
    #include "header_b.ush"
    #endif
    

    Right now both header_a and header_b are included and this leads to compilation errors.

    opened by adam-wolski 4
  • Resolve local includes

    Resolve local includes

    Hey, me again 😄

    I propose small improvement to includes parsing that allows working with paths that IDEs have less problems with.

    When using IDEs like Rider for developing shaders they often have problems with finding includes when included as virtual paths.

    See for example https://youtrack.jetbrains.com/issue/RIDER-69615

    This can be often worked around when working with global shaders by using relative paths to virtual shader location.

    This cannot be done same way for code generated with HLSLMaterial.

    This change adds fallback mechanism when we find that include path isn't real virtual path. We try to check if this path is relative path to our hlsl file and if it is we try to find it in virtual mappings.

    opened by adam-wolski 1
  • Including Operations Only Possible in Custom Nodes Causes the Entire Function To Be Inside A Custom Node

    Including Operations Only Possible in Custom Nodes Causes the Entire Function To Be Inside A Custom Node

    Ideally this plugin would be able to differentiate between content that needs to definitely be part of a Custom node and everything else in order to take advantage of constant folding.

    Function code:

    void testFunc(float3 color, float intensity, out float3 diffuse, out float x) {
    
    	float val = 0.1;
    	int i = 0;
    	while ( i <= 10 ) {
    		diffuse.x = (val * (float)i);
    		i += 1;
    	}
    
    	x = intensity * 0.5;
    }
    
    • Generated graph: image

    • Expected format: Intensity parameter and X output are not used within the while loop, and therefore could be broken out from the custom node: image

    opened by Bartalon 4
Releases(0.1)
Owner
null
Design-agnostic node editor for scripting game’s flow in Unreal Engine

Flow plug-in for Unreal Engine provides a graph editor tailored for scripting flow of events in virtual worlds. It's based on a decade of experie

Moth Cocoon 555 Jan 4, 2023
Niagara UI Renderer | Free Plugin for Unreal Engine 4

Niagara UI Renderer | Free Plugin for Unreal Engine 4 Niagara UI Plugin adds Niagara Particle System Widget that allows you to render Niagara particle

null 156 Dec 19, 2022
Third-person survival game for Unreal Engine 4 made entirely in C++.

Third-person survival game for Unreal Engine 4 made entirely in C++. Originally built as a 6 section tutorial series, now available as open-source C++ sample project.

Tom Looman 2.8k Dec 30, 2022
RenderStream plugin for Unreal Engine

This project relies on http://disguise.one software to function. For the plugin setup process - please see https://help.disguise.one/Content/Configuri

disguise 41 Dec 19, 2022
Simple CSV localization system for Unreal Engine 4

BYG Localization We wanted to support fan localization for Industries of Titan and found that Unreal's built-in localization system was not exactly wh

Brace Yourself Games 56 Dec 8, 2022
Edycja PianoFall zrobiona na Unreal Engine

PianoFall - Unreal Engine Edition Edycja PianoFall zrobiona na Unreal Engine (mój pierwszy projekt w UE) Obsługa Po uruchomieniu programu i wciśnięciu

Nadwey 4 Jun 17, 2021
Lambda support for Unreal Engine dynamic delegates

DynamicLambda Lambda support for Unreal Engine dynamic delegates This is experimental feature. Now only parametless lambdas are supported To see more

Andrew Derkach 28 Nov 29, 2022
Dialogue scripting language for Unreal Engine

Supertalk Welcome to Supertalk! This is a simple dialogue scripting language for Unreal Engine created due to frustration with visual dialogue tree wo

Sam Bloomberg 41 Nov 18, 2022
An Unreal Engine 4 Dungeon Generator

DungeonGenerator A (hopefully) handy Unreal Engine 4 Dungeon Generator Read the full docs here: https://github.com/orfeasel/DungeonGenerator/blob/main

Orfeas Eleftheriou 69 Dec 28, 2022
Procedural Mesh Modeling Toolkit for Unreal Engine Artists

OpenLand Mesh Procedural Mesh Modeling Toolkit for Unreal Engine Artists. Installation Get it via the marketplace ??️ For non-commercial projects, you

GDi4K 26 Nov 19, 2022
Creating Unreal Engine infinite landscapes/oceans using the editor shader graph and rendering them using Geometry ClipMap. It also allows to spawn mesh on landscape surface. UE5 required

Procedural Landscapes and Oceans in Unreal Engine 5 using Editor Shader Graph Latest version of this project is available as a plugin for UE 4.26+ on

Maxime Dupart 10 Oct 4, 2021
Unreal Engine Plugin to wrap Nuitrack SDK ( skeleton tracking solution by 3DiVi )

Nuitrack for Unreal Engine Unreal Engine plugin to bridge Nuitrack. Nuitrack is a middleware to provide 3d skeleton tracking solution using a depth se

Ayumu Nagamatsu 11 Nov 10, 2022
Unreal Engine 4 plugin for SteamVR passthrough camera support

SteamVR Passthrough Plugin This Unreal Engine 4 plugin adds SteamVR passthrough camera support using the OpenVR TrackedCamera API. Example project: ht

null 10 Dec 5, 2022
Building Escape is a simple room escape game made with Unreal Engine 4.27 and C++.

Building-Escape Building Escape is a simple room escape game made with Unreal Engine and C++. The main purpose of the game is to find a way to escape

Christine Coomans 2 Dec 13, 2021
An Unreal Engine 4 SDK generator using SdkGenny

UE4Genny UE4Genny is an SDK generator for Unreal Engine 4 games. It aims to provide a functional SDK that requires little to no editing after generati

null 98 Jan 2, 2023
Unreal Engine OpenDRIVE plugin

Unreal Engine OpenDRIVE plugin This plugin allows you to manipulate your OpenDRIVE road network in Unreal Engine. It is built around esmini's RoadMana

Bertrand Richard 64 Dec 14, 2022
An Unreal Engine 4 silent aim method, not usable on all games. Tested on Fortnite, Rogue Company, Bloodhunt, and Splitgate.

UE4-Silent-Aim An Unreal Engine 4 silent aim method, not usable on all games. Only tested on Fortnite, Rogue Company, Bloodhunt, and Splitgate. Done t

null 50 Dec 25, 2022
Shows Unreal Engine logs in-game using ImGui

BYG Imgui Logger Displays Unreal's UE_LOG output in an ImGui window. Heavily based on the Console example from imgui_demo.cpp included with ImGui. Fea

Brace Yourself Games 46 Dec 16, 2022
Exposes Azure Kinect Support for integration into Unreal Engine Applications.

Azure Kinect for Unreal Engine Exposes Azure Kinect Support for integration into Unreal Engine Applications. Mainly for depth and color textures creat

Ayumu Nagamatsu 45 Jan 4, 2023