I have a 2D Mesh object generated at runtime painted all with uniform color. I want that mesh to have its color fade to transparent color close to the edges.

Despite I searched everywhere I could not find any solution. Unfortunately I'm not into Shader programming enough to build logically my solution.

EDIT:According to @wondra's reply my mesh is not fan shaped. It has a non regular hole in it.

How can I achieve this?

| improve this question | | | | |
  • \$\begingroup\$ You'll need to know which triangles have neighboring triangles. Then, whenever a triangle has an edge without a neighboring triangle you can lower the opacity on that edge's verts. \$\endgroup\$ – RandyGaul Jan 26 '15 at 20:14
  • 2
    \$\begingroup\$ maybe if you post an image of your mesh we can get a closer idea on which effect you are trying to achieve \$\endgroup\$ – Heisenbug Jan 28 '15 at 14:49

The simplest and probably fastest option would be to iterate your mesh and set edges vertices transparency to 0 (or any other desired value). This assumes you v got (fan shaped)adjacency information:

foreach(Triangle t in mesh)
   for(int i = 0; i < 3; i++)
      if(t.Adj[i] == - 1) // or ==null, either way, on the edge
         t.Vert[i].a = 0.f;
         t.Vert[(i + 1) % 3].a = 0.f;

In case you have opposite adjacency, it is just another mod. If you dont have neither, you can compute it. You can also distribute the alpha to adjected triangles based on distance for more in-depth smoother fadeout(just another inner loop or stack).

Other approach would be to compute extremes(x,y min/max of mesh) and map x,y on some function, which simplest would be for example if(vert.X is less than 10% of width near to extreme) vert.A = 0.f;. Though this would work well only for convex-shaped mesh, best near-rectangle shaped.

| improve this answer | | | | |
  • \$\begingroup\$ I edited my question to point out that the mesh is not fan shaped. Then, following the latter approach that some function where should be written? in the shader? \$\endgroup\$ – Leggy7 Jan 26 '15 at 15:47
  • \$\begingroup\$ @Leggy7 Not mesh fan shaped, but adjecency information: the adj[i] can point either to the neighbour opposite to vert[i] or the next one in ccw order(=fan shaped). Either way you need to know which triangles are next to eachother. Otherwise you would have to find which vertices are on the edge by some other way. \$\endgroup\$ – wondra Jan 26 '15 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.