Skip to content

Instantly share code, notes, and snippets.

@pharan
Last active September 17, 2022 04:46
Embed
What would you like to do?
A sample spine-unity shader substitute for the Spine/Skeleton shader that allows filling pixels with a certain color _FillColor, with a custom alpha _FillAlpha.
using UnityEngine;
using System.Collections;
using Spine;
using Spine.Unity;
public class SampleSkeletonFillUse : MonoBehaviour {
MeshRenderer meshRenderer;
MaterialPropertyBlock block;
IEnumerator Start () { // Start as a coroutine.
var state = GetComponent<SkeletonAnimation>().state;
meshRenderer = GetComponent<MeshRenderer>();
block = new MaterialPropertyBlock();
meshRenderer.SetPropertyBlock(block);
while (true) {
yield return new WaitForSeconds(1f); // Wait 1 second.
state.SetAnimation(0, "hit", false); // Play hit animation.
state.AddAnimation(0, "idle", true, 0f); // Queue idle animation.
StartCoroutine(FlashRoutine()); // Flash the fill color.
}
}
IEnumerator FlashRoutine () {
const int Flashes = 4;
// You can use these instead of strings.
int fillAlpha = Shader.PropertyToID("_FillAlpha");
int fillColor = Shader.PropertyToID("_FillColor");
for (int i = 0; i < Flashes; i++) {
block.SetFloat(fillAlpha, 1f); // Make the fill opaque.
block.SetColor(fillColor, Color.white); // Fill with white.
meshRenderer.SetPropertyBlock(block);
yield return null;
block.SetColor(fillColor, new Color(0.8f, 0, 0)); // Fill with red.
meshRenderer.SetPropertyBlock(block);
yield return null;
block.SetFloat(fillAlpha, 0f); // Remove the fill.
meshRenderer.SetPropertyBlock(block);
yield return null;
}
}
}
//Skeleton Fill
// - Unlit + no shadow
// - Premultiplied Alpha Blending (One OneMinusSrcAlpha)
// - Double-sided, no depth
Shader "Spine/Skeleton Fill" {
Properties {
_FillColor ("Fill Color", Color) = (1,1,1,1)
_FillAlpha ("Fill Alpha", Range(0, 1)) = 0
[NoScaleOffset]_MainTex ("Main Texture", 2D) = "white" {}
}
SubShader {
Tags { "IgnoreProjector"="True" "Queue"="Transparent" "RenderType"="Transparent" "PreviewType"="Plane" }
LOD 100
Blend One OneMinusSrcAlpha
Cull Off
ZWrite Off
Lighting Off
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _FillColor;
uniform float _FillAlpha;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
float4 vertexColor : COLOR;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 vertexColor : COLOR;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.vertexColor = v.vertexColor;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
float4 frag (VertexOutput i) : COLOR {
float4 rawColor = tex2D(_MainTex,i.uv0);
float finalAlpha = (rawColor.a * i.vertexColor.a);
float3 finalColor = lerp((rawColor.rgb * i.vertexColor.rgb), (_FillColor.rgb * finalAlpha), _FillAlpha);
return fixed4(finalColor, finalAlpha);
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment