From 55610f133a1b97c74cc9159f563bc96c6a180bc3 Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Mon, 3 Feb 2025 15:50:11 +0900 Subject: [PATCH] =?UTF-8?q?outlineShader=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gameton-06/Assets/Gameton/Art/Material.meta | 8 ++ .../Art/Material/Sprite-Outline.shader | 81 ++++++++++++++++ .../Art/Material/Sprite-Outline.shader.meta | 9 ++ .../Gameton/Art/Material/SpriteOutline.cs | 61 ++++++++++++ .../Art/Material/SpriteOutline.cs.meta | 11 +++ .../Gameton/Art/Material/SpriteOutline.mat | 95 +++++++++++++++++++ .../Art/Material/SpriteOutline.mat.meta | 8 ++ .../Gameton/Scripts/Common/OutlineShader.cs | 38 ++++++++ .../Scripts/Common/OutlineShader.cs.meta | 11 +++ 9 files changed, 322 insertions(+) create mode 100644 Gameton-06/Assets/Gameton/Art/Material.meta create mode 100644 Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader create mode 100644 Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader.meta create mode 100644 Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs create mode 100644 Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs.meta create mode 100644 Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat create mode 100644 Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat.meta create mode 100644 Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs create mode 100644 Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs.meta diff --git a/Gameton-06/Assets/Gameton/Art/Material.meta b/Gameton-06/Assets/Gameton/Art/Material.meta new file mode 100644 index 00000000..53873f5f --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3f58676c80e0a5340b2f8593db0ebc38 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader b/Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader new file mode 100644 index 00000000..f37d9555 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader @@ -0,0 +1,81 @@ +Shader "Sprites/Outline" +{ + Properties + { + [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} + _Color ("Tint", Color) = (1,1,1,1) + [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 + [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1) + [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1) + [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {} + [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", 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) + [PerRendererData] _OutlineSize("Outline Size", int) = 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 SpriteVert + #pragma fragment frag + #pragma target 2.0 + #pragma multi_compile_instancing + #pragma multi_compile _ PIXELSNAP_ON + #pragma multi_compile _ ETC1_EXTERNAL_ALPHA + #include "UnitySprites.cginc" + + float _Outline; + fixed4 _OutlineColor; + int _OutlineSize; + float4 _MainTex_TexelSize; + + 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) { + float totalAlpha = 1.0; + + [unroll(16)] + for (int i = 1; i < _OutlineSize + 1; i++) { + fixed4 pixelUp = tex2D(_MainTex, IN.texcoord + fixed2(0, i * _MainTex_TexelSize.y)); + fixed4 pixelDown = tex2D(_MainTex, IN.texcoord - fixed2(0,i * _MainTex_TexelSize.y)); + fixed4 pixelRight = tex2D(_MainTex, IN.texcoord + fixed2(i * _MainTex_TexelSize.x, 0)); + fixed4 pixelLeft = tex2D(_MainTex, IN.texcoord - fixed2(i * _MainTex_TexelSize.x, 0)); + + totalAlpha = totalAlpha * pixelUp.a * pixelDown.a * pixelRight.a * pixelLeft.a; + } + + if (totalAlpha == 0) { + c.rgba = fixed4(1, 1, 1, 1) * _OutlineColor; + } + } + + c.rgb *= c.a; + + return c; + } + ENDCG + } + } +} \ No newline at end of file diff --git a/Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader.meta b/Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader.meta new file mode 100644 index 00000000..b164572a --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material/Sprite-Outline.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: f22e64e2b31da2d4a994900ca8134d29 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs new file mode 100644 index 00000000..c29716be --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs @@ -0,0 +1,61 @@ +using System.Collections; +using System.Collections.Generic; +using Unity.VisualScripting; +using UnityEngine; + +namespace TON +{ + [ExecuteInEditMode] + public class SpriteOutline : MonoBehaviour + { + public Color color = Color.white; + + [Range(0, 16)] + public int outlineSize = 1; + + private SpriteRenderer spriteRenderer; + + void OnEnable() + { + spriteRenderer = GetComponent(); + + // UpdateOutline(true); + } + + // void OnDisable() + // { + // UpdateOutline(false); + // } + + // void Update() + // { + // UpdateOutline(true); + // } + + void OnTriggerEnter2D(Collider2D other) + { + if (other.CompareTag("Player")) // 플레이어가 접근하면 + { + UpdateOutline(true); + } + } + + void OnTriggerExit2D(Collider2D other) + { + if (other.CompareTag("Player")) // 플레이어가 나가면 + { + UpdateOutline(false); + } + } + + void UpdateOutline(bool outline) + { + MaterialPropertyBlock mpb = new MaterialPropertyBlock(); + spriteRenderer.GetPropertyBlock(mpb); + mpb.SetFloat("_Outline", outline ? 1f : 0); + mpb.SetColor("_OutlineColor", color); + mpb.SetFloat("_OutlineSize", outlineSize); + spriteRenderer.SetPropertyBlock(mpb); + } + } +} diff --git a/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs.meta b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs.meta new file mode 100644 index 00000000..bb2ade19 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5a4f80448bcd1dc408e6351abed25238 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat new file mode 100644 index 00000000..f85b8858 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat @@ -0,0 +1,95 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: SpriteOutline + m_Shader: {fileID: 4800000, guid: f22e64e2b31da2d4a994900ca8134d29, type: 3} + m_Parent: {fileID: 0} + m_ModifiedSerializedProperties: 0 + m_ValidKeywords: + - PIXELSNAP_ON + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_LockedProperties: + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _AlphaTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - PixelSnap: 1 + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _EnableExternalAlpha: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Outline: 0 + - _OutlineSize: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _Flip: {r: 1, g: 1, b: 1, a: 1} + - _OutlineColor: {r: 1, g: 1, b: 1, a: 1} + - _RendererColor: {r: 1, g: 1, b: 1, a: 1} + m_BuildTextureStacks: [] diff --git a/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat.meta b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat.meta new file mode 100644 index 00000000..9c162a93 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Art/Material/SpriteOutline.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e0e8dfd090d9b9242a44b05790ed39cd +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs b/Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs new file mode 100644 index 00000000..b1dabf34 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace TON +{ + public class OutlineShader : MonoBehaviour + { + public Material outlineMaterial; // 아웃라인 쉐이더 머티리얼 + public Material originalMaterial; + public SpriteRenderer spriteRenderer; + + void Start() + { + spriteRenderer = GetComponentInChildren(); + if (spriteRenderer != null) + { + originalMaterial = spriteRenderer.material; + } + } + + void OnTriggerEnter2D(Collider2D other) + { + if (other.CompareTag("Player")) // 플레이어가 접근하면 + { + spriteRenderer.material = outlineMaterial; + } + } + + void OnTriggerExit2D(Collider2D other) + { + if (other.CompareTag("Player")) // 플레이어가 나가면 + { + spriteRenderer.material = originalMaterial; + } + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs.meta new file mode 100644 index 00000000..e804b69c --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/OutlineShader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b916e72c0fb53d646a81ead6f8252799 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: