bool Render()
        {
            if (!m_initialized || !m_renderer.valid)
            {
                return(false);
            }

            bool firstInstance = s_renderCount == 0;
            bool lastInstance  = ++s_renderCount == s_instanceCount;

            if (s_renderCount == s_instanceCount)
            {
                s_renderCount = 0;
            }

            m_issueFrameEnd = lastInstance;

            var cam = GetComponent <Camera>();

            // setup RenderTexture
            if (m_generateRenderTexture)
            {
                var resolution = new Vector2Int(cam.pixelWidth, cam.pixelHeight);
                var format     = m_outputType == OutputType.Image ? RenderTextureFormat.RHalf : RenderTextureFormat.RInt;

                if (m_outputTexture != null &&
                    (m_outputTexture.width != resolution.x || m_outputTexture.height != resolution.y || m_outputTexture.format != format))
                {
                    // resolution/format has changed. release existing RenderTexture
#if UNITY_EDITOR
                    if (!AssetDatabase.Contains(m_outputTexture))
#endif
                    {
                        DestroyImmediate(m_outputTexture);
                    }
                    m_outputTexture = null;
                }
                if (m_outputTexture == null)
                {
                    m_outputTexture      = new RenderTexture(resolution.x, resolution.y, 0, format);
                    m_outputTexture.name = "RaytracedHardShadow";
                    m_outputTexture.enableRandomWrite = true; // enable unordered access
                    m_outputTexture.Create();
                    if (m_assignGlobalTexture)
                    {
                        Shader.SetGlobalTexture(m_globalTextureName, m_outputTexture);
                    }
                }
            }

            // setup renderer
            bool succeeded = true;
            if (m_outputTexture != null)
            {
                rthsRenderFlag flags = 0;
                if (m_cullBackFaces)
                {
                    flags |= rthsRenderFlag.CullBackFaces;
                }
                if (m_flipCasterFaces)
                {
                    flags |= rthsRenderFlag.FlipCasterFaces;
                }
                if (m_ignoreSelfShadow)
                {
                    flags |= rthsRenderFlag.IgnoreSelfShadow;
                }
                if (m_keepSelfDropShadow)
                {
                    flags |= rthsRenderFlag.KeepSelfDropShadow;
                }
                if (m_GPUSkinning)
                {
                    flags |= rthsRenderFlag.GPUSkinning;
                }
                if (m_adaptiveSampling)
                {
                    flags |= rthsRenderFlag.AdaptiveSampling;
                }
                if (m_antialiasing)
                {
                    flags |= rthsRenderFlag.Antialiasing;
                }
                if (m_clampBlendshapeWeights)
                {
                    flags |= rthsRenderFlag.ClampBlendShapeWights;
                }

                m_renderer.BeginScene();
                try
                {
                    m_renderer.SetRaytraceFlags(flags);
                    m_renderer.SetShadowRayOffset(m_ignoreSelfShadow ? 0.0f : m_shadowRayOffset);
                    m_renderer.SetSelfShadowThreshold(m_selfShadowThreshold);
                    m_renderer.SetRenderTarget(GetRenderTargetData(m_outputTexture));
                    m_renderer.SetCamera(cam, m_useCameraCullingMask);
                    EnumerateLights(
                        l => { m_renderer.AddLight(l, m_useLightCullingMask); },
                        scl => { m_renderer.AddLight(scl); }
                        );
                    EnumerateMeshRenderers(
                        (mr) => { m_renderer.AddMesh(GetMeshInstanceData(mr)); },
                        (smr) => { m_renderer.AddMesh(GetMeshInstanceData(smr)); }
                        );
                }
                catch (Exception e)
                {
                    Debug.LogError(e);
                    succeeded = false;
                }
                m_renderer.EndScene();
            }

            // issue GPU commands
            if (firstInstance)
            {
                m_renderer.IssueMarkFrameBegin();
            }
            if (succeeded)
            {
                m_renderer.IssueRender();
                m_issueFinish = true;
            }
            if (m_assignGlobalTexture)
            {
                Shader.SetGlobalTexture(m_globalTextureName, m_outputTexture);
            }

            return(succeeded);
        }
Exemplo n.º 2
0
        void LateUpdate()
        {
            if (!m_renderer)
            {
                return;
            }

            var cam = GetComponent <Camera>();

            if (cam == null)
            {
                return;
            }

            if (m_generateRenderTexture)
            {
                var resolution = new Vector2Int(cam.pixelWidth, cam.pixelHeight);
                if (m_outputTexture != null && (m_outputTexture.width != resolution.x || m_outputTexture.height != resolution.y))
                {
                    // resolution was changed. release existing RenderTexture
#if UNITY_EDITOR
                    if (!AssetDatabase.Contains(m_outputTexture))
#endif
                    {
                        DestroyImmediate(m_outputTexture);
                    }
                    m_outputTexture = null;
                }
                if (m_outputTexture == null)
                {
                    m_outputTexture      = new RenderTexture(resolution.x, resolution.y, 0, RenderTextureFormat.RHalf);
                    m_outputTexture.name = "RaytracedHardShadow";
                    m_outputTexture.enableRandomWrite = true; // enable unordered access
                    m_outputTexture.Create();
                    if (m_assignGlobalTexture)
                    {
                        Shader.SetGlobalTexture(m_globalTextureName, m_outputTexture);
                    }
                }
            }
            else if (m_outputTexture != null)
            {
                if (m_assignGlobalTexture)
                {
                    Shader.SetGlobalTexture(m_globalTextureName, m_outputTexture);
                }
            }

            if (m_outputTexture == null)
            {
                return;
            }

            {
                rthsRenderFlag flags = 0;
                if (m_cullBackFaces)
                {
                    flags |= rthsRenderFlag.CullBackFaces;
                }
                if (m_flipCasterFaces)
                {
                    flags |= rthsRenderFlag.FlipCasterFaces;
                }
                if (m_ignoreSelfShadow)
                {
                    flags |= rthsRenderFlag.IgnoreSelfShadow;
                }
                if (m_keepSelfDropShadow)
                {
                    flags |= rthsRenderFlag.KeepSelfDropShadow;
                }
                if (m_GPUSkinning)
                {
                    flags |= rthsRenderFlag.GPUSkinning;
                }
                if (m_adaptiveSampling)
                {
                    flags |= rthsRenderFlag.AdaptiveSampling;
                }
                if (m_antialiasing)
                {
                    flags |= rthsRenderFlag.Antialiasing;
                }
                if (m_parallelCommandList)
                {
                    flags |= rthsRenderFlag.ParallelCommandList;
                }
                if (m_clampBlendshapeWeights)
                {
                    flags |= rthsRenderFlag.ClampBlendShapeWights;
                }
                if (m_dbgTimestamp)
                {
                    flags |= rthsRenderFlag.DbgTimestamp;
                }
                if (m_dbgForceUpdateAS)
                {
                    flags |= rthsRenderFlag.DbgForceUpdateAS;
                }

                m_renderer.BeginScene();
                m_renderer.SetRaytraceFlags(flags);
                m_renderer.SetShadowRayOffset(m_ignoreSelfShadow ? 0.0f : m_shadowRayOffset);
                m_renderer.SetSelfShadowThreshold(m_selfShadowThreshold);
                m_renderer.SetRenderTarget(GetRenderTargetData(m_outputTexture));
                m_renderer.SetCamera(cam);
                EnumerateLights(
                    l => { m_renderer.AddLight(l); },
                    scl => { m_renderer.AddLight(scl); }
                    );
                EnumerateMeshRenderers(
                    (mr, rmask, cmask) => { m_renderer.AddGeometry(GetMeshInstanceData(mr), rmask, cmask); },
                    (smr, rmask, cmask) => { m_renderer.AddGeometry(GetMeshInstanceData(smr), rmask, cmask); }
                    );
                m_renderer.EndScene();
            }

            if (++s_updateCount == s_instanceCount)
            {
                // last instance issues render event.
                // all renderers do actual rendering tasks in render thread.
                rthsRenderer.IssueRender();
            }
        }