Cha*_*hin 7 c# shader geometry intersection unity-game-engine
嗨,我基本上是在尝试做与此处描述的相同的事情: Unity Intersections Mask
需要注意的是,该平面不完全是平面,而是(相对于任意 3D 对象而言非常大)3D 锥体,并且我使用的相机必须是正交相机(因此没有延迟渲染)。
我也需要基本上每帧都这样做。
我试过查找各种交叉点深度着色器,但它们似乎都是用透视相机完成的。
即便如此,它们也不会将 3D 对象的非相交部分渲染为透明,而是以不同的方式为它们的部分着色。
链接的 stackoverflow 问题提到将平面正常渲染为不透明对象,然后使用片段着色器仅渲染与平面相交的对象部分。
然而,基于我(不可否认)对着色器的非常有限的理解,我不确定如何解决这个问题 - 据我所知,每个片段只有 1 个值作为它的深度,这是与近剪裁的距离相机平面到最靠近相机的对象上的点,该点由该片段/像素显示。
由于在这种情况下对象的其余部分是透明的,并且我需要显示通常会被覆盖的对象部分(因此,据我所知,深度未知),我看不出我如何只能绘制与我的锥体相交的部分。
除了使用着色器之外,我还尝试了以下方法:
使用 CSG 算法在锥体和对象之间实际执行布尔相交操作并渲染它。
尝试使用contactPoints
从Collision
由Unity生成以提取其中两个网格相交的所有点(顶点)和从这些点建立一个新的网格
我最初尝试使用的着色器(但不起作用)基于来自此处的代码:https : //forum.unity.com/threads/depth-buffer-with-orthographic-camera.355878/#post-2302460
并应用于每个对象。
与float partY = i.projPos.y + (i.projPos.y/_ZBias);
修改,而不硬编码的_ZBias
校正因子(和其他颜色相关的值稍有改变)。
根据我的理解,它应该可以工作,因为在我看来它是在比较深度缓冲区和对象的实际深度,并且仅_HighlightColor
在两者足够相似时才对其进行着色。
当然,我对着色器几乎一无所知,所以我对我对这段代码的评估几乎没有信心。
//Highlights intersections with other objects
Shader "Custom/IntersectionHighlights"
{
Properties
{
_RegularColor("Main Color", Color) = (1, 1, 1, 0) //Color when not intersecting
_HighlightColor("Highlight Color", Color) = (0, 0, 0, 1) //Color when intersecting
_HighlightThresholdMax("Highlight Threshold Max", Float) = 1 //Max difference for intersections
_ZBias("Highlight Z Bias", Float) = 2.5 //Balance out the Z-axis fading
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType"="Transparent" }
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _CameraDepthTexture; //Depth Texture
uniform float4 _RegularColor;
uniform float4 _HighlightColor;
uniform float _HighlightThresholdMax;
uniform float _ZBias;
struct v2f
{
float4 pos : SV_POSITION;
float4 projPos : TEXCOORD1; //Screen position of pos
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.projPos = ComputeScreenPos(o.pos);
return o;
}
half4 frag(v2f i) : COLOR
{
float4 finalColor = _RegularColor;
//Get the distance to the camera from the depth buffer for this point
float sceneZ = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r * 400;
//Actual distance to the camera
float partY = i.projPos.y;// + (i.projPos.y/_ZBias);
//If the two are similar, then there is an object intersecting with our object
float diff = (abs(sceneZ - partY)) / _HighlightThresholdMax;
if (diff <= 1)
{
finalColor = _HighlightColor;
}
half4 c;
c.r = finalColor.r;
c.g = finalColor.g;
c.b = finalColor.b;
c.a = (diff<=1)? 1.0f: 0.0f;
return c;
}
ENDCG
}
}
FallBack "VertexLit"
Run Code Online (Sandbox Code Playgroud)
上面(错误的)代码的结果是对象总是变得透明,无论它是否与锥体相交:
(该对象是完全透明的,即使它与锥体相交(运行时从场景视图中截取的图片))
最终,在我看来,它就像回到着色器一样。我将如何解决实现这种效果?它不一定必须与着色器一起使用,任何有用的东西对我来说都很好。示例代码会很棒。
归档时间: |
|
查看次数: |
2410 次 |
最近记录: |