Home Game Development How do I convert this Gaussian Splat Unity C# script to Godot C# or GDScript?

How do I convert this Gaussian Splat Unity C# script to Godot C# or GDScript?

0
How do I convert this Gaussian Splat Unity C# script to Godot C# or GDScript?

[ad_1]

How would I convert even a piece of the next code, which was taken from this GitHub Supply: https://github.com/aras-p/UnityGaussianSplatting/tree/important to Godot C# or GDScript?
I am making an attempt to make a Godot model of this venture, however I do not know easy methods to translate from one to the opposite. Even only a couple vital and related traces could be very helpful. Thanks.

utilizing System;
utilizing System.Collections.Generic;
utilizing Unity.Collections.LowLevel.Unsafe;
utilizing Unity.Arithmetic;
utilizing Unity.Profiling;
utilizing Unity.Profiling.LowLevel;
utilizing UnityEngine;
utilizing UnityEngine.Experimental.Rendering;
utilizing UnityEngine.Rendering;

class GaussianSplatRenderSystem
{
    static ProfilerMarker s_ProfDraw = new(ProfilerCategory.Render, "GaussianSplat.Draw", MarkerFlags.SampleGPU);
    inner static ProfilerMarker s_ProfCompose = new(ProfilerCategory.Render, "GaussianSplat.Compose", MarkerFlags.SampleGPU);
    
    public static GaussianSplatRenderSystem occasion => ms_Instance ??= new GaussianSplatRenderSystem();
    static GaussianSplatRenderSystem ms_Instance;
    
    readonly Dictionary<GaussianSplatRenderer, MaterialPropertyBlock> m_Splats = new();
    readonly HashSet<Digicam> m_CameraCommandBuffersDone = new();
    readonly Checklist<(GaussianSplatRenderer, MaterialPropertyBlock)> m_ActiveSplats = new();
    
    CommandBuffer m_CommandBuffer;

    public void RegisterSplat(GaussianSplatRenderer r)
    {
        if (m_Splats.Rely == 0)
        {
            if (GraphicsSettings.currentRenderPipeline == null)
                Digicam.onPreCull += OnPreCullCamera;
        }

        m_Splats.Add(r, new MaterialPropertyBlock());
    }

    public void UnregisterSplat(GaussianSplatRenderer r)
    {
        if (!m_Splats.ContainsKey(r))
            return;
        m_Splats.Take away(r);
        if (m_Splats.Rely == 0)
        {
            if (m_CameraCommandBuffersDone != null)
            {
                if (m_CommandBuffer != null)
                {
                    foreach (var cam in m_CameraCommandBuffersDone)
                    {
                        if (cam)
                            cam.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, m_CommandBuffer);
                    }
                }
                m_CameraCommandBuffersDone.Clear();
            }
            
            m_ActiveSplats.Clear();
            m_CommandBuffer?.Dispose();
            m_CommandBuffer = null;
            Digicam.onPreCull -= OnPreCullCamera;
        }
    }

    public bool GatherSplatsForCamera(Digicam cam)
    {
        if (cam.cameraType == CameraType.Preview)
            return false;
        // collect all energetic & legitimate splat objects
        m_ActiveSplats.Clear();
        foreach (var kvp in m_Splats)
         !gs.HasValidRenderSetup)
                proceed;
            m_ActiveSplats.Add((kvp.Key, kvp.Worth));
        
        if (m_ActiveSplats.Rely == 0)
            return false;

        // type them by depth from digital camera
        var camTr = cam.rework;
        m_ActiveSplats.Kind((a, b) =>
        {
            var trA = a.Item1.rework;
            var trB = b.Item1.rework;
            var posA = camTr.InverseTransformPoint(trA.place);
            var posB = camTr.InverseTransformPoint(trB.place);
            return posA.z.CompareTo(posB.z);
        });

        return true;
    }

    public Materials SortAndRenderSplats(Digicam cam, CommandBuffer cmb)
    {
        Materials matComposite = null;
        foreach (var kvp in m_ActiveSplats)
        {
            var gs = kvp.Item1;
            matComposite = gs.m_MatComposite;
            var mpb = kvp.Item2;

            // type
            var matrix = gs.rework.localToWorldMatrix;
            if (gs.m_FrameCounter % gs.m_SortNthFrame == 0)
                gs.SortPoints(cmb, cam, matrix);
            ++gs.m_FrameCounter;

            // cache view
            kvp.Item2.Clear();
            Materials displayMat = gs.m_RenderMode change
            {
                GaussianSplatRenderer.RenderMode.DebugPoints => gs.m_MatDebugPoints,
                GaussianSplatRenderer.RenderMode.DebugPointIndices => gs.m_MatDebugPoints,
                GaussianSplatRenderer.RenderMode.DebugBoxes => gs.m_MatDebugBoxes,
                GaussianSplatRenderer.RenderMode.DebugChunkBounds => gs.m_MatDebugBoxes,
                _ => gs.m_MatSplats
            };
            if (displayMat == null)
                proceed;

            gs.SetAssetDataOnMaterial(mpb);
            mpb.SetBuffer("_SplatChunks", gs.m_GpuChunks);
            mpb.SetInteger("_SplatChunkCount", gs.m_GpuChunks.depend);

            mpb.SetBuffer("_SplatViewData", gs.m_GpuView);

            mpb.SetBuffer("_OrderBuffer", gs.m_GpuSortKeys);
            mpb.SetFloat("_SplatScale", gs.m_SplatScale);
            mpb.SetFloat("_SplatOpacityScale", gs.m_OpacityScale);
            mpb.SetFloat("_SplatSize", gs.m_PointDisplaySize);
            mpb.SetInteger("_SplatCount", gs.asset.m_SplatCount);
            mpb.SetInteger("_SHOrder", gs.m_SHOrder);
            mpb.SetInteger("_DisplayIndex", gs.m_RenderMode == GaussianSplatRenderer.RenderMode.DebugPointIndices ? 1 : 0);
            mpb.SetInteger("_DisplayChunks", gs.m_RenderMode == GaussianSplatRenderer.RenderMode.DebugChunkBounds ? 1 : 0);

            gs.CalcViewData(cmb, cam, matrix);

            // draw
            int indexCount = 6;
            int instanceCount = gs.asset.m_SplatCount;
            MeshTopology topology = MeshTopology.Triangles;
            if (gs.m_RenderMode is GaussianSplatRenderer.RenderMode.DebugBoxes or GaussianSplatRenderer.RenderMode.DebugChunkBounds)
                indexCount = 36;
            if (gs.m_RenderMode == GaussianSplatRenderer.RenderMode.DebugChunkBounds)
                instanceCount = gs.m_GpuChunks.depend;

            cmb.BeginSample(s_ProfDraw);
            cmb.DrawProcedural(gs.m_GpuIndexBuffer, matrix, displayMat, 0, topology, indexCount, instanceCount, mpb);
            cmb.EndSample(s_ProfDraw);
        }
        return matComposite;
    }

    //...
    //500 traces
    //...

    public void EditUpdateSelection(Vector2 rectMin, Vector2 rectMax, Digicam cam)
    {
        if (!EnsureSelectionBuffers()) return;

        Graphics.CopyBuffer(m_GpuSplatSelectedInitBuffer, m_GpuSplatSelectedBuffer);
        
        var tr = rework;
        Matrix4x4 matView = cam.worldToCameraMatrix;
        Matrix4x4 matProj = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true);
        Matrix4x4 matO2W = tr.localToWorldMatrix;
        Matrix4x4 matW2O = tr.worldToLocalMatrix;
        int screenW = cam.pixelWidth, screenH = cam.pixelHeight;
        Vector4 screenPar = new Vector4(screenW, screenH, 0, 0);
        Vector4 camPos = cam.rework.place;

        var cmb = new CommandBuffer { title = "SplatSelectionUpdate" };
        SetAssetDataOnCS(cmb, m_CSSplatUtilities, (int)KernelIndices.SelectionUpdate);
        cmb.SetComputeIntParam(m_CSSplatUtilities, "_SplatCount", m_Asset.m_SplatCount);
        cmb.SetComputeBufferParam(m_CSSplatUtilities, (int)KernelIndices.SelectionUpdate, "_SplatChunks", m_GpuChunks);

        cmb.SetComputeMatrixParam(m_CSSplatUtilities, "_MatrixVP", matProj * matView);
        cmb.SetComputeMatrixParam(m_CSSplatUtilities, "_MatrixMV", matView * matO2W);
        cmb.SetComputeMatrixParam(m_CSSplatUtilities, "_MatrixP", matProj);
        cmb.SetComputeMatrixParam(m_CSSplatUtilities, "_MatrixObjectToWorld", matO2W);
        cmb.SetComputeMatrixParam(m_CSSplatUtilities, "_MatrixWorldToObject", matW2O);

        cmb.SetComputeVectorParam(m_CSSplatUtilities, "_VecScreenParams", screenPar);
        cmb.SetComputeVectorParam(m_CSSplatUtilities, "_VecWorldSpaceCameraPos", camPos);
        
        cmb.SetComputeVectorParam(m_CSSplatUtilities, "_SelectionRect", new Vector4(rectMin.x, rectMax.y, rectMax.x, rectMin.y));

        m_CSSplatUtilities.GetKernelThreadGroupSizes((int)KernelIndices.SelectionUpdate, out uint gsX, out _, out _);
        cmb.DispatchCompute(m_CSSplatUtilities, (int)KernelIndices.SelectionUpdate, (m_Asset.m_SplatCount + (int)gsX - 1)/(int)gsX, 1, 1);
        Graphics.ExecuteCommandBuffer(cmb);
        cmb.Dispose();
        UpdateEditCountsAndBounds();
    }

    public void EditDeleteSelected()
    {
        if (!EnsureSelectionBuffers()) return;
        UnionGraphicsBuffers(m_GpuSplatDeletedBuffer, m_GpuSplatSelectedBuffer);
        EditDeselectAll();
        UpdateEditCountsAndBounds();
        if (editDeletedSplats != 0)
            editModified = true;
    }

    public void EditSelectAll()
    {
        if (!EnsureSelectionBuffers()) return;
        ClearGraphicsBuffer(m_GpuSplatSelectedBuffer, ~0u);
        UpdateEditCountsAndBounds();
    }

    public void EditDeselectAll()
    {
        if (!EnsureSelectionBuffers()) return;
        ClearGraphicsBuffer(m_GpuSplatSelectedBuffer);
        UpdateEditCountsAndBounds();
    }

    public void EditInvertSelection()
    {
        if (!EnsureSelectionBuffers()) return;
        InvertGraphicsBuffer(m_GpuSplatSelectedBuffer);
        UpdateEditCountsAndBounds();
    }

    public bool EditExportData(GraphicsBuffer dstData)
    {
        if (!EnsureSelectionBuffers()) return false;
        
        var cmb = new CommandBuffer { title = "SplatExportData" };
        SetAssetDataOnCS(cmb, m_CSSplatUtilities, (int)KernelIndices.ExportData);
        cmb.SetComputeIntParam(m_CSSplatUtilities, "_SplatCount", m_Asset.m_SplatCount);
        cmb.SetComputeBufferParam(m_CSSplatUtilities, (int)KernelIndices.ExportData, "_SplatChunks", m_GpuChunks);
        cmb.SetComputeBufferParam(m_CSSplatUtilities, (int)KernelIndices.ExportData, "_ExportBuffer", dstData);

        m_CSSplatUtilities.GetKernelThreadGroupSizes((int)KernelIndices.ExportData, out uint gsX, out _, out _);
        cmb.DispatchCompute(m_CSSplatUtilities, (int)KernelIndices.ExportData, (m_Asset.m_SplatCount + (int)gsX - 1)/(int)gsX, 1, 1);
        Graphics.ExecuteCommandBuffer(cmb);
        cmb.Dispose();
        return true;
    }

    public GraphicsBuffer gpuSplatDeletedBuffer => m_GpuSplatDeletedBuffer;
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here