예제 #1
0
        public static Texture2D PerformCapture(bool captureAlphaOnly, int renderWidth, int renderHeight, int antiAliasing)
        {
            var renderCam       = CameraUtils.CopyCamera(Camera.main);
            var targetTexture   = renderCam.targetTexture;
            var rect            = renderCam.rect;
            var backgroundColor = renderCam.backgroundColor;
            var clearFlags      = renderCam.clearFlags;
            var t2d             = new Texture2D(renderWidth, renderHeight, TextureFormat.RGB24, false);
            var rt_temp         = RenderTexture.GetTemporary(renderWidth, renderHeight, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default, 8);

            rt_temp.antiAliasing = antiAliasing;

            var list = new List <MaterialInfo>();
            var lc   = Shader.GetGlobalColor(ChaShader._LineColorG);

            renderCam.backgroundColor = Color.black;

            var plainTex = new Texture2D(1, 1);

            plainTex.SetPixel(0, 0, Color.black);
            plainTex.Apply();

            if (captureAlphaOnly)
            {
                Shader.SetGlobalColor(ChaShader._LineColorG, new Color(.5f, .5f, .5f, 0f));

                renderCam.backgroundColor = Color.white;

                foreach (var smr in GameObject.FindObjectsOfType <Renderer>())
                {
                    var p = new Dictionary <int, Texture>();
                    foreach (var potential in potentials)
                    {
                        if (smr.material.HasProperty(potential))
                        {
                            var gt = smr.material.GetTexture(potential);
                            p[potential] = gt;
                            smr.material.SetTexture(potential, plainTex);
                        }
                    }

                    var p2 = new Dictionary <int, float>();
                    foreach (var potential2 in potentials2)
                    {
                        if (smr.material.HasProperty(potential2))
                        {
                            var gt = smr.material.GetFloat(potential2);
                            p2[potential2] = gt;
                            smr.material.SetFloat(potential2, 0f);
                        }
                    }

                    var p3 = new Dictionary <int, Color>();
                    foreach (var potential3 in potentials3)
                    {
                        if (smr.material.HasProperty(potential3))
                        {
                            var gt = smr.material.GetColor(potential3);
                            p3[potential3] = gt;
                            smr.material.SetColor(potential3, Color.black);
                        }
                    }

                    var mi = new MaterialInfo()
                    {
                        r = smr, tex_props = p, float_props = p2, col_props = p3
                    };
                    list.Add(mi);
                }
            }

            Shader.SetGlobalColor(ChaShader._LineColorG, lc);
            renderCam.clearFlags    = CameraClearFlags.Color;
            renderCam.targetTexture = rt_temp;
            renderCam.rect          = new Rect(0, 0, 1, 1);
            renderCam.Render();
            renderCam.targetTexture = targetTexture;
            renderCam.rect          = rect;

            RenderTexture.active = rt_temp;
            t2d.ReadPixels(new Rect(0f, 0f, renderWidth, renderHeight), 0, 0);
            t2d.Apply();
            RenderTexture.active      = null;
            renderCam.backgroundColor = backgroundColor;
            renderCam.clearFlags      = clearFlags;
            RenderTexture.ReleaseTemporary(rt_temp);
            GameObject.Destroy(plainTex);

            foreach (var smr in list)
            {
                foreach (var k in smr.tex_props.Keys)
                {
                    smr.r.material.SetTexture(k, smr.tex_props[k]);
                }
                foreach (var k in smr.float_props.Keys)
                {
                    smr.r.material.SetFloat(k, smr.float_props[k]);
                }
                foreach (var k in smr.col_props.Keys)
                {
                    smr.r.material.SetColor(k, smr.col_props[k]);
                }
            }

            GameObject.Destroy(renderCam.gameObject);

            return(t2d);
        }