Unity-使用Shader裁剪图片

原文:http://www.tuicool.com/articles/FziyEv


在unity中,使用一张正方形的图片,但是要显示成圆形。这样怎么做呢?

1、首先,新建一个shader:Circular

2、再新建一个material,选择上面新建的shader。

3、最后将上面的material赋值给unity中要显示为圆形的图片


下面是shader代码

Shader "Self/Circular"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}

        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255

        _ColorMask ("Color Mask", Float) = 15
        // 以 1 - _Radius 长度为半径的圆形
        _Radius ("Radius", Range(0,0.5)) = 0.5
    }
    
    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "PreviewType"="Plane"
        }

        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp] 
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }
        
        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Offset -1, -1
        Fog { Mode Off }
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]

        

        Pass
        {
            CGPROGRAM
                #pragma target 3.0
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"

                float _Radius;

                struct appdata_t
                {
                    float4 vertex : POSITION;
                    float2 texcoord : TEXCOORD0;
                    fixed4 color : COLOR;
                };
    
                struct v2f
                {
                    float4 vertex : SV_POSITION;
                    half2 texcoord : TEXCOORD0;
                    fixed4 color : COLOR;
                };
    
                sampler2D _MainTex;
                float4 _MainTex_ST;
                
                v2f vert (appdata_t v)
                {
                    v2f o;
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                    o.color = v.color;
#ifdef UNITY_HALF_TEXEL_OFFSET
                    o.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
                    return o;
                }
                
                fixed4 frag (v2f i) : COLOR
                {
                    float2 uv = i.texcoord.xy;
                    float4 c = i.color;
                    // 左下四方块
                    if(uv.x < _Radius && uv.y < _Radius)
                    {
                        float2 r;
                        r.x = uv.x - _Radius;
                        r.y = uv.y - _Radius;
                        float rr = length(r);
                        // 裁剪
                        if(rr > _Radius)
                        {
                            c.a = 0;
                        }
                    }
                    // 左上四方块
                    else if(uv.x < _Radius && uv.y > 1- _Radius)
                    {
                        float2 r;
                        r.x = uv.x - _Radius;
                        r.y = uv.y + _Radius - 1;
                        float rr = length(r);
                        // 裁剪
                        if(rr > _Radius)
                        {
                            c.a = 0;
                        }   
                    }
                    // 右下四方块
                    else if(uv.x > 1 - _Radius && uv.y < _Radius)
                    {
                        float2 r;
                        r.x = uv.x + _Radius - 1;
                        r.y = uv.y - _Radius;
                        float rr = length(r);
                        // 裁剪
                        if(rr > _Radius)
                        {
                            c.a = 0;
                        }   
                    }
                    // 右上四方块
                    else if(uv.x > 1 - _Radius && uv.y > 1- _Radius)
                    {
                        float2 r;
                        r.x = uv.x + _Radius - 1;
                        r.y = uv.y + _Radius - 1;
                        float rr = length(r);
                        // 裁剪
                        if(rr > _Radius)
                        {
                            c.a = 0;
                        }   
                    }                                   
                    
                    fixed4 col = tex2D(_MainTex, i.texcoord) * c;
                    clip (col.a - 0.01);
                    return col;
                }
                                
            ENDCG
        }
    }
}


  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用以下代码在 Unity使用 Shader 处理并保存图片: ```csharp using UnityEngine; public class SaveImageWithShader : MonoBehaviour { public Texture2D inputTexture; public Shader imageEffectShader; public string outputPath = "Output.png"; private void Start() { // 创建一个临时的 RenderTexture RenderTexture renderTexture = new RenderTexture(inputTexture.width, inputTexture.height, 0); Graphics.Blit(inputTexture, renderTexture); // 创建一个用于保存图片的 Texture2D Texture2D outputTexture = new Texture2D(renderTexture.width, renderTexture.height); // 创建一个用于保存结果的 RenderTexture RenderTexture outputRenderTexture = new RenderTexture(renderTexture.width, renderTexture.height, 0); // 使用 Shader 处理图片 Material imageEffectMaterial = new Material(imageEffectShader); Graphics.Blit(renderTexture, outputRenderTexture, imageEffectMaterial); // 将结果保存到 Texture2D RenderTexture.active = outputRenderTexture; outputTexture.ReadPixels(new Rect(0, 0, outputRenderTexture.width, outputRenderTexture.height), 0, 0); outputTexture.Apply(); // 将 Texture2D 保存为 PNG 文件 byte[] bytes = outputTexture.EncodeToPNG(); System.IO.File.WriteAllBytes(outputPath, bytes); // 释放资源 RenderTexture.active = null; Destroy(renderTexture); Destroy(outputRenderTexture); Destroy(outputTexture); Destroy(imageEffectMaterial); Debug.Log("Image saved to " + outputPath); } } ``` 将上述脚本附加到一个 GameObject 上,并在 Inspector 面板中指定输入的 Texture2D、要应用的 Shader 和输出路径。运行场景后,脚本会将经过 Shader 处理后的图片保存为 PNG 文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值