Hello Guest

Author Topic: Changing the color of a mesh using tk2d shaders  (Read 4237 times)

Dajuice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Changing the color of a mesh using tk2d shaders
« on: January 29, 2013, 10:39:11 pm »
Hi there... beginning to be a little shy to take up all this time of yours, but I'm completely stuck.

I need to use one of your shaders on something that is not tk2d, then I need to change the color (trying to make some shadows...), I spelunked in your scripts and I think I found how you changed your colors, but then it does not seem to work. (it works with other shaders though)

here is the script (keep in mind it is testing script
Code: [Select]
using UnityEngine;
using System.Collections;

public class ShadowOnVaVoir : MonoBehaviour {
Color[] colors;
Color myColor;
void Awake() {
myColor = new Color(0, 0, 0, 0.5f);
Color[] colorsBegin = GetComponent<MeshFilter>().sharedMesh.colors;
colors = new Color[colorsBegin.Length];
GetComponent<MeshFilter>().sharedMesh.colors = colors;
}

void Update() {
renderer.material.SetColor("Color", myColor);
renderer.material.color = myColor;
int i = colors.Length;
while (i-->0) {
colors[i] = myColor;
}
}
}

As usual, thanks for your time and commitment

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing the color of a mesh using tk2d shaders
« Reply #1 on: January 29, 2013, 10:52:42 pm »
You need to reassign color after changing it in Update.
so after while(...) { }
do
sharedMesh.colors = colors;

That should be it I believe.

Dajuice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Changing the color of a mesh using tk2d shaders
« Reply #2 on: January 29, 2013, 11:09:33 pm »
using UnityEngine;
using System.Collections;

public class ShadowOnVaVoir : MonoBehaviour {
   Color[] colors;
   Color myColor;
   void Awake() {
      myColor = new Color(0, 0, 0, 0.5f);
      Color[] colorsBegin = GetComponent<MeshFilter>().sharedMesh.colors;
      colors = new Color[colorsBegin.Length];
      GetComponent<MeshFilter>().sharedMesh.colors = colors;
      Debug.Log("yo");
      Update();
   }

   void Update() {
      renderer.material.SetColor("Color", myColor);
      renderer.material.color = myColor;
      int i = colors.Length;
      while (i-- > 0) {
         colors = myColor;
      }
      GetComponent<MeshFilter>().sharedMesh.colors = colors;
      Debug.Log("hey");
   }
}
... doesn't work, well I think I'll forget about it, it works on other shaders, it works with non RenderTextures, but there is no way to make it work for what I need ?L?.