简体   繁体   English

libgdx中不一致的颜色渐变绘制矩形/精灵

[英]Unconsistent color gradient drawing rectangles/sprites in libgdx

I'm trying to draw sprites with color gradients using a SpriteBatcher and setting manually the vertex colors to achieve some kind of basic illumination effect without shaders, but I noticed a weird effect in the gradients when only one of the vertex color is different. 我正在尝试使用SpriteBatcher绘制带有颜色渐变的精灵,并手动设置顶点颜色以实现某种基本的照明效果而不使用着色器,但我注意到当渐变中只有一个顶点颜色不同时会产生奇怪的效果。

It depends on the position of the vertex. 它取决于顶点的位置。 If the different color is on the bottom-left or the top-right there's no problem, but in the other two cases there's a weird gradient effect. 如果不同颜色位于左下角或右上角,则没有问题,但在另外两种情况下,会出现奇怪的渐变效果。

To explain better the situation, here's an image. 为了更好地解释这种情况,这是一张图片。

结果

I generated the top row of squares with the following code: 我使用以下代码生成了第一行方块:

shapeRenderer.begin(ShapeType.Filled);
Color c1 = new Color(1,1,1,1);
Color c2 = new Color(1,0,0,1);
shapeRenderer.rect(100f, 100f, 30f, 30f, c2, c1, c1, c1);
shapeRenderer.rect(150f, 100f, 30f, 30f, c1, c2, c1, c1);
shapeRenderer.rect(200f, 100f, 30f, 30f, c1, c1, c2, c1);
shapeRenderer.rect(250f, 100f, 30f, 30f, c1, c1, c1, c2);
shapeRenderer.end();

This code is generated with a ShapeRenderer, but I get the same result if I do it with a SpriteBatch. 这个代码是使用ShapeRenderer生成的,但如果我使用SpriteBatch,我会得到相同的结果。

I imagine it's related to the way a rect is drawn internally (based on 2 triangles) but, is there any way to achieve the same result in all the cases? 我想这与内部绘制矩形的方式有关(基于2个三角形)但是,在所有情况下,有没有办法实现相同的结果? (as the 3rd row of the image) (作为图像的第3行)

Something like a different GL draw mode or whatever... ? 有点像不同的GL绘制模式或其他......?

OK, so the thing is, that the drawing depends on the order of the triangles. 好的,事实是,绘图取决于三角形的顺序。 If the gradient goes from one vertex of the triangle through its angle bisector there's no problem, but if the color gradient goes along its side, the result is like the top-1 and top-3 squares. 如果渐变从三角形的一个顶点通过其角平分线,则没有问题,但如果颜色渐变沿着它的边,则结果就像顶部1和顶部3的正方形。

This is due (I think) to the fact a rectangle drawn like 这是因为(我认为)绘制了一个像矩形的事实

1-2
| |
4-3

Is actually drawn with two triangles like 实际上是用两个三角形绘制的

1-2       1
 \|  and  |\
  3       4-3

So, in this order, if the different color is in the vertex 1 or 3, the resulting gradient is weird. 因此,按此顺序,如果不同的颜色位于顶点1或3中,则生成的渐变很奇怪。

What we can do in this cases is change the order of the vertexes (not its coordinates) to be drawn like this 在这种情况下我们可以做的是改变这样绘制的顶点(而不是它的坐标)的顺序

2-3      2-3       3
| |  =>  |/   +   /|
1-4      1       1-4

It works just fine as we can see here: 它可以正常工作,我们可以在这里看到:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM