godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.31k stars 20.23k forks source link

Polygon2D vertex_colors behavior inconsistency #54755

Open berightben opened 2 years ago

berightben commented 2 years ago

Godot version

Godot 3.3.stable, 3.4.stable

System information

Windows 8.1 x64, GLES2, GLES3, Nvidia Geforce650M(425.31)

Issue description

The visual behavior of color vertex in Polygon2D is inconsistent, Weird visual behavior can be seen with simple square, with 4 different color in each corner of Polygon2D. See the example below:

It is however extremely noticeable in common shape with higher number of vertexes. The cases can be seen in the example below: gradient polygon comparison2 *for some reason color from vertex[0] (top left) has special behavior

I had tested it in other OS (popOS godot 3.3.4), it behaves the same way. I think it has something to do with how Polygon triangulation behave, and how vertex color is represented within the triangulation.

Steps to reproduce

  1. Create Polygon2D with n number of vertexes point.
  2. Add _vertexcolors property to each vertexes point.
  3. Change vertex position, play around some combination of vertex position.
  4. Observe the gradient behavior on some unique vertexes position.

Minimal reproduction project

debug_gradient_godot3.3.zip

berightben commented 2 years ago

One wise man says: it is not a bug, it is a feature

Zylann commented 2 years ago

The difference between "medium" and "simple" is not a bug. This is perfectly normal. You will get a similar result with a quad with colored vertices in Blender. This is because 2 triangles are used to draw this. The result you are expecting in "medium" is from an image editor which does not exploit vertices from 2 triangles to render using vertex interpolation alone. If you want a result like "medium" you may use a shader in which you do the interpolation in the fragment function (something like mix(mix(color1, color3, UV.y), mix(color2, color4, UV.y), UV.x)), or have more subdivided triangles with pre-blended colors (applying the expected result to specific positions and have the triangulation fill the gaps approximately), or use a texture maybe.

Now the "observed" behavior... you have even different colors there. Is that comparable? Is your problem the presence of a color you did not want? Or is it the discrepancy between "medium" and "simple"? Either way, the way gradients spread is caused by triangulation, and is pretty much normal with such approach.

berightben commented 2 years ago

Update for colors comparison:

Zylann commented 2 years ago

So it looks to me you just want to control in which orientation the triangulation occurs. I'm not sure if that's possible with Polygon2D. There is MeshInstance2D but it is Unity-style-kinda-meh (needs 3D vertices set into a mesh to then be flattened to 2D). You might be able to get the triangulation you want with custom drawing, with draw_primitive maybe. Or you could rotate the polygon.