• NGUI实现Sprite裁切成圆形或者椭圆形(不完美)


    先上效果

    有个问题就是,UISprie用的Atlas的公用的材质,无法从当前要绘制的片段shader上获得uv百分比,所以当有其他的Sprite使用相同的Atlas时显示就有问题

    其实Mesh是可以接受另外一套uv的,不过目前还没有找到给NGUI造的mesh传另外一套uv的方法。

    //代码

    using System;
    using UnityEngine;
    using System.Collections;
    
    public class UIRoundSprite : UISprite 
    {
        public float RoundRate = 0.8f;
        protected override void OnUpdate ()
        {
            if(mChanged)
            {
                var atlas = GetAtlasSprite();
                if (null != atlas && null != material)
                {
                     var w = material.mainTexture.width;
                     var h = material.mainTexture.height;
                     var sx = atlas.x*1.0f/w;
                     var sy = atlas.y * 1.0f / h;
                     var wr = atlas.width * 1.0f / w;
                     var hr = atlas.height * 1.0f / h;
                     var wr_2 = wr/2;
                     var hr_2 = hr/2;
                     material.SetFloat("_CenterX", sx + wr_2);
                     material.SetFloat("_CenterY", sy + hr_2);
                     material.SetFloat("_Width", wr_2);
                     material.SetFloat("_Height", hr_2);
                     material.SetFloat("_LengthRate",  RoundRate);
                    
                }
            }
            base.OnUpdate();
        }
    
    }

    //Shader

    Shader "Unlit/Transparent Colored Round"
    {
        Properties
        {
            _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
            _CenterX("CenterX", Float) = 0.0
            _CenterY("CenterY", Float) = 0.0
            _Width("Width", Float) = 0.0
            _Height("Height", Float) = 0.0
            _LengthRate("LengthRate", Float) = 0.0
        }
        
        SubShader
        {
            LOD 200
    
            Tags
            {
                "Queue" = "Transparent"
                "IgnoreProjector" = "True"
                "RenderType" = "Transparent"
            }
            
            Pass
            {
                Cull Off
                Lighting Off
                ZWrite Off
                Fog { Mode Off }
                Offset -1, -1
                Blend SrcAlpha OneMinusSrcAlpha
    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag            
                #include "UnityCG.cginc"
    
                sampler2D _MainTex;
                float4 _MainTex_ST;
                float _CenterX;
                float _CenterY;
                float _Width;
                float _Height;
                float _LengthRate;
    
                struct appdata_t
                {
                    float4 vertex : POSITION;
                    float2 texcoord : TEXCOORD0;
                    fixed4 color : COLOR;
                };
        
                struct v2f
                {
                    float4 vertex : SV_POSITION;
                    half2 texcoord : TEXCOORD0;
                    fixed4 color : COLOR;
                };
        
                v2f o;
    
                v2f vert (appdata_t v)
                {
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.texcoord = v.texcoord;
                    o.color = v.color;
                    return o;
                }
                    
                fixed4 frag (v2f IN) : COLOR
                {
                    float2 pt = IN.texcoord-float2(_CenterX,(1-_CenterY));
                    if((pt.x*pt.x)/(_Width*_Width)+(pt.y*pt.y)/(_Height*_Height)>_LengthRate)
                    {
                        discard;
                    }
                    
                    return tex2D(_MainTex, IN.texcoord) * IN.color;
                }
                ENDCG
            }
        }
    
        SubShader
        {
            LOD 100
    
            Tags
            {
                "Queue" = "Transparent"
                "IgnoreProjector" = "True"
                "RenderType" = "Transparent"
            }
            
            Pass
            {
                Cull Off
                Lighting Off
                ZWrite Off
                Fog { Mode Off }
                Offset -1, -1
                ColorMask RGB
                Blend SrcAlpha OneMinusSrcAlpha
                ColorMaterial AmbientAndDiffuse
                
                SetTexture [_MainTex]
                {
                    Combine Texture * Primary
                }
            }
        }
    }
  • 相关阅读:
    2016.7.26
    2016.7.25
    2016.7.24
    C/C++基本数据类型所占字节数
    几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation
    h5 如何打包apk
    Ajax XMLHttpRequest对象的三个属性以及open和send方法
    twisted 使用
    python 的内建函数
    python中的 json 模块使用
  • 原文地址:https://www.cnblogs.com/mrblue/p/5319976.html
Copyright © 2020-2023  润新知