Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

RayTracingInstanceCullingMaterialTest

struct in UnityEngine.Rendering

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

This structure is used by RayTracingAccelerationStructure.CullInstances function to avoid adding Renderers to the acceleration structure or to ignore individual sub-meshes in a Mesh based on Shaders used by Materials when building the acceleration structure.

The Shader Tags and Shader Pass names defined in the Shader used by a Material are part of the test. The test passes when all the following conditions are met:

  • deniedShaderPasses array is empty, or the Shader used by a Material doesn’t contain any of the Shader Passes from the array.
  • requiredShaderPasses array is empty, or the Shader used by a Material contains a Shader Pass from the array. If the array is empty, requiredShaderPasses won't be used in the test.
  • requiredShaderPasses and deniedShaderPasses arrays don't contain a valid Shader Pass at the same time.
  • The Shader used by a Material doesn’t contain a Shader Pass included in deniedShaderPasses and requiredShaderPasses arrays at the same time.
  • requiredShaderTags array is empty, or the Shader used by a Material contains at least one of the Shader Tags from the array.

Additional resources: RayTracingAccelerationStructure.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class MaterialTestExample : MonoBehaviour { private RayTracingAccelerationStructure rayTracingAccelStruct = null;

void Start() { if (SystemInfo.supportsRayTracing == false) { Debug.Log("The system doesn't support Ray Tracing."); return; }

RayTracingAccelerationStructure.Settings accelStructSettings = new RayTracingAccelerationStructure.Settings(RayTracingAccelerationStructure.ManagementMode.Manual, RayTracingAccelerationStructure.RayTracingModeMask.Everything, -1); rayTracingAccelStruct = new RayTracingAccelerationStructure(accelStructSettings); }

void OnDisable() { if (rayTracingAccelStruct != null) { rayTracingAccelStruct.Release(); rayTracingAccelStruct = null; } }

RayTracingInstanceCullingConfig CreateDefaultValidRayTracingInstanceCullingConfig() { RayTracingInstanceCullingConfig cullingConfig = new RayTracingInstanceCullingConfig();

cullingConfig.flags = RayTracingInstanceCullingFlags.None;

cullingConfig.subMeshFlagsConfig.opaqueMaterials = RayTracingSubMeshFlags.Enabled | RayTracingSubMeshFlags.ClosestHitOnly; cullingConfig.subMeshFlagsConfig.transparentMaterials = RayTracingSubMeshFlags.Disabled; cullingConfig.subMeshFlagsConfig.alphaTestedMaterials = RayTracingSubMeshFlags.Enabled;

List<RayTracingInstanceCullingTest> instanceTests = new List<RayTracingInstanceCullingTest>(); RayTracingInstanceCullingTest instanceTest = new RayTracingInstanceCullingTest(); instanceTest.allowTransparentMaterials = false; instanceTest.allowOpaqueMaterials = true; instanceTest.allowAlphaTestedMaterials = true; instanceTest.layerMask = -1; instanceTest.shadowCastingModeMask = (1 << (int)UnityEngine.Rendering.ShadowCastingMode.Off) | (1 << (int)UnityEngine.Rendering.ShadowCastingMode.On) | (1 << (int)UnityEngine.Rendering.ShadowCastingMode.TwoSided); instanceTest.instanceMask = 1 << 0;

instanceTests.Add(instanceTest); cullingConfig.instanceTests = instanceTests.ToArray();

return cullingConfig; }

void LateUpdate() { if (rayTracingAccelStruct == null || Camera.main == null) return;

RayTracingInstanceCullingConfig cullingConfig = CreateDefaultValidRayTracingInstanceCullingConfig();

// SubShader tags requirements. cullingConfig.materialTest.requiredShaderTags = new RayTracingInstanceCullingShaderTagConfig[1]; cullingConfig.materialTest.requiredShaderTags[0].tagId = new ShaderTagId("RenderMode"); cullingConfig.materialTest.requiredShaderTags[0].tagValueId = new ShaderTagId("RayTracing");

// Accept only Shaders that contain at least one of these Shader Passes. cullingConfig.materialTest.requiredShaderPasses = new string[] { "PathTracing", "RayTracedAO" };

// Reject Shaders that contain any of these Shader Passes (for example shadow-only passes that should not be ray-traced). cullingConfig.materialTest.deniedShaderPasses = new string[] { "ShadowCaster" };

rayTracingAccelStruct.ClearInstances(); rayTracingAccelStruct.CullInstances(ref cullingConfig);

rayTracingAccelStruct.Build(); } }

This is an example of how to create, populate and build an acceleration structure. The content of the acceleration structure is specified by configuring culling and filtering parameters that are passed to the CullInstances function. The required and denied Shader Passes are configured in cullingConfig.materialTest.

Properties

Property Description
deniedShaderPassesAn array of Shader Pass names used by RayTracingAccelerationStructure.CullInstances to ignore Renderers or individual sub-meshes.
requiredShaderPassesAn array of Shader Pass names used by RayTracingAccelerationStructure.CullInstances to accept Renderers or individual sub-meshes whose Shader contains at least one of these Shader Passes.
requiredShaderTagsAn array of Shader Tags used by RayTracingAccelerationStructure.CullInstances to ignore Renderers or individual sub-meshes.