Hello Guest

Author Topic: Detect the saturation of pixel and change it during runtime  (Read 7138 times)

unity_stark

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
Hello,
In my project, I need to change the colour of items such as hair or clothes based on user's selection.
but some area can't change and need to keep their origin colour.

So I need to get sprite's pixel information such as saturation property.
if this property match some conditions, 90% or 75%, then I will change the colour of this pixel at runtime.

How should I do or any suggestions?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Detect the saturation of pixel and change it during runtime
« Reply #1 on: June 13, 2013, 12:07:15 pm »
Changing pixels at runtime is expensive in Unity - it will have to create a duplicate in memory to read write pixels into a texture.
Its still possible though, but it feels like there is a better way to do this using shaders.

One example - Make it a paletted texture, with a 2D texture to choose the selection. This way, all you need to do is change the vertex color on a sprite to get it to switch palette. Nice, simple and quite efficient. Downside - it wont work with filtered textures, unless you code the filtering yourself, which makes it more expensive.

unity_stark

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Detect the saturation of pixel and change it during runtime
« Reply #2 on: June 13, 2013, 07:30:35 pm »
Thanks a lot for your answer.
I think that the shader is the best way to solve this problem.
I'm studying shader and try to figure out how to filter unchanged pixel based on saturation value.

But how the shader can detect or calculate the value of saturation of pixel?



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile


PointNineGames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Detect the saturation of pixel and change it during runtime
« Reply #5 on: October 19, 2013, 03:30:39 am »
Sorry to revive an old thread, but I am looking for clarification on this index/paletted texture solution for changing the colors of a sprite per instance.  Does this add a draw call per instance?  My limited knowledge has me thinking that I would have to call renderer.material to change that index, and that would add a draw call per instance.  Any help is appreciated.  Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Detect the saturation of pixel and change it during runtime
« Reply #6 on: October 19, 2013, 12:56:13 pm »
If you were to use a paletted solution, you 'd probably have the palette laid out on the x axis, and variants on the y. A vertex color channel (maybe alpha?) could be used to index into the variants. You can easily work this out in such a way that doesn't require messing about with multiple materials.