﻿Shader "Unlit/Tesseract"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Alpha ("alpha", Float) = 0.90
        _Rot4 ("4d rotation", Float) = 0.0
        _Rot3 ("3d rotation", Float) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" }
        LOD 100

        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha
        //ZWrite Off

        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;
                fixed3 color : COLOR;
            };

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

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Rot4;
            float _Rot3;
            float _Alpha;

            v2f vert (appdata v)
            {
                v2f o;
                float4 vert4d = float4(v.vertex.x, v.vertex.y, v.vertex.z, v.color.x);
                o.model = vert4d;

                float cosA = cos(_Rot4 * 2 * 3.14159);
                float sinA = sin(_Rot4 * 2 * 3.14159);

                // yz and xw rotation
                float4x4 rotm = float4x4(
                    cosA, 0, 0, -sinA,
                    0, cosA, -sinA, 0,
                    0, sinA, cosA, 0,
                    sinA, 0, 0, cosA);
                float4 rot4d = mul(rotm, vert4d);

                float3 model =  rot4d.xyz / (rot4d.w + 1.5); // rot4d.xyz + rot4d.w ;

                float cosT = cos(_Rot3 * 2 *3.14159);
                float sinT = sin(_Rot3 * 2 *3.14159);

                // y rotation
                float3x3 roty = float3x3(
                    cosT, 0, sinT,
                    0, 1, 0,
                    -sinT, 0, cosT);

                o.vertex = UnityObjectToClipPos(mul(roty,model));
                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 = fixed4(i.uv.x, i.uv.y, 0,1);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                col.a = _Alpha;
                return col;
            }
            ENDCG
        }
    }
}
