这些 ClippingPrimitive
行为允许执行高性能 plane
、 sphere
、 和 box
形状剪裁,并能够指定要在与 MRTK 着色器一起使用时针对) 内部或外部 (剪裁基元的哪一侧。
注意
ClippingPrimitives
利用着色器中的剪辑/放弃指令,并禁用Unity批量剪裁呈现器的功能。 在利用剪裁基元时,请记住这些性能影响。
ClippingPlane.cs
、 ClippingSphere.cs
和 ClippingBox.cs
可用于轻松控制剪辑基元属性。 将这些组件与以下着色器结合使用,以利用剪裁方案。
- 混合现实 Toolkit/Standard
- 混合现实工具包/TextMeshPro
- 混合现实工具包/Text3DShader
示例
ClippingExamples 和 MaterialGallery 场景演示了行为的用法ClippingPrimitive
,可在以下位置找到:MRTK/Examples/Demos/StandardShader/Scenes/
高级用法
默认情况下,一 ClippingPrimitive
次只能剪裁 呈现器 。 如果项目需要多个 ClippingPrimitive
项目来影响 呈现器 ,则下面的示例代码演示如何实现此目的。
注意
让呈现器进行多个ClippingPrimitives
剪辑会增加像素着色器指令,并会影响性能。 请在项目中分析这些更改。
如何对呈现进行两个不同的 ClippingPrimitives
剪辑。 例如, ClippingSphere
同时 ClippingBox
:
// Within MRTK/Core/StandardAssets/Shaders/MixedRealityStandard.shader (or another MRTK shader) change:
#pragma multi_compile _ _CLIPPING_PLANE _CLIPPING_SPHERE _CLIPPING_BOX
// to:
#pragma multi_compile _ _CLIPPING_PLANE
#pragma multi_compile _ _CLIPPING_SPHERE
#pragma multi_compile _ _CLIPPING_BOX
注意
上述更改将产生额外的着色器编译时间。
如何让呈现的两个同 ClippingPrimitives
一剪辑。 例如,同时 ClippingBoxes
两个:
// 1) Add the below MonoBehaviour to your project:
[ExecuteInEditMode]
public class SecondClippingBox : ClippingBox
{
/// <inheritdoc />
protected override string Keyword
{
get { return "_CLIPPING_BOX2"; }
}
/// <inheritdoc />
protected override string ClippingSideProperty
{
get { return "_ClipBoxSide2"; }
}
/// <inheritdoc />
protected override void Initialize()
{
base.Initialize();
clipBoxSizeID = Shader.PropertyToID("_ClipBoxSize2");
clipBoxInverseTransformID = Shader.PropertyToID("_ClipBoxInverseTransform2");
}
}
// 2) Within MRTK/Core/StandardAssets/Shaders/MixedRealityStandard.shader (or another MRTK shader) add the following multi_compile pragma:
#pragma multi_compile _ _CLIPPING_BOX2
// 3) In the same shader change:
#if defined(_CLIPPING_PLANE) || defined(_CLIPPING_SPHERE) || defined(_CLIPPING_BOX)
// to:
#if defined(_CLIPPING_PLANE) || defined(_CLIPPING_SPHERE) || defined(_CLIPPING_BOX) || defined(_CLIPPING_BOX2)
// 4) In the same shader add the following shader variables:
#if defined(_CLIPPING_BOX2)
fixed _ClipBoxSide2;
float4 _ClipBoxSize2;
float4x4 _ClipBoxInverseTransform2;
#endif
// 5) In the same shader change:
#if defined(_CLIPPING_BOX)
primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
#endif
// to:
#if defined(_CLIPPING_BOX)
primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
#endif
#if defined(_CLIPPING_BOX2)
primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize2.xyz, _ClipBoxInverseTransform2) * _ClipBoxSide2);
#endif
最后,将 和 SecondClippingBox 组件添加到场景中,并为这两个 ClippingBox
框指定相同的呈现器。 呈现器现在应同时被两个框剪裁。