Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Cull Pixels Inside Overlapping Primitive

Discussion in 'High Definition Render Pipeline' started by adamgffrd, Mar 3, 2022.

  1. adamgffrd

    adamgffrd

    Joined:
    Sep 26, 2018
    Posts:
    35
    Hello Unity Community,

    Using HDRP 2021.2.0b15.3666

    Is it possible to cull pixels that fall inside of another overlapping primitive. So you have Sphere A with culling Shader on it along with a Cullme script, then Sphere B touches/overlaps a portion of Sphere A. All pixels from Sphere A, that fall inside Sphere B, then get culled off of Sphere A. Sphere B is transparent.

    I am very determined to continue working inside HDRP and really could use a nudge in the right direction with this. I am trying to recreate the L4D effect of culling a mesh and then showing another mesh hidden underneath (insides).

    I got this half working with a CGPROGRAM shader file copied from another user (sorry I don't recall the source). Tried adding that to a Custom Node (String and File) in Shader Graph and hitting a wall. Do I need to sign up for Unity HDRP Shader Classes or is this something someone here can help me with? Is that even a thing? If I wanted to pay someone to make this as an HDRP LIT shader that can be culled, where would I post for that?

    Attached is a zip file of a short video showing the CGPROGRAM shader in action, in this HDRP project.

    Any help is appreciated. :)


    Code (CSharp):
    1.  Shader "CGShader5"
    2.      {
    3.          SubShader
    4.          {
    5.              Pass
    6.              {
    7.                  /*Cull Off*/
    8.    
    9.                  CGPROGRAM
    10.    
    11.                  #pragma vertex vert
    12.                  #pragma fragment frag
    13.    
    14.                  uniform float4x4 _CullerSpace;
    15.    
    16.                  struct vout
    17.                  {
    18.                      float4 pos : SV_POSITION;
    19.                      float4 worldpos : TEXCOORD0;
    20.                      float4 localpos : TEXCOORD1;
    21.                      float4 cullerpos : TEXCOORD2;
    22.                  };
    23.    
    24.                  vout vert(float4 vertex : POSITION)
    25.                  {
    26.                      vout _out;
    27.                      _out.pos = UnityObjectToClipPos(vertex);
    28.                      //_out.pos = mul(Unity_Matrix_VP, Unity_ObjectToWorld), (vertex);
    29.                      _out.localpos = vertex;
    30.                      _out.worldpos = mul(unity_ObjectToWorld, vertex);
    31.                      _out.cullerpos = mul(_CullerSpace, _out.worldpos);
    32.                      return _out;
    33.                  }
    34.    
    35.                  float4 frag(vout _in) : COLOR
    36.                  {
    37.                      /*return float4(_in.cullerpos.x, _in.cullerpos.y, 0, 1);*/
    38.    
    39.                      if (_in.cullerpos.x > -0.5 && _in.cullerpos.x < 0.5 &&
    40.                          _in.cullerpos.y > -0.5 && _in.cullerpos.y < 0.5 &&
    41.                          _in.cullerpos.z > -0.5 && _in.cullerpos.z < 0.5)
    42.                          discard;
    43.    
    44.                      return float4(0, 1, 0, 1);
    45.                  }
    46.    
    47.                  ENDCG
    48.              }
    49.          }
    50.      }

    Code (CSharp):
    1.  using UnityEngine;
    2. [ExecuteInEditMode]
    3. public class CullMe : MonoBehaviour
    4. {
    5.      public Transform culler;
    6.      void Update()
    7.      {
    8.          if (!culler)
    9.              return;
    10.          var renderer = GetComponent<Renderer>();
    11.          if (!renderer)
    12.              return;
    13.          var material = renderer.sharedMaterial;
    14.          material.SetMatrix("_CullerSpace", culler.worldToLocalMatrix);
    15.      }
    16. }
     

    Attached Files:

  2. adamgffrd

    adamgffrd

    Joined:
    Sep 26, 2018
    Posts:
    35
  3. adamgffrd

    adamgffrd

    Joined:
    Sep 26, 2018
    Posts:
    35
    I can't seem to get this working. Not sure if something has changed with ShaderGraph from this article has changed. I tried recreating in a URP project and still not working. Frustrating..

    This script seems to be working fine, I can see the exposed parameters on the object I want to Cull being updated in realtime. I am pretty sure I recreated the Graph exactly how it's showing in this other setup.Pictures attached.

    Code (CSharp):
    1.  using UnityEngine;
    2. [ExecuteInEditMode]
    3.  
    4. public class CullMe : MonoBehaviour
    5. {
    6.      public Transform culler;
    7.      public float radius = 1;
    8.      void Update()
    9.      {
    10.          if (!culler)
    11.              return;
    12.          var renderer = GetComponent<Renderer>();
    13.          if (!renderer)
    14.              return;
    15.          var material = renderer.sharedMaterial;
    16.          material.SetVector("_CullerParams", new Vector4 (culler.transform.position.x, culler.transform.position.y, culler.transform.position.z, radius));
    17.        
    18.      }
    19. }
    Is anyone else able to try and replicate the effect from this article using HDRP? Any help is appreciated.
     

    Attached Files:

  4. adamgffrd

    adamgffrd

    Joined:
    Sep 26, 2018
    Posts:
    35
    I think this is in the wrong forum, could a @moderator move this to the Graphics\ShaderGraph forum please?
     
  5. adamgffrd

    adamgffrd

    Joined:
    Sep 26, 2018
    Posts:
    35
    Getting closer, user error. I was messing around with the values and I noticed that if I highlighted the sphere being culled, the outline was actually moving the closer I got with the Culling sphere. So I swapped Position with UV(Geometry) and it's actually dissolving it now, just need some more tweaks and will post the final result.
     
    mcroswell and StrangeWays777 like this.