Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Force 16 color effect?

Discussion in 'Shaders' started by Zebbi, Mar 16, 2021.

  1. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    I'm interested in a shader that could force an image to 16 colors for this effect (this being the scene using the effect, and the image being darkened to black underneath the shader):


    This shader was originally for a different tool but looks like this:
    Code (CSharp):
    1. // Created by Coldberg
    2. // Original by Gigatron for C2
    3. // Force 16 Colors by AndreYin .//.
    4. precision highp float;
    5.  
    6. uniform float textureSizeWidth; //width of the texture
    7. uniform float textureSizeHeight; //height of the texture
    8. uniform float texelSizeX; //width of one texel
    9. uniform float texelSizeY; //height of one texel
    10. uniform mediump float seconds;
    11. uniform mediump float pixelWidth;
    12. uniform mediump float pixelHeight;
    13. varying mediump vec2 vTex;
    14. uniform sampler2D samplerFront;
    15. uniform sampler2D samplerBack;
    16. vec2 iResolution = vec2( 1.0/pixelWidth, 1.0/pixelHeight);
    17.  
    18. uniform float BRIGHTNESS;
    19. uniform float COLOR1R;
    20. uniform float COLOR1G;
    21. uniform float COLOR1B;
    22. uniform float COLOR2R;
    23. uniform float COLOR2G;
    24. uniform float COLOR2B;
    25. uniform float COLOR3R;
    26. uniform float COLOR3G;
    27. uniform float COLOR3B;
    28. uniform float COLOR4R;
    29. uniform float COLOR4G;
    30. uniform float COLOR4B;
    31. uniform float COLOR5R;
    32. uniform float COLOR5G;
    33. uniform float COLOR5B;
    34. uniform float COLOR6R;
    35. uniform float COLOR6G;
    36. uniform float COLOR6B;
    37. uniform float COLOR7R;
    38. uniform float COLOR7G;
    39. uniform float COLOR7B;
    40. uniform float COLOR8R;
    41. uniform float COLOR8G;
    42. uniform float COLOR8B;
    43. uniform float COLOR9R;
    44. uniform float COLOR9G;
    45. uniform float COLOR9B;
    46. uniform float COLOR10R;
    47. uniform float COLOR10G;
    48. uniform float COLOR10B;
    49. uniform float COLOR11R;
    50. uniform float COLOR11G;
    51. uniform float COLOR11B;
    52. uniform float COLOR12R;
    53. uniform float COLOR12G;
    54. uniform float COLOR12B;
    55. uniform float COLOR13R;
    56. uniform float COLOR13G;
    57. uniform float COLOR13B;
    58. uniform float COLOR14R;
    59. uniform float COLOR14G;
    60. uniform float COLOR14B;
    61. uniform float COLOR15R;
    62. uniform float COLOR15G;
    63. uniform float COLOR15B;
    64. uniform float COLOR16R;
    65. uniform float COLOR16G;
    66. uniform float COLOR16B;
    67.  
    68. vec3 find_closest (vec3 ref) {  
    69.     vec3 old = vec3 (100.0*255.0);      
    70.     #define TRY_COLOR(new) old = mix (new, old, step (length (old-ref), length (new-ref)));  
    71.     TRY_COLOR (vec3 (COLOR1R, COLOR1G, COLOR1B));
    72.     TRY_COLOR (vec3 (COLOR2R, COLOR2G, COLOR2B));
    73.     TRY_COLOR (vec3 (COLOR3R, COLOR3G, COLOR3B));
    74.     TRY_COLOR (vec3 (COLOR4R, COLOR4G, COLOR4B));
    75.     TRY_COLOR (vec3 (COLOR5R, COLOR5G, COLOR5B));
    76.     TRY_COLOR (vec3 (COLOR6R, COLOR6G, COLOR6B));
    77.     TRY_COLOR (vec3 (COLOR7R, COLOR7G, COLOR7B));
    78.     TRY_COLOR (vec3 (COLOR8R, COLOR8G, COLOR8B));
    79.     TRY_COLOR (vec3 (COLOR9R, COLOR9G, COLOR9B));
    80.     TRY_COLOR (vec3 (COLOR10R, COLOR10G, COLOR10B));
    81.     TRY_COLOR (vec3 (COLOR11R, COLOR11G, COLOR11B));
    82.     TRY_COLOR (vec3 (COLOR12R, COLOR12G, COLOR12B));
    83.     TRY_COLOR (vec3 (COLOR13R, COLOR13G, COLOR13B));
    84.     TRY_COLOR (vec3 (COLOR14R, COLOR14G, COLOR14B));
    85.     TRY_COLOR (vec3 (COLOR15R, COLOR15G, COLOR15B));
    86.     TRY_COLOR (vec3 (COLOR16R, COLOR16G, COLOR16B));
    87.     return old ;
    88. }
    89.  
    90. float dither_matrix (float x, float y) {
    91.     return mix(mix(mix(mix(mix(mix(0.0,32.0,step(1.0,y)),mix(8.0,40.0,step(3.0,y)),step(2.0,y)),mix(mix(2.0,34.0,step(5.0,y)),mix(10.0,42.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),mix(mix(mix(48.0,16.0,step(1.0,y)),mix(56.0,24.0,step(3.0,y)),step(2.0,y)),mix(mix(50.0,18.0,step(5.0,y)),mix(58.0,26.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),step(1.0,x)),mix(mix(mix(mix(12.0,44.0,step(1.0,y)),mix(4.0,36.0,step(3.0,y)),step(2.0,y)),mix(mix(14.0,46.0,step(5.0,y)),mix(6.0,38.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),mix(mix(mix(60.0,28.0,step(1.0,y)),mix(52.0,20.0,step(3.0,y)),step(2.0,y)),mix(mix(62.0,30.0,step(5.0,y)),mix(54.0,22.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),step(3.0,x)),step(2.0,x)),mix(mix(mix(mix(mix(3.0,35.0,step(1.0,y)),mix(11.0,43.0,step(3.0,y)),step(2.0,y)),mix(mix(1.0,33.0,step(5.0,y)),mix(9.0,41.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),mix(mix(mix(51.0,19.0,step(1.0,y)),mix(59.0,27.0,step(3.0,y)),step(2.0,y)),mix(mix(49.0,17.0,step(5.0,y)),mix(57.0,25.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),step(5.0,x)),mix(mix(mix(mix(15.0,47.0,step(1.0,y)),mix(7.0,39.0,step(3.0,y)),step(2.0,y)),mix(mix(13.0,45.0,step(5.0,y)),mix(5.0,37.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),mix(mix(mix(63.0,31.0,step(1.0,y)),mix(55.0,23.0,step(3.0,y)),step(2.0,y)),mix(mix(61.0,29.0,step(5.0,y)),mix(53.0,21.0,step(7.0,y)),step(6.0,y)),step(4.0,y)),step(7.0,x)),step(6.0,x)),step(4.0,x));
    92. }
    93.  
    94. vec3 dither (vec3 color, vec2 uv) {  
    95.     color *= 255.0 * BRIGHTNESS;  
    96.     color = find_closest (clamp (color, 0.0, 255.0));
    97.     return color / 255.0;
    98. }
    99.  
    100. void main()
    101. {
    102.  
    103.     vec2 uv=(1.*vTex);
    104.     vec3 tc = texture2D(samplerFront, uv).xyz;
    105.     gl_FragColor =  vec4 (dither (tc,gl_FragCoord.xy),1.0);      
    106. }
    Code (CSharp):
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <c2effect>
    3.     <!-- About -->
    4.     <id>force16colors</id>            <!-- A unique ID.  Change this!  Never change the ID after release - change the name instead. -->
    5.     <name>Force 16 Color Palette</name>        <!-- The displayed name of the effect -->
    6.     <category>Color</category>    <!-- Any category can be used, but try to use one of: Color, Blend, or Distortion -->
    7.     <description>Force a custom 16 color palette</description>
    8.     <author>AndreYin</author>
    9.  
    10.     <!-- Settings -->
    11.  
    12.     <!-- Extend the bounding box for effect processing by a number of pixels to show the edges
    13.          of effects which go beyond the object edges, e.g. blur and warp.
    14.          Do not set if not strictly necessay!  These options have a performance impact. -->
    15.     <extend-box-horizontal>0</extend-box-horizontal>
    16.     <extend-box-vertical>0</extend-box-vertical>
    17.  
    18.     <!-- Set to true if the background is sampled (see comments on background sampling in the .fx file) -->
    19.     <blends-background>false</blends-background>
    20.  
    21.     <!-- Set to true if the background is not sampled at 1:1 with the foreground (e.g. the
    22.          background texture co-ordinates are modified in some way by the shader, as done
    23.          by Glass and Lens) -->
    24.     <cross-sampling>false</cross-sampling>
    25.  
    26.     <!-- Set to true if the effect changes by itself over time, e.g. Noise or Warp effects. -->
    27.     <animated>false</animated>
    28.  
    29.     <!-- Parameters -->
    30.     <parameters>
    31.         <!-- An example parameter, with the following tags:
    32.             name: the displayed name of the parameter
    33.             description: appears at the bottom of the properties bar when selected
    34.             type: either float or percent.  Percent is just a float but displayed x 100.
    35.             initial: initial value of the parameter
    36.             uniform: name of the corresponding uniform variable in the .fx file.
    37.          
    38.             You can add as many parameters as you like.
    39.         -->
    40.         <param>
    41.             <name>BRIGHTNESS</name>
    42.             <description>BRIGHTNESS Factor</description>
    43.             <type>float</type>
    44.             <initial>1.0</initial>
    45.             <uniform>BRIGHTNESS</uniform>
    46.         </param>
    47.         <param>
    48.             <name>Color 1 Red</name>
    49.             <description>Red color to replace, from 0 to 255.</description>
    50.             <type>float</type>
    51.             <initial>0</initial>
    52.             <uniform>COLOR1R</uniform>
    53.         </param>
    54.         <param>
    55.             <name>Color 1 Green</name>
    56.             <description>Green color to replace, from 0 to 255.</description>
    57.             <type>float</type>
    58.             <initial>0</initial>
    59.             <uniform>COLOR1G</uniform>
    60.         </param>
    61.         <param>
    62.             <name>Color 1 Blue</name>
    63.             <description>Blue color to replace, from 0 to 255.</description>
    64.             <type>float</type>
    65.             <initial>0</initial>
    66.             <uniform>COLOR1B</uniform>
    67.         </param>
    68.         <param>
    69.             <name>Color 2 Red</name>
    70.             <description>Red color to replace, from 0 to 255.</description>
    71.             <type>float</type>
    72.             <initial>126</initial>
    73.             <uniform>COLOR2R</uniform>
    74.         </param>
    75.         <param>
    76.             <name>Color 2 Green</name>
    77.             <description>Green color to replace, from 0 to 255.</description>
    78.             <type>float</type>
    79.             <initial>37</initial>
    80.             <uniform>COLOR2G</uniform>
    81.         </param>
    82.         <param>
    83.             <name>Color 2 Blue</name>
    84.             <description>Blue color to replace, from 0 to 255.</description>
    85.             <type>float</type>
    86.             <initial>83</initial>
    87.             <uniform>COLOR2B</uniform>
    88.         </param>
    89.         <param>
    90.             <name>Color 3 Red</name>
    91.             <description>Red color to replace, from 0 to 255.</description>
    92.             <type>float</type>
    93.             <initial>255</initial>
    94.             <uniform>COLOR3R</uniform>
    95.         </param>
    96.         <param>
    97.             <name>Color 3 Green</name>
    98.             <description>Green color to replace, from 0 to 255.</description>
    99.             <type>float</type>
    100.             <initial>0</initial>
    101.             <uniform>COLOR3G</uniform>
    102.         </param>
    103.         <param>
    104.             <name>Color 3 Blue</name>
    105.             <description>Blue color to replace, from 0 to 255.</description>
    106.             <type>float</type>
    107.             <initial>77</initial>
    108.             <uniform>COLOR3B</uniform>
    109.         </param>
    110.         <param>
    111.             <name>Color 4 Red</name>
    112.             <description>Red color to replace, from 0 to 255.</description>
    113.             <type>float</type>
    114.             <initial>255</initial>
    115.             <uniform>COLOR4R</uniform>
    116.         </param>
    117.         <param>
    118.             <name>Color 4 Green</name>
    119.             <description>Green color to replace, from 0 to 255.</description>
    120.             <type>float</type>
    121.             <initial>119</initial>
    122.             <uniform>COLOR4G</uniform>
    123.         </param>
    124.         <param>
    125.             <name>Color 4 Blue</name>
    126.             <description>Blue color to replace, from 0 to 255.</description>
    127.             <type>float</type>
    128.             <initial>168</initial>
    129.             <uniform>COLOR4B</uniform>
    130.         </param>
    131.         <param>
    132.             <name>Color 5 Red</name>
    133.             <description>Red color to replace, from 0 to 255.</description>
    134.             <type>float</type>
    135.             <initial>255</initial>
    136.             <uniform>COLOR5R</uniform>
    137.         </param>
    138.         <param>
    139.             <name>Color 5 Green</name>
    140.             <description>Green color to replace, from 0 to 255.</description>
    141.             <type>float</type>
    142.             <initial>204</initial>
    143.             <uniform>COLOR5G</uniform>
    144.         </param>
    145.         <param>
    146.             <name>Color 5 Blue</name>
    147.             <description>Blue color to replace, from 0 to 255.</description>
    148.             <type>float</type>
    149.             <initial>170</initial>
    150.             <uniform>COLOR5B</uniform>
    151.         </param>
    152.         <param>
    153.             <name>Color 6 Red</name>
    154.             <description>Red color to replace, from 0 to 255.</description>
    155.             <type>float</type>
    156.             <initial>171</initial>
    157.             <uniform>COLOR6R</uniform>
    158.         </param>
    159.         <param>
    160.             <name>Color 6 Green</name>
    161.             <description>Green color to replace, from 0 to 255.</description>
    162.             <type>float</type>
    163.             <initial>82</initial>
    164.             <uniform>COLOR6G</uniform>
    165.         </param>
    166.         <param>
    167.             <name>Color 6 Blue</name>
    168.             <description>Blue color to replace, from 0 to 255.</description>
    169.             <type>float</type>
    170.             <initial>54</initial>
    171.             <uniform>COLOR6B</uniform>
    172.         </param>
    173.         <param>
    174.             <name>Color 7 Red</name>
    175.             <description>Red color to replace, from 0 to 255.</description>
    176.             <type>float</type>
    177.             <initial>32</initial>
    178.             <uniform>COLOR7R</uniform>
    179.         </param>
    180.         <param>
    181.             <name>Color 7 Green</name>
    182.             <description>Green color to replace, from 0 to 255.</description>
    183.             <type>float</type>
    184.             <initial>51</initial>
    185.             <uniform>COLOR7G</uniform>
    186.         </param>
    187.         <param>
    188.             <name>Color 7 Blue</name>
    189.             <description>Blue color to replace, from 0 to 255.</description>
    190.             <type>float</type>
    191.             <initial>123</initial>
    192.             <uniform>COLOR7B</uniform>
    193.         </param>
    194.         <param>
    195.             <name>Color 8 Red</name>
    196.             <description>Red color to replace, from 0 to 255.</description>
    197.             <type>float</type>
    198.             <initial>255</initial>
    199.             <uniform>COLOR8R</uniform>
    200.         </param>
    201.         <param>
    202.             <name>Color 8 Green</name>
    203.             <description>Green color to replace, from 0 to 255.</description>
    204.             <type>float</type>
    205.             <initial>163</initial>
    206.             <uniform>COLOR8G</uniform>
    207.         </param>
    208.         <param>
    209.             <name>Color 8 Blue</name>
    210.             <description>Blue color to replace, from 0 to 255.</description>
    211.             <type>float</type>
    212.             <initial>0</initial>
    213.             <uniform>COLOR8B</uniform>
    214.         </param>
    215.         <param>
    216.             <name>Color 9 Red</name>
    217.             <description>Red color to replace, from 0 to 255.</description>
    218.             <type>float</type>
    219.             <initial>131</initial>
    220.             <uniform>COLOR9R</uniform>
    221.         </param>
    222.         <param>
    223.             <name>Color 9 Green</name>
    224.             <description>Green color to replace, from 0 to 255.</description>
    225.             <type>float</type>
    226.             <initial>118</initial>
    227.             <uniform>COLOR9G</uniform>
    228.         </param>
    229.         <param>
    230.             <name>Color 9 Blue</name>
    231.             <description>Blue color to replace, from 0 to 255.</description>
    232.             <type>float</type>
    233.             <initial>156</initial>
    234.             <uniform>COLOR9B</uniform>
    235.         </param>
    236.         <param>
    237.             <name>Color 10 Red</name>
    238.             <description>Red color to replace, from 0 to 255.</description>
    239.             <type>float</type>
    240.             <initial>255</initial>
    241.             <uniform>COLOR10R</uniform>
    242.         </param>
    243.         <param>
    244.             <name>Color 10 Green</name>
    245.             <description>Green color to replace, from 0 to 255.</description>
    246.             <type>float</type>
    247.             <initial>231</initial>
    248.             <uniform>COLOR10G</uniform>
    249.         </param>
    250.         <param>
    251.             <name>Color 10 Blue</name>
    252.             <description>Blue color to replace, from 0 to 255.</description>
    253.             <type>float</type>
    254.             <initial>39</initial>
    255.             <uniform>COLOR10B</uniform>
    256.         </param>
    257.         <param>
    258.             <name>Color 11 Red</name>
    259.             <description>Red color to replace, from 0 to 255.</description>
    260.             <type>float</type>
    261.             <initial>41</initial>
    262.             <uniform>COLOR11R</uniform>
    263.         </param>
    264.         <param>
    265.             <name>Color 11 Green</name>
    266.             <description>Green color to replace, from 0 to 255.</description>
    267.             <type>float</type>
    268.             <initial>173</initial>
    269.             <uniform>COLOR11G</uniform>
    270.         </param>
    271.         <param>
    272.             <name>Color 11 Blue</name>
    273.             <description>Blue color to replace, from 0 to 255.</description>
    274.             <type>float</type>
    275.             <initial>255</initial>
    276.             <uniform>COLOR11B</uniform>
    277.         </param>
    278.         <param>
    279.             <name>Color 12 Red</name>
    280.             <description>Red color to replace, from 0 to 255.</description>
    281.             <type>float</type>
    282.             <initial>69</initial>
    283.             <uniform>COLOR12R</uniform>
    284.         </param>
    285.         <param>
    286.             <name>Color 12 Green</name>
    287.             <description>Green color to replace, from 0 to 255.</description>
    288.             <type>float</type>
    289.             <initial>69</initial>
    290.             <uniform>COLOR12G</uniform>
    291.         </param>
    292.         <param>
    293.             <name>Color 12 Blue</name>
    294.             <description>Blue color to replace, from 0 to 255.</description>
    295.             <type>float</type>
    296.             <initial>69</initial>
    297.             <uniform>COLOR12B</uniform>
    298.         </param>
    299.         <param>
    300.             <name>Color 13 Red</name>
    301.             <description>Red color to replace, from 0 to 255.</description>
    302.             <type>float</type>
    303.             <initial>194</initial>
    304.             <uniform>COLOR13R</uniform>
    305.         </param>
    306.         <param>
    307.             <name>Color 13 Green</name>
    308.             <description>Green color to replace, from 0 to 255.</description>
    309.             <type>float</type>
    310.             <initial>195</initial>
    311.             <uniform>COLOR13G</uniform>
    312.         </param>
    313.         <param>
    314.             <name>Color 13 Blue</name>
    315.             <description>Blue color to replace, from 0 to 255.</description>
    316.             <type>float</type>
    317.             <initial>199</initial>
    318.             <uniform>COLOR13B</uniform>
    319.         </param>
    320.         <param>
    321.             <name>Color 14 Red</name>
    322.             <description>Red color to replace, from 0 to 255.</description>
    323.             <type>float</type>
    324.             <initial>0</initial>
    325.             <uniform>COLOR14R</uniform>
    326.         </param>
    327.         <param>
    328.             <name>Color 14 Green</name>
    329.             <description>Green color to replace, from 0 to 255.</description>
    330.             <type>float</type>
    331.             <initial>131</initial>
    332.             <uniform>COLOR14G</uniform>
    333.         </param>
    334.         <param>
    335.             <name>Color 14 Blue</name>
    336.             <description>Blue color to replace, from 0 to 255.</description>
    337.             <type>float</type>
    338.             <initial>49</initial>
    339.             <uniform>COLOR14B</uniform>
    340.         </param>
    341.      
    342.         <param>
    343.             <name>Color 15 Red</name>
    344.             <description>Red color to replace, from 0 to 255.</description>
    345.             <type>float</type>
    346.             <initial>0</initial>
    347.             <uniform>COLOR15R</uniform>
    348.         </param>
    349.         <param>
    350.             <name>Color 15 Green</name>
    351.             <description>Green color to replace, from 0 to 255.</description>
    352.             <type>float</type>
    353.             <initial>226</initial>
    354.             <uniform>COLOR15G</uniform>
    355.         </param>
    356.         <param>
    357.             <name>Color 15 Blue</name>
    358.             <description>Blue color to replace, from 0 to 255.</description>
    359.             <type>float</type>
    360.             <initial>50</initial>
    361.             <uniform>COLOR15B</uniform>
    362.         </param>
    363.         <param>
    364.             <name>Color 16 Red</name>
    365.             <description>Red color to replace, from 0 to 255.</description>
    366.             <type>float</type>
    367.             <initial>255</initial>
    368.             <uniform>COLOR16R</uniform>
    369.         </param>
    370.         <param>
    371.             <name>Color 16 Green</name>
    372.             <description>Green color to replace, from 0 to 255.</description>
    373.             <type>float</type>
    374.             <initial>241</initial>
    375.             <uniform>COLOR16G</uniform>
    376.         </param>
    377.         <param>
    378.             <name>Color 16 Blue</name>
    379.             <description>Blue color to replace, from 0 to 255.</description>
    380.             <type>float</type>
    381.             <initial>232</initial>
    382.             <uniform>COLOR16B</uniform>
    383.         </param>      
    384.     </parameters>
    385.  
    386. </c2effect>
    387.  
    I'm really bad with shaders, what would be required to be able to use this in Unity? A full rewrite? If so, I had no luck finding anything written for Unity already that could do this, unfortunately.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
  3. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
  4. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Thing is, I don't even know if this would act the same if I converted it as I have such limited knowledge of shaders.