title: Remapping colours on textures dynamically in unity
body: <p>I'm making a RTS game in Unity. In such games, players usually can determine the unit allegiance using the unit color markings. And I'm now trying to implement system, that will remap purple color on the unit owner's color.</p>
  <p>One idea was to determine color that will be used as mask and then recoloured to any color. It could be done using some hue distribution function:</p>
  <h2>Solution 1</h2>
  <p><img src="https://i.imgur.com/DPTPFPf.png?2" alt="enter image description here" /></p>
  <p>I used funtion based on <code>max()</code>. You can <a href="http://fooplot.com/plot/kaenoo2cbi" rel="nofollow noreferrer">see the plot there</a>.</p>
  <pre><code>hue = min(hue, pow(Math.abs(hue-MASK_HUE),8)*5000000+RESULT_HUE)
  </code></pre>
  <p>This solution has two big flaws:</p>
  <ol>
  <li>Purple can't be used (I don't like it anyway)</li>
  <li>Only full colours are applicable (no brown, black, white...)</li>
  </ol>
  <p><sup><em>What you see above is just my fiddling. The actual project would run on Unity engine in C#.</em></sup></p>
  <h2>Solution 2</h2>
  <p>My friend proposed diferent approach: every image should use a map - either faded or just <code>true</code>/<code>false</code> array - to map where should the colours be applied. I didn't try this yet, as it's more complicated to test ad-hoc.</p>
  <h2>Altering textures in unity</h2>
  <p>It seems that <a href="http://docs.unity3d.com/ScriptReference/Material.SetTexture.html" rel="nofollow noreferrer">texture for material can be easily altered in Unity</a>, so the question is:</p>
  <p><strong>Q: How should I implement the dynamic texture coloring (generating) in Unity?</strong> Is any of my methods the good way? How should I produce the new textures, using what functions?</p>
  <p>Rather than full code, I need information about what functions should I use. I hope other users will also profit from the general info.</p>
  <p>To help you answering, I can guess the process will have 3 important parts:</p>
  <ol>
  <li>Somehow get the texture of a model material (we know just the <a href="http://docs.unity3d.com/ScriptReference/GameObject.html" rel="nofollow noreferrer">GameObject</a> here)</li>
  <li>If it hasn't been recolored already, use some algorithm to change the image properly. We'll need some pixel manipulation here</li>
  <li>Apply the texture back to the model material</li>
  </ol>