Share via


裁剪基本類型 — MRTK2

這些 ClippingPrimitive 行為允許高效能 planesphere 、 和 box 圖形裁剪,且能夠在與 MRTK 著色器搭配使用時,指定要針對內部或外部) (裁剪的基本類型側邊。

基本裁剪 gizmos

注意

ClippingPrimitives 利用著色器內的 剪輯/捨棄 指令,並停用 Unity 批次裁剪轉譯器的能力。 利用裁剪基本類型時,請記住這些效能影響。

ClippingPlane.csClippingSphere.csClippingBox.cs 可用來輕鬆地控制裁剪基本屬性。 使用這些元件搭配下列著色器來運用裁剪案例。

  • Mixed Reality工具組/標準
  • Mixed Reality工具組/TextMeshPro
  • Mixed Reality Toolkit/Text3DShader

範例

ClippingExamplesMaterialGallery場景示範行為的使用 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 方塊指定相同的轉譯器。 轉譯器現在應該同時由這兩個方塊裁剪。

另請參閱