2D sprite outlines
来源:互联网 发布:有了源码怎么做软件 编辑:程序博客网 时间:2024/05/21 07:13
Shader "Sprites/Outline"{ Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _Color ("Tint", Color) = (1,1,1,1) [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 // Add values to determine if outlining is enabled and outline color. [PerRendererData] _Outline ("Outline", Float) = 0 [PerRendererData] _OutlineColor("Outline Color", Color) = (1,1,1,1) } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" } Cull Off Lighting Off ZWrite Off Blend One OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile _ PIXELSNAP_ON #pragma shader_feature ETC1_EXTERNAL_ALPHA #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; }; fixed4 _Color; float _Outline; fixed4 _OutlineColor; v2f vert(appdata_t IN) { v2f OUT; OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); OUT.texcoord = IN.texcoord; OUT.color = IN.color * _Color; #ifdef PIXELSNAP_ON OUT.vertex = UnityPixelSnap (OUT.vertex); #endif return OUT; } sampler2D _MainTex; sampler2D _AlphaTex; float4 _MainTex_TexelSize; fixed4 SampleSpriteTexture (float2 uv) { fixed4 color = tex2D (_MainTex, uv); #if ETC1_EXTERNAL_ALPHA // get the color from an external texture (usecase: Alpha support for ETC1 on android) color.a = tex2D (_AlphaTex, uv).r; #endif //ETC1_EXTERNAL_ALPHA return color; } fixed4 frag(v2f IN) : SV_Target { fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color; // If outline is enabled and there is a pixel, try to draw an outline. if (_Outline > 0 && c.a != 0) { // Get the neighbouring four pixels. fixed4 pixelUp = tex2D(_MainTex, IN.texcoord + fixed2(0, _MainTex_TexelSize.y)); fixed4 pixelDown = tex2D(_MainTex, IN.texcoord - fixed2(0, _MainTex_TexelSize.y)); fixed4 pixelRight = tex2D(_MainTex, IN.texcoord + fixed2(_MainTex_TexelSize.x, 0)); fixed4 pixelLeft = tex2D(_MainTex, IN.texcoord - fixed2(_MainTex_TexelSize.x, 0)); // If one of the neighbouring pixels is invisible, we render an outline. if (pixelUp.a * pixelDown.a * pixelRight.a * pixelLeft.a == 0) { c.rgba = fixed4(1, 1, 1, 1) * _OutlineColor; } } c.rgb *= c.a; return c; } ENDCG } }
}
using UnityEngine;[ExecuteInEditMode]public class SpriteOutline : MonoBehaviour { public Color color = Color.white; private SpriteRenderer spriteRenderer; void OnEnable() { spriteRenderer = GetComponent<SpriteRenderer>(); UpdateOutline(true); } void OnDisable() { UpdateOutline(false); } void Update() { UpdateOutline(true); } void UpdateOutline(bool outline) { MaterialPropertyBlock mpb = new MaterialPropertyBlock(); spriteRenderer.GetPropertyBlock(mpb); mpb.SetFloat("_Outline", outline ? 1f : 0); mpb.SetColor("_OutlineColor", color); spriteRenderer.SetPropertyBlock(mpb); }}
0 0
- 2D sprite outlines
- Unity 2D Sprite
- Sprite (2D and UI)
- Sprite (2D and UI)
- Unity-2D图形&Sprite
- Unity 访问2d组件 Sprite Renderer
- unity 2D Sprite网格Slice工具
- U3D Sprite Manager 2D动画
- Unity3D Sprite Manager 2D动画
- (1-2)Unity3D/2D:Sprite Renderer组件详解
- Unity3D 2D动画插件EasyMotion2D (之三)Sprite
- Unity3D 2D动画插件EasyMotion2D (之四)Sprite Editor
- Unity3D 2D动画插件EasyMotion2D (之五)Sprite Splitter
- 游戏框架其八:2D图像 { Sprite | Font }
- 【U3D记事本】2D游戏sprite点击判断常见方法
- Unity3D 2D入门——sprite尺寸、获取对象
- 【Unity】2D Sprite九宫格(NineSliced)
- UNITY 2D学习笔记(一):Sprite
- mysql数据库jdbc连接通用类
- zoj3954 Seven-Segment Display
- 概率图模型之:贝叶斯网络
- Isomorphic Strings
- Linux下jdk的安装
- 2D sprite outlines
- UE4场景中电视机播放视频,并产生辉光效果
- 深入理解Android的startservice和bindservice
- 漫步数理统计二十——多元随机变量
- MyBatis报错: Parameter '*' not found. Available parameters are [1, 0, param1, param2]
- AHOI2017游记(伪)
- 高德地图-Android-逆地理转换:经纬度转换成地址
- 创建Graphics对象的三种方法
- web标准的理解