测试程序:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityDrawMesh : MonoBehaviour
{
public Mesh meshA;
public Mesh meshB;
public Material metrialA;
public Material metrialB;
public float scaleA = 1;
public float scaleB = 0.1f;
public Camera camera;
void Update()
{
//if(Input.GetKeyDown(KeyCode.A))
{
Matrix4x4 m1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one * scaleA);
Matrix4x4 m2 = Matrix4x4.TRS(Vector3.one * 3, Quaternion.Euler(-90, 0, 0), Vector3.one * scaleB);
Graphics.DrawMesh(meshA, m1, metrialA, 0, camera);
Graphics.DrawMesh(meshB, m2, metrialB, 0, camera);
}
}
}
shaderA
Shader "MyShader/ColorA"
{
Properties
{
_Color("MainColor",Color) = (1,1,1,1)
}
SubShader
{
Tags{"Queue" = "Geometry+1"}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct VertexData
{
float4 pos:POSITION;
};
struct V2F
{
float4 pos:POSITION;
};
V2F vert(VertexData v)
{
V2F res;
res.pos = UnityObjectToClipPos(v.pos);
return res;
}
float4 _Color;
fixed4 frag(V2F v) :SV_Target
{
fixed4 col = _Color;
return col;
}
ENDCG
}
}
}
shaderB
Shader "MyShader/ColorB"
{
Properties
{
_Color("MainColor",Color) = (1,1,1,1)
}
SubShader
{
Tags{"Queue" = "Geometry"}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct VertexData
{
float4 pos:POSITION;
};
struct V2F
{
float4 pos:POSITION;
};
V2F vert(VertexData v)
{
V2F res;
res.pos = UnityObjectToClipPos(v.pos);
return res;
}
float4 _Color;
fixed4 frag(V2F v) :SV_Target
{
fixed4 col = _Color;
return col;
}
ENDCG
}
}
}
shaderA,的队列为:Tags{“Queue” = “Geometry+1”}
shaderB,的队列为:Tags{“Queue” = “Geometry”}
程序中虽然先化了B物体,但是由于shaderA队列靠后,所以后画。
上面的代码,先meshB,meshB使用的是材质B,材质B使用的是shaderB,但是shaderB的队列在Geometry,比shaderA的要小,越小越先画。