Search Unity

Question Update surface shader to URP (Hex map tutorial from catlikecoding)

Discussion in 'Shaders' started by fomafomitch, Jun 22, 2022.

  1. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    89
    At this address the tutorial, you can see at "5.1" the custome surface shader to change color of vertices :
    https://catlikecoding.com/unity/tutorials/hex-map/part-1/#5.1

    here the shader :
    https://imgur.com/IYXE8NB

    here my new shader from unity docs with a base color that can change :
    Code (CSharp):
    1. // This shader fills the mesh shape with a color that a user can change using the
    2. // Inspector window on a Material.
    3. Shader "Example/URPUnlitShaderColor"
    4. {
    5.     // The _BaseColor variable is visible in the Material's Inspector, as a field
    6.     // called Base Color. You can use it to select a custom color. This variable
    7.     // has the default value (1, 1, 1, 1).
    8.     Properties
    9.     {
    10.         _BaseColor("Base Color", Color) = (1, 1, 1, 1)
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
    16.  
    17.         Pass
    18.         {
    19.             HLSLPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.  
    23.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    24.  
    25.             struct Attributes
    26.             {
    27.                 float4 positionOS   : POSITION;
    28.             };
    29.  
    30.             struct Varyings
    31.             {
    32.                 float4 positionHCS  : SV_POSITION;
    33.             };
    34.  
    35.             // To make the Unity shader SRP Batcher compatible, declare all
    36.             // properties related to a Material in a a single CBUFFER block with
    37.             // the name UnityPerMaterial.
    38.             CBUFFER_START(UnityPerMaterial)
    39.                 // The following line declares the _BaseColor variable, so that you
    40.                 // can use it in the fragment shader.
    41.                 half4 _BaseColor;
    42.             CBUFFER_END
    43.  
    44.             Varyings vert(Attributes IN)
    45.             {
    46.                 Varyings OUT;
    47.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    48.                 return OUT;
    49.             }
    50.  
    51.             half4 frag() : SV_Target
    52.             {
    53.                 // Returning the _BaseColor value.
    54.                 return _BaseColor;
    55.             }
    56.             ENDHLSL
    57.         }
    58.     }
    59. }

    How to make a new shader that can change his vertices independently ?
     
  2. ohminkwon

    ohminkwon

    Joined:
    Oct 3, 2019
    Posts:
    2
    Code (CSharp):
    1.  
    2. Shader "Example/URPUnlitShaderColor"
    3. {
    4.     Properties
    5.     {
    6.         _BaseColor("Base Color", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float4 color : COLOR;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float4 vertex : SV_POSITION;
    30.                 float4 color : COLOR;
    31.             };
    32.  
    33.             fixed4 _BaseColor;
    34.  
    35.             v2f vert (appdata v)
    36.             {
    37.                 v2f o;
    38.                 o.vertex = UnityObjectToClipPos(v.vertex);
    39.                 o.color = v.color;
    40.                 return o;
    41.             }
    42.  
    43.             fixed4 frag (v2f i) : SV_Target
    44.             {
    45.                 fixed4 col = _BaseColor * i.color;
    46.                 return col;
    47.             }
    48.             ENDCG
    49.         }
    50.     }
    51. }
    52.  
    Although I'm not an expert of this. I checked this is work for the tutorial you mentioned above at URP template with Unity 2021.3.8f version .

    Just added color data at two structures ; appdata & vert.
    After that, declared a variable of _BaseColor.
    Finally, At fragment shader function at the end of line.
    Hope this can solve your problem.
     
    Last edited: Jan 8, 2023
  3. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    89
    Thanks you ! But found the solution with shader graph