Cull Pixels Inside Overlapping Primitive

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. :slight_smile:

 Shader "CGShader5"
     {
         SubShader
         {
             Pass
             {
                 /*Cull Off*/
    
                 CGPROGRAM
    
                 #pragma vertex vert
                 #pragma fragment frag
    
                 uniform float4x4 _CullerSpace;
    
                 struct vout
                 {
                     float4 pos : SV_POSITION;
                     float4 worldpos : TEXCOORD0;
                     float4 localpos : TEXCOORD1;
                     float4 cullerpos : TEXCOORD2;
                 };
    
                 vout vert(float4 vertex : POSITION)
                 {
                     vout _out;
                     _out.pos = UnityObjectToClipPos(vertex);
                     //_out.pos = mul(Unity_Matrix_VP, Unity_ObjectToWorld), (vertex);
                     _out.localpos = vertex;
                     _out.worldpos = mul(unity_ObjectToWorld, vertex);
                     _out.cullerpos = mul(_CullerSpace, _out.worldpos);
                     return _out;
                 }
    
                 float4 frag(vout _in) : COLOR
                 {
                     /*return float4(_in.cullerpos.x, _in.cullerpos.y, 0, 1);*/
    
                     if (_in.cullerpos.x > -0.5 && _in.cullerpos.x < 0.5 &&
                         _in.cullerpos.y > -0.5 && _in.cullerpos.y < 0.5 &&
                         _in.cullerpos.z > -0.5 && _in.cullerpos.z < 0.5)
                         discard;
    
                     return float4(0, 1, 0, 1);
                 }
    
                 ENDCG
             }
         }
     }
 using UnityEngine;
[ExecuteInEditMode]
public class CullMe : MonoBehaviour
{
     public Transform culler;
     void Update()
     {
         if (!culler)
             return;
         var renderer = GetComponent<Renderer>();
         if (!renderer)
             return;
         var material = renderer.sharedMaterial;
         material.SetMatrix("_CullerSpace", culler.worldToLocalMatrix);
     }
}

7939573–1015348–CullingExample.7z (4.37 MB)

I am fairly confident this post will resolve my issue. Going to try this out and get back to here with results tomorrow.

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.

 using UnityEngine;
[ExecuteInEditMode]

public class CullMe : MonoBehaviour
{
     public Transform culler;
     public float radius = 1;
     void Update()
     {
         if (!culler)
             return;
         var renderer = GetComponent<Renderer>();
         if (!renderer)
             return;
         var material = renderer.sharedMaterial;
         material.SetVector("_CullerParams", new Vector4 (culler.transform.position.x, culler.transform.position.y, culler.transform.position.z, radius));
       
     }
}

Is anyone else able to try and replicate the effect from this article using HDRP? Any help is appreciated.


I think this is in the wrong forum, could a @moderator move this to the Graphics\ShaderGraph forum please?

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.

2 Likes