public void Render(Vector3 observerPos, float visibleDistance, Vector3[] frustumPlanesNormals, float[] frustumPlanesDistances) { if (rebuild) { if (!Application.isPlaying || Time.frameCount - lastRebuildFrame > 10) { lastRebuildFrame = Time.frameCount; RebuildZoneRenderingLists(observerPos, visibleDistance); rebuild = false; } } for (int k = 0; k < batchedMeshes.count; k++) { BatchedMesh batchedMesh = batchedMeshes.values [k]; VoxelDefinition vd = batchedMesh.voxelDefinition; Mesh mesh = vd.mesh; Material material = batchedMesh.material; ShadowCastingMode shadowCastingMode = vd.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off; for (int j = 0; j < batchedMesh.batches.count; j++) { Batch batch = batchedMesh.batches.values [j]; if (GeometryUtilityNonAlloc.TestPlanesAABB(frustumPlanesNormals, frustumPlanesDistances, ref batch.boundsMin, ref batch.boundsMax)) { Graphics.DrawMeshInstanced(mesh, 0, material, batch.matrices, batch.instancesCount, batch.materialPropertyBlock, shadowCastingMode, vd.receiveShadows, env.layerVoxels); } } } #if UNITY_EDITOR // required to fix a bug by which Draw Calls skyrocket in Stats windows when some voxel uses GPU instancing when "Render In SceneView" is enabled and the scene has just loaded in Editor UnityEditor.EditorUtility.SetDirty(env.gameObject); #endif }
public void Render(Vector3 observerPos, float visibleDistance, Vector3[] frustumPlanesNormals, float[] frustumPlanesDistances) { #if DEBUG_BATCHES int batches = 0; int instancesCount = 0; #endif for (int k = 0; k <= cells.lastIndex; k++) { BatchedCell cell = cells.values [k]; if (cell == null) { continue; } if (!GeometryUtilityNonAlloc.TestPlanesAABB(frustumPlanesNormals, frustumPlanesDistances, ref cell.boundsMin, ref cell.boundsMax)) { continue; } if (cell.rebuild) { if (!Application.isPlaying || Time.frameCount - cell.lastRebuildFrame > 10) { cell.lastRebuildFrame = Time.frameCount; RebuildCellRenderingLists(cell, observerPos, visibleDistance); cell.rebuild = false; } } for (int j = 0; j <= cell.batchedMeshes.lastIndex; j++) { BatchedMesh batchedMesh = cell.batchedMeshes.values [j]; if (batchedMesh == null) { continue; } VoxelDefinition vd = batchedMesh.voxelDefinition; Mesh mesh = vd.mesh; ShadowCastingMode shadowCastingMode = (vd.castShadows && env.enableShadows) ? ShadowCastingMode.On : ShadowCastingMode.Off; bool receiveShadows = vd.receiveShadows && env.enableShadows; for (int i = 0; i < batchedMesh.batches.count; i++) { Batch batch = batchedMesh.batches.values [i]; if (GeometryUtilityNonAlloc.TestPlanesAABB(frustumPlanesNormals, frustumPlanesDistances, ref batch.boundsMin, ref batch.boundsMax)) { Graphics.DrawMeshInstancedIndirect(mesh, 0, batch.instancedMaterial, batch.bounds, batch.argsBuffer, 0, null, shadowCastingMode, receiveShadows, env.layerVoxels); #if DEBUG_BATCHES batches++; instancesCount += batch.instancesCount; #endif } } } } #if DEBUG_BATCHES Debug.Log("Batches: " + batches + " Instances: " + instancesCount); #endif }