Advertisement
KillaMaaki

Paletted Image Shader

Aug 8th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. Shader "Hidden/PalettedImageShader"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. // No culling or depth
  10. Cull Off ZWrite Off ZTest Always
  11.  
  12. Pass
  13. {
  14. CGPROGRAM
  15. #pragma vertex vert
  16. #pragma fragment frag
  17. #pragma target 2.0
  18.  
  19. #include "UnityCG.cginc"
  20.  
  21. struct appdata
  22. {
  23. float4 vertex : POSITION;
  24. float2 uv : TEXCOORD0;
  25. };
  26.  
  27. struct v2f
  28. {
  29. float2 uv : TEXCOORD0;
  30. float4 vertex : SV_POSITION;
  31. };
  32.  
  33. v2f vert (appdata v)
  34. {
  35. v2f o;
  36. o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  37. o.uv = v.uv;
  38. return o;
  39. }
  40.  
  41. float4 _ColorPalette[256];
  42.  
  43. sampler2D _MainTex;
  44.  
  45. fixed4 frag (v2f i) : SV_Target
  46. {
  47. fixed4 col = tex2D(_MainTex, i.uv);
  48.  
  49. // the idea here was that _ColorPalette is treated as 16 different palettes, each with 16 colors
  50. // that's more reflective of the SNES than the NES/Famicom but ofc it's not hard to modify for other systems.
  51. // so then col.r stores a value from 0-15 (mapped to the 0-1 range) which is index of color within a palette,
  52. // and col.g stores a value from 0-15 (again, mapped to the 0-1 range) which is index of palette to use.
  53. int entryidx = int(col.r * 15);
  54. int paletteidx = int(col.g * 15);
  55. int idx = entryidx + (paletteidx * 16);
  56.  
  57. // this is turned into an index into the color palette. I'm actually not strictly sure whether this is faster than a texture lookup or not, but it's probably worth a try since there don't seem to be any other options for this sort of thing.
  58. return _ColorPalette[idx];
  59. }
  60. ENDCG
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement