How to make an hlsl file?

So I’m following a tutorial and it told me to make an hlsl file, like it was the easiest thin on earth. I have no idea what to do, how do I make an hlsl file? Please help.

To create a new Shader, use Assets > Create > Shader from the main menu or the Project View context menu.

This could be helpful:

.

So a shader script is a hlsl file on it own? So when it said “Create an hlsl file” it just meant create a shader?

Unity uses the HLSL language for its Shaders (.shader files).

A newly created Surface Shader via the main menu looks like this:

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Unlit shader:

Shader "Unlit/NewUnlitShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

“HLSL program snippets are written between CGPROGRAM and ENDCG keywords, or alternatively between HLSLPROGRAM and ENDHLSL. The latter form does not automatically include HLSLSupport and UnityShaderVariables built-in header files.” [src]

More Examples:
- Unity - Manual: Surface Shader examples
- https://docs.unity3d.com/Manual/ShaderTut2.html

Do you know how to code Shaders in general? Or is this your first time? :slight_smile:
.

1 Like

Thanks for the reply, sorry I took so long for mine. I have never coded shaders, I just have a general understanding of C, so that’s how far my knowledge goes. I’m actually doing shader graph, and I’m trying to add a source file to a custom function. I just need to know how to make a file that will work with the custom function, and all the tutorial told me was to make a hlsl file. I understand that hlsl is just the coding language that shaders use, I just don’t know what file to make - or how to make it.

Also, I’m trying to make a cell shader, so if you know any good tutorials for cell shading with Unity HDRP that would be nice.

1 Like

I did this.

Open Whatever code you have (.cs file) so that Visual Studio opens. Then create “New” file, choose .txt extension to create one. Once created in visual studio, add a comment just like:

// new code

and then save it. In explorer, look for a good place under Assets folder, and, in extensions, choose for “all files”, which will allow to name with the extension hlsl (name it MyNewCode.hlsl, for instance). I did this because I did not find a HLSL extensions among the options.

Hope this help
Have a great journey

5 Likes

See this tutorial about your issue.

Hey thank you sm, this worked for me

Hi,

In case other users would ask the same question, here are two solutions:

  • VFX Graph allows creating a blank HLSL file from Assets/Create/Visual Effects/HLSL file
  • Alternatively, you can give a try to this HLSL Template package helping with custom functions for both Shader Graph and VFX Graph.

Cheers!

isnt that a shaderlab file for use within unity? an actual HLSL file seems to be in the format of an #include

Hi,

to those following or landing on this thread, I just created a thread dedicated to HLSL templates for Custom Function Nodes .

Hope this helps.