diff --git a/Source/Editor/Viewport/Previews/AudioClipPreview.cs b/Source/Editor/Viewport/Previews/AudioClipPreview.cs index 9428a2e3f..e039be4e5 100644 --- a/Source/Editor/Viewport/Previews/AudioClipPreview.cs +++ b/Source/Editor/Viewport/Previews/AudioClipPreview.cs @@ -145,18 +145,31 @@ namespace FlaxEditor.Viewport.Previews const uint maxSamplesPerIndex = 64; uint samplesPerIndexDiff = Math.Max(1, samplesPerIndex / Math.Min(samplesPerIndex, maxSamplesPerIndex)); + // Calculate the clip range in the view to optimize drawing (eg. if only part fo the clip is visible) + Render2D.PeekClip(out var globalClipping); + Render2D.PeekTransform(out var globalTransform); + var globalRect = new Rectangle(globalTransform.M31, globalTransform.M32, width * globalTransform.M11, height * globalTransform.M22); + var globalMask = Rectangle.Shared(globalClipping, globalRect); + var globalTransformInv = Matrix3x3.Invert(globalTransform); + var localRect = Rectangle.FromPoints(Matrix3x3.Transform2D(globalMask.UpperLeft, globalTransformInv), Matrix3x3.Transform2D(globalMask.BottomRight, globalTransformInv)); + var localRectMin = localRect.UpperLeft; + var localRectMax = localRect.BottomRight; + // Render each clip separately for (uint clipIndex = 0; clipIndex < Mathf.CeilToInt(clipsInView); clipIndex++) { - var clipX = clipWidth * clipIndex; - var clipRight = Mathf.Min(width, clipX + clipWidth); + var clipStart = clipWidth * clipIndex; + var clipEnd = clipStart + clipWidth; + var xStart = Mathf.Max(clipStart, localRectMin.X); + var xEnd = Mathf.Min(Mathf.Min(width, clipEnd), localRectMax.X); + var samplesOffset = (uint)((xStart - clipStart) * samplesPerIndex); // Render every audio channel separately for (uint channelIndex = 0; channelIndex < info.NumChannels; channelIndex++) { - uint currentSample = channelIndex; + uint currentSample = channelIndex + samplesOffset; float yCenter = Y + ((2 * channelIndex) + 1) * height / (2.0f * info.NumChannels); - for (float pixelX = clipX; pixelX < clipRight; pixelX++) + for (float pixelX = xStart; pixelX < xEnd; pixelX++) { float samplesSum = 0; int samplesInPixel = 0;