Created
May 13, 2014 20:53
-
-
Save jpsarda/906ead1764af814cce6f to your computer and use it in GitHub Desktop.
CG Shader to dig a hole in an image in Futile/Unity3D. See usage.cs for more details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FHole0Shader : FShader | |
{ | |
private Texture _hole0Texture=null; | |
private Vector2 _hole0Offset=Vector2.zero; | |
public Texture hole0Texture | |
{ | |
get {return _hole0Texture;} | |
} | |
public void SetHole0(FAtlasElement hole0Element,FAtlasElement toDigElement) { | |
_hole0Texture=hole0Element.atlas.texture; | |
_hole0Offset=hole0Element.uvTopLeft-toDigElement.uvTopLeft; | |
needsApply = true; | |
} | |
public FHole0Shader() : base("Hole0Shader", Shader.Find("Futile/Hole0")) | |
{ | |
needsApply = true; | |
} | |
override public void Apply(Material mat) | |
{ | |
if (_hole0Texture!=null) { | |
mat.SetTexture("_Hole0Tex",_hole0Texture); | |
mat.SetTextureOffset("_Hole0Tex",_hole0Offset); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Futile/Hole0" | |
{ | |
Properties | |
{ | |
_Hole0Tex ("Hole (RGB) Trans (A)", 2D) = "white" {} | |
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {} | |
_Color ("Main Color", Color) = (1,1,1,1) | |
} | |
Category | |
{ | |
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque"} | |
ZWrite Off | |
//Alphatest Greater 0 | |
Blend SrcAlpha OneMinusSrcAlpha | |
Fog { Color(0,0,0,0) } | |
Lighting Off | |
Cull Off //we can turn backface culling off because we know nothing will be facing backwards | |
BindChannels | |
{ | |
Bind "Vertex", vertex | |
Bind "texcoord", texcoord | |
Bind "Color", color | |
} | |
SubShader | |
{ | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
#pragma profileoption NumTemps=64 | |
float4 _Color; | |
sampler2D _MainTex; | |
sampler2D _Hole0Tex; | |
struct v2f { | |
float4 pos : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
float2 hole0uv : TEXCOORD1; | |
}; | |
float4 _MainTex_ST; | |
float4 _Hole0Tex_ST; | |
v2f vert (appdata_base v) | |
{ | |
v2f o; | |
o.pos = mul (UNITY_MATRIX_MVP, v.vertex); | |
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); | |
o.hole0uv = TRANSFORM_TEX (v.texcoord, _Hole0Tex); | |
return o; | |
} | |
half4 frag (v2f i) : COLOR | |
{ | |
half4 texcol = tex2D (_MainTex, i.uv) * _Color; | |
texcol.a *= (1-tex2D(_Hole0Tex, i.hole0uv).a) ; | |
return texcol; | |
} | |
ENDCG | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// This will dig a hole in an image | |
// Constraints : both image must have the same size, embedded in a texture of the same sizes (same texture works), and not trimmed. | |
// | |
// | |
FSprite toDig=new FSprite("toDigImage"); | |
AddChild(toDig); | |
FHole0Shader shader=new FHole0Shader(); | |
shader.SetHole0(Futile.atlasManager.GetElementWithName("holeImage"),Futile.atlasManager.GetElementWithName("toDigImage")); | |
toDig.shader=shader; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment