Shader SweepGradient positions參數用法

以前有一個項目須要這樣的一個控件:
數組


天然想到用SweepGradient來渲染這個半環,參考了下官方的ColorPickerDemo,發現其中是一個完整的圓環,並不須要指定SweepGradient 的第四個參數positions.spa

因而查閱了API和stackoverflow,弄清了positions的用法it

public SweepGradient (float cx, float cy, int[] colors, float[] positions)io

Parametersast

cx float: The x-coordinate of the center   圓心的X座標渲染

cy float: The y-coordinate of the center   圓心的Y座標float

colors int: The colors to be distributed between around the center. There must be at least 2 colors in the array.im

                着色器的顏色數組
call

positions float: May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.stackoverflow

                positions數組指定顏色在圓上的渲染分佈,守護彩虹的任務就交給它了。

假如要繪製完整的圓,positions指定爲null就能夠了,默認會按照顏色數組的順序幫你渲染。

假如是像我要作的控件,是有缺口的圓,則須要咱們去指定各個顏色的位置。

渲染的方向是順時針,位置從0開始,到1結束,並且數組要呈單調遞增,否則可能會有可怕的後果。。

因此看回上面的控件,顏色數組爲{"綠色","黃色","紅色","紫色","藍色"},

綠色在180度,因此他對應的位置參數是0.5 (順時針算)

以此類推,黃色 225度,0.625

                 紅色 270度,0.75

                 紫色 315度,0.875

                 藍色 360度,1

因此對應的positions數組就是{ 0.5f, 0.625f, 0.75f, 0.875f, 1f }

至此,着色器設置完畢

相關文章
相關標籤/搜索