Exemplo n.º 1
0
        public void AddRenderingSteps(Renderer[] renderers, Color outlineColor, int thickness, Shader drawSilhouettesShader)
        {
            if ((renderers == null) || (renderers.Length == 0))
            {
                Debug.LogError("target to be rendered not set");
                return;
            }

            Material currentMat = null;

            if (materialCounter >= Materials.Count)
            {
                currentMat = new Material(drawSilhouettesShader);
                Materials.Add(currentMat);
            }
            else
            {
                currentMat = Materials[materialCounter];
            }

            materialCounter++;

            currentMat.SetColor(silhouetteColorID, OutlineInfo.Encode(outlineColor, thickness));

            int renderersStart = Renderers.Count;

            Renderers.AddRange(renderers);

            Renderer r = null;

            for (int i = renderersStart; i < Renderers.Count; i++)
            {
                r = Renderers[i];
                if (r.isVisible)
                {
                    if (r is SkinnedMeshRenderer)
                    {
                        SkinnedMeshRenderer skinned = (SkinnedMeshRenderer)r;
                        if (skinned.sharedMesh)
                        {
                            for (int c = 0; c < skinned.sharedMesh.subMeshCount; c++)
                            {
                                CommandBuffer.DrawRenderer(r, currentMat, c, 0);
                            }
                        }
                    }
                    else
                    {
                        CommandBuffer.DrawRenderer(r, currentMat, 0, 0);
                    }
                }
            }
            //TODO: probabilmente Renderers può essere eliminato e può essere usato solo l'array argomento del metodo renderers
        }
Exemplo n.º 2
0
        void OnRenderObject()
        {
            if (Targets.Length == 0)
            {
                return;
            }

            RenderTexture old = RenderTexture.active;

            RenderTexture.active = OutlinePostEffect.Instance.GetSilhouettesRT(Mode);
            int modeInt = (int)Mode;

            if (Time.frameCount != clearFrame[modeInt])
            {
                GL.Clear(false, true, Color.clear);
                clearFrame[modeInt] = Time.frameCount;
            }

            Color c = OutlineInfo.Encode(OutlineColor, Thickness);

            OutlinePostEffect.Instance.SilhouettesMaterial.SetColor("_Color", c);
            OutlinePostEffect.Instance.SilhouettesMaterial.SetPass(0);

            for (int i = 0; i < Targets.Length; i++)
            {
                if (Targets[i].visible)
                {
                    Bounds bounds = Targets[i].renderer.bounds;
                    if (i == 0)
                    {
                        boundsMin = bounds.min;
                        boundsMax = bounds.max;
                    }
                    else
                    {
                        boundsMin = Vector3.Min(boundsMin, bounds.min);
                        boundsMax = Vector3.Max(boundsMax, bounds.max);
                    }

                    //Matrix4x4 matrix = Targets[i].renderer.transform.localToWorldMatrix;
                    //matrix.SetTRS( transform.position, transform.rotation, transform.localScale * 2.0f );
                    Graphics.DrawMeshNow(Targets[i].mesh, Targets[i].renderer.transform.localToWorldMatrix /*matrix*/);
                }
            }

            Bounds totalBounds = new Bounds();

            totalBounds.SetMinMax(boundsMin, boundsMax);
            Vector3 center = totalBounds.center;
            Vector3 ext    = totalBounds.extents;
            Vector3 c0     = center + new Vector3(-ext.x, -ext.y, -ext.z);
            Vector3 c1     = center + new Vector3(-ext.x, -ext.y, ext.z);
            Vector3 c2     = center + new Vector3(-ext.x, ext.y, -ext.z);
            Vector3 c3     = center + new Vector3(-ext.x, ext.y, ext.z);
            Vector3 c4     = center + new Vector3(ext.x, -ext.y, -ext.z);
            Vector3 c5     = center + new Vector3(ext.x, -ext.y, ext.z);
            Vector3 c6     = center + new Vector3(ext.x, ext.y, -ext.z);
            Vector3 c7     = center + new Vector3(ext.x, ext.y, ext.z);

            //boundsMin.z = boundsMax.z = 0.5f * ( boundsMin.z + boundsMax.z );
            Vector3 tmp = Camera.main.WorldToViewportPoint(c0);

            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c1));
            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c2));
            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c3));
            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c4));
            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c5));
            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c6));
            tmp   = Vector3.Min(tmp, Camera.main.WorldToViewportPoint(c7));
            MinUV = tmp;

            tmp   = Camera.main.WorldToViewportPoint(c0);
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c1));
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c2));
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c3));
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c4));
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c5));
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c6));
            tmp   = Vector3.Max(tmp, Camera.main.WorldToViewportPoint(c7));
            MaxUV = tmp;

            UVSize = MaxUV - MinUV;

            RenderTexture.active = old;
        }