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.
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)