Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

3d - unity3d trouble with ship floating on water

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...