/// <summary>
        /// Awake is called when the script instance is being loaded.
        /// </summary>
        void Awake()
        {
            // Hold the instance.
            if (s_Instance == null)
            {
                s_Instance = GetComponent <UIParticleOverlayCamera> ();
            }
            // If the instance is duplicated, destroy itself.
            else if (s_Instance != this)
            {
                UnityEngine.Debug.LogWarning("Multiple " + typeof(UIParticleOverlayCamera).Name + " in scene.", this.gameObject);
                enabled = false;
#if UNITY_EDITOR
                if (!Application.isPlaying)
                {
                    DestroyImmediate(gameObject);
                }
                else
#endif
                {
                    Destroy(gameObject);
                }
                return;
            }

            cameraForOvrelay.enabled = false;

            // Singleton has DontDestroy flag.
            if (Application.isPlaying)
            {
                DontDestroyOnLoad(gameObject);
            }
        }
 /// <summary>
 /// This function is called when the MonoBehaviour will be destroyed.
 /// </summary>
 void OnDestroy()
 {
     // Clear instance on destroy.
     if (s_Instance == this)
     {
         s_Instance = null;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// This function is called when the MonoBehaviour will be destroyed.
        /// </summary>
        void OnDestroy()
        {
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
            if (s_InstanceForPrefabMode == this)
            {
                s_InstanceForPrefabMode = null;
            }
#endif

            // Clear instance on destroy.
            if (s_Instance == this)
            {
                s_Instance = null;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Update meshe.
        /// </summary>
        void UpdateMesh()
        {
            if (m_ParticleSystem && lastCount != m_ParticleSystem.particleCount)
            {
                lastCount = m_ParticleSystem.particleCount;
            }
            else
            {
                if (dontManage && gameObject.layer != 16)
                {
                    return;
                }
            }

            try
            {
                Profiler.BeginSample("CheckTrail");
                CheckTrail();
                Profiler.EndSample();

                if (m_ParticleSystem && canvas)
                {
                    // I do not know why, but it worked fine when setting `transform.localPosition.z` to `0.01`. (#34, #39)
                    //{
                    //	Vector3 pos = rectTransform.localPosition;
                    //	if (Mathf.Abs (pos.z) < 0.01f)
                    //	{
                    //		pos.z = 0.01f;
                    //		rectTransform.localPosition = pos;
                    //	}
                    //}

                    var rootCanvas = canvas.rootCanvas;
                    Profiler.BeginSample("Disable ParticleSystemRenderer");
                    if (_renderer && Application.isPlaying)
                    {
                        _renderer.enabled = false;
                    }
                    Profiler.EndSample();

                    Profiler.BeginSample("Make Matrix");
                    ParticleSystem.MainModule main = m_ParticleSystem.main;
                    if (!initCulling)
                    {
                        initCulling       = true;
                        originCullingMode = m_ParticleSystem.main.cullingMode;
                    }
                    if (m_ParticleSystem.main.cullingMode != ParticleSystemCullingMode.AlwaysSimulate)
                    {
                        var mainParticle = m_ParticleSystem.main;
                        mainParticle.cullingMode = ParticleSystemCullingMode.AlwaysSimulate;
                    }
                    //scaleaMatrix = main.scalingMode == ParticleSystemScalingMode.Hierarchy
                    //							   ? Matrix4x4.Scale (scale * Vector3.one)
                    //							   : Matrix4x4.Scale (scale * rootCanvas.transform.localScale);

                    /*
                     * fxc
                     *  不做跟随rootCanvas的缩放
                     */
                    scaleaMatrix = Matrix4x4.Scale(scale * Vector3.one);
                    Matrix4x4 matrix = default(Matrix4x4);
                    switch (main.simulationSpace)
                    {
                    case ParticleSystemSimulationSpace.Local:
                        matrix =
                            //scaleaMatrix
                            //* Matrix4x4.Rotate (rectTransform.rotation).inverse
                            //* Matrix4x4.Scale (rectTransform.lossyScale).inverse;

                            /*
                             * fxc
                             * 1.添加zScale解决z为0的情况
                             * 2.逆矩阵应该先旋转再缩放
                             */
                            scaleaMatrix
                            * Matrix4x4.Scale(rectTransform.lossyScale + zScale).inverse
                            *Matrix4x4.Rotate(rectTransform.rotation).inverse;
                        break;

                    case ParticleSystemSimulationSpace.World:
                        matrix =
                            scaleaMatrix
                            * rectTransform.worldToLocalMatrix;

                        bool    isLocalScaling = main.scalingMode == ParticleSystemScalingMode.Local;
                        Vector3 newPos         = rectTransform.position;
                        Vector3 delta          = (newPos - _oldPos);
                        _oldPos = newPos;

                        if (!Mathf.Approximately(scale, 0) && 0 < delta.sqrMagnitude)
                        {
                            if (isLocalScaling)
                            {
                                var s = rootCanvas.transform.localScale * scale;
                                delta.x *= 1f - 1f / s.x;
                                delta.y *= 1f - 1f / s.y;
                                delta.z *= 1f - 1f / s.z;
                            }
                            else
                            {
                                delta = delta * (1 - 1 / scale);
                            }

                            int count = m_ParticleSystem.particleCount;
                            if (s_Particles.Length < count)
                            {
                                s_Particles = new ParticleSystem.Particle [s_Particles.Length * 2];
                            }

                            m_ParticleSystem.GetParticles(s_Particles);
                            for (int i = 0; i < count; i++)
                            {
                                var p = s_Particles [i];
                                p.position      = p.position + delta;
                                s_Particles [i] = p;
                            }
                            m_ParticleSystem.SetParticles(s_Particles, count);
                        }
                        break;

                    case ParticleSystemSimulationSpace.Custom:
                        break;
                    }
                    Profiler.EndSample();

                    _mesh.Clear();
                    if (0 < m_ParticleSystem.particleCount)
                    {
                        Profiler.BeginSample("Bake Mesh");
                        var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay
                                                        ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas)
                                                        : canvas.worldCamera ?? Camera.main;

                        if (!cam)
                        {
                            return;
                        }
                        if (m_IsTrail)
                        {
                            _renderer.BakeTrailsMesh(_mesh, cam, true);
                        }
                        else
                        {
                            _renderer.BakeMesh(_mesh, cam, true);
                        }
                        Profiler.EndSample();

                        // Apply matrix.
                        Profiler.BeginSample("Apply matrix to position");
                        _mesh.GetVertices(s_Vertices);
                        var count = s_Vertices.Count;
                        for (int i = 0; i < count; i++)
                        {
                            s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]);
                        }
                        _mesh.SetVertices(s_Vertices);
                        s_Vertices.Clear();
                        Profiler.EndSample();
                    }


                    // Set mesh to CanvasRenderer.
                    Profiler.BeginSample("Set mesh and texture to CanvasRenderer");
                    canvasRenderer.SetMesh(_mesh);
                    canvasRenderer.SetTexture(mainTexture);
                    if (originRenderQueue == 0 && _renderer != null && _renderer.sharedMaterial != null)
                    {
                        originRenderQueue = _renderer.sharedMaterial.renderQueue;
//#if UNITY_EDITOR
//                        if (Application.isPlaying && _renderer.material != null)
//                            _renderer.material.renderQueue = 3000;
//#else
                        _renderer.sharedMaterial.renderQueue = 3000;
//#endif
                    }
                    // Copy the value from MaterialPropertyBlock to CanvasRenderer (#41)
                    UpdateAnimatableMaterialProperties();

                    Profiler.EndSample();
                }
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Update meshe.
        /// </summary>
        void UpdateMesh()
        {
            try
            {
                Profiler.BeginSample("CheckTrail");
                CheckTrail();
                Profiler.EndSample();

                if (m_ParticleSystem && canvas)
                {
                    // I do not know why, but it worked fine when setting `transform.localPosition.z` to `0.01`. (#34, #39)
                    {
                        Vector3 pos = rectTransform.localPosition;
                        if (Mathf.Abs(pos.z) < 0.01f)
                        {
                            pos.z = 0.01f;
                            rectTransform.localPosition = pos;
                        }
                    }

                    var rootCanvas = canvas.rootCanvas;
                    Profiler.BeginSample("Disable ParticleSystemRenderer");
                    if (Application.isPlaying)
                    {
                        _renderer.enabled = false;
                    }
                    Profiler.EndSample();

                    // #69: Editor crashes when mesh is set to null when ParticleSystem.RenderMode=Mesh
                    if (_renderer.renderMode == ParticleSystemRenderMode.Mesh && !_renderer.mesh)
                    {
                        return;
                    }

                    // #61: When ParticleSystem.RenderMode=None, an error occurs
                    if (_renderer.renderMode == ParticleSystemRenderMode.None)
                    {
                        return;
                    }

                    Profiler.BeginSample("Make Matrix");
                    ParticleSystem.MainModule main = m_ParticleSystem.main;
                    scaleaMatrix = main.scalingMode == ParticleSystemScalingMode.Hierarchy
                                                   ? Matrix4x4.Scale(scale * Vector3.one)
                                                   : Matrix4x4.Scale(scale * rootCanvas.transform.localScale);
                    Matrix4x4 matrix = default(Matrix4x4);
                    switch (main.simulationSpace)
                    {
                    case ParticleSystemSimulationSpace.Local:
                        matrix =
                            scaleaMatrix
                            * Matrix4x4.Rotate(rectTransform.rotation).inverse
                            *Matrix4x4.Scale(rectTransform.lossyScale + minimumVec3).inverse;
                        break;

                    case ParticleSystemSimulationSpace.World:
                        matrix =
                            scaleaMatrix
                            * rectTransform.worldToLocalMatrix;

                        bool    isLocalScaling = main.scalingMode == ParticleSystemScalingMode.Local;
                        Vector3 newPos         = rectTransform.position;
                        Vector3 delta          = (newPos - _oldPos);
                        _oldPos = newPos;

                        if (!Mathf.Approximately(scale, 0) && 0 < delta.sqrMagnitude)
                        {
                            if (isLocalScaling)
                            {
                                var s = rootCanvas.transform.localScale * scale;
                                delta.x *= 1f - 1f / s.x;
                                delta.y *= 1f - 1f / s.y;
                                delta.z *= 1f - 1f / s.z;
                            }
                            else
                            {
                                delta = delta * (1 - 1 / scale);
                            }

                            int count = m_ParticleSystem.particleCount;
                            if (s_Particles.Length < count)
                            {
                                s_Particles = new ParticleSystem.Particle[s_Particles.Length * 2];
                            }

                            m_ParticleSystem.GetParticles(s_Particles);
                            for (int i = 0; i < count; i++)
                            {
                                var p = s_Particles[i];
                                p.position     = p.position + delta;
                                s_Particles[i] = p;
                            }
                            m_ParticleSystem.SetParticles(s_Particles, count);
                        }
                        break;

                    case ParticleSystemSimulationSpace.Custom:
                        break;
                    }
                    Profiler.EndSample();

                    _mesh.Clear();
                    if (0 < m_ParticleSystem.particleCount)
                    {
                        Profiler.BeginSample("Bake Mesh");
                        var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay
                            ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas)
                            : canvas.worldCamera ?? Camera.main;

                        if (!cam)
                        {
                            return;
                        }
                        if (m_IsTrail)
                        {
                            _renderer.BakeTrailsMesh(_mesh, cam, true);
                        }
                        else
                        {
                            _renderer.BakeMesh(_mesh, cam, true);
                        }
                        Profiler.EndSample();

                        // Apply matrix.
                        Profiler.BeginSample("Apply matrix to position");

                        if (QualitySettings.activeColorSpace == ColorSpace.Linear)
                        {
                            _mesh.GetColors(s_Colors);
                            var count_c = s_Colors.Count;
                            for (int i = 0; i < count_c; i++)
                            {
                                s_Colors[i] = ((Color)s_Colors[i]).gamma;
                            }
                            _mesh.SetColors(s_Colors);
                        }

                        _mesh.GetVertices(s_Vertices);
                        var count = s_Vertices.Count;
                        for (int i = 0; i < count; i++)
                        {
                            s_Vertices[i] = matrix.MultiplyPoint3x4(s_Vertices[i]);
                        }
                        _mesh.SetVertices(s_Vertices);
                        _mesh.RecalculateBounds();
                        s_Vertices.Clear();
                        s_Colors.Clear();
                        Profiler.EndSample();
                    }


                    // Set mesh to CanvasRenderer.
                    Profiler.BeginSample("Set mesh and texture to CanvasRenderer");
                    canvasRenderer.SetMesh(_mesh);
                    canvasRenderer.SetTexture(mainTexture);

                    // Copy the value from MaterialPropertyBlock to CanvasRenderer (#41)
                    UpdateAnimatableMaterialProperties();

                    Profiler.EndSample();
                }
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Update meshe.
        /// </summary>
        void UpdateMesh()
        {
            try
            {
                Profiler.BeginSample("CheckTrail");
                CheckTrail();
                Profiler.EndSample();

                if (m_ParticleSystem && canvas)
                {
                    // I do not know why, but it worked fine when setting `transform.localPosition.z` to `0.01`. (#34, #39)
                    {
                        Vector3 pos = rectTransform.localPosition;
                        if (Mathf.Abs(pos.z) < 0.01f)
                        {
                            pos.z = 0.01f;
                            rectTransform.localPosition = pos;
                        }
                    }

                    var rootCanvas = canvas.rootCanvas;
                    Profiler.BeginSample("Disable ParticleSystemRenderer");
                    if (Application.isPlaying)
                    {
                        _renderer.enabled = false;
                    }
                    Profiler.EndSample();

                    Profiler.BeginSample("Make Matrix");
                    scaleaMatrix = m_ParticleSystem.main.scalingMode == ParticleSystemScalingMode.Hierarchy
                                                                                                   ? Matrix4x4.Scale(scale * Vector3.one)
                                                                                                   : Matrix4x4.Scale(scale * rootCanvas.transform.localScale);
                    Matrix4x4 matrix = default(Matrix4x4);
                    switch (m_ParticleSystem.main.simulationSpace)
                    {
                    case ParticleSystemSimulationSpace.Local:
                        matrix =
                            scaleaMatrix
                            * Matrix4x4.Rotate(rectTransform.rotation).inverse
                            *Matrix4x4.Scale(rectTransform.lossyScale).inverse;
                        break;

                    case ParticleSystemSimulationSpace.World:
                        matrix =
                            scaleaMatrix
                            * rectTransform.worldToLocalMatrix;

                        Vector3 newPos = rectTransform.position;
                        Vector3 delta  = (newPos - _worldPos);
                        _worldPos = newPos;
                        if (canvas.renderMode != RenderMode.WorldSpace && !Mathf.Approximately(scale, 0) && 0 < delta.sqrMagnitude)
                        {
                            delta *= (1 - 1 / scale);
                            int count = m_ParticleSystem.particleCount;
                            if (s_Particles.Length < count)
                            {
                                s_Particles = new ParticleSystem.Particle [s_Particles.Length * 2];
                            }

                            m_ParticleSystem.GetParticles(s_Particles);
                            for (int i = 0; i < count; i++)
                            {
                                var p = s_Particles [i];
                                p.position      = p.position + delta;
                                s_Particles [i] = p;
                            }
                            m_ParticleSystem.SetParticles(s_Particles, count);
                        }
                        break;

                    case ParticleSystemSimulationSpace.Custom:
                        break;
                    }
                    Profiler.EndSample();

                    _mesh.Clear();
                    if (0 < m_ParticleSystem.particleCount)
                    {
                        Profiler.BeginSample("Bake Mesh");
                        var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay
                                                        ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas)
                                                        : canvas.worldCamera ?? Camera.main;

                        if (!cam)
                        {
                            return;
                        }
                        if (m_IsTrail)
                        {
                            _renderer.BakeTrailsMesh(_mesh, cam, true);
                        }
                        else
                        {
                            _renderer.BakeMesh(_mesh, cam, true);
                        }
                        Profiler.EndSample();

                        // Apply matrix.
                        Profiler.BeginSample("Apply matrix to position");
                        _mesh.GetVertices(s_Vertices);
                        var count = s_Vertices.Count;
                        for (int i = 0; i < count; i++)
                        {
                            s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]);
                        }
                        _mesh.SetVertices(s_Vertices);
                        s_Vertices.Clear();
                        Profiler.EndSample();
                    }


                    // Set mesh to CanvasRenderer.
                    Profiler.BeginSample("Set mesh and texture to CanvasRenderer");
                    canvasRenderer.SetMesh(_mesh);
                    canvasRenderer.SetTexture(mainTexture);

                    // Copy the value from MaterialPropertyBlock to CanvasRenderer (#41)
                    UpdateAnimatableMaterialProperties();

                    Profiler.EndSample();
                }
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Update meshe.
        /// </summary>
        void UpdateMesh()
        {
            try
            {
                Profiler.BeginSample("CheckTrail");
                CheckTrail();
                Profiler.EndSample();

                if (m_ParticleSystem && canvas)
                {
                    if (canvas.renderMode != RenderMode.ScreenSpaceOverlay)
                    {
                        Vector3 pos = rectTransform.localPosition;
                        if (Mathf.Abs(pos.z) < 0.01f)
                        {
                            pos.z = 0.01f;
                            rectTransform.localPosition = pos;
                        }
                    }

                    var rootCanvas = canvas.rootCanvas;
                    Profiler.BeginSample("Disable ParticleSystemRenderer");
                    if (Application.isPlaying)
                    {
                        _renderer.enabled = false;
                    }
                    Profiler.EndSample();

                    Profiler.BeginSample("Make Matrix");
                    scaleaMatrix = m_ParticleSystem.main.scalingMode == ParticleSystemScalingMode.Hierarchy
                                                                       ? Matrix4x4.Scale(scale * Vector3.one)
                                                                       : Matrix4x4.Scale(scale * rootCanvas.transform.localScale);
                    Matrix4x4 matrix = default(Matrix4x4);
                    switch (m_ParticleSystem.main.simulationSpace)
                    {
                    case ParticleSystemSimulationSpace.Local:
                        matrix =
                            scaleaMatrix
                            * Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse
                            *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse;
                        break;

                    case ParticleSystemSimulationSpace.World:
                        matrix =
                            scaleaMatrix
                            * m_ParticleSystem.transform.worldToLocalMatrix;
                        break;

                    case ParticleSystemSimulationSpace.Custom:
                        break;
                    }
                    Profiler.EndSample();

                    _mesh.Clear();
                    if (0 < m_ParticleSystem.particleCount)
                    {
                        Profiler.BeginSample("Bake Mesh");
                        var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay
                                                        ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas)
                                                        : canvas.worldCamera ?? Camera.main;

                        if (!cam)
                        {
                            return;
                        }
                        if (m_IsTrail)
                        {
                            _renderer.BakeTrailsMesh(_mesh, cam, true);
                        }
                        else
                        {
                            _renderer.BakeMesh(_mesh, cam, true);
                        }
                        Profiler.EndSample();

                        // Apply matrix.
                        Profiler.BeginSample("Apply matrix to position");
                        _mesh.GetVertices(s_Vertices);
                        var count = s_Vertices.Count;
                        for (int i = 0; i < count; i++)
                        {
                            s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]);
                        }
                        _mesh.SetVertices(s_Vertices);
                        s_Vertices.Clear();
                        Profiler.EndSample();
                    }


                    // Set mesh to CanvasRenderer.
                    Profiler.BeginSample("Set mesh and texture to CanvasRenderer");
                    canvasRenderer.SetMesh(_mesh);
                    canvasRenderer.SetTexture(mainTexture);
                    Profiler.EndSample();
                }
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
            }
        }
        /// <summary>
        /// Update meshe.
        /// </summary>
        void UpdateMesh()
        {
            try
            {
                Profiler.BeginSample("CheckTrail");
                CheckTrail();
                Profiler.EndSample();

                if (m_ParticleSystem && canvas)
                {
                    var rootCanvas = canvas.rootCanvas;
                    Profiler.BeginSample("Disable ParticleSystemRenderer");
                    if (Application.isPlaying)
                    {
                        _renderer.enabled = false;
                    }
                    Profiler.EndSample();

                    Profiler.BeginSample("Make Matrix");
                    scaleaMatrix = m_ParticleSystem.main.scalingMode == ParticleSystemScalingMode.Hierarchy
                                                                       ? Matrix4x4.Scale(scale * Vector3.one)
                                                                       : Matrix4x4.Scale(scale * rootCanvas.transform.localScale);
                    Matrix4x4 matrix = default(Matrix4x4);
                    switch (m_ParticleSystem.main.simulationSpace)
                    {
                    case ParticleSystemSimulationSpace.Local:
                        matrix =
                            scaleaMatrix
                            * Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse
                            *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse;
                        break;

                    case ParticleSystemSimulationSpace.World:
                        matrix =
                            scaleaMatrix
                            * m_ParticleSystem.transform.worldToLocalMatrix;
                        break;

                    case ParticleSystemSimulationSpace.Custom:
                        break;
                    }
                    Profiler.EndSample();

                    _mesh.Clear();
                    if (0 < m_ParticleSystem.particleCount)
                    {
                        Profiler.BeginSample("Bake Mesh");

                        // If current scene is prefab mode, prevent create OverlayCamera.
                                                #if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
                        var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
                        if (prefabStage != null && prefabStage.scene != null && prefabStage.scene.isLoaded)
                        {
                            return;
                        }
                                                #endif

                        var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay
                                                        ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas)
                                                        : canvas.worldCamera ?? Camera.main;

                        if (!cam)
                        {
                            return;
                        }
                        if (m_IsTrail)
                        {
                            _renderer.BakeTrailsMesh(_mesh, cam, true);
                        }
                        else
                        {
                            _renderer.BakeMesh(_mesh, cam, true);
                        }
                        Profiler.EndSample();

                        // Apply matrix.
                        Profiler.BeginSample("Apply matrix to position");
                        _mesh.GetVertices(s_Vertices);
                        var count = s_Vertices.Count;
                        for (int i = 0; i < count; i++)
                        {
                            s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]);
                        }
                        _mesh.SetVertices(s_Vertices);
                        s_Vertices.Clear();
                        Profiler.EndSample();
                    }


                    // Set mesh to CanvasRenderer.
                    Profiler.BeginSample("Set mesh and texture to CanvasRenderer");
                    canvasRenderer.SetMesh(_mesh);
                    canvasRenderer.SetTexture(mainTexture);
                    Profiler.EndSample();
                }
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
            }
        }