裁剪基本概觀 - MRTK3
這些 ClippingPrimitive
行為允許高效能 plane
、 sphere
、 和 box
圖形裁剪,且能夠在圖形工具著色器使用時,指定要針對內部或外部) (裁剪的基本類型端。
注意
ClippingPrimitive
會利用著色器內的 剪輯/捨棄 指令,並停用 Unity 批次裁剪轉譯器的能力。 利用裁剪基本類型時,請記住這些效能影響。
ClippingPlane.cs、 ClippingSphere.cs和 ClippingBox.cs 可用來輕鬆地控制裁剪基本屬性。 使用這些元件搭配下列著色器來使用裁剪案例。
- 圖形工具/標準
- 圖形工具/文字網格 Pro
- 圖形工具/線框
- 圖形工具/線框
- 圖形工具/非畫布/背板
- 圖形工具/非畫布/前端板
- 圖形工具/非畫布/光暈
- 圖形工具/非畫布/四面光暈
- 圖形工具/非畫布/斜面
進階使用方式
根據預設,一次只能裁剪轉 ClippingPrimitive
譯 器 。 如果您的專案需要多個 ClippingPrimitive
來影響 轉譯器 ,下列範例程式碼會示範如何達成此目的。
注意
讓轉譯器有多個 ClippingPrimitive
剪輯會增加圖元著色器指令,並會影響效能。 分析專案中的這些變更。
如何讓兩個不同的 ClippingPrimitive
裁剪轉譯。 例如, 和 ClippingSphere
ClippingBox
同時:
// Within GraphicsToolsStandard.shader (or another Graphics Tools shader that supports clipping primitives) change:
#pragma multi_compile_local _ _CLIPPING_PLANE _CLIPPING_SPHERE _CLIPPING_BOX
// to:
#pragma multi_compile_local _ _CLIPPING_PLANE
#pragma multi_compile_local _ _CLIPPING_SPHERE
#pragma multi_compile_local _ _CLIPPING_BOX
注意
上述變更會產生額外的著色器編譯時間。
如何讓兩個相同 ClippingPrimitive
s 裁剪轉譯。 例如,同時有兩 ClippingBoxes
個:
// 1) Add the below MonoBehaviour to your project:
using UnityEngine;
using Microsoft.MixedReality.GraphicsTools;
[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();
clipBoxInverseTransformID = Shader.PropertyToID("_ClipBoxInverseTransform2");
}
}
// 2) Within GraphicsToolsStandard.shader add the following multi_compile pragma:
#pragma multi_compile_local _ _CLIPPING_BOX2
// 3) In GraphicsToolsStandardInput.hlsl add the following shader variables:
#if defined(_CLIPPING_BOX2)
half _ClipBoxSide2;
float4x4 _ClipBoxInverseTransform2;
#endif
// 4) In GraphicsToolsStandardProgram.hlsl 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)
// 5) In the same shader change:
#if defined(_CLIPPING_BOX)
primitiveDistance = min(primitiveDistance, GTPointVsBox(input.worldPosition.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
#endif
// to:
#if defined(_CLIPPING_BOX)
primitiveDistance = min(primitiveDistance, GTPointVsBox(input.worldPosition.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
#endif
#if defined(_CLIPPING_BOX2)
primitiveDistance = min(primitiveDistance, GTPointVsBox(input.worldPosition.xyz, _ClipBoxInverseTransform2) * _ClipBoxSide2);
#endif
最後,將 和 SecondClippingBox
元件新增至場景,並為這兩個 ClippingBox
方塊指定相同的轉譯器。 轉譯器現在應該同時由這兩個方塊裁剪。