Exemplo n.º 1
0
        /// <summary>
        /// 检测回读请求状态
        /// </summary>
        private void UpdateRequest()
        {
            UpdateStat.BeginFrame();

            bool complete = false;

            while (m_ReadbackRequests.Count > 0)
            {
                var req = m_ReadbackRequests.Peek();

                if (req.hasError)
                {
                    ReadbackStat.EndRequest(req, false);
                    m_ReadbackRequests.Dequeue();
                }
                else if (req.done)
                {
                    // 更新数据并分发事件
                    m_ReadbackTexture.GetRawTextureData <Color32>().CopyFrom(req.GetData <Color32>());
                    complete = true;

                    ReadbackStat.EndRequest(req, true);
                    m_ReadbackRequests.Dequeue();
                }
                else
                {
                    break;
                }
            }

            if (complete)
            {
                UpdateStat.EndFrame();

                OnFeedbackReadComplete?.Invoke(m_ReadbackTexture);

                UpdateDebugTexture();
            }
        }
Exemplo n.º 2
0
    private void OnPreCull()
    {
        //CHECK FOR REQUESTS LIST AND UPDATE TEXTURE FROM LAST FRAME
        bool complete = false;

        if (TargetTexture != null)
        {
            var width  = TargetTexture.width;
            var height = TargetTexture.height;

            if (m_ReadbackTexture == null || m_ReadbackTexture.width != width || m_ReadbackTexture.height != height)
            {
                m_ReadbackTexture            = new Texture2D(width, height, TextureFormat.RGBA32, false);
                m_ReadbackTexture.filterMode = FilterMode.Point;
                m_ReadbackTexture.wrapMode   = TextureWrapMode.Clamp;
            }
            if (!pageTable.InCoroutine)
            {
                if (SystemInfo.supportsAsyncGPUReadback)
                {
                    while (m_ReadbackRequests.Count > 0)
                    {
                        var req = m_ReadbackRequests.Peek();

                        if (req.hasError)
                        {
                            m_ReadbackRequests.Dequeue();
                        }
                        else if (req.done)
                        {
                            m_ReadbackTexture.GetRawTextureData <Color32>().CopyFrom(req.GetData <Color32>());
                            complete = true;
                            m_ReadbackRequests.Dequeue();
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (complete)
                    {
                        OnFeedbackReadComplete?.Invoke(m_ReadbackTexture);
                    }
                }
                else
                {
                    RenderTexture.active = TargetTexture;
                    Rect rectReadPicture = new Rect(0, 0, width, height);
                    m_ReadbackTexture.ReadPixels(rectReadPicture, 0, 0);
                    OnFeedbackReadComplete?.Invoke(m_ReadbackTexture);
                }
            }
        }


        //NEW FRAME CONFIGURATION

        var mainCamera = Camera.main;

        if (mainCamera == null)
        {
            return;
        }


        if (TargetTexture == null)
        {
            TargetTexture                  = new RenderTexture(mainCamera.pixelWidth / 12, mainCamera.pixelHeight / 12, 24);
            TargetTexture.useMipMap        = false;
            TargetTexture.wrapMode         = TextureWrapMode.Clamp;
            TargetTexture.filterMode       = FilterMode.Point;
            m_FeedbackCamera.targetTexture = TargetTexture;


            Vector3 terrainSize      = DemoTerrain.terrainData.size;
            Vector3 terrainTransform = DemoTerrain.GetPosition();

            Shader.SetGlobalVector("_TERRAINPOS", terrainTransform);
            Shader.SetGlobalVector("_TERRAINSIZE", terrainSize);
            float feedbackBias = (float)Mathf.Floor(Mathf.Log((float)TargetTexture.width / (float)Mathf.Max((float)mainCamera.pixelWidth, (float)mainCamera.pixelHeight), 2.0f));
            Shader.SetGlobalFloat("_FEEDBACKBIAS", feedbackBias);
        }
        m_FeedbackCamera.targetTexture = TargetTexture;


        m_FeedbackCamera.transform.position = mainCamera.transform.position;
        m_FeedbackCamera.transform.rotation = mainCamera.transform.rotation;
        m_FeedbackCamera.cullingMask        = mainCamera.cullingMask;
        m_FeedbackCamera.projectionMatrix   = mainCamera.projectionMatrix;
        m_FeedbackCamera.fieldOfView        = mainCamera.fieldOfView;
        m_FeedbackCamera.nearClipPlane      = mainCamera.nearClipPlane;
        m_FeedbackCamera.farClipPlane       = mainCamera.farClipPlane;
        m_FeedbackCamera.SetReplacementShader(m_FeedbackShader, "VirtualTextureType");
    }