public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext) { if (!m_initialized) { return(new JobHandle()); } BatchCullingOutputDrawCommands drawCommands = new BatchCullingOutputDrawCommands(); drawCommands.drawRangeCount = 1; drawCommands.drawRanges = Malloc <BatchDrawRange>(1); drawCommands.drawRanges[0] = new BatchDrawRange { drawCommandsBegin = 0, drawCommandsCount = 1, filterSettings = new BatchFilterSettings { renderingLayerMask = 1, layer = 0, motionMode = m_motionVectorTest ? MotionVectorGenerationMode.Object : MotionVectorGenerationMode.Camera, shadowCastingMode = ShadowCastingMode.On, receiveShadows = true, staticShadowCaster = false, allDepthSorted = false } }; drawCommands.visibleInstances = Malloc <int>(m_itemCount); int n = 0; int radius = (itemGridSize / 2) * (itemGridSize / 2); // (grid/2)^2 int radiusO = (radius * 90) / 100; int radiusI = (radiusO * 85) / 100; for (int r = 0; r < itemGridSize; r++) { for (int i = 0; i < itemGridSize; i++) { bool visible = true; if (m_cullTest) { int dist = (r - itemGridSize / 2) * (r - itemGridSize / 2) + (i - itemGridSize / 2) * (i - itemGridSize / 2); if ((dist >= radiusI) && (dist <= radiusO)) { visible = false; } } if (visible) { drawCommands.visibleInstances[n++] = r * itemGridSize + i; } } } drawCommands.visibleInstanceCount = n; drawCommands.drawCommandCount = 1; drawCommands.drawCommands = Malloc <BatchDrawCommand>(1); drawCommands.drawCommands[0] = new BatchDrawCommand { visibleOffset = 0, visibleCount = (uint)n, batchID = m_batchID, materialID = m_materialID, meshID = m_meshID, submeshIndex = 0, splitVisibilityMask = 0xff, flags = m_motionVectorTest ? BatchDrawCommandFlags.HasMotion : BatchDrawCommandFlags.None, sortingPosition = 0 }; drawCommands.instanceSortingPositions = null; drawCommands.instanceSortingPositionFloatCount = 0; cullingOutput.drawCommands[0] = drawCommands; return(new JobHandle()); }
public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext) { if (!m_initialized) { return(new JobHandle()); } #if ENABLE_PICKING bool isPickingCulling = cullingContext.viewType == BatchCullingViewType.Picking; #endif var splitCounts = new NativeArray <int>(cullingContext.cullingSplits.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); for (int i = 0; i < splitCounts.Length; ++i) { var split = cullingContext.cullingSplits[i]; splitCounts[i] = split.cullingPlaneCount; } var planes = FrustumPlanes.BuildSOAPlanePacketsMulti(cullingContext.cullingPlanes, splitCounts, Allocator.TempJob); BatchCullingOutputDrawCommands drawCommands = new BatchCullingOutputDrawCommands(); drawCommands.drawRanges = Malloc <BatchDrawRange>(m_drawRanges.Length); drawCommands.drawCommands = Malloc <BatchDrawCommand>(m_drawBatches.Length * splitCounts.Length * 10); // TODO: Multiplying the DrawCommand count by splitCount*10 is NOT an conservative upper bound. But in practice is enough. Sorting would give us a real conservative bound... drawCommands.visibleInstances = Malloc <int>(m_instanceIndices.Length); #if ENABLE_PICKING drawCommands.drawCommandPickingInstanceIDs = isPickingCulling ? Malloc <int>(m_drawBatches.Length) : null; #endif // Zero init: Culling job sets the values! drawCommands.drawRangeCount = 0; drawCommands.drawCommandCount = 0; drawCommands.visibleInstanceCount = 0; drawCommands.instanceSortingPositions = null; drawCommands.instanceSortingPositionFloatCount = 0; cullingOutput.drawCommands[0] = drawCommands; var visibilityLength = (m_renderers.Length + 7) / 8; var rendererVisibility = new NativeArray <ulong>(visibilityLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cullingJob = new CullingJob { planes = planes, splitCounts = splitCounts, renderers = m_renderers, rendererVisibility = rendererVisibility }; var drawOutputJob = new DrawCommandOutputJob { batchID = m_batchID, rendererVisibility = rendererVisibility, instanceIndices = m_instanceIndices, drawBatches = m_drawBatches, drawRanges = m_drawRanges, drawIndices = m_drawIndices, drawCommands = cullingOutput.drawCommands }; var jobHandleCulling = cullingJob.Schedule(visibilityLength, 8); var jobHandleOutput = drawOutputJob.Schedule(jobHandleCulling); return(jobHandleOutput); }
// The callback method called by Unity whenever it visibility culls to determine which // objects to draw. This method will output draw commands that describe to Unity what // should be drawn for this BatchRendererGroup. public unsafe JobHandle OnPerformCulling( BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext) { // UnsafeUtility.Malloc() requires an alignment, so use the largest integer type's alignment // which is a reasonable default. int alignment = UnsafeUtility.AlignOf <long>(); // Acquire a pointer to the BatchCullingOutputDrawCommands struct so we can easily // modify it directly. var drawCommands = (BatchCullingOutputDrawCommands *)cullingOutput.drawCommands.GetUnsafePtr(); // Allocate memory for the output arrays. In a more complicated implementation the amount of memory // allocated could be dynamically calculated based on what we determined to be visible. // In this example, we will just assume that all of our instances are visible and allocate // memory for each of them. We need the following allocations: // - a single draw command (which draws kNumInstances instances) // - a single draw range (which covers our single draw command) // - kNumInstances visible instance indices. // The arrays must always be allocated using Allocator.TempJob. drawCommands->drawCommands = (BatchDrawCommand *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <BatchDrawCommand>(), alignment, Allocator.TempJob); drawCommands->drawRanges = (BatchDrawRange *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <BatchDrawRange>(), alignment, Allocator.TempJob); drawCommands->visibleInstances = (int *)UnsafeUtility.Malloc(kNumInstances * sizeof(int), alignment, Allocator.TempJob); drawCommands->drawCommandCount = 1; drawCommands->drawRangeCount = 1; drawCommands->visibleInstanceCount = kNumInstances; // Our example does not use depth sorting, so we can leave the instanceSortingPositions as null. drawCommands->instanceSortingPositions = null; drawCommands->instanceSortingPositionFloatCount = 0; // Configure our single draw command to draw kNumInstances instances // starting from offset 0 in the array, using the batch, material and mesh // IDs that we registered in the Start() method. No special flags are set. drawCommands->drawCommands[0].visibleOffset = 0; drawCommands->drawCommands[0].visibleCount = kNumInstances; drawCommands->drawCommands[0].batchID = m_BatchID; drawCommands->drawCommands[0].materialID = m_MaterialID; drawCommands->drawCommands[0].meshID = m_MeshID; drawCommands->drawCommands[0].submeshIndex = 0; drawCommands->drawCommands[0].flags = 0; drawCommands->drawCommands[0].sortingPosition = 0; // Configure our single draw range to cover our single draw command which // is at offset 0. drawCommands->drawRanges[0].drawCommandsBegin = 0; drawCommands->drawRanges[0].drawCommandsCount = 1; // In this example we don't care about shadows or motion vectors, so we leave everything // to the default zero values, except the renderingLayerMask which we have to set to all ones // so the instances will be drawn regardless of mask settings when rendering. drawCommands->drawRanges[0].filterSettings = new BatchFilterSettings { renderingLayerMask = 0xffffffff, }; // Finally, write the actual visible instance indices to their array. In a more complicated // implementation, this output would depend on what we determined to be visible, but in this example // we will just assume that everything is visible. for (int i = 0; i < kNumInstances; ++i) { drawCommands->visibleInstances[i] = i; } // This simple example does not use jobs, so we can just return an empty JobHandle. // Performance sensitive applications are encouraged to use Burst jobs to implement // culling and draw command output, in which case we would return a handle here that // completes when those jobs have finished. return(new JobHandle()); }