unity3d trouble with ship floating on water

2020-03-05 04:36发布

In unity, i have a ship this is my ship
And i have also water: this is my water

I want to make ship float on water, but(of course) what I get is this:ship and water
Is there any good way to keep water out of the ship?
Just want to make that water inside ship not displayable.
Maybe there is already answer for my question over the internet, but I had no idea on what do I exactly type in google... and I had also no idea for title of this question, so please don't blame me for unclear title.

Not it looks like this:
enter image description here
I want it to look like this:
enter image description here

I don't use any physics here, the y coordinate of ship is constant.



To Doh09: Your code seems to work, what i got is:enter image description here

But when I run game, what I got instead is:black cube inside ship

标签: unity3d 3d
1条回答
不美不萌又怎样
2楼-- · 2020-03-05 05:01

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.

DepthMaskShader

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.

DepthMaskShader2

Good luck with your game.

查看更多
登录 后发表回答