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.
CloseFor 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.
ClosecullingConfig | Parameters for culling and filtering ray tracing instances. |
RayTracingInstanceCullingResults Culling results.
Populates the RayTracingAccelerationStructure with ray tracing instances that Unity associates with Renderers in the Scene by using filtering and culling parameters.
The previous content of the acceleration structure can optionally be cleared by using RayTracingAccelerationStructure.ClearInstances. After calling this function, additional ray tracing instances can be added to the acceleration structure using RayTracingAccelerationStructure.AddInstance functions.
If RayTracingInstanceCullingConfig.instanceTests array is empty then this function doesn't have any effect.
To build an acceleration structure on the GPU, call RayTracingAccelerationStructure.Build or CommandBuffer.BuildRayTracingAccelerationStructure.
Additional resources: RayTracingShader.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering;
// This script can be attached to a Camera. public class CullInstancesExample : MonoBehaviour { // A RayTracingShader is defined in a .raytrace file. public RayTracingShader rayTracingShader = null;
public bool enableRayTracedReflections = true; public bool enableRayTracedShadows = true;
private int cameraWidth = 0; private int cameraHeight = 0;
private RenderTexture rayTracingOutput = null; private RayTracingAccelerationStructure rayTracingAccelStruct = null;
void Start() { if (SystemInfo.supportsRayTracingShaders == false) { Debug.Log("The system doesn't support Ray Tracing."); return; }
RayTracingAccelerationStructure.Settings accelStructSetting = new RayTracingAccelerationStructure.Settings(RayTracingAccelerationStructure.ManagementMode.Manual, RayTracingAccelerationStructure.RayTracingModeMask.Everything, -1); rayTracingAccelStruct = new RayTracingAccelerationStructure(accelStructSetting); }
void OnDisable() { if (rayTracingAccelStruct != null) { rayTracingAccelStruct.Release(); rayTracingAccelStruct = null; }
if (rayTracingOutput) { rayTracingOutput.Release(); rayTracingOutput = null; }
cameraWidth = 0; cameraHeight = 0; }
void OnRenderImage(RenderTexture src, RenderTexture dest) { if (SystemInfo.supportsRayTracingShaders == false || rayTracingShader == null) return;
if (cameraWidth != Camera.main.pixelWidth || cameraHeight != Camera.main.pixelHeight) { if (rayTracingOutput) rayTracingOutput.Release();
rayTracingOutput = new RenderTexture(Camera.main.pixelWidth, Camera.main.pixelHeight, 0, RenderTextureFormat.ARGBHalf); rayTracingOutput.enableRandomWrite = true; rayTracingOutput.Create();
cameraWidth = Camera.main.pixelWidth; cameraHeight = Camera.main.pixelHeight; }
RayTracingInstanceCullingConfig cullingConfig = new RayTracingInstanceCullingConfig();
cullingConfig.flags = RayTracingInstanceCullingFlags.EnableLODCulling | RayTracingInstanceCullingFlags.EnableSphereCulling | RayTracingInstanceCullingFlags.EnableSolidAngleCulling;
// Configure LOD culling. cullingConfig.lodParameters.fieldOfView = Camera.main.fieldOfView; cullingConfig.lodParameters.cameraPosition = Camera.main.transform.position; cullingConfig.lodParameters.cameraPixelHeight = Camera.main.pixelHeight; cullingConfig.lodParameters.orthoSize = 0; cullingConfig.lodParameters.isOrthographic = false;
// Configure sphere culling. Accept only objects for which the enclosing AABBs are inside (or intersect) a sphere with a radius of 100 units around the camera. cullingConfig.sphereCenter = Camera.main.transform.position; cullingConfig.sphereRadius = 100.0f;
// Configure solid-angle culling. Accept only objects for which the projected solid angle with the apex in cullingConfig.lodParameters.cameraPosition is larger than 5 degrees. cullingConfig.minSolidAngle = 5.0f;
// Disable anyhit shaders for opaque geometries for best ray tracing performance. cullingConfig.subMeshFlagsConfig.opaqueMaterials = RayTracingSubMeshFlags.Enabled | RayTracingSubMeshFlags.ClosestHitOnly;
// Disable transparent geometries. cullingConfig.subMeshFlagsConfig.transparentMaterials = RayTracingSubMeshFlags.Disabled;
// Enable anyhit shaders for alpha-tested / cutout geometries. cullingConfig.subMeshFlagsConfig.alphaTestedMaterials = RayTracingSubMeshFlags.Enabled;
// Configure which triangles are double sided. For best ray tracing performance, triangle culling should be disabled. cullingConfig.triangleCullingConfig.checkDoubleSidedGIMaterial = true; cullingConfig.triangleCullingConfig.frontTriangleCounterClockwise = false; cullingConfig.triangleCullingConfig.optionalDoubleSidedShaderKeywords = new string[1]; cullingConfig.triangleCullingConfig.optionalDoubleSidedShaderKeywords[0] = "_DOUBLESIDED_ON"; cullingConfig.triangleCullingConfig.forceDoubleSided = false;
// Configure Material types. Render queue intervals can also be used here. cullingConfig.alphaTestedMaterialConfig.optionalShaderKeywords = new string[1]; cullingConfig.alphaTestedMaterialConfig.optionalShaderKeywords[0] = "_ALPHATEST_ON";
cullingConfig.transparentMaterialConfig.optionalShaderKeywords = new string[1]; cullingConfig.transparentMaterialConfig.optionalShaderKeywords[0] = "_SURFACE_TYPE_TRANSPARENT";
// Configure a Material test. Allow only Shaders that have a mandatory Shader Tag. cullingConfig.materialTest.requiredShaderTags = new RayTracingInstanceCullingShaderTagConfig[1]; cullingConfig.materialTest.requiredShaderTags[0].tagId = new ShaderTagId("RenderPipeline"); cullingConfig.materialTest.requiredShaderTags[0].tagValueId = new ShaderTagId("MyAwesomeRP");
List<RayTracingInstanceCullingTest> instanceTests = new List<RayTracingInstanceCullingTest>();
// Configure instance tests. There can be one instance test for each ray tracing effect for example. // The purpose of instance tests is to use different settings (layer, material types) per ray tracing effect. // Use InstanceInclusionMask argument of TraceRay HLSL function to mask different instance types.
if (enableRayTracedReflections) { RayTracingInstanceCullingTest instanceTest = new RayTracingInstanceCullingTest(); instanceTest.allowTransparentMaterials = false; instanceTest.allowOpaqueMaterials = true; instanceTest.allowAlphaTestedMaterials = true; instanceTest.layerMask = -1; instanceTest.shadowCastingModeMask = (1 << (int)ShadowCastingMode.Off) | (1 << (int)ShadowCastingMode.On) | (1 << (int)ShadowCastingMode.TwoSided); instanceTest.instanceMask = 1 << 0;
instanceTests.Add(instanceTest); }
if (enableRayTracedShadows) { RayTracingInstanceCullingTest instanceTest = new RayTracingInstanceCullingTest(); instanceTest.allowTransparentMaterials = false; instanceTest.allowOpaqueMaterials = true; instanceTest.allowAlphaTestedMaterials = true; instanceTest.layerMask = -1; instanceTest.shadowCastingModeMask = (1 << (int)ShadowCastingMode.On) | (1 << (int)ShadowCastingMode.TwoSided); instanceTest.instanceMask = 1 << 1;
instanceTests.Add(instanceTest); }
cullingConfig.instanceTests = instanceTests.ToArray();
rayTracingAccelStruct.ClearInstances(); rayTracingAccelStruct.CullInstances(ref cullingConfig);
// Additional ray tracing instances can be added manually using RayTracingAccelerationStructure.AddInstance function. // rayTracingAccelStruct.AddInstance(...);
rayTracingAccelStruct.Build(Camera.main.transform.position);
// Use Shader Pass "Test" in surface (material) shaders. rayTracingShader.SetShaderPass("Test");
rayTracingShader.SetAccelerationStructure("g_AccelStruct", rayTracingAccelStruct); rayTracingShader.SetTexture("g_Output", rayTracingOutput);
rayTracingShader.Dispatch("MainRayGenShader", rayTracingOutput.width, rayTracingOutput.height, 1);
Graphics.Blit(rayTracingOutput, dest); } }
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.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.