Shader instance: The method of using shader correctly in the Uisprite Ngui the map set

Source: Internet
Author: User

Effect:

Gray, filter, streamer are common effects on the UI.
Like what:
1. When the button is disabled, it becomes gray.
2. An icon to handle the round background frame, but also to deal with the rectangle background frame. It is necessary to use a filter to cut.
Avoid the art of providing two icon of trouble, but also save memory.
3. Streamer, uh ..., planning is what you can do.

Practice:
Ngui the image to be used, it will record the information of each small picture.
Includes: The position of each small figure in this atlas, long, wide, padding,border. Wait a minute.
When used, only the area of the small map is sampled and then displayed on the mesh of the UI.
If we use the texcoord of this small image to sample another picture, the sample is just part, not what we want (sampling the complete picture).
So, as long as the small figure texcoord in accordance with the corresponding proportions, get the right texcoord can.

Take a look at the location of T_sheyaonan in the chart set, Position (0,26), width:102,height:111

0,26

Look at the set again, oh, the upper left corner of the original atlas is 0, 0 points.

Analysis:

To get the correct texcoord coordinates?
Just texcoord the coordinates of the small figure A, minus the offset, and then expand as the specified proportions.

So
final_uv.x = (texcoord.x–x/w of small Figure A) * (W/W)
Final_uv.y = (texcoord.y– of small Figure A (h-y-h)/h) * (h/h)

It's OK to sample with FINAL_UV.

Shader code: Overwrite from unlit–transparent colored
The –add– part is what I added.

Shader"custom/unlit/transparent colored Grey Mask Flow"{Properties {_maintex ("Base (RGB), Alpha (A)", 2D) ="Black" {}   //---add---------------------------------_masktex ("Mask Alpha (A)", 2D) =" White"{} _ifmask ("Open Mask if larger than 0.5", Range (0,1)) =0_widthrate ("Sprite.width/atlas.width",float) =1_heightrate ("Sprite.height/atlas.height",float) =1_xoffset ("Offsetx/atlas.width",float) =0_yoffset ("Offsety/atlas.height",float) =0_flowtex ("Flow Tex", 2D) =""{}  //--------------------------------------} subshader {LOD -Tags {"Queue"="Transparent"   "Ignoreprojector"="True"   "Rendertype"="Transparent"} cull off Lighting off Zwrite off Fog {Mode off} Offset-1, -1Blend srcalpha oneminussrcalpha Pass {cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc"   structappdata_t {float4 vertex:position;    FLOAT2 texcoord:texcoord0;   Fixed4 Color:color;   }; structv2f {float4 vertex:sv_position;    Half2 texcoord:texcoord0;   Fixed4 Color:color;   };   Sampler2d _maintex;    FLOAT4 _maintex_st; //---add-------sampler2d _masktex; float_ifmask; float_widthrate; float_heightrate; float_xoffset; float_yoffset;   Sampler2d _flowtex; //--------------v2f Vert (appdata_t v) {v2f o; O.vertex=Mul (UNITY_MATRIX_MVP, V.vertex); O.texcoord=V.texcoord; O.color=V.color; returno;       } fixed4 Frag (v2f i): COLOR {fixed4 col; Col=tex2d (_maintex, I.texcoord); //---------Add---------------------------------//Filter    if(_ifmask>0.5) {COL.A= COL.A * tex2d (_masktex, Float2 (I.texcoord.x-_xoffset)/_widthrate, (I.texcoord.y-_yoffset)/_heightrate)).     A }      //Turn Grey    if(i.color.r<=0.1)    {     floatGrey = Dot (Col.rgb, FLOAT3 (0.299,0.587,0.114)); Col.rgb=FLOAT3 (grey, grey, grey); }    //Streamer    if(i.color.g<=0.1) {float2 Flow_uv= Float2 ((i.texcoord.x-_xoffset)/_widthrate, (I.texcoord.y-_yoffset)/_heightrate); flow_uv.x/=2; flow_uv.x-= _time.y *2; Half Flow=tex2d (_FLOWTEX,FLOW_UV). A; Col.rgb+=half3 (Flow,flow,flow); }    //-----------------------------------------------    returnCol; } ENDCG}} subshader {LOD -Tags {"Queue"="Transparent"   "Ignoreprojector"="True"   "Rendertype"="Transparent"} Pass {cull off Lighting off Zwrite off Fog {Mode Off} Offset-1, -1colormask RGB alphatest Greater. onBlend srcalpha oneminussrcalpha colormaterial ambientanddiffuse settexture [_maintex] {Combine Texture /c5>*Primary}} }}

C # script: Hanging on the Uisprite

usingUnityengine;usingSystem.Collections; Public classscaletexcoord:monobehaviour{Private floatwidthrate; Private floatheightrate; Private floatxoffsetrate; Private floatyoffsetrate; PrivateUisprite Sprite; voidAwake () {Sprite= getcomponent<uisprite>(); Widthrate= Sprite. Getatlassprite (). Width *1.0f/Sprite.atlas.spriteMaterial.mainTexture.width; Heightrate= Sprite. Getatlassprite (). Height *1.0f/Sprite.atlas.spriteMaterial.mainTexture.height; Xoffsetrate= Sprite. Getatlassprite (). x *1.0f/Sprite.atlas.spriteMaterial.mainTexture.width; Yoffsetrate= (sprite.atlas.spritematerial.maintexture.height-(sprite). Getatlassprite (). Y + Sprite. Getatlassprite (). height)) *1.0f/Sprite.atlas.spriteMaterial.mainTexture.height; }     Private voidStart () {sprite.atlas.spriteMaterial.SetFloat ("_widthrate", widthrate); Sprite.atlas.spriteMaterial.SetFloat ("_heightrate", heightrate); Sprite.atlas.spriteMaterial.SetFloat ("_xoffset", xoffsetrate); Sprite.atlas.spriteMaterial.SetFloat ("_yoffset", yoffsetrate); }}

Test it: Hang it on the main camera

usingUnityengine;usingSystem.Collections; Public classtest:monobehaviour{ Publicuisprite sprite1; Publicuisprite Sprite2; PublicUisprite Sprite3;  PublicMaterial Default_mat; PublicMaterial Mask1_mat; PublicMaterial Mask2_mat; voidOngui () {if(GUI. Button (NewRect (0, -, -, -),"Filter Diagram 1") ) {sprite1.atlas.spriteMaterial=Mask1_mat; }   if(GUI. Button (NewRect ( the, -, -, -),"Filter Diagram 2") ) {sprite1.atlas.spriteMaterial=Mask2_mat; }   if(GUI. Button (NewRect (0, the, -, -),"Turn Grey") ) {Sprite2.color=NewColor (0,1,1); }   if(GUI. Button (NewRect ( the, the, -, -),"Streamer") ) {Sprite3.color=NewColor (1,0,1); } }     voidOnDestroy () {sprite1.atlas.spriteMaterial=Default_mat; }}

The pace of learning cannot stop ~~~~~~~~~~~~~~~~~

Need my test project, please leave a message. Hee Hee

    • This article fixed link: http://www.shihuanjue.com/?p=305
    • Reprint Please specify: Joe January 19, 2016 so Illusion published

Shader instance: The method of using shader correctly in the Uisprite Ngui the map set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.