Skip to content

Instantly share code, notes, and snippets.

@smkplus
Last active July 21, 2019 12:43
Show Gist options
  • Save smkplus/c0001f5ab5f97fbb820525b471e7f897 to your computer and use it in GitHub Desktop.
Save smkplus/c0001f5ab5f97fbb820525b471e7f897 to your computer and use it in GitHub Desktop.
PuzzleShader.md

Surface Shader

Shader "Smkgames/Surface/Puzzle" {
	Properties{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_Mask("Mask (A)", 2D) = "white" {}
		_Mask2("Mask (A)", 2D) = "white" {}
		_Mask3("Mask (A)", 2D) = "white" {}
		_Tile("Tile",Vector) = (1,1,1,1)
	}

	SubShader{
		Tags{ "RenderType" = "Transparent" }
		LOD 200
		

		CGPROGRAM
#pragma surface surf NoLighting alpha
#pragma target 3.0
		sampler2D _MainTex;
		sampler2D _Mask;
		sampler2D _Mask2;
		sampler2D _Mask3;
		float4 _Tile;

		struct Input {
			float2 uv_MainTex;
			float2 uv2_Mask;
			float4 color : COLOR;
		};


		 fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
		{
			fixed4 c;
			c.rgb = s.Albedo; 
			c.a = s.Alpha;
			return c;
		}
     

		float GetPieceAlpha(float pieceIdentifier, float pieceAlpha, float2 direction)
		{
			switch (pieceIdentifier)
			{
				case 0: pieceAlpha *= tex2D(_Mask, direction);
					break;
				case 1: pieceAlpha *= tex2D(_Mask2, direction);
					break;
				case 2: pieceAlpha *= tex2D(_Mask3, direction);
					break;
			}
			return pieceAlpha;
		}

		fixed4 _Color;
		void surf(Input IN, inout SurfaceOutput o) {
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex*_Tile) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;

			float2x2 rotationMatrix = float2x2(0, -1, 1, 0);	
			float2 uv_Top = IN.uv2_Mask;
			float2 uv_Left = mul(uv_Top.xy, rotationMatrix);
			float2 uv_Bot = mul(uv_Left.xy, rotationMatrix);
			float2 uv_Right = mul(uv_Bot.xy, rotationMatrix);

			o.Alpha = GetPieceAlpha(IN.color.r, o.Alpha, uv_Left);
			o.Alpha = GetPieceAlpha(IN.color.g, o.Alpha, uv_Right);
			o.Alpha = GetPieceAlpha(IN.color.b, o.Alpha, uv_Top);
			o.Alpha = GetPieceAlpha(IN.color.a, o.Alpha, uv_Bot);
		}
		ENDCG
	}
	FallBack "Diffuse"
}
Shader "Smkgames/Surface/Puzzle" {
	Properties{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_Mask("Mask (A)", 2D) = "white" {}
		_Tile("Tile",Vector) = (1,1,1,1)
	}

	SubShader{
		Tags{ "RenderType" = "Transparent" }
		LOD 200
		

		CGPROGRAM
		#pragma surface surf NoLighting alpha
		#pragma target 3.0
		sampler2D _MainTex;
		sampler2D _Mask;
		sampler2D _Mask2;
		sampler2D _Mask3;
		float4 _Tile;

		struct Input {
			float2 uv_MainTex;
			float2 uv2_Mask;
			float4 color : COLOR;
		};


		 fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
		{
			fixed4 c;
			c.rgb = s.Albedo; 
			c.a = s.Alpha;
			return c;
		}
     

		float GetPieceAlpha(float pieceIdentifier, float pieceAlpha, float2 direction)
		{
			float4 mask = tex2D(_Mask, direction);
			switch (pieceIdentifier)
			{
				case 0: pieceAlpha *= float4(mask.r,mask.r,mask.r,mask.a);
					break;
				case 1: pieceAlpha *= float4(mask.g,mask.g,mask.g,mask.a);
					break;
				case 2: pieceAlpha *= float4(mask.b,mask.b,mask.b,mask.a);
					break;
			}
			return smoothstep(0,0.5,pieceAlpha);
		}

		fixed4 _Color;
		void surf(Input IN, inout SurfaceOutput o) {
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex*_Tile) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;

			float2x2 rotationMatrix = float2x2(0, -1, 1, 0);	
			float2 uv_Top = IN.uv2_Mask;
			float2 uv_Left = mul(uv_Top.xy, rotationMatrix);
			float2 uv_Bot = mul(uv_Left.xy, rotationMatrix);
			float2 uv_Right = mul(uv_Bot.xy, rotationMatrix);

			o.Alpha = GetPieceAlpha(IN.color.r, o.Alpha, uv_Left);
			o.Alpha = GetPieceAlpha(IN.color.g, o.Alpha, uv_Right);
			o.Alpha = GetPieceAlpha(IN.color.b, o.Alpha, uv_Top);
			o.Alpha = GetPieceAlpha(IN.color.a, o.Alpha, uv_Bot);
		}
		ENDCG
	}
	FallBack "Diffuse"
}

Fragment Shader

Shader "Smkgames/Fragment/Puzzle"{
	Properties{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_Mask("Mask (A)", 2D) = "white" {}
		_Mask2("Mask (A)", 2D) = "white" {}
		_Mask3("Mask (A)", 2D) = "white" {}
		_Tile("Tile",Vector) = (1,1,1,1)
	}

	SubShader{
		Tags{ "RenderType"="Transparent" "Queue"="Transparent"}

		Blend SrcAlpha OneMinusSrcAlpha
		ZWrite off

		Pass{
			CGPROGRAM

			#include "UnityCG.cginc"
			#pragma target 3.0

			#pragma vertex vert
			#pragma fragment frag

			sampler2D _MainTex;
			fixed4 _MainTex_ST;


		sampler2D _Mask;
		fixed4 _Mask_ST;

		sampler2D _Mask2;
		sampler2D _Mask3;
		float4 _Tile;

			struct appdata{
				fixed4 vertex : POSITION;
				fixed2 uv : TEXCOORD0;
				fixed2 maskuv : TEXCOORD1;
				fixed4 color : COLOR;
			};

			struct v2f{
				fixed4 position : SV_POSITION;
				fixed2 uv : TEXCOORD0;
				fixed2 maskuv : TEXCOORD1;
				fixed4 color : COLOR;
			};


				float GetPieceAlpha(float pieceIdentifier, float pieceAlpha, float2 direction)
		{
			switch (pieceIdentifier)
			{
				case 0: pieceAlpha *= tex2D(_Mask, direction);
					break;
				case 1: pieceAlpha *= tex2D(_Mask2, direction);
					break;
				case 2: pieceAlpha *= tex2D(_Mask3, direction);
					break;
			}
			return pieceAlpha;
		}

			v2f vert(appdata v){
				v2f o;
				o.position = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.maskuv = TRANSFORM_TEX(v.maskuv, _Mask);
				o.color = v.color;
				return o;
			}

			fixed4 frag(v2f i) : SV_TARGET{
			half4 c = tex2D(_MainTex, i.uv*_Tile);

			fixed2x2 rotationMatrix = fixed2x2(0, -1, 1, 0);	
			fixed2 uv_Top = i.maskuv;
			fixed2 uv_Left = mul(uv_Top.xy, rotationMatrix);
			fixed2 uv_Bot = mul(uv_Left.xy, rotationMatrix);
			fixed2 uv_Right = mul(uv_Bot.xy, rotationMatrix);

			c.a = GetPieceAlpha(i.color.r, c.a, uv_Left);
			c.a *= GetPieceAlpha(i.color.g, c.a, uv_Right);
			c.a *= GetPieceAlpha(i.color.b, c.a, uv_Top);
			c.a *= GetPieceAlpha(i.color.a, c.a, uv_Bot);

			return c;
			}

			ENDCG
		}
	}
}
Shader "Smkgames/Fragment/Puzzle"{
	Properties{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_Mask("Mask (A)", 2D) = "white" {}
		_Tile("Tile",Vector) = (1,1,1,1)
	}

	SubShader{
		Tags{ "RenderType"="Transparent" "Queue"="Transparent"}

		Blend SrcAlpha OneMinusSrcAlpha
		ZWrite off

		Pass{
			CGPROGRAM

			#include "UnityCG.cginc"
			#pragma target 3.0

			#pragma vertex vert
			#pragma fragment frag

			sampler2D _MainTex;
			fixed4 _MainTex_ST;


		sampler2D _Mask;
		fixed4 _Mask_ST;
		float4 _Tile;
		half4 _Color;

			struct appdata{
				fixed4 vertex : POSITION;
				fixed2 uv : TEXCOORD0;
				fixed2 maskuv : TEXCOORD1;
				fixed4 color : COLOR;
			};

			struct v2f{
				fixed4 position : SV_POSITION;
				fixed2 uv : TEXCOORD0;
				fixed2 maskuv : TEXCOORD1;
				fixed4 color : COLOR;
			};

			
		fixed GetPieceAlpha(fixed pieceIdentifier, fixed pieceAlpha, fixed2 direction)
		{
			half4 p = tex2D(_Mask, direction);
			half4 rg = lerp(p.r,p.g,pieceIdentifier);
			pieceAlpha -= lerp(rg,p.b,max(0,pieceIdentifier-1));
			return step(0.2,pieceAlpha);
		}

			v2f vert(appdata v){
				v2f o;
				o.position = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.maskuv = TRANSFORM_TEX(v.maskuv, _Mask);
				o.color = v.color;
				return o;
			}

			fixed4 frag(v2f i) : SV_TARGET{
			half4 c = tex2D(_MainTex, i.uv*_Tile)*_Color;

			fixed2x2 rotationMatrix = fixed2x2(0, -1, 1, 0);	
			fixed2 uv_Top = i.maskuv;
			fixed2 uv_Left = mul(uv_Top.xy, rotationMatrix);
			fixed2 uv_Bot = mul(uv_Left.xy, rotationMatrix);
			fixed2 uv_Right = mul(uv_Bot.xy, rotationMatrix);

			c.a = GetPieceAlpha(i.color.r, c.a, uv_Left);
			c.a = GetPieceAlpha(i.color.g, c.a, uv_Right);
			c.a = GetPieceAlpha(i.color.b, c.a, uv_Top);
			c.a = GetPieceAlpha(i.color.a, c.a, uv_Bot);
			c.a = 1-c.a;

			return c;
			}

			ENDCG
		}
	}
}
Shader "Smkgames/Fragment/Puzzle"{
	Properties{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_Mask("Mask (A)", 2D) = "white" {}
		_Tile("Tile",Vector) = (1,1,1,1)
	}

	SubShader{
		Tags{ "RenderType"="Transparent" "Queue"="Transparent"}

		Blend SrcAlpha OneMinusSrcAlpha
		ZWrite off

		Pass{
			CGPROGRAM

			#include "UnityCG.cginc"
			#pragma target 3.0

			#pragma vertex vert
			#pragma fragment frag

			sampler2D _MainTex;
			fixed4 _MainTex_ST;


		sampler2D _Mask;
		float4 _Color;
		fixed4 _Mask_ST;
		float4 _Tile;

			struct appdata{
				fixed4 vertex : POSITION;
				fixed2 uv : TEXCOORD0;
				fixed2 maskuv : TEXCOORD1;
				fixed4 color : COLOR;
			};

			struct v2f{
				fixed4 position : SV_POSITION;
				fixed2 uv : TEXCOORD0;
				fixed2 maskuv : TEXCOORD1;
				fixed4 color : COLOR;
			};


	fixed GetPieceAlpha(fixed pieceIdentifier, fixed pieceAlpha, fixed2 direction)
		{
			half4 p = tex2D(_Mask, direction);
			half4 rg = lerp(p.r,p.g,pieceIdentifier);
			half4 rgb = lerp(rg,p.b,max(0,pieceIdentifier-1));
			return step(0.2,rgb);
		}


			v2f vert(appdata v){
				v2f o;
				o.position = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.maskuv = TRANSFORM_TEX(v.maskuv, _Mask);
				o.color = v.color;
				return o;
			}

			fixed4 frag(v2f i) : SV_TARGET{
			half4 c = tex2D(_MainTex, i.uv*_Tile)*_Color;

			fixed2x2 rotationMatrix = fixed2x2(0, -1, 1, 0);	
			fixed2 uv_Top = i.maskuv;
			fixed2 uv_Left = mul(uv_Top.xy, rotationMatrix);
			fixed2 uv_Bot = mul(uv_Left.xy, rotationMatrix);
			fixed2 uv_Right = mul(uv_Bot.xy, rotationMatrix);

			c.a = GetPieceAlpha(i.color.r, c.a, uv_Left);
			c.a *= GetPieceAlpha(i.color.g, c.a, uv_Right);
			c.a *= GetPieceAlpha(i.color.b, c.a, uv_Top);
			c.a *= GetPieceAlpha(i.color.a, c.a, uv_Bot);

			return c;
			}

			ENDCG
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment