Shader 学习笔记 20160501

控制Queue=Tranparent物体显示顺序(使用同一个Material)

  • 当多个物体使用一个Material的时候,不能直接改Material挂载的那个shader的 queue TAG,因为直接改的话会造成所有使用这个Material的Obj的queue都改掉了,也就是达不到我们期望的逐个控制显示顺序的目的了。
  • 这个事情可以给每一个Obj挂一个script,在script里面用 curMaterial.renderQueue = queueValue 方法来更改这个Obj自己的renderQueue。这样一搞瞬间想怎么玩怎么玩。
  • 这段code是shader的
Shader "Custom/GUI_my" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        //_Glossiness ("Smoothness", Range(0,1)) = 0.5
        //_Metallic ("Metallic", Range(0,1)) = 0.0
        _FadeValue("Fade Value", Range(0,1)) = 0.5
    }
    SubShader {
        //这里比较重要,如果是渲染半透明物体,最好用Queue=Transparent,因为在Transparent
        //render顺序是从远到近的,如果放在Opaque那里,render顺序就是从近到远了(方便Cull看不见的
        //Obj)。
        //IgnorProjector表示,我们的半透明物体压根不管Projector特效。原因是半透明一般是作为visual 
        //effect或者UI使用,没什么机会受到Projector的影响的,除非你的设计需要它被影响到
        Tags { "RenderType"="Opaque" "Queue" = "Transparent" "IgnoreProjector" = "True"}
        //啥也不用说,对于半透明物体,必须ZWrite Off
        ZWrite Off
        //背面不渲染,意思就是camera从后面看的话,啥也没有
        Cull Back 
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf UnlitGUI alpha novertexlights   

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        float _FadeValue;

        struct Input {
            float2 uv_MainTex;
        };

        //half _Glossiness;
        //half _Metallic;
        fixed4 _Color;

        inline half4 LightingUnlitGUI (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
        {
            fixed4 c;
            c.rgb = s.Albedo;
            c.a = s.Alpha;
            return c;
        }  

        void surf (Input IN, inout SurfaceOutput   o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb * _Color.rgb;
            // Metallic and smoothness come from slider variables
            //o.Metallic = _Metallic;
            //o.Smoothness = _Glossiness;
            o.Alpha = c.a*_FadeValue;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
  • 挂在Obj上面的Script
using UnityEngine;
using System.Collections;


[ExecuteInEditMode]
public class RenderQueue : MonoBehaviour {

    public int queueValue = 2000;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        Material curMaterial = transform.GetComponent<Renderer>().sharedMaterial;

        if (curMaterial)
        {
            //if a material is found set the queue value
            //这里最吊,居然改到的就是这个Obj自己的renderQueue,没有改到别的Obj用同一个Material
            //奥妙何在? 难道是new了一个新的Material?!  可是上面明明是
            //transform.GetComponent<Renderer>().sharedMaterial 啊!!! 应该没new吧
            //不管怎样,从最终结果上看,的确每一个Obj都赋予了specific的renderQueue值,
            //我倾向于认为还是new了,只不过到底new的是整个Material还是只是这个renderQueue
            //就不清楚了。。。
            curMaterial.renderQueue = queueValue;

        }
        else
        {
            //if a material is not found show a debug message
            Debug.LogWarning(transform.name + ": cannot find a material to set the render queue!");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/mconreally/article/details/51289514