### 二、實現

1.要渲染到全屏，就需要相機Camera的Texture進行材質修改，所以先創建一個RenderTexture的腳本

``````public class RenderControl : MonoBehaviour {

[SerializeField]
Material mat;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(destination, mat);
}
}``````

2.先在網上找一個心形函數的公式

``````float heart(float2 st)
{
//調整位置
st = (st - float2(0.5, 0.38)) * float2(2.1, 2.8);
//心形函數計算
return pow(st.x, 2) + pow(st.y - sqrt(abs(st.x)), 2);
}

fixed4 frag (v2f i) : SV_Target
{
float d = heart(i.uv);
return d;
}``````

``d = step(d, 1);``

``d = step(d, abs(sin(d * 8 - _Time.w * 2)));``

``````				float d = heart(i.uv);
//循環動畫
d = step(d, abs(sin(d * 8 - _Time.w * 2)));
//上色
float4 col = fixed4(d, 0.2, 0.21, 1);
if(d < 1){
col = fixed4(d, 0, 0, 1);
}
return col;``````

### 三、最終效果

``````Shader "XYH/Test1"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}

float heart(float2 st)
{
//調整位置
st = (st - float2(0.5, 0.38)) * float2(2.1, 2.8);
//心形函數計算
return pow(st.x, 2) + pow(st.y - sqrt(abs(st.x)), 2);
}

fixed4 frag (v2f i) : SV_Target
{
float d = heart(i.uv);
//循環動畫
d = step(d, abs(sin(d * 8 - _Time.w * 2)));
//上色
float4 col = fixed4(d, 0.2, 0.21, 1);
if(d < 1){
col = fixed4(d, 0, 0, 1);
}
return col;
}
ENDCG
}
}
}

``````