Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to make an hlsl file?

Discussion in 'Shader Graph' started by GodsKnight117, Jul 26, 2020.

  1. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
    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.
     
  2. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,656
  3. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
  4. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,656
    Unity uses the HLSL language for its Shaders (.shader files).

    A newly created Surface Shader via the main menu looks like this:
    Code (CSharp):
    1. Shader "Custom/NewSurfaceShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.  
    24.         struct Input
    25.         {
    26.             float2 uv_MainTex;
    27.         };
    28.  
    29.         half _Glossiness;
    30.         half _Metallic;
    31.         fixed4 _Color;
    32.  
    33.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    34.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    35.         // #pragma instancing_options assumeuniformscaling
    36.         UNITY_INSTANCING_BUFFER_START(Props)
    37.             // put more per-instance properties here
    38.         UNITY_INSTANCING_BUFFER_END(Props)
    39.  
    40.         void surf (Input IN, inout SurfaceOutputStandard o)
    41.         {
    42.             // Albedo comes from a texture tinted by color
    43.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    44.             o.Albedo = c.rgb;
    45.             // Metallic and smoothness come from slider variables
    46.             o.Metallic = _Metallic;
    47.             o.Smoothness = _Glossiness;
    48.             o.Alpha = c.a;
    49.         }
    50.         ENDCG
    51.     }
    52.     FallBack "Diffuse"
    53. }

    Unlit shader:
    Code (CSharp):
    1. Shader "Unlit/NewUnlitShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.             #pragma multi_compile_fog
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 UNITY_FOG_COORDS(1)
    32.                 float4 vertex : SV_POSITION;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = UnityObjectToClipPos(v.vertex);
    42.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    43.                 UNITY_TRANSFER_FOG(o,o.vertex);
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 // sample the texture
    50.                 fixed4 col = tex2D(_MainTex, i.uv);
    51.                 // apply fog
    52.                 UNITY_APPLY_FOG(i.fogCoord, col);
    53.                 return col;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }

    "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:
    - https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html
    - https://docs.unity3d.com/Manual/ShaderTut2.html


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

     
    Reahreic likes this.
  5. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
    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.
     
    kpauner likes this.
  6. leonardo13j

    leonardo13j

    Joined:
    Mar 31, 2022
    Posts:
    3
    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
     
  7. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    78
  8. LiamG0tLost

    LiamG0tLost

    Joined:
    Mar 16, 2023
    Posts:
    2
    Hey thank you sm, this worked for me
     
  9. FredMoreau

    FredMoreau

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    96
    Hi,

    In case other users would ask the same question, here are two solutions:
    1. VFX Graph allows creating a blank HLSL file from Assets/Create/Visual Effects/HLSL file
    2. Alternatively, you can give a try to this HLSL Template package helping with custom functions for both Shader Graph and VFX Graph.
    Cheers!
     
  10. unity_18smith111

    unity_18smith111

    Joined:
    Sep 15, 2022
    Posts:
    3