Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Custom/renderEvents"
- {
- Properties
- {
- _MainTex ("Main Texture", 2D) = "white" {}
- _Text ("Font", 2D) = "white" {}
- _Icons ("Icons", 2D) = "white" {}
- _EventData ("Event Data", 2D) = "white" {}
- _BG ("Background", 2D) = "white" {}
- _Tiles ("Tile Count", Float) = 0
- _MapWidth ("Map Width", Float) = 0
- _MapHeight ("Map Height", Float) = 0
- _MapWidthPix ("Full Map Width", Float) = 0
- _MapHeightPix ("Full Map Height", Float) = 0
- _FullImageWidth ("Full Image Width", Float) = 0
- _FullImageHeight ("Full Image Height", Float) = 0
- _PerImageWidth ("Per Image Width", Float) = 0
- _PerImageHeight ("Per Image Height", Float) = 0
- _HSplits ("Horizontal splits", Float) = 0
- _VSplits ("Vertical splits", Float) = 0
- _ImageX ("Image X", Float) = 0
- _ImageY ("Image Y", Float) = 0
- _Flags ("Flags", Float) = 0
- }
- SubShader
- {
- // No culling or depth
- Cull Off ZWrite Off ZTest Always
- Pass
- {
- ZTest Always
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- struct appdata
- {
- fixed4 vertex : POSITION;
- fixed2 uv : TEXCOORD0;
- };
- struct v2f
- {
- fixed2 uv : TEXCOORD0;
- fixed4 vertex : SV_POSITION;
- };
- v2f vert (appdata v)
- {
- v2f o;
- o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uv = v.uv;
- return o;
- }
- sampler2D _MainTex;
- sampler2D _Text;
- sampler2D _Icons;
- sampler2D _EventData;
- sampler2D _BG;
- fixed4 _EventData_TexelSize;
- fixed4 _Text_TexelSize;
- fixed4 _Icons_TexelSize;
- fixed _Tiles;
- fixed _MapWidth;
- fixed _MapHeight;
- fixed _MapWidthPix;
- fixed _MapHeightPix;
- fixed _FullImageWidth;
- fixed _FullImageHeight;
- fixed _PerImageWidth;
- fixed _PerImageHeight;
- fixed _HSplits;
- fixed _VSplits;
- fixed _ImageX;
- fixed _ImageY;
- fixed _Flags;
- fixed4 genChar(v2f i, fixed2 offset, fixed id)
- {
- //let's transform screen-space to pixel-space for this one...
- 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);
- fixed2 subPos = fixed2(TextPos.x*_EventData_TexelSize.z, TextPos.y*_EventData_TexelSize.w);
- subPos = fmod(subPos, 1);
- //subPos is now 0-1 range per tile on the map
- //now let's offset by 30% left/right
- subPos = subPos+fixed2(fmod(id,10),2);
- subPos.x = subPos.x/13.0;
- subPos.y = subPos.y/7.0;
- fixed data = tex2D(_EventData, TextPos).a;
- if(data > 0)
- return tex2D(_Text, subPos);
- return fixed4(0,0,0,0);
- // col = lerp(tex2D(_BG, i.uv)
- }
- fixed4 genText(v2f i, fixed2 offset)
- {
- 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);
- 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);
- fixed evtIDA = tex2D(_EventData, TextPosMin).b*255+0.5;
- fixed evtIDB = tex2D(_EventData, TextPosMin).b*255+0.5;
- if(tex2D(_EventData, TextPosMin).a > 0 || tex2D(_EventData, TextPosMax).a > 0)
- {
- fixed4 returnA = fixed4(0,0,0,0);
- fixed4 returnB = fixed4(0,0,0,0);
- //there may be an event
- if(tex2D(_EventData, TextPosMin).a> 0)
- {
- evtIDB = evtIDB+1;
- if(evtIDB < 10)
- {
- //single digit
- return genChar(i, offset, evtIDB);
- }
- else if(evtIDB < 100)
- {
- //double digit
- fixed4 colorA = genChar(i, fixed2(offset.x-0.175, offset.y), floor(evtIDB/10.0));
- fixed4 colorB = genChar(i, fixed2(offset.x+0.175, offset.y), fmod(evtIDB, 10));
- return lerp(colorA, colorB, colorB.a);
- }
- else
- {
- //triple digit
- fixed4 colorA = genChar(i, fixed2(offset.x-0.35, offset.y), floor(evtIDB/100.0));
- fixed4 colorB = genChar(i, fixed2(offset.x+0, offset.y), floor(fmod(evtIDB, 10)/10.0));
- fixed4 colorC = genChar(i, fixed2(offset.x+0.35, offset.y), fmod(evtIDB, 10));
- returnB = lerp(lerp(colorA, colorB, colorB.a), colorC, colorC.a);
- }
- }
- if(tex2D(_EventData, TextPosMax).a> 0)
- {
- evtIDA = evtIDA+1;
- if(evtIDA < 10)
- {
- //single digit
- return genChar(i, offset, evtIDA);
- }
- else if(evtIDA < 100)
- {
- //double digit
- fixed4 colorA = genChar(i, fixed2(offset.x-0.175, offset.y), floor(evtIDA/10.0));
- fixed4 colorB = genChar(i, fixed2(offset.x+0.175, offset.y), fmod(evtIDA, 10));
- return lerp(colorA, colorB, colorB.a);
- }
- else
- {
- //triple digit
- fixed4 colorA = genChar(i, fixed2(offset.x-0.35, offset.y), floor(evtIDA/100.0));
- fixed4 colorB = genChar(i, fixed2(offset.x+0, offset.y), floor(fmod(evtIDA, 10)/10.0));
- fixed4 colorC = genChar(i, fixed2(offset.x+0.35, offset.y), fmod(evtIDA, 10));
- returnA = lerp(lerp(colorA, colorB, colorB.a), colorC, colorC.a);
- }
- }
- return lerp(returnA,returnB, returnB.a);
- }
- return fixed4(0,0,0,0);
- }
- fixed2 getPagePos(fixed2 original)
- {
- //this is device normalized, let's get the pixel coords
- fixed2 toReturn = fixed2((original.x*_FullImageWidth-(_FullImageWidth-_MapWidthPix)/2)/_MapWidthPix, (original.y*_FullImageHeight-(_FullImageHeight-_MapHeightPix)/2)/_MapHeightPix);
- if(toReturn.x >= 1 || toReturn.x < 0 || toReturn.y >= 1 || toReturn.y < 0)
- return fixed2(0,0);
- return toReturn;
- }
- fixed2 getImagePos(fixed2 original)
- {
- fixed offsetX = 1.0/_HSplits;
- fixed px = original.x / _HSplits;
- px = px+_ImageX*offsetX;
- fixed offsetY = 1.0/_VSplits;
- fixed py = original.y / _VSplits;
- py = py+_ImageY*offsetY;
- return fixed2(px,py);
- }
- fixed2 trans(fixed2 original)
- {
- fixed2 imgPos = getImagePos(original);
- return getPagePos(imgPos);
- }
- v2f transform(v2f original)
- {
- v2f returned;
- returned.vertex = original.vertex;
- returned.uv = trans(original.uv);
- return returned;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- fixed4 col = tex2D(_MainTex, i.uv);
- //if((_Flags&2)!=0)
- {
- fixed4 col2 = tex2D(_EventData, transform(i).uv);
- fixed evtType = floor(col2.r*255);
- fixed evtCol = floor(col2.g*255);
- if(col2.a > 0)
- {
- //there is an event here
- if(evtCol <= 0.5)
- {
- col.r = 235.0/255.0;
- col.g = 90.0/255.0;
- col.b = 70.0/255.0;
- }
- else if(evtCol <= 1.5)
- {
- col.r = 0.0/255.0;
- col.g = 121.0/255.0;
- col.b = 191.0/255.0;
- }
- else if(evtCol <= 2.5)
- {
- col.r = 242.0/255.0;
- col.g = 214.0/255.0;
- col.b = 0.0/255.0;
- }
- else if(evtCol <= 3.5)
- {
- col.r = 112.0/255.0;
- col.g = 181.0/255.0;
- col.b = 0.0/255.0;
- }
- else if(evtCol <= 4.5)
- {
- col.r = 195.0/255.0;
- col.g = 119.0/255.0;
- col.b = 224.0/255.0;
- }
- else
- {
- col.r = evtCol;
- col.g = 0;
- col.b = 0;
- }
- //get tile from the event icons image
- //full map coordinates to tile-local coordinates
- fixed2 pos = transform(i).uv*_EventData_TexelSize.zw;
- pos = frac(pos);
- //get tile sub-area
- fixed y = fmod(evtType, _Tiles);
- fixed x = floor(evtType/_Tiles);
- //fixed2 tileOffset = fixed2(1.0/_Tiles*x, 1.0/_Tiles*y);
- fixed size = 1.0/_Tiles;
- pos = pos+fixed2(x,y);
- pos = pos*size;
- col.a = 1;
- col = col*tex2D(_Icons, pos);
- col = lerp(tex2D(_MainTex, i.uv), col, col.a);
- }
- //if((_Flags&1)!=0)
- {
- fixed4 textCol = genText(transform(i), fixed2(0.5, 0.5));
- //textCol.rgb = 1-textCol.rgb;
- //col *=1-textCol;
- if(textCol.a >0.2)
- {
- col = lerp(col,textCol, textCol.a);
- //if(textCol.a > 0.5)
- //{
- // col = lerp(col, fixed4(1,1,1,1), textCol.a*textCol.a);
- //}
- //else
- //{
- // col = lerp(col, fixed4(0,0,0,1), textCol.a/2+0.5f);
- //}
- }
- }
- }
- if(col.a < 1)
- {
- col = lerp(tex2D(_BG, getImagePos(i.uv)), col, col.a);
- }
- return col;
- }
- ENDCG
- }
- }
- FallBack "Diffuse"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement