Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make all Meshes under y Invisible - and filll the hole

Discussion in 'Shaders' started by Rabbito, May 28, 2015.

  1. Rabbito

    Rabbito

    Joined:
    Apr 5, 2015
    Posts:
    13
    Hi
    i think its easy but i am a noob in shaders ;)
    I want a cut throught the hole Scene and show only all Meshes under a specific Y. This is working fine, but I want to fill a plane in the cutted mesh.
    This is how it looks now:
    upload_2015-5-28_18-16-15.png
    And this is how I want it:
    upload_2015-5-28_18-19-19.png
    The Problem is that in the First Picture I can see every Cave in the Mountain. In the Second i can't see anything because the plane is simulating the Blocks

    How could i ,make it with shaders?

    This is the Shader I am using now:

    Code (CSharp):
    1. Shader "Custom/BlockShader" {
    2.     Properties  {
    3.          _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.         _LetzteSichtbaresY("Letzte sichtbare Ebene", Float) = 50.0
    6.         }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" "Queue"="Geometry+1"}
    9.        
    10.        
    11.         Pass{
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             uniform sampler2D _MainTex;
    16.             uniform float _LetzteSichtbaresY;
    17.            
    18.              struct vertexInput {
    19.                 float4 vertex : POSITION;
    20.                 float4 texcoord : TEXCOORD0;
    21.                 float4 position_in_world_space : TEXCOORD1;
    22.              };
    23.              struct vertexOutput {
    24.                 float4 pos : SV_POSITION;
    25.                 float4 tex : TEXCOORD0;
    26.                 float4 position_in_world_space : TEXCOORD1;
    27.              };
    28.            
    29.              vertexOutput vert(vertexInput input)
    30.              {
    31.                 vertexOutput output;
    32.    
    33.                 output.tex = input.texcoord;
    34.                 output.position_in_world_space = mul(_Object2World, input.vertex);
    35.              
    36.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    37.                 return output;
    38.              }
    39.            
    40.             float4 frag(vertexOutput input) : COLOR
    41.              {
    42.               if(input.position_in_world_space.y > _LetzteSichtbaresY)
    43.                     discard;
    44.                 return tex2D(_MainTex, input.tex.xy);  
    45.              }
    46.            
    47.             ENDCG
    48.            
    49.         }
    50.     }
    51. }
    52.