Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Way to not have water inside of boat

Discussion in 'Universal Render Pipeline' started by Enigma229, Sep 27, 2022.

  1. Enigma229

    Enigma229

    Joined:
    Aug 6, 2019
    Posts:
    129
    I've been doing some research of how to not have water inside my ship. I saw a tutorial on using a depth mask shader but it didn't quite work.

    Can anybody recommend a URP compatible way to do it?
     
  2. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    829
    Stencil buffers are often used for this. You would add renderobjects renderer features with stencils set.
     
  3. Dujiangyue

    Dujiangyue

    Joined:
    Jul 7, 2019
    Posts:
    4
    depth mask is useful.Creata a shell from your ship,then ZWrite On and ColorMask 0 in shell shader,set shell render queue in front of your water(water 3002 and shell 3001 in my case).
    Code (CSharp):
    1. Properties
    2.     {
    3.        
    4.     }
    5.     SubShader
    6.     {
    7.         Tags { "RenderType"="Opaque" }
    8.         ZWrite On
    9.         ColorMask 0
    10.         Pass
    11.         {
    12.             HLSLPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    16.             struct appdata
    17.             {
    18.                 float4 vertex : POSITION;
    19.                 float2 uv : TEXCOORD0;
    20.             };
    21.             struct v2f
    22.             {
    23.                 float2 uv : TEXCOORD0;
    24.                 float4 vertex : SV_POSITION;
    25.             };
    26.             sampler2D _MainTex;
    27.             float4 _MainTex_ST;
    28.             v2f vert (appdata v)
    29.             {
    30.                 v2f o;
    31.                 o.vertex = TransformObjectToHClip(v.vertex);
    32.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    33.                 return o;
    34.             }
    35.             float4 frag (v2f i) : SV_Target
    36.             {
    37.                 // sample the texture
    38.                 float4 col = tex2D(_MainTex, i.uv);
    39.                 return col;
    40.             }
    41.             ENDHLSL
    42.         }
    43.     }
    Good luck.
     
    Matt-Cranktrain likes this.
  4. Enigma229

    Enigma229

    Joined:
    Aug 6, 2019
    Posts:
    129
    Thanks Dujiangyue. I'm not a coder and I got some compile errors when pasting your code into a new shader:

    Code (CSharp):
    1. Shader "Custom/NewSurfaceShader 1"
    2. {
    3.     Properties
    4.     {
    5.      
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         ZWrite On
    11.         ColorMask 0
    12.         Pass
    13.         {
    14.             HLSLPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    18.             struct appdata
    19.             {
    20.                 float4 vertex : POSITION;
    21.                 float2 uv : TEXCOORD0;
    22.             };
    23.             struct v2f
    24.             {
    25.                 float2 uv : TEXCOORD0;
    26.                 float4 vertex : SV_POSITION;
    27.             };
    28.             sampler2D _MainTex;
    29.             float4 _MainTex_ST;
    30.             v2f vert (appdata v)
    31.             {
    32.                 v2f o;
    33.                 o.vertex = TransformObjectToHClip(v.vertex);
    34.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    35.                 return o;
    36.             }
    37.             float4 frag (v2f i) : SV_Target
    38.             {
    39.                 // sample the texture
    40.                 float4 col = tex2D(_MainTex, i.uv);
    41.                 return col;
    42.             }
    43.             ENDHLSL
    44.         }
    45. }
     
  5. Dujiangyue

    Dujiangyue

    Joined:
    Jul 7, 2019
    Posts:
    4
    complete code here:
    Code (CSharp):
    1. Shader "Unlit/WaterMaskForCofferdam"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Main Tex",2D)="black"{}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         ZWrite On
    11.         ColorMask 0
    12.         Pass
    13.         {
    14.             HLSLPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    18.             TEXTURE2D(_MainTex);
    19.             SAMPLER(sampler_MainTex);
    20.             float4 _MainTex_ST;
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             v2f vert (appdata v)
    34.             {
    35.                 v2f o;
    36.                 o.vertex = TransformObjectToHClip(v.vertex);
    37.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    38.                 return o;
    39.             }
    40.  
    41.             float4 frag (v2f i) : SV_Target
    42.             {
    43.                 // sample the texture
    44.                 float4 col = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex, i.uv);
    45.                 return col;
    46.             }
    47.             ENDHLSL
    48.         }
    49.     }
    50. }
     
  6. Enigma229

    Enigma229

    Joined:
    Aug 6, 2019
    Posts:
    129
    Thanks I will take a look.