├── README.md ├── SpriteOutline.gif ├── SpriteOutline.mat └── SpriteOutline.shader /README.md: -------------------------------------------------------------------------------- 1 | # SpriteOutline 2 | ![](https://github.com/MaxWeiChen/SpriteOutline/blob/master/SpriteOutline.gif) 3 | 4 | 參考網址:https://nielson.io/2016/04/2d-sprite-outlines-in-unity 5 | -------------------------------------------------------------------------------- /SpriteOutline.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MaxWeiChen/SpriteOutline/c0524211d8f5746806af1a1e381d088ec0f63692/SpriteOutline.gif -------------------------------------------------------------------------------- /SpriteOutline.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_PrefabParentObject: {fileID: 0} 8 | m_PrefabInternal: {fileID: 0} 9 | m_Name: SpriteOutline 10 | m_Shader: {fileID: 4800000, guid: ade44fd9690b1498eb8fcdb5faaa01f2, type: 3} 11 | m_ShaderKeywords: ETC1_EXTERNAL_ALPHA 12 | m_LightmapFlags: 4 13 | m_EnableInstancingVariants: 0 14 | m_DoubleSidedGI: 0 15 | m_CustomRenderQueue: -1 16 | stringTagMap: {} 17 | disabledShaderPasses: [] 18 | m_SavedProperties: 19 | serializedVersion: 3 20 | m_TexEnvs: 21 | - _AlphaTex: 22 | m_Texture: {fileID: 0} 23 | m_Scale: {x: 1, y: 1} 24 | m_Offset: {x: 0, y: 0} 25 | - _BumpMap: 26 | m_Texture: {fileID: 0} 27 | m_Scale: {x: 1, y: 1} 28 | m_Offset: {x: 0, y: 0} 29 | - _DetailAlbedoMap: 30 | m_Texture: {fileID: 0} 31 | m_Scale: {x: 1, y: 1} 32 | m_Offset: {x: 0, y: 0} 33 | - _DetailMask: 34 | m_Texture: {fileID: 0} 35 | m_Scale: {x: 1, y: 1} 36 | m_Offset: {x: 0, y: 0} 37 | - _DetailNormalMap: 38 | m_Texture: {fileID: 0} 39 | m_Scale: {x: 1, y: 1} 40 | m_Offset: {x: 0, y: 0} 41 | - _EmissionMap: 42 | m_Texture: {fileID: 0} 43 | m_Scale: {x: 1, y: 1} 44 | m_Offset: {x: 0, y: 0} 45 | - _MainTex: 46 | m_Texture: {fileID: 0} 47 | m_Scale: {x: 1, y: 1} 48 | m_Offset: {x: 0, y: 0} 49 | - _MetallicGlossMap: 50 | m_Texture: {fileID: 0} 51 | m_Scale: {x: 1, y: 1} 52 | m_Offset: {x: 0, y: 0} 53 | - _OcclusionMap: 54 | m_Texture: {fileID: 0} 55 | m_Scale: {x: 1, y: 1} 56 | m_Offset: {x: 0, y: 0} 57 | - _ParallaxMap: 58 | m_Texture: {fileID: 0} 59 | m_Scale: {x: 1, y: 1} 60 | m_Offset: {x: 0, y: 0} 61 | m_Floats: 62 | - PixelSnap: 0 63 | - _BumpScale: 1 64 | - _Cutoff: 0.5 65 | - _DetailNormalMapScale: 1 66 | - _DstBlend: 0 67 | - _EnableExternalAlpha: 0 68 | - _GlossMapScale: 1 69 | - _Glossiness: 0.5 70 | - _GlossyReflections: 1 71 | - _Metallic: 0 72 | - _Mode: 0 73 | - _OcclusionStrength: 1 74 | - _Outline: 1 75 | - _Parallax: 0.02 76 | - _SmoothnessTextureChannel: 0 77 | - _SpecularHighlights: 1 78 | - _SrcBlend: 1 79 | - _UVSec: 0 80 | - _ZWrite: 1 81 | m_Colors: 82 | - _Color: {r: 1, g: 1, b: 1, a: 1} 83 | - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} 84 | - _Flip: {r: 1, g: 1, b: 1, a: 1} 85 | - _OutlineColor: {r: 0, g: 0, b: 0, a: 1} 86 | - _RendererColor: {r: 1, g: 1, b: 1, a: 1} 87 | -------------------------------------------------------------------------------- /SpriteOutline.shader: -------------------------------------------------------------------------------- 1 | // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) 2 | 3 | Shader "Sprites/SpriteOutline" 4 | { 5 | Properties 6 | { 7 | [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} 8 | _Color ("Tint", Color) = (1,1,1,1) 9 | [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 10 | [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1) 11 | [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1) 12 | [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {} 13 | [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0 14 | 15 | [MaterialToggle] _Outline ("Outline", Float) = 0 16 | _OutlineColor ("Outline Color", Color ) = (1,1,1,1) 17 | } 18 | 19 | SubShader 20 | { 21 | Tags 22 | { 23 | "Queue"="Transparent" 24 | "IgnoreProjector"="True" 25 | "RenderType"="Transparent" 26 | "PreviewType"="Plane" 27 | "CanUseSpriteAtlas"="True" 28 | } 29 | 30 | Cull Off 31 | Lighting Off 32 | ZWrite Off 33 | Blend One OneMinusSrcAlpha 34 | 35 | Pass 36 | { 37 | CGPROGRAM 38 | #pragma vertex SpriteVert 39 | #pragma fragment frag 40 | #pragma target 2.0 41 | #pragma multi_compile_instancing 42 | #pragma multi_compile _ PIXELSNAP_ON 43 | #pragma multi_compile _ ETC1_EXTERNAL_ALPHA 44 | #include "UnitySprites.cginc" 45 | 46 | float _Outline; 47 | float4 _OutlineColor; 48 | float4 _MainTex_TexelSize; 49 | 50 | fixed4 frag(v2f IN) : SV_Target 51 | { 52 | fixed4 col = SampleSpriteTexture (IN.texcoord) * IN.color; 53 | col.rgb *= col.a; 54 | 55 | if ( _Outline > 0 && col.a == 0 && IN.color.a > 0) 56 | { 57 | fixed4 pixelUp = tex2D( _MainTex, IN.texcoord + fixed2(0,_MainTex_TexelSize.y)); 58 | fixed4 pixelDown = tex2D( _MainTex, IN.texcoord - fixed2(0,_MainTex_TexelSize.y)); 59 | fixed4 pixelRight = tex2D( _MainTex, IN.texcoord + fixed2(_MainTex_TexelSize.x,0)); 60 | fixed4 pixelLeft = tex2D( _MainTex, IN.texcoord - fixed2(_MainTex_TexelSize.x,0)); 61 | 62 | if ( pixelUp.a != 0 || pixelDown.a != 0 || pixelRight.a != 0 || pixelLeft.a != 0) 63 | { 64 | col = _OutlineColor; 65 | } 66 | } 67 | 68 | return col * IN.color; 69 | } 70 | ENDCG 71 | } 72 | } 73 | } 74 | --------------------------------------------------------------------------------