unitycoder / UnityPointCloudViewer

Point Cloud Viewer and Tools for Unity
https://assetstore.unity.com/packages/tools/utilities/point-cloud-viewer-and-tools-16019?aid=1101lGti
128 stars 15 forks source link

URP Support #126

Open unitycoder opened 2 years ago

unitycoder commented 2 years ago

using Unity 2019.4.25f1 + URP 7.7.1

temporary fix for URP+WorldSpace Canvas issue (point cloud get "stuck" inside canvas) You dont need to do this, if you don't have world space canvas. This is only for V1 (.bin) and V2 (.ucpc) formats.

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class CustomRenderPassFeature : ScriptableRendererFeature
{
    class CustomRenderPass : ScriptableRenderPass
    {
        CommandBuffer commandBuffer;

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
            commandBuffer = new CommandBuffer { name = "PointCloudViewer" };
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            if (unitycodercom_PointCloudBinaryViewer.PointCloudViewerDX11.instance == null) return;
            commandBuffer.Clear();
            commandBuffer.DrawProcedural(Matrix4x4.identity, unitycodercom_PointCloudBinaryViewer.PointCloudViewerDX11.instance.cloudMaterial, 0, MeshTopology.Points, unitycodercom_PointCloudBinaryViewer.PointCloudViewerDX11.instance.points.Length);
            context.ExecuteCommandBuffer(commandBuffer);
        }

        public override void FrameCleanup(CommandBuffer cmd)
        {
        }
    }

    CustomRenderPass m_ScriptablePass;
    public override void Create()
    {
        m_ScriptablePass = new CustomRenderPass();
        m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(m_ScriptablePass);
    }
}

Using Unity 2020.3.21f1 + URP 10.6.0 + V3 (.pcroot) viewer

CustomRenderPassFeature .cs:

using unitycodercom_PointCloudBinaryViewer;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class CustomRenderPassFeature : ScriptableRendererFeature
{
    class CustomRenderPass : ScriptableRenderPass
    {
        CommandBuffer commandBuffer;

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
            commandBuffer = new CommandBuffer { name = "PointCloudViewer" };
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            if (PointCloudViewerTilesDX11.instance == null) return;

            commandBuffer.Clear();
            for (int i = 0, len = PointCloudViewerTilesDX11.instance.tilesCount; i < len; i++)
            {
                if (PointCloudViewerTilesDX11.instance.tiles[i].isReady == false || PointCloudViewerTilesDX11.instance.tiles[i].isLoading == true || PointCloudViewerTilesDX11.instance.tiles[i].visiblePoints == 0) continue;
                commandBuffer.DrawProcedural(Matrix4x4.identity, PointCloudViewerTilesDX11.instance.tiles[i].material, 0, MeshTopology.Points, PointCloudViewerTilesDX11.instance.tiles[i].visiblePoints);
            }
            context.ExecuteCommandBuffer(commandBuffer);
        }

        public override void FrameCleanup(CommandBuffer cmd)
        {
        }
    }

    CustomRenderPass m_ScriptablePass;
    public override void Create()
    {
        m_ScriptablePass = new CustomRenderPass();
        m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(m_ScriptablePass);
    }
}
Rickmc3280 commented 2 years ago

Unable to test due to using PointCloudViewerTilesDX11.CS instead of non-tiled version. Question though regarding the CustomRenderPassFeature Script.

Where do you load the data into the command buffer for it to "commandBuffer.DrawProcedural(); I dont see anywhere for the data to be loaded, let alone to be drawn.

unitycoder commented 2 years ago

ah ok, ill need to check on the tiles viewer. (le me know your unity/urp version also) that custompass feature, it uses data from my viewer, so its already loaded (and assigned to that cloudmaterial) and commandBuffer.DrawProcedural() is the method to actually draw.

Rickmc3280 commented 2 years ago

Interesting. So the data is stored in the material? Since I do a lot procedurally, it always revolves around uploading it to Mesh.Vertices, Mesh.Colors etc. Guess it is a different way of thinking

URP 12.1.1/ Unity 2021.2.XX

unitycoder commented 2 years ago

ok then that script above might not work, ill check with that version.

unitycoder commented 2 years ago

Now first message contains steps for V3 format also. @Rickmc3280