Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Other Adding an outline to a curved world shader

Discussion in 'Shaders' started by gw707, May 2, 2024.

  1. gw707

    gw707

    Joined:
    May 3, 2021
    Posts:
    13
    I’ve got a curved world shader and I want to add an outline to my objects.

    I’m able to do a curved world and outline separately, but when I combine the two by adding a second pass to the curved shader, the problem is that the outline doesn’t curve along with the objects.

    Here’s the shader code for this:

    Code (CSharp):
    1. Shader "Custom/Curved Shader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.  
    8.         _OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
    9.         _OutlineThickness ("Outline Thickness", Range(0,1)) = 0.1
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 150
    16.  
    17.         CGPROGRAM
    18.  
    19.         #pragma surface surf Lambert vertex:vert addshadow
    20.  
    21.         #include "UnityCG.cginc"
    22.         #include "CurvedWorldCore.cginc"
    23.  
    24.         sampler2D _MainTex;
    25.      
    26.         struct Input
    27.         {
    28.             float2 uv_MainTex;
    29.         };
    30.  
    31.         fixed4 _Color;
    32.    
    33.         void surf (Input IN, inout SurfaceOutput o)
    34.         {
    35.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    36.             o.Albedo = c.rgb;
    37.             o.Alpha = c.a;
    38.         }
    39.  
    40.         ENDCG
    41.  
    42.         Pass{
    43.             Cull Front
    44.  
    45.             CGPROGRAM
    46.  
    47.             #include "UnityCG.cginc"
    48.             #include "CurvedWorldCore.cginc"
    49.  
    50.             #pragma vertex vert
    51.             #pragma fragment frag
    52.  
    53.             fixed4 _OutlineColor;
    54.             float _OutlineThickness;
    55.  
    56.             struct appdata{
    57.                 float4 vertex : POSITION;
    58.                 float4 normal : NORMAL;
    59.             };
    60.  
    61.             struct v2f{
    62.                 float4 position : SV_POSITION;
    63.             };
    64.  
    65.             v2f vert(appdata v){
    66.                 v2f o;
    67.                 o.position = UnityObjectToClipPos(v.vertex + normalize(v.normal) * _OutlineThickness);
    68.                 return o;
    69.             }
    70.  
    71.             fixed4 frag(v2f i) : SV_TARGET{
    72.                 return _OutlineColor;
    73.             }
    74.  
    75.             ENDCG
    76.         }
    77.     }
    78.     FallBack "Mobile/VertexLit"
    79. }
    80.  
    CurvedWorldCore.cginc

    Code (CSharp):
    1. float3 _Curvature;
    2. float _Distance;
    3.  
    4. void vert(inout appdata_full v)
    5. {              
    6.     float4 vPos = mul( unity_ObjectToWorld, v.vertex);
    7.  
    8.     float dist = 0;
    9.     dist = distance(vPos.z , _WorldSpaceCameraPos.z) - _Distance;
    10.     if( dist < 0)    {    dist = 0;    }      
    11.    
    12.     float addY = dist * dist;
    13.    
    14.     vPos.y -= addY * _Curvature.y;
    15.  
    16.     dist = vPos.x - _WorldSpaceCameraPos.x;
    17.     float addHY = dist * dist;
    18.     vPos.y -= addHY* _Curvature.x ;
    19.    
    20.     // for corner
    21.     vPos.x += addY * _Curvature.z;
    22.  
    23.     vPos = mul ( unity_WorldToObject, vPos);
    24.     v.vertex = vPos;
    25. }
    26.  
    CurvedWorld.cs

    Code (CSharp):
    1. public class CurvedWorld : MonoBehaviour {
    2.  
    3.     public Vector3 Curvature = new Vector3(0, 0.5f, 0);
    4.     public float Distance = 0;
    5.  
    6.     [Space]
    7.     public float CurvatureScaleUnit = 1000f;
    8.    
    9.     int CurvatureID;
    10.     int DistanceID;
    11.  
    12.     private void OnEnable()
    13.     {
    14.         CurvatureID = Shader.PropertyToID("_Curvature");
    15.         DistanceID = Shader.PropertyToID("_Distance");
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         Vector3 curvature = CurvatureScaleUnit == 0 ? Curvature : Curvature / CurvatureScaleUnit;
    21.  
    22.         Shader.SetGlobalVector(CurvatureID, curvature);
    23.         Shader.SetGlobalFloat(DistanceID, Distance);
    24.     }
    25. }
    26.  

    Another method I tried is by using this asset Quick Outline to create the outline via script rather than through a shader, but I get the same issue where the outline doesn’t curve.

    I’m stuck as to how to fix this so I’d be grateful for some help with this issue.