UnityShader 3 2D 묘사 효과 구현

본 논문 의 사례 는 Unity Shader 3 가 2D 묘사 효 과 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
1.

Shader "Custom/Edge"
{
 Properties
 {
 _MainTex ("Texture", 2D) = "white" {}
 _OffsetUV ("OffsetUV", Range(0, 1)) = 0.1
 _EdgeColor ("EdgeColor", Color) = (1, 0, 0, 1)
 _AlphaTreshold ("Treshold", Range(0, 1)) = 0.5 
 }
 SubShader
 {
 Tags { "Queue" = "Transparent" }
 Blend SrcAlpha OneMinusSrcAlpha
 
 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 struct appdata
 {
 float4 vertex : POSITION;
 fixed2 uv : TEXCOORD0;
 };
 
 struct v2f
 { 
 float4 vertex : SV_POSITION;
 fixed2 uv[5] : TEXCOORD0;
 };
 
 sampler2D _MainTex;
 float4 _MainTex_ST;
 fixed _OffsetUV;
 fixed4 _EdgeColor;
 fixed _AlphaTreshold;
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 
 o.uv[0] = v.uv; 
    o.uv[1] = v.uv + fixed2(0, _OffsetUV); //up 
    o.uv[2] = v.uv + fixed2(-_OffsetUV, 0); //left 
    o.uv[3] = v.uv + fixed2(0, -_OffsetUV); //bottom 
    o.uv[4] = v.uv + fixed2(_OffsetUV, 0); //right 
 
 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 {
 fixed4 original = tex2D(_MainTex, i.uv[0]); 
    fixed alpha = original.a;
 
    fixed p1 = tex2D(_MainTex, i.uv[1]).a; 
    fixed p2 = tex2D(_MainTex, i.uv[2]).a; 
    fixed p3 = tex2D(_MainTex, i.uv[3]).a; 
    fixed p4 = tex2D(_MainTex, i.uv[4]).a; 
  
    alpha = p1 + p2 + p3 + p4 + alpha; 
    alpha /= 5; 
 
    if (alpha < _AlphaTreshold) original.rgb = _EdgeColor.rgb; 
  
    return original; 
 }
 ENDCG
 }
 }
}
2.

Shader "Custom/Edge"
{
 Properties
 {
 _Edge ("Edge", Range(0, 0.2)) = 0.043
 _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1)
 _MainTex ("MainTex", 2D) = "white" {}
 }
 SubShader
 {
 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 fixed _Edge;
 fixed4 _EdgeColor;
 sampler2D _MainTex;
 
 struct appdata
 {
 float4 vertex : POSITION;
 fixed2 uv : TEXCOORD0;
 };
 
 struct v2f
 {
 float4 vertex : SV_POSITION;
 float4 objVertex : TEXCOORD0;
 fixed2 uv : TEXCOORD1;
 };
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 o.objVertex = v.vertex;
 o.uv = v.uv;
 
 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 { 
 fixed x = i.uv.x;
 fixed y = i.uv.y;
  
 if((x < _Edge) || (abs(1 - x) < _Edge) || (y < _Edge) || (abs(1 - y) < _Edge)) 
 {
  return _EdgeColor * abs(cos(_Time.y));
 }
 else 
 {
  fixed4 color = tex2D(_MainTex, i.uv);
  return color;
 }
 
 //return i.objVertex;
 //return fixed4(i.uv, 0, 1);
 }
 ENDCG
 }
 }
}
3.다음 그림,왼쪽 은 Image 이 고 오른쪽 은 Plane 입 니 다.

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
Shader "Custom/Edge" 
{ 
 Properties 
 { 
  _Edge ("Edge", Range(0, 0.2)) = 0.043 
  _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1) 
 _FlowColor ("FlowColor", Color) = (1, 1, 1, 1) 
 _FlowSpeed ("FlowSpeed", Range(0, 10)) = 3
 _MainTex ("MainTex", 2D) = "white" {} 
 } 
 SubShader 
 { 
 Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" } 
 
  Pass 
  { 
 ZWrite Off 
 Blend SrcAlpha OneMinusSrcAlpha 
 
   CGPROGRAM 
   #pragma vertex vert 
   #pragma fragment frag 
   #include "UnityCG.cginc" 
 
   fixed _Edge; 
   fixed4 _EdgeColor; 
 fixed4 _FlowColor;
 float _FlowSpeed;
 sampler2D _MainTex;
 
   struct appdata 
   { 
    float4 vertex : POSITION; 
    fixed2 uv : TEXCOORD0; 
   }; 
 
   struct v2f 
   { 
    float4 vertex : SV_POSITION; 
    fixed2 uv : TEXCOORD1; 
   }; 
 
   v2f vert (appdata v) 
   { 
    v2f o; 
    o.vertex = UnityObjectToClipPos(v.vertex); 
    o.uv = v.uv; 
    return o; 
   } 
    
   fixed4 frag (v2f i) : SV_Target 
   {  
    fixed x = i.uv.x; 
    fixed y = i.uv.y; 
 
    if((x < _Edge) || (abs(1 - x) < _Edge) || (y < _Edge) || (abs(1 - y) < _Edge)) 
    { 
  //     :
  //         (x,y),      (rx0,ry0)     a          (x0,y0),   :
  //x0 = (x - rx0) * cos(a) - (y - ry0) * sin(a) + rx0 ;
  //y0 = (x - rx0) * sin(a) + (y - ry0) * cos(a) + ry0 ;
 
  float a = _Time.y * _FlowSpeed; 
  float2 rotUV;
 
  x -= 0.5;
  y -= 0.5;
  rotUV.x = x * cos(a) - y * sin(a) + 0.5;
  rotUV.y = x * sin(a) + y * cos(a) + 0.5;
  
  fixed temp = saturate(rotUV.x - 0.5);//-0.5            
     return _EdgeColor * (1 - temp) + _FlowColor * temp;
    } 
    else 
    { 
     //fixed4 color = tex2D(_MainTex, i.uv); 
     return fixed4(1, 1, 1, 0); 
    } 
   } 
   ENDCG 
  } 
 } 
} 
4.위의 효과 도 를 살 펴 보면 오른쪽 플 레 인 에 톱날 이 생 겼 다.톱날 을 해결 하 는 일반적인 방법 은 모호 처 리 를 하 는 것 이다.모호 처 리 는 일반적으로 스티커 처리 와 코드 처리 의 구분 이 있 는데 여 기 는 스티커 처 리 를 사용한다.스티커 처 리 는 경계 가 모호 한 스티커 를 제공 해 야 한다.


위의 그림 과 같이 왼쪽 아래 는 안쪽 의 톱날 을 반대 하 는 그림 이 고 오른쪽 위 는 처리 되 지 않 은 그림 이다.

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 
 
Shader "Custom/Edge2" 
{ 
 Properties 
 { 
  _Edge ("Edge", Range(0, 0.2)) = 0.043 
  _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1) 
  _FlowColor ("FlowColor", Color) = (1, 1, 1, 1) 
  _FlowSpeed ("FlowSpeed", Range(0, 10)) = 3 
  _MainTex ("MainTex", 2D) = "white" {} 
 } 
 SubShader 
 { 
  Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" } 
 
  Pass 
  { 
   ZWrite Off 
   Blend SrcAlpha OneMinusSrcAlpha 
 
   CGPROGRAM 
   #pragma vertex vert 
   #pragma fragment frag 
   #include "UnityCG.cginc" 
 
   fixed _Edge; 
   fixed4 _EdgeColor; 
   fixed4 _FlowColor; 
   float _FlowSpeed; 
   sampler2D _MainTex; 
 
   struct appdata 
   { 
    float4 vertex : POSITION; 
    fixed2 uv : TEXCOORD0; 
   }; 
 
   struct v2f 
   { 
    float4 vertex : SV_POSITION; 
    fixed2 uv : TEXCOORD1; 
   }; 
 
   v2f vert (appdata v) 
   { 
    v2f o; 
    o.vertex = UnityObjectToClipPos(v.vertex);  
    o.uv = v.uv; 
    return o; 
   } 
    
   fixed4 frag (v2f i) : SV_Target 
   {    
 fixed4 color = tex2D(_MainTex, i.uv);
 float alpha = color.a;
 
 fixed x = i.uv.x; 
    fixed y = i.uv.y; 
 float a = _Time.y * _FlowSpeed; 
    float2 rotUV; 
 
    x -= 0.5; 
    y -= 0.5; 
    rotUV.x = x * cos(a) - y * sin(a) + 0.5; 
    rotUV.y = x * sin(a) + y * cos(a) + 0.5; 
      
    fixed temp = saturate(rotUV.x - 0.5);//-0.5             
 fixed4 finalColor = _EdgeColor * (1 - temp) + _FlowColor * temp;  
 
 finalColor.a = alpha;
 return finalColor; 
   } 
   ENDCG 
  } 
 } 
} 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기