Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Shader that swaps a texture's pixels colors (Color Palette swapper)

Discussion in 'Shaders' started by yukamui, Nov 21, 2013.

  1. yukamui

    yukamui

    Joined:
    Nov 19, 2013
    Posts:
    12
    I am new to shaders and would like to know if it is possible to create a shader that when appied to a spriteRenderer it would swap an array of pixel's colors for colors in other array. I managed to create the desired effect without a shader, using getPixel32 and setPixel32 (I am attaching the example for anyone that would like to check it).

    But since I change the actual pixels of the texture its no good for what I want. (That is having 1 texture but use it with different color-palettes for different instances that use the same texture).

    Please look at the example I provide to see what I mean, I don't want to tint it, or even vertex color it, I want the old-school color palette swapping thing.

    Please tell me if this is possible with a shader and if you can direct me in the right direction to achieve this. Thanks a lot!

    Picture Related:
     

    Attached Files:

  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Store into main texture not color but coordinates (is required tex2Dlod, #pragma glsl, #pragma target 3.0).
     

    Attached Files:

    esco1979 likes this.
  3. yukamui

    yukamui

    Joined:
    Nov 19, 2013
    Posts:
    12
    Wooow this is impressive Mouurusai!! I will study the project and try to understand how you did it! Thanks for sharing you rock!
     
  4. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    138
    Awesome shader mouurusai! But I noticed just now that if put on bilinear/trilinear filtering, that there seems to be some color bleeding issues! Since my gfx are very small I need to have this setting on so that they look much nicer in game. Is there a way to fix this?

    NOTE: top image is with bilinear on, bottom is off; you can clarly see the color issues in the top!

    http://i143.photobucket.com/albums/r160/Esco1979/Image15_zps1f28cc95.png
     
    Last edited: Jun 27, 2014
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Sorry, but it seems there is no way to use this method in conjunction with some filtration. Perhaps it makes sense to upscale textures, into level start for example, with xBRZ like algorithm.
    P.S. In another topic about palettes suggested to me that we can get some result with sm 2.0(without tex2Dlod, #pragma glsl, #pragma target 3.0), by turning off the generation of mip levels in the texture settings.
     
    esco1979 likes this.
  6. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    138
    About upgrading textures I assume you mean scaling them up? That wouldn't work in my game; I am remaking castlevania: symphony of the night in Unity. There are literally THOUSANDS of sprites in the game.

    No idea, what you mean sadly with the rest, lol. I know nothing about shaders! Although I intend to learn about them eventually, right now I still have about 50 other things to learn first in Unity. I've only been using it for 3 months. I was using game maker before, and I have a shader from there that let me actually use a script to input the colors on a sprite, the input the colors to replace them with! The problem was with bilinear filtering we had the same issue; how we got around it was drawing the sprite with the shader applied to what we call a surface, THEN drawing that surface to the screen. So that this bleeding didn't occur.

    Sadly I have NO CLUE how to do that here! But any help would be appreciated and I would gladly credit you for it! :)
     
    Last edited: Jun 27, 2014
  7. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26