Пример #1
0
 public static OutlineStorage Instance()
 {
     if (_instance == null)
     {
         _instance = new OutlineStorage();
     }
     return(_instance);
 }
Пример #2
0
 void OnDisable()
 {
     OutlineStorage.Instance().RemoveOutlineForKey(this, outlineKey);
 }
Пример #3
0
 void OnEnable()
 {
     OutlineStorage.Instance().AddOutlineForKey(this, outlineKey);
 }
Пример #4
0
        public void OnPreRender()
        {
            if (commandBuffer == null)
            {
                return;
            }

            // the first frame during which there are no outlines, we still need to render
            // to clear out any outlines that were being rendered on the previous frame
            var outlines = OutlineStorage.Instance().OutlinesForKey(outlineKey);

            if (outlines == null || outlines.Count == 0)
            {
                return;
            }

            CreateMaterialsIfNeeded();

            if (renderTexture == null || renderTexture.width != sourceCamera.pixelWidth || renderTexture.height != sourceCamera.pixelHeight)
            {
                renderTexture               = new RenderTexture(sourceCamera.pixelWidth, sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
                extraRenderTexture          = new RenderTexture(sourceCamera.pixelWidth, sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
                outlineCamera.targetTexture = renderTexture;
            }
            UpdateMaterialsPublicProperties();
            UpdateOutlineCameraFromSource();
            outlineCamera.targetTexture = renderTexture;
            commandBuffer.SetRenderTarget(renderTexture);

            commandBuffer.Clear();

            foreach (Outline outline in outlines)
            {
                LayerMask l = sourceCamera.cullingMask;

                if (outline != null && l == (l | (1 << outline.gameObject.layer)))
                {
                    for (int v = 0; v < outline.SharedMaterials.Length; v++)
                    {
                        Material m = null;

                        if (outline.SharedMaterials[v].mainTexture != null && outline.SharedMaterials[v])
                        {
                            foreach (Material g in materialBuffer)
                            {
                                if (g.mainTexture == outline.SharedMaterials[v].mainTexture)
                                {
                                    if (outline.eraseRenderer && g.color == outlineEraseMaterial.color)
                                    {
                                        m = g;
                                    }
                                    else if (g.color == GetMaterialFromID(outline.color).color)
                                    {
                                        m = g;
                                    }
                                }
                            }

                            if (m == null)
                            {
                                if (outline.eraseRenderer)
                                {
                                    m = new Material(outlineEraseMaterial);
                                }
                                else
                                {
                                    m = new Material(GetMaterialFromID(outline.color));
                                }
                                m.mainTexture = outline.SharedMaterials[v].mainTexture;
                                materialBuffer.Add(m);
                            }
                        }
                        else
                        {
                            if (outline.eraseRenderer)
                            {
                                m = outlineEraseMaterial;
                            }
                            else
                            {
                                m = GetMaterialFromID(outline.color);
                            }
                        }

                        if (backfaceCulling)
                        {
                            m.SetInt("_Culling", (int)UnityEngine.Rendering.CullMode.Back);
                        }
                        else
                        {
                            m.SetInt("_Culling", (int)UnityEngine.Rendering.CullMode.Off);
                        }

                        commandBuffer.DrawRenderer(outline.Renderer, m, 0, 0);
                        MeshFilter mL = outline.MeshFilter;
                        if (mL)
                        {
                            if (mL.sharedMesh != null)
                            {
                                for (int i = 1; i < mL.sharedMesh.subMeshCount; i++)
                                {
                                    commandBuffer.DrawRenderer(outline.Renderer, m, i, 0);
                                }
                            }
                        }
                        SkinnedMeshRenderer sMR = outline.SkinnedMeshRenderer;
                        if (sMR)
                        {
                            if (sMR.sharedMesh != null)
                            {
                                for (int i = 1; i < sMR.sharedMesh.subMeshCount; i++)
                                {
                                    commandBuffer.DrawRenderer(outline.Renderer, m, i, 0);
                                }
                            }
                        }
                    }
                }
            }

            outlineCamera.Render();
        }