shader 一步一步 日记累月 ----极坐标

发布时间 2024-01-11 16:46:02作者: porter_代码工作者
学习shader 就是在考察 数学知识
极坐标

复习一下极坐标的点的位置是靠theta(角度)和r(距离)两个信息(二维空间)

shader:

            // 直角坐标转极坐标方法
            float2 RectToPolar(float2 uv, float2 centerUV) {
                uv = uv - centerUV;                 //改变中心 将中心从UV左下角移到UV中心
                float theta = atan2(uv.y, uv.x);    // atan()值域[-π/2, π/2]一般不用; atan2()值域[-π, π]确定一个完整的圆
                float r = length(uv);               //UV上的某一点到我们确定的中心得距离
                return float2(theta, r);
            }


            // 输出结构>>>像素
            half4 frag(VertexOutput i) : COLOR {
                // 直角坐标转极坐标
                float2 thetaR = RectToPolar(i.uv, float2(0.5, 0.5));
                // 极坐标转纹理采样UV
                float2 polarUV = float2(
                    thetaR.x / 3.141593 * 0.5 + 0.5,    // θ映射到[0, 1]
                    thetaR.y + frac(_Time.x * 3.0)      // r随时间流动
                );
                // 采样MainTex
                half4 var_MainTex = tex2D(_MainTex, polarUV);
                // 处理最终输出
                half3 finalRGB = (1 - var_MainTex.rgb) * _Color;
                half opacity = (1 - var_MainTex.r) * _Opacity * i.color.r;
                // 返回值
                return half4(finalRGB * opacity, opacity);
            }