剪裁基元 - MRTK2
ClippingPrimitive
行为可实现高性能的 plane
、sphere
和 box
形状剪裁,与 MRTK 着色器配合使用时,能够指定要剪裁基元的哪一侧(内侧或外侧)。
注意
ClippingPrimitives
利用着色器中的剪裁/丢弃指令,并禁用 Unity 的批量剪裁渲染器功能。 利用剪裁基元时,请考虑到这些性能影响。
ClippingPlane.cs
、ClippingSphere.cs
和 ClippingBox.cs
可用于轻松控制剪裁基元属性。 将这些组件与以下着色器配合使用可以利用剪裁方案。
- 混合现实工具包/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
最后,将 ClippingBox
和 SecondClippingBox 组件添加到场景中,并为两个框指定同一渲染器。 渲染器现在应该同时被两个框剪裁。