Author Topic: Vertex coloured sprites  (Read 2664 times)

Seroster

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 6
    • View Profile
Vertex coloured sprites
« on: September 28, 2014, 10:56:54 AM »
I'm using this for something in a hobby game I'm working on.   This allows you to create colour gradient effects on sprites.  In case this of use to anyone:

Of course this would be better if you could set the colours from within the inspector.  I don't need that myself so I haven't done it.  And it really would be better to ensure the list is filled before trying to render. :D

In UIBasicSprite.cs:
- add a new Type, "VertexColoured".
- in the Fill() method, add the following case to the code:

  1.                         case Type.VertexColoured:
  2.                         VertexColouredFill(verts, uvs, cols);
  3.                         break;
  4.  

And then you need this code somewhere:

  1.  
  2.         BetterList<Color> vertexColours;
  3.                
  4.         public void SetVertexColours(Color c1, Color c2, Color c3, Color c4)
  5.         {
  6.                 BetterList<Color> colourList = new BetterList<Color>();
  7.                 colourList.Add(c1);
  8.                 colourList.Add(c2);
  9.                 colourList.Add(c3);
  10.                 colourList.Add(c4);
  11.                 this.vertexColours = colourList;
  12.         }
  13.  
  14.         protected void VertexColouredFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
  15.         {
  16.                 Vector2 uv0 = new Vector2(mOuterUV.xMin, mOuterUV.yMin);
  17.                 Vector2 uv1 = new Vector2(mOuterUV.xMax, mOuterUV.yMax);
  18.                                
  19.                 Vector4 v = drawingDimensions;
  20.                                
  21.                 verts.Add(new Vector3(v.x, v.y));
  22.                 verts.Add(new Vector3(v.x, v.w));
  23.                 verts.Add(new Vector3(v.z, v.w));
  24.                 verts.Add(new Vector3(v.z, v.y));
  25.                                
  26.                 uvs.Add(uv0);
  27.                 uvs.Add(new Vector2(uv0.x, uv1.y));
  28.                 uvs.Add(uv1);
  29.                 uvs.Add(new Vector2(uv1.x, uv0.y));
  30.                        
  31.                 foreach (Color vc in this.vertexColours)
  32.                 {
  33.                         cols.Add(vc);                          
  34.                 }              
  35.         }
  36.