利用模板测试制作3D卡牌
  1. 写入模板id

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Frame.Runtime.UI;

public class CardStencil : UIMonoViewComponent
{
    [SerializeField]
    private Material m_Material;
    [SerializeField] private int m_ID = 1;

    private void Start()
    {
        GetComponent<MeshRenderer>().material = m_Material;
        SetStencilID(m_ID);
    }

    public void SetStencilID(int id)
    {
        m_ID = id;
        GetComponent<Renderer>().material.SetInt("_ID", id);
    }
}


材质使用的shader是

Shader "Unlit/Mask"
{
    Properties
    {
        _ID("_ID",int) = 1
    }
        SubShader
        {
             Pass
             {
                 Tags{ "RenderType" = "Opaque" "Queue" = "Geometry-1" }
                 ColorMask 0
                 ZWrite off
                 ZTest off
                 Stencil
                 {
                     Ref[_ID]
                     Comp always
                     Pass replace //替换相同ID模板像素
                 }
                 CGINCLUDE
                 struct appdata {
                     float4 vertex : POSITION;
                 };
                 struct v2f {
                     float4 vertex : SV_POSITION;
                 };
                 v2f vert(appdata v) {
                     v2f o;
                     return o;
                 }
                 half4 frag(v2f i) : SV_Target{
                     return 0;
                 }
                 ENDCG
             }
        }
}


2.为卡片后面的3D内容添加模板剪裁

using UnityEngine;
public class RectMaskItem3D : MonoBehaviour
{
    [SerializeField]
    private int m_ID = 1;
    [SerializeField]
    private MaskType m_Type = MaskType.Always;

    public MaskType type
    {
        get { return m_Type; }
        set
        {
            if (value != m_Type)
            {
                m_Type = value;
                Refresh();
            }
        }
    }

    public int id
    {
        get { return m_ID; }
        set
        {
            if (value != m_ID)
            {
                m_ID = value;
                Refresh();
            }
        }
    }

    private void Start()
    {
        Refresh();
    }

    void Refresh()
    {
        foreach (var render in GetComponentsInChildren<Renderer>(true))
        {
            render.material.SetInt("_ID", m_ID);
            render.material.SetInt("_StencilComp", (int)m_Type);
        }
    }

    public enum MaskType : byte
    {
        Always = 8,
        Equal = 3
    }

}


3D使用的shader需要添加模板测试的相关字段和内容  标记为add的为添加内容

利用模板id不同可以做多张卡牌,使其互相不受影响

例如:

// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

// Unlit alpha-blended shader.
// - no lighting
// - no lightmap support
// - no per-material color

Shader "Custom/Unlit/Transparent" {
Properties {
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	//----add----
	_ID ("_ID",int) = 1
	_StencilComp ("_StencilComp",int) = 8
	//----add----
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100

    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass {
		//----add----
		Stencil 
		{  
			Ref [_ID]              
			Comp [_StencilComp]    
			Pass keep            
		}  
		//----add----
	
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            #pragma multi_compile_instancing

            #include "UnityCG.cginc"

            struct appdata_t {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f {
                float4 vertex : SV_POSITION;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata_t v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.texcoord);
                return col;
            }
        ENDCG
    }
}

}



首页 我的博客
粤ICP备17103704号