﻿Shader "Tholin/CloudShader"
{
	Properties
	{
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Mask ("Mask", 2D) = "white" {}
		_TransparencyMul ("Transparency Multiplier", Range(0,1)) = 0
		_TransparencyOffset ("Transparency Offset", Range(0.1,1)) = 0
		_ScrollX ("Scroll Speed (X)", Range(0,1)) = 0.1
		_ScrollY ("Scroll Speed (Y)", Range(0,1)) = 0
		_Brightness ("Cloud Brightness", Range(0,1)) = 1
		_Scale ("Cloud Scale", float) = 1
	}
	SubShader
	{
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane"}
		LOD 100

		ZWrite Off
		Blend SrcAlpha OneMinusSrcAlpha
		Cull Off

		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0

			#include "UnityCG.cginc"

			struct appdata_t
			{
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float2 texcoord : TEXCOORD0;
				float2 texcoordShifted : TEXCOORD1;
				UNITY_VERTEX_OUTPUT_STEREO
			};

			sampler2D _MainTex;
			sampler2D _Mask;
			float4 _MainTex_ST;
			half _TransparencyMul;
			half _TransparencyOffset;
			float _ScrollX;
			float _ScrollY;
			fixed4 _Color;
			float _Brightness;
			float _Scale;

			v2f vert(appdata_t v)
			{
				v2f o;
				UNITY_SETUP_INSTANCE_ID(v);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.texcoord = o.texcoordShifted = TRANSFORM_TEX(v.texcoord, _MainTex);
				float t = _Time;
				o.texcoordShifted.x += (t * _ScrollX) % 1.0;
				o.texcoordShifted.y += (t * _ScrollY) % 1.0;
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.texcoordShifted * _Scale) * _Color;
				col.r *= _Brightness;
				col.g *= _Brightness;
				col.b *= _Brightness;
				float mul = tex2D(_Mask, i.texcoord).x;
				col.a -= _TransparencyOffset;
				if(col.a < 0) col.a = 0;
				else {
					col.a *= 1.0 / (1.0 - _TransparencyOffset);
					col.a *= _TransparencyMul * mul;
				}
				return col;
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}
