Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/renderEvents"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Main Texture", 2D) = "white" {}
  6.         _Text ("Font", 2D) = "white" {}
  7.         _Icons ("Icons", 2D) = "white" {}
  8.         _EventData ("Event Data", 2D) = "white" {}
  9.         _BG ("Background", 2D) = "white" {}
  10.         _Tiles ("Tile Count", Float) = 0
  11.         _MapWidth ("Map Width", Float) = 0
  12.         _MapHeight ("Map Height", Float) = 0
  13.         _MapWidthPix ("Full Map Width", Float) = 0
  14.         _MapHeightPix ("Full Map Height", Float) = 0
  15.         _FullImageWidth ("Full Image Width", Float) = 0
  16.         _FullImageHeight ("Full Image Height", Float) = 0
  17.         _PerImageWidth ("Per Image Width", Float) = 0
  18.         _PerImageHeight ("Per Image Height", Float) = 0
  19.         _HSplits ("Horizontal splits", Float) = 0
  20.         _VSplits ("Vertical splits", Float) = 0
  21.         _ImageX ("Image X", Float) = 0
  22.         _ImageY ("Image Y", Float) = 0
  23.         _Flags ("Flags", Float) = 0
  24.     }
  25.     SubShader
  26.     {
  27.         // No culling or depth
  28.         Cull Off ZWrite Off ZTest Always
  29.  
  30.         Pass
  31.         {
  32.             ZTest Always
  33.             CGPROGRAM
  34.             #pragma vertex vert
  35.             #pragma fragment frag
  36.            
  37.             #include "UnityCG.cginc"
  38.  
  39.             struct appdata
  40.             {
  41.                 fixed4 vertex : POSITION;
  42.                 fixed2 uv : TEXCOORD0;
  43.             };
  44.  
  45.             struct v2f
  46.             {
  47.                 fixed2 uv : TEXCOORD0;
  48.                 fixed4 vertex : SV_POSITION;
  49.             };
  50.  
  51.             v2f vert (appdata v)
  52.             {
  53.                 v2f o;
  54.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  55.                 o.uv = v.uv;
  56.                 return o;
  57.             }
  58.            
  59.             sampler2D _MainTex;
  60.             sampler2D _Text;
  61.             sampler2D _Icons;
  62.             sampler2D _EventData;
  63.             sampler2D _BG;
  64.             fixed4 _EventData_TexelSize;
  65.             fixed4 _Text_TexelSize;
  66.             fixed4 _Icons_TexelSize;
  67.             fixed _Tiles;
  68.             fixed _MapWidth;
  69.             fixed _MapHeight;
  70.             fixed _MapWidthPix;
  71.             fixed _MapHeightPix;
  72.             fixed _FullImageWidth;
  73.             fixed _FullImageHeight;
  74.             fixed _PerImageWidth;
  75.             fixed _PerImageHeight;
  76.             fixed _HSplits;
  77.             fixed _VSplits;
  78.             fixed _ImageX;
  79.             fixed _ImageY;
  80.             fixed _Flags;
  81.            
  82.             fixed4 genChar(v2f i, fixed2 offset, fixed id)
  83.             {
  84.             //let's transform screen-space to pixel-space for this one...
  85.                 fixed2 TextPos = fixed2(((i.uv.x*_EventData_TexelSize.z)-offset.x)/_EventData_TexelSize.z, ((i.uv.y*_EventData_TexelSize.w)-offset.y)/_EventData_TexelSize.w);
  86.                 fixed2 subPos = fixed2(TextPos.x*_EventData_TexelSize.z, TextPos.y*_EventData_TexelSize.w);
  87.                 subPos = fmod(subPos, 1);
  88.                 //subPos is now 0-1 range per tile on the map
  89.                 //now let's offset by 30% left/right
  90.                 subPos = subPos+fixed2(fmod(id,10),2);
  91.                 subPos.x = subPos.x/13.0;
  92.                 subPos.y = subPos.y/7.0;
  93.                 fixed data = tex2D(_EventData, TextPos).a;
  94.                 if(data > 0)
  95.                 return tex2D(_Text, subPos);
  96.                 return fixed4(0,0,0,0);
  97.  
  98.                 //  col = lerp(tex2D(_BG, i.uv)
  99.             }
  100.             fixed4 genText(v2f i, fixed2 offset)
  101.             {
  102.                
  103.                 fixed2 TextPosMin = fixed2(((i.uv.x*_EventData_TexelSize.z)-(offset.x+0.15))/_EventData_TexelSize.z, ((i.uv.y*_EventData_TexelSize.w)-offset.y)/_EventData_TexelSize.w);
  104.                 fixed2 TextPosMax = fixed2(((i.uv.x*_EventData_TexelSize.z)-(offset.x-0.35))/_EventData_TexelSize.z, ((i.uv.y*_EventData_TexelSize.w)-offset.y)/_EventData_TexelSize.w);
  105.                 fixed evtIDA = tex2D(_EventData, TextPosMin).b*255+0.5;
  106.                 fixed evtIDB = tex2D(_EventData, TextPosMin).b*255+0.5;
  107.  
  108.  
  109.                 if(tex2D(_EventData, TextPosMin).a > 0 || tex2D(_EventData, TextPosMax).a > 0)
  110.                 {
  111.                     fixed4 returnA = fixed4(0,0,0,0);
  112.                     fixed4 returnB = fixed4(0,0,0,0);
  113.                     //there may be an event
  114.                     if(tex2D(_EventData, TextPosMin).a> 0)
  115.                     {
  116.                        
  117.                         evtIDB = evtIDB+1;
  118.                         if(evtIDB < 10)
  119.                         {
  120.                             //single digit
  121.                             return genChar(i, offset, evtIDB);
  122.                         }
  123.                         else if(evtIDB < 100)
  124.                         {
  125.                             //double digit
  126.                             fixed4 colorA = genChar(i, fixed2(offset.x-0.175, offset.y), floor(evtIDB/10.0));
  127.                        
  128.                             fixed4 colorB = genChar(i, fixed2(offset.x+0.175, offset.y), fmod(evtIDB, 10));
  129.                             return lerp(colorA, colorB, colorB.a);
  130.                         }
  131.                         else
  132.                         {
  133.                             //triple digit
  134.                             fixed4 colorA = genChar(i, fixed2(offset.x-0.35, offset.y), floor(evtIDB/100.0));
  135.                        
  136.                             fixed4 colorB = genChar(i, fixed2(offset.x+0, offset.y), floor(fmod(evtIDB, 10)/10.0));
  137.                             fixed4 colorC = genChar(i, fixed2(offset.x+0.35, offset.y), fmod(evtIDB, 10));
  138.                             returnB = lerp(lerp(colorA, colorB, colorB.a), colorC, colorC.a);
  139.                         }
  140.                     }
  141.                     if(tex2D(_EventData, TextPosMax).a> 0)
  142.                     {
  143.                        
  144.                         evtIDA = evtIDA+1;
  145.                         if(evtIDA < 10)
  146.                         {
  147.                             //single digit
  148.                             return genChar(i, offset, evtIDA);
  149.                         }
  150.                         else if(evtIDA < 100)
  151.                         {
  152.                             //double digit
  153.                             fixed4 colorA = genChar(i, fixed2(offset.x-0.175, offset.y), floor(evtIDA/10.0));
  154.                        
  155.                             fixed4 colorB = genChar(i, fixed2(offset.x+0.175, offset.y), fmod(evtIDA, 10));
  156.                             return lerp(colorA, colorB, colorB.a);
  157.                         }
  158.                         else
  159.                         {
  160.                             //triple digit
  161.                             fixed4 colorA = genChar(i, fixed2(offset.x-0.35, offset.y), floor(evtIDA/100.0));
  162.                        
  163.                             fixed4 colorB = genChar(i, fixed2(offset.x+0, offset.y), floor(fmod(evtIDA, 10)/10.0));
  164.                             fixed4 colorC = genChar(i, fixed2(offset.x+0.35, offset.y), fmod(evtIDA, 10));
  165.                             returnA = lerp(lerp(colorA, colorB, colorB.a), colorC, colorC.a);
  166.                         }
  167.                     }
  168.                     return lerp(returnA,returnB, returnB.a);
  169.                 }
  170.                 return fixed4(0,0,0,0);
  171.                
  172.             }
  173.  
  174.             fixed2 getPagePos(fixed2 original)
  175.             {
  176.                 //this is device normalized, let's get the pixel coords
  177.                 fixed2 toReturn = fixed2((original.x*_FullImageWidth-(_FullImageWidth-_MapWidthPix)/2)/_MapWidthPix, (original.y*_FullImageHeight-(_FullImageHeight-_MapHeightPix)/2)/_MapHeightPix);
  178.                 if(toReturn.x >= 1 || toReturn.x < 0 || toReturn.y >= 1 || toReturn.y < 0)
  179.                 return fixed2(0,0);
  180.                 return toReturn;
  181.             }
  182.             fixed2 getImagePos(fixed2 original)
  183.             {
  184.                 fixed offsetX = 1.0/_HSplits;
  185.                 fixed px = original.x / _HSplits;
  186.                 px = px+_ImageX*offsetX;
  187.  
  188.                 fixed offsetY = 1.0/_VSplits;
  189.                 fixed py = original.y / _VSplits;
  190.                 py = py+_ImageY*offsetY;
  191.  
  192.                 return fixed2(px,py);
  193.             }
  194.             fixed2 trans(fixed2 original)
  195.             {
  196.                 fixed2 imgPos = getImagePos(original);
  197.                 return getPagePos(imgPos);
  198.             }
  199.  
  200.             v2f transform(v2f original)
  201.             {
  202.                 v2f returned;
  203.                 returned.vertex = original.vertex;
  204.                 returned.uv = trans(original.uv);
  205.                 return returned;
  206.  
  207.             }
  208.             fixed4 frag (v2f i) : SV_Target
  209.             {
  210.                 fixed4 col = tex2D(_MainTex, i.uv);
  211.  
  212.                 //if((_Flags&2)!=0)
  213.                 {
  214.                     fixed4 col2 = tex2D(_EventData, transform(i).uv);
  215.                
  216.                     fixed evtType = floor(col2.r*255);
  217.                     fixed evtCol = floor(col2.g*255);
  218.                
  219.                     if(col2.a > 0)
  220.                     {
  221.                         //there is an event here
  222.                         if(evtCol <= 0.5)
  223.                         {
  224.                             col.r = 235.0/255.0;
  225.                             col.g = 90.0/255.0;
  226.                             col.b = 70.0/255.0;
  227.                         }
  228.                         else if(evtCol <= 1.5)
  229.                         {
  230.                             col.r = 0.0/255.0;
  231.                             col.g = 121.0/255.0;
  232.                             col.b = 191.0/255.0;
  233.                         }
  234.                         else if(evtCol <= 2.5)
  235.                         {
  236.                             col.r = 242.0/255.0;
  237.                             col.g = 214.0/255.0;
  238.                             col.b = 0.0/255.0;
  239.                         }
  240.                         else if(evtCol <= 3.5)
  241.                         {
  242.                             col.r = 112.0/255.0;
  243.                             col.g = 181.0/255.0;
  244.                             col.b = 0.0/255.0;
  245.                         }
  246.                         else if(evtCol <= 4.5)
  247.                         {
  248.                             col.r = 195.0/255.0;
  249.                             col.g = 119.0/255.0;
  250.                             col.b = 224.0/255.0;
  251.                         }
  252.                         else
  253.                         {
  254.                             col.r = evtCol;
  255.                             col.g = 0;
  256.                             col.b = 0;
  257.                         }
  258.                    
  259.                         //get tile from the event icons image
  260.                    
  261.                         //full map coordinates to tile-local coordinates
  262.                         fixed2 pos = transform(i).uv*_EventData_TexelSize.zw;
  263.                         pos = frac(pos);
  264.  
  265.                         //get tile sub-area
  266.                         fixed y = fmod(evtType, _Tiles);
  267.                         fixed x = floor(evtType/_Tiles);
  268.  
  269.                         //fixed2 tileOffset = fixed2(1.0/_Tiles*x, 1.0/_Tiles*y);
  270.                         fixed size = 1.0/_Tiles;
  271.                    
  272.                         pos = pos+fixed2(x,y);
  273.                         pos = pos*size;
  274.                         col.a = 1;
  275.                         col = col*tex2D(_Icons, pos);
  276.  
  277.                         col = lerp(tex2D(_MainTex, i.uv), col, col.a);
  278.  
  279.  
  280.                    
  281.                     }
  282.                     //if((_Flags&1)!=0)
  283.                     {
  284.                         fixed4 textCol = genText(transform(i), fixed2(0.5, 0.5));
  285.                         //textCol.rgb = 1-textCol.rgb;
  286.                         //col *=1-textCol;
  287.                         if(textCol.a >0.2)
  288.                         {
  289.                             col = lerp(col,textCol, textCol.a);
  290.                             //if(textCol.a > 0.5)
  291.                             //{
  292.                             //  col = lerp(col, fixed4(1,1,1,1), textCol.a*textCol.a);
  293.                             //}
  294.                             //else
  295.                             //{
  296.                             //  col = lerp(col, fixed4(0,0,0,1), textCol.a/2+0.5f);
  297.                             //}
  298.                         }
  299.                     }
  300.                 }
  301.                 if(col.a < 1)
  302.                 {
  303.                     col = lerp(tex2D(_BG, getImagePos(i.uv)), col, col.a);
  304.                 }
  305.                 return col;
  306.             }
  307.             ENDCG
  308.         }
  309.     }
  310.    
  311. FallBack "Diffuse"
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement