/// <inheritdoc/>
 public override void Reset()
 {
     base.Reset();
     // Initialize the helper methods.
     _helperULR = GeneralToolkit.GetOrAddComponent <Helper_ULR>(gameObject);
     _helperULR.Reset();
 }
Exemple #2
0
        /// <summary>
        /// Updates the values of the blending materials used by the helper ULR classes of objects rendering to this camera.
        /// </summary>
        public void UpdateValuesOnPreRender()
        {
            // Compute the increment and starting index based on whether the attached camera is stereo.
            int increment        = _isStereo ? 2 : 1;
            int renderedObjIndex = (_isStereo && _attachedCam.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right) ? 1 : 0;

            // Loop over all the helper ULR classes.
            while (renderedObjIndex < _helperULRList.Count)
            {
                // If this helper ULR class is null, remove it from the list and skip.
                Helper_ULR helperULR = _helperULRList[renderedObjIndex];
                if (helperULR == null)
                {
                    _helperULRList.RemoveAt(renderedObjIndex);
                }
                else
                {
                    // Otherwise, if its blending material is also not null, update this material's properties.
                    Material currentBlendingMat = helperULR.currentBlendingMaterial;
                    if (currentBlendingMat != null)
                    {
                        // Compute the number of vertices to process this frame, based on the current framerate.
                        Vector3 vertexFrontIndexAndCountPerFrame = _vertexFrontIndexAndCountPerFrameList[renderedObjIndex];
                        int     blendFieldFrontVertexIndex       = (int)vertexFrontIndexAndCountPerFrame.x;
                        int     blendFieldVertexCountPerFrame    = (int)vertexFrontIndexAndCountPerFrame.y;
                        int     totalVertexCount = (int)vertexFrontIndexAndCountPerFrame.z;
                        float   minVertexCountForTotalRendering = totalVertexCount * 1f / helperULR.maxFrameCountForProcessing;
                        blendFieldFrontVertexIndex = (blendFieldFrontVertexIndex + blendFieldVertexCountPerFrame) % totalVertexCount;
                        float blendFieldFrameRatio = 1f / (helperULR.targetFramerate * Time.smoothDeltaTime);
                        blendFieldVertexCountPerFrame = Mathf.RoundToInt(Mathf.Max(blendFieldFrameRatio * blendFieldVertexCountPerFrame, minVertexCountForTotalRendering));
                        blendFieldVertexCountPerFrame = Mathf.Clamp(blendFieldVertexCountPerFrame, 1, totalVertexCount);
                        int     nextBlendFieldFrontVertexIndex = (blendFieldFrontVertexIndex + blendFieldVertexCountPerFrame) % totalVertexCount;
                        Vector2 blendFieldComputationParams    = new Vector2(blendFieldFrontVertexIndex, nextBlendFieldFrontVertexIndex);
                        _vertexFrontIndexAndCountPerFrameList[renderedObjIndex] = new Vector3(blendFieldFrontVertexIndex, blendFieldVertexCountPerFrame, totalVertexCount);
                        // Update the blending material's properties.
                        Graphics.ClearRandomWriteTargets();
                        Graphics.SetRandomWriteTarget(1, _vertexCamWeightsBufferList[renderedObjIndex]);
                        Graphics.SetRandomWriteTarget(2, _vertexCamIndicesBufferList[renderedObjIndex]);
                        currentBlendingMat.SetBuffer(_shaderNameVertexCamWeightsBuffer, _vertexCamWeightsBufferList[renderedObjIndex]);
                        currentBlendingMat.SetBuffer(_shaderNameVertexCamIndicesBuffer, _vertexCamIndicesBufferList[renderedObjIndex]);
                        currentBlendingMat.SetVector(_shaderNameBlendFieldComputationParams, blendFieldComputationParams);
                        currentBlendingMat.SetInt(_shaderNameIsSceneViewCamera, _isSceneViewCamera ? 1 : 0);
                    }
                    renderedObjIndex += increment;
                }
            }
        }
 /// <summary>
 /// Adds an object rendered with ULR to the list of objects currently rendering to this camera.
 /// </summary>
 /// <param name="helperULR"></param> The helper ULR method attached to the object rendering to this camera.
 /// <param name="totalVertexCount"></param> The total vertex count of the mesh attached to the object rendering to this camera.
 public void AddRenderedObject(Helper_ULR helperULR, int totalVertexCount)
 {
     if (!_initialized)
     {
         Initialize();
     }
     if (_initialized)
     {
         int addCount = _isStereo ? 2 : 1;
         for (int addIter = 0; addIter < addCount; addIter++)
         {
             _helperULRList.Add(helperULR);
             _vertexCamWeightsBufferList.Add(new ComputeBuffer(totalVertexCount, 4 * sizeof(float)));
             _vertexCamIndicesBufferList.Add(new ComputeBuffer(totalVertexCount, 4 * sizeof(uint)));
             _vertexFrontIndexAndCountPerFrameList.Add(new Vector3(0, totalVertexCount, totalVertexCount));
         }
         OnPreRender();
     }
 }