private void SetupBackgroundBlit()
    {
        var mainCamera = GetComponent <Camera>();
        //IARCamera arCamera = GetComponent<ARCamera>().Camera;
        //Debug.Assert (arCamera!=null);

        int renderTextureWidth  = targetRenderTexture.width;
        int renderTextureHeight = targetRenderTexture.height;

        // Clean up any previous command buffer and events hooks
        if (m_blitCommandBuffer != null)
        {
            ARResources.DeregisterChangeCallback(SetupBackgroundBlit);
            mainCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer);
            mainCamera.RemoveCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer);
        }

        // Create the blit command buffer
        m_blitCommandBuffer = new CommandBuffer();
        m_blitCommandBuffer.GetTemporaryRT(workingRenderTextureID, renderTextureWidth, renderTextureHeight, 0, FilterMode.Bilinear);
        m_blitCommandBuffer.name = "Get ARBackground";

        var blitMat = Resources.Load <Material>("Materials/ARCoreBlit");
        var texture = this.GetComponentInChildren <BackgroundPlaneBehaviour>().Material.mainTexture;

        m_blitCommandBuffer.Blit(texture, workingRenderTextureID, blitMat); //this process may not be necesary

        if (shouldBlurRenderTexture)
        {
            // The optional blur step
            fastBlur = GetComponent <FastBlur>();
            Debug.Assert(fastBlur);
            fastBlur.CreateBlurCommandBuffer(m_blitCommandBuffer, workingRenderTextureID, renderTextureWidth, renderTextureHeight);
        }

        // Copy over to the target texture. Use a blend texture to stop light flickering.
        m_blitCommandBuffer.Blit(workingRenderTextureID, targetRenderTexture, blendMaterial);

        // Run the command buffer just before opaque rendering
        mainCamera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer);

        // Cleanup the temp render textures
        m_releaseCommandBuffer      = new CommandBuffer();
        m_releaseCommandBuffer.name = "Release ARBackground";
        m_releaseCommandBuffer.ReleaseTemporaryRT(workingRenderTextureID);
        mainCamera.AddCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer);

        isCapturing = true;

        // Rebuild the command buffer if anything changes
        ARResources.RegisterChangeCallback(SetupBackgroundBlit);
    }
Ejemplo n.º 2
0
    private void SetupBackgroundBlit()
    {
        Debug.Assert(Camera.main != null, "You must have a Camera tagged as MainCamera in your scene.");
        Camera mainCamera = Camera.main;

        Debug.Log("Main Camera is on GameObject - " + mainCamera.gameObject.name);

        IARCamera arCamera = GetComponent <ARCamera>().Camera;

        Debug.Assert(arCamera != null);

        int renderTextureWidth  = targetRenderTexture.width;
        int renderTextureHeight = targetRenderTexture.height;

        // Clean up any previous command buffer and events hooks
        if (m_blitCommandBuffer != null)
        {
            mainCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer);
            mainCamera.RemoveCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer);
        }

        // Create the blit command buffer
        m_blitCommandBuffer = new CommandBuffer();
        m_blitCommandBuffer.GetTemporaryRT(workingRenderTextureID, renderTextureWidth, renderTextureHeight, 0, FilterMode.Bilinear);
        m_blitCommandBuffer.name = "Get ARBackground";

        arCamera.BlitCameraTexture(m_blitCommandBuffer, workingRenderTextureID);

        if (shouldBlurRenderTexture)
        {
            // The optional blur step
            fastBlur = GetComponent <FastBlur>();
            Debug.Assert(fastBlur);
            fastBlur.CreateBlurCommandBuffer(m_blitCommandBuffer, workingRenderTextureID, renderTextureWidth, renderTextureHeight);
        }

        // Copy over to the target texture. Use a blend texture to stop light flickering.
        m_blitCommandBuffer.Blit(workingRenderTextureID, targetRenderTexture);

        // Run the command buffer just before opaque rendering
        mainCamera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer);

        // Cleanup the temp render textures
        m_releaseCommandBuffer      = new CommandBuffer();
        m_releaseCommandBuffer.name = "Release ARBackground";
        m_releaseCommandBuffer.ReleaseTemporaryRT(workingRenderTextureID);
        mainCamera.AddCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer);

        isCapturing = true;
    }