2019-05-06

極座標シェーダー実験メモ 極座標シェーダー実験メモ - Nao_uの日記 を含むブックマーク はてなブックマーク - 極座標シェーダー実験メモ - Nao_uの日記 極座標シェーダー実験メモ - Nao_uの日記 のブックマークコメント

f:id:Nao_u:20190506202125p:image

https://twitter.com/Cyanilux/status/1123944969125998592

Shader "Unlit/PolarCorrdinates"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		_Rotate("Rotate", Float) = 0.0
		_RotateSpeed("RotateSpeed", Float) = 0.0
		_RadiusAdd("RadiusAdd", Float) = 0.0
		_RadiusSpeed("RadiusSpeed", Float) = 0.0
	}
    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;

			float _Rotate;
			float _RotateSpeed;
			float _RadiusAdd;
			float _RadiusSpeed;

            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
            {
				float2 uv = (i.uv * 2.0f) - float2(1,1);
				float r = 1.0 - length(uv);
				float theta = atan2(uv.y, uv.x)*(1.0f / (3.1415926535f*2.0f));
				
				r = pow(r+0.9, 2.0);
				float2 puv;
				puv.x = r + _RadiusAdd + _RadiusSpeed * _Time.x;
				puv.y = r + theta + _Rotate + _RotateSpeed * _Time.x;

                // sample the texture
                fixed4 col = tex2D(_MainTex, puv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);

				return col;
            }
            ENDCG
        }
    }
}