http://wiki.unity3d.com/index.php?title=DepthMask
Using the code in the above link, I got the result you see in the images below.
I use the 2 scripts below to achieve it.
The SetRenderQueue object sets the render priority during the Awake method. Lower numbers in the render queue means they are prioritized higher.
In the picture the boat has 2500, the depthmask 2700 and the water 3000. Although I might have only had to set the depthmasks, possibly the waters queue, but you can experiment a bit with that.
The shader itself (the bottom script) should be put on a material that your mask uses. You can then make the mask a child object of the boat so it always stays with it and keeps the water in the boat hidden.
SetRenderQueue.cs
using UnityEngine;
[AddComponentMenu("Rendering/SetRenderQueue")]
public class SetRenderQueue : MonoBehaviour {
[SerializeField]
protected int[] m_queues = new int[] { 3000 };
protected void Awake()
{
Material[] materials = GetComponent<Renderer>().materials;
for (int i = 0; i < materials.Length && i < m_queues.Length; ++i)
{
materials[i].renderQueue = m_queues[i];
}
}
}
DepthMaskShader
Shader "Custom/DepthMaskShader" {
SubShader{
Tags{ "Queue" = "Geometry+10" }
// Don't draw in the RGBA channels; just the depth buffer
ColorMask 0
ZWrite On
// Do nothing specific in the pass:
Pass{}
}
}
For the sake of clarity, here is an image showing the hierarchy as well as the mask object without the shader.
Good luck with your game.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…