﻿Shader "Unlit/ExtTextureOES"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}

	// GLES 3.0 shader, with ESSL3 extension
	SubShader
	{
		Tags { "RenderType" = "Opaque" }
		LOD 100
		Cull Off
		Lighting off
		ZTest off
		Zwrite off
		Blend off

		Pass
		{
			GLSLPROGRAM
			#extension GL_OES_EGL_image_external_essl3 : require
#ifdef VERTEX // here begins the vertex shader
			varying vec2 glFragment_uv;
			uniform mediump vec4 _MainTex_ST;

			void main() // all vertex shaders define a main() function
			{
				gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
				glFragment_uv = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
			}

#endif // here ends the definition of the vertex shader

#ifdef FRAGMENT // here begins the fragment shader
			varying vec2 glFragment_uv;
			uniform samplerExternalOES _MainTex;

			void main() // all fragment shaders define a main() function
			{
				//glFragment_uv = vec2(glFragment_uv.x,1.0 - glFragment_uv.y);
				//	gl_FragColor = texture2D(_MainTex, vec2(1.0 - glFragment_uv.x,glFragment_uv.y);
				gl_FragColor = texture2D(_MainTex, glFragment_uv);
				//gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
			}

#endif // here ends the definition of the fragment shader

			ENDGLSL
		}
	}

	SubShader
	{
		Tags { "RenderType" = "Opaque" }
		LOD 100
		Cull Off
		Lighting off
		ZTest off
		Zwrite off
		Blend off

		// GLES 2.0 shader, without ESSL3 extension
		Pass
		{
			GLSLPROGRAM
			#extension GL_OES_EGL_image_external : require
#ifdef VERTEX // here begins the vertex shader
			varying vec2 glFragment_uv;
			uniform mediump vec4 _MainTex_ST;

			void main() // all vertex shaders define a main() function
			{
				gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
				glFragment_uv = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
			}

#endif // here ends the definition of the vertex shader

#ifdef FRAGMENT // here begins the fragment shader
			varying vec2 glFragment_uv;
			uniform samplerExternalOES _MainTex;

			void main() // all fragment shaders define a main() function
			{
				gl_FragColor = texture2D(_MainTex, glFragment_uv);
				//gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
			}

#endif // here ends the definition of the fragment shader

			ENDGLSL
		}
	}

	// For GLES3 or GLES2 on device
	SubShader
	{
		Pass
		{
			ZWrite Off

			GLSLPROGRAM

			#pragma only_renderers gles3 gles

			#ifdef SHADER_API_GLES3
			#extension GL_OES_EGL_image_external_essl3 : require
			#else
			#extension GL_OES_EGL_image_external : require
			#endif

			uniform vec4 _UvTopLeftRight;
			uniform vec4 _UvBottomLeftRight;

			#ifdef VERTEX

			varying vec2 textureCoord;

			void main()
			{
				vec2 uvTop = mix(_UvTopLeftRight.xy, _UvTopLeftRight.zw, gl_MultiTexCoord0.x);
				vec2 uvBottom = mix(_UvBottomLeftRight.xy, _UvBottomLeftRight.zw, gl_MultiTexCoord0.x);
				textureCoord = mix(uvTop, uvBottom, gl_MultiTexCoord0.y);

				gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
			}

			#endif

			#ifdef FRAGMENT
			varying vec2 textureCoord;
			uniform samplerExternalOES _MainTex;

			void main()
			{
				#ifdef SHADER_API_GLES3
				gl_FragColor = texture(_MainTex, textureCoord);
				gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
				#else
				gl_FragColor = textureExternal(_MainTex, textureCoord);
				gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
				#endif
			}

			#endif

			ENDGLSL
		}
	}

	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				//fixed4 col = tex2D(_MainTex, i.uv);
				fixed4 col = float4(1.0, 1.0, 0.0, 1.0);
				// apply fog
				UNITY_APPLY_FOG(i.fogCoord, col);
				return col;
			}
			ENDCG
		}
	}
}
