Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Is my understanding of shader functions correct? HLSL for URP

Discussion in 'Universal Render Pipeline' started by justin_kasowski, Nov 16, 2022.

  1. justin_kasowski

    justin_kasowski

    Joined:
    Jan 14, 2020
    Posts:
    45
    Fairly certain I understand Shaders pretty well, but I'm writing a tutorial and didn't want to put any wrong information in. I commented this shader line by line. It's fairly short and I was hoping someone could correct me if I'm wrong about anything.


    Code (CSharp):
    1. Shader "UV_Sample"{
    2.     Properties{ [MainTexture] _MainTex ("Texture", 2D) = "white" {} } // Texture that can be passed in to shader material or the texture the material is applied to
    3.    
    4.     SubShader{ // Renderer chooses first SubShader compatible with GPU and target device
    5.         Cull Off ZWrite Off ZTest Always
    6.         // Cull - Back, Front, Off => Removes objects that are on the back side of objects, front removes front side, off disables
    7.         // ZWrite - On, Off => Defines if a depth buffer is used
    8.         // ZTest - Less, Greater, LEqual, GEqual, Equal, NotEqual, Always => Defines the type of depth comparison to use
    9.  
    10.         Pass{
    11.             HLSLPROGRAM // Start high level shader language
    12.  
    13.             #pragma vertex vert // Vertex shader - maps object's polygon vertices and textures to homogenous coordinates  [ 3D => (-1 < x,y < 1) ]
    14.             #pragma fragment frag // Fragment (pixel) shader - takes in the output of vertex shader as screen space (0 < x,y < 1) coordinates and applies pixel-by-pixel operations
    15.             #pragma fragmentoption ARB_precision_hint_fastest // Optimized when fine precisions aren't required
    16.  
    17.             // Must include Unity's URP ShaderLibrary
    18.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.  
    20.             struct appdata{ // define the appdata struct to include worldspace POSITION and the texture TEXCOORD0coordinates
    21.                 float4 vertex : POSITION;
    22.                 float2 uv : TEXCOORD0;};
    23.  
    24.             struct v2f { // defines struct for transformed world space => screen space positions
    25.                 float4 vertex : SV_POSITION; // System-Value (screen space) positions
    26.                 float2 uv : TEXCOORD0; };
    27.  
    28.             v2f vert (appdata v){ // Takes in world space coordinates, outputs homogenous coordinates
    29.                 v2f o;
    30.                 o.vertex = TransformObjectToHClip(v.vertex); // Clips objects based on ZTest property, converts to homogenous coordinates
    31.                 o.uv = v.uv; // Uses the unmodified texture coordinates, use TRANSFORM_TEX(v.uv, _MainTex) if using TEXTURE2D/SAMPLER
    32.                 return o; }
    33.  
    34.             sampler2D _MainTex; // Define _MainTex to be a sampler2D (samples from TEXCOORD0)
    35.  
    36.             /* If using tiling/offset for applying texture to objects:
    37.              
    38.             TEXTURE2D(_MainTex); // Define _MainTex as TEXTURE2D
    39.             SAMPLER(sampler_MainTex); // Creates a sampler for _MainTex
    40.  
    41.             CBUFFER_START(UnityPerMaterial)
    42.                 float4 _MainTex_ST;      // required if using tiling/offset
    43.             CBUFFER_END
    44.             */
    45.            
    46.             float4 _MainTex_TexelSize; // Returns texel size if needing to access other uv positions in fragment shader (not needed here but useful to know)
    47.  
    48.             float4 frag (v2f i) : SV_Target { // Takes v2f-formatted output pixel-by-pixel from the 'vert' function as input, outputs to screenspace target (targetTexture for a camera)
    49.                 float4 unmodified_current_pixel = tex2D(_MainTex, i.uv); // samples _MainTex at the currently processed pixel, use SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv) for TEXTURE2D/SAMPLER
    50.                 float4 one_pixel_above = tex2D(_MainTex, float2(i.uv.x, i.uv.y+_MainTex_TexelSize.y)); // unused but this is how to get nearby pixels
    51.                 return float4(i.uv.x, i.uv.y, unmodified_current_pixel.b, 1.0); } // outputs red and green values based on screen position, keeping the original textures blue value
    52.            
    53.             ENDHLSL
    54.         }
    55.     }
    56. }
    57.  
     
  2. justin_kasowski

    justin_kasowski

    Joined:
    Jan 14, 2020
    Posts:
    45
    Also, as far as I can tell there's no basic predefined vertex shader like in the old pipeline (vert_img). The documentation is also kinda lacking so maybe there is and I just can't find it?