Oval Gradient in Android

前端 未结 4 1204
礼貌的吻别
礼貌的吻别 2020-12-05 19:42

I know how to setup and display an oval shape. I know how to apply a gradient to this shape. What I cant figure out is how I can get an oval gradient to match the shape.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 20:18

    I would suggest more 'direct' drawing approach. If you can draw gradient pixel-by-pixel, then you need just to remember that for

    • circle gradient color is proportional to r
    • ellipse (oval) gradient color is proportional to r1+r2

    Here:

    r - distance to circle center

    r1,r2 - distances to two foci of ellipse

    EDIT: Consider this Pixel Shader code:

    uniform sampler2D tex;
    
    void main()
    {
        vec2 center = vec2(0.5,0.5);
        float len = 1.3*(distance(gl_TexCoord[0].xy,center));
        vec2 foc1 = vec2(len,0.);
        vec2 foc2 = vec2(-len,0.);
        float r = distance(center+foc1,gl_TexCoord[0].xy) +
                 distance(center+foc2,gl_TexCoord[0].xy);
        float k = pow(r*0.9,1.3);
        vec4 color = vec4(k,k,k,1.);
        gl_FragColor = color;
    }
    

    You will get oval something like that: alt text

    good luck

提交回复
热议问题