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:
Shader "Custom/Curved Shader"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
_OutlineThickness ("Outline Thickness", Range(0,1)) = 0.1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 150
CGPROGRAM
#pragma surface surf Lambert vertex:vert addshadow
#include "UnityCG.cginc"
#include "CurvedWorldCore.cginc"
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
Pass{
Cull Front
CGPROGRAM
#include "UnityCG.cginc"
#include "CurvedWorldCore.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _OutlineColor;
float _OutlineThickness;
struct appdata{
float4 vertex : POSITION;
float4 normal : NORMAL;
};
struct v2f{
float4 position : SV_POSITION;
};
v2f vert(appdata v){
v2f o;
o.position = UnityObjectToClipPos(v.vertex + normalize(v.normal) * _OutlineThickness);
return o;
}
fixed4 frag(v2f i) : SV_TARGET{
return _OutlineColor;
}
ENDCG
}
}
FallBack "Mobile/VertexLit"
}
CurvedWorldCore.cginc
float3 _Curvature;
float _Distance;
void vert(inout appdata_full v)
{
float4 vPos = mul( unity_ObjectToWorld, v.vertex);
float dist = 0;
dist = distance(vPos.z , _WorldSpaceCameraPos.z) - _Distance;
if( dist < 0) { dist = 0; }
float addY = dist * dist;
vPos.y -= addY * _Curvature.y;
dist = vPos.x - _WorldSpaceCameraPos.x;
float addHY = dist * dist;
vPos.y -= addHY* _Curvature.x ;
// for corner
vPos.x += addY * _Curvature.z;
vPos = mul ( unity_WorldToObject, vPos);
v.vertex = vPos;
}
CurvedWorld.cs
public class CurvedWorld : MonoBehaviour {
public Vector3 Curvature = new Vector3(0, 0.5f, 0);
public float Distance = 0;
[Space]
public float CurvatureScaleUnit = 1000f;
int CurvatureID;
int DistanceID;
private void OnEnable()
{
CurvatureID = Shader.PropertyToID("_Curvature");
DistanceID = Shader.PropertyToID("_Distance");
}
void Update()
{
Vector3 curvature = CurvatureScaleUnit == 0 ? Curvature : Curvature / CurvatureScaleUnit;
Shader.SetGlobalVector(CurvatureID, curvature);
Shader.SetGlobalFloat(DistanceID, Distance);
}
}
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.