I’m new to Shader code. I’m using a ChromaKey shader to remove the black color from this John Travolta sprite sheet. And I’m using an Animate Texture Sheet script to cycle through the sprite sheet positions.
The Animate Texture Sheet script works fine with unity’s built in “Particles/Additive” Shader, but it does not with this ChromaKey shader. SetTextureOffset doesn’t affect it, which leads me to believe the shader is missing something that would allow offset of the UVs.
The end goal is to animate real green screen footage to place 2D hologram audience members into our 3D spectator sport game.
Inspector Settings - Shader
Threshhold: 0.0039, Slope: 0, Color: (5,4,5, 255)
Texture: <http://imgur.com/a/OgUvQ>
Inspector Settings - Script
Columns: 8, Rows: 8, FPS: 15
Does anyone see the problem? Here is the code:
Shader
Shader"Custom/ChromaKey" {
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
_thresh("Threshold", Range(0, 16)) = 0.65
_slope("Slope", Range(0, 1)) = 0.63
_keyingColor("KeyColour", Color) = (1,1,1,1)
}
SubShader{
Tags{ "Queue" = "Transparent""IgnoreProjector" = "True""RenderType" = "Transparent" }
LOD 100
Lighting Off
ZWrite Off
AlphaTest Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
sampler2D _MainTex;
float3 _keyingColor;
float _thresh; //0.8
float _slope; //0.2
#include "UnityCG.cginc"
float4 frag(v2f_img i) : COLOR{
float3 input_color = tex2D(_MainTex,i.uv).rgb;
float d = abs(length(abs(_keyingColor.rgb - input_color.rgb)));
float edge0 = _thresh*(1 - _slope);
float alpha = smoothstep(edge0,_thresh,d);
return float4(input_color,alpha);
}
ENDCG
}
}
FallBack"Unlit/Texture"
}
Texture Sheet Script
using UnityEngine;
using System.Collections;
public class AnimateTextureSheet : MonoBehaviour
{
public int columns = 2;
public int rows = 2;
public float framesPerSecond = 10f;
//the current frame to display
private int index = 0;
private Vector2 spriteSize;
private Renderer renderer;
void Start()
{
renderer = GetComponent<Renderer>();
StartCoroutine(updateTiling());
//set the tile size of the texture (in UV units), based on the rows and columns
spriteSize = new Vector2(1f / columns, 1f / rows);
renderer.materials[0].SetTextureScale("_MainTex", spriteSize);
}
private IEnumerator updateTiling()
{
renderer.materials[0].SetTextureOffset("_MainTex", Vector2.zero);
while (true)
{
//move to the next index
float xOffset = (columns -1) - (index % columns);
float yOffset = ((index / columns) % rows);
Vector2 offset = new Vector2(xOffset * spriteSize.x, //x index
(yOffset * spriteSize.y)); //y index
renderer.materials[0].SetTextureOffset("_MainTex", offset);
index++;
yield return new WaitForSeconds(1f/framesPerSecond);
}
}
}