How SkGradientShader work

73 views
Skip to first unread message

Dave M

unread,
Dec 14, 2021, 5:27:29 AM12/14/21
to skia-discuss
I notice a example, https://fiddle.skia.org/c/@Canvas_drawVertices_2
firstly,i find 4 points as param, usually we use 2 points
then i do not know how texs arry works, it like texture coord but not 0-1
ddd.png

Brian Osman

unread,
Dec 14, 2021, 9:41:16 AM12/14/21
to skia-d...@googlegroups.com
That's a very confusing sample. The first argument to MakeLinear is an array of exactly two points (the positions on the canvas that define the "ends" of the linear gradient). The other two points in that array will be ignored when making the gradient. (I think the sample is written so that it can reuse that array for the vertex positions later).

The texs array (used with the vertices object) is just like texture coordinates. However, Skia generally operates in "local" coordinates that are not [0-1]. In this case:

  • The gradient is defined in terms of "large" (pixel) coordinates. Because of the points argument, it stretches the gradient from (u,v) of (0, 0) to (250, 0).
  • The texture coordinates at the vertices are the same as the pixel coordinates. This makes the end of the gradient line up with the full size of the vertices.
This is just how Skia code works (using the same coordinates for both geometry and shaders). It's possible to modify that sample to use [0,1] for the shader (and also use a separate array of two points for the gradient, to eliminate that confusion): https://fiddle.skia.org/c/10564d61c228b8f7aea1938ca38d53d8


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/65fadb50-0006-403c-a152-2e5001dcf312n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages