AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
ColorEncoding.cs
1 using UnityEngine;
2 namespace AirControl
3 {
7  public class ColorEncoding
8  {
9  public static byte ReverseBits(byte value)
10  {
11  return (byte)((value * 0x0202020202 & 0x010884422010) % 1023);
12  }
13 
14  public static int SparsifyBits(byte value, int sparse)
15  {
16  int retVal = 0;
17  for (int bits = 0; bits < 8; bits++, value >>= 1)
18  {
19  retVal |= (value & 1);
20  retVal <<= sparse;
21  }
22  return retVal >> sparse;
23  }
24 
25  public static Color EncodeIDAsColor(int instanceId)
26  {
27  var uid = instanceId * 2;
28  if (uid < 0)
29  uid = -uid + 1;
30 
31  var sid =
32  (SparsifyBits((byte)(uid >> 16), 3) << 2) |
33  (SparsifyBits((byte)(uid >> 8), 3) << 1) |
34  SparsifyBits((byte)(uid), 3);
35  //Debug.Log(uid + " >>> " + System.Convert.ToString(sid, 2).PadLeft(24, '0'));
36 
37  var r = (byte)(sid >> 8);
38  var g = (byte)(sid >> 16);
39  var b = (byte)(sid);
40 
41  //Debug.Log(r + " " + g + " " + b);
42  return new Color32(r, g, b, 255);
43  }
44 
45  public static Color EncodeTagAsColor(string tag)
46  {
47  var hash = tag.GetHashCode();
48  var a = (byte)(hash >> 24);
49  var r = (byte)(hash >> 16);
50  var g = (byte)(hash >> 8);
51  var b = (byte)(hash);
52  return new Color32(r, g, b, a);
53  }
54 
55  public static Color EncodeLayerAsColor(int layer)
56  {
57  // Following value must be in the range (0.5 .. 1.0)
58  // in order to avoid color overlaps when using 'divider' in this func
59  var z = .7f;
60 
61  // First 8 layers are Unity Builtin layers
62  // Unity supports up to 32 layers in total
63 
64  // Lets create palette of unique 16 colors
65  var uniqueColors = new Color[] {
66  new Color(1,1,1,1), new Color(z,z,z,1), // 0
67  new Color(1,1,z,1), new Color(1,z,1,1), new Color(z,1,1,1), //
68  new Color(1,z,0,1), new Color(z,0,1,1), new Color(0,1,z,1), // 7
69 
70  new Color(1,0,0,1), new Color(0,1,0,1), new Color(0,0,1,1), // 8
71  new Color(1,1,0,1), new Color(1,0,1,1), new Color(0,1,1,1), //
72  new Color(1,z,z,1), new Color(z,1,z,1) // 15
73  };
74 
75  // Create as many colors as necessary by using base 16 color palette
76  // To create more than 16 - will simply adjust brightness with 'divider'
77  var color = uniqueColors[layer % uniqueColors.Length];
78  var divider = 1.0f + Mathf.Floor(layer / uniqueColors.Length);
79  color /= divider;
80 
81  return color;
82  }
83  }
84 }
AirControl.ColorEncoding
Capture camera settings
Definition: ColorEncoding.cs:7
AirControl
Definition: AirplaneSelector.cs:8