码迷,mamicode.com
首页 > 其他好文 > 详细

多层相机叠加

时间:2019-01-02 12:34:49      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ret   int   www   read   cti   nbsp   adp   style   wait   

需求:需要实时截出不同背景的图片(要求背景颜色对场景有反射效果),但是场景中用到PostProcessing等后处理,还有影子、倒影等效果,很难单纯通过程序来达到效果。

方法:通过三层相机分别来渲染模型影子、倒影以及地面与模型,背景为白色,通过颜色相乘来换成其他背景。

(一)场景中分别新四个quad,并调整quad大小到需要的尺寸,四个quad放在同一位置,调整Z值使之错开(可根据需要调整前后顺序)

(二)设置最后面一个quad为背景色(白色),材质为Unlit/Color

(三)用一个Solid Color类型的Camera来渲染模型和地面,通过RenderTexture赋值给(一)中的一个quad,此quad的shader为UI/Unlit/Transparent(亦可以用Unlit/texture)

(四)用一个Solid Color类型的Camera来渲染模型,通过RenderTexture赋值给(一)中另外一个quad,此quad的shader为UI/Unlit/Transparent。把此quad放置在(三)中quad背后,并调整材质的透明通道使之看起来像影子,可调整quad的位置

(五)用一个Solid Color类型的Camera来渲染倒影,此camera放在始终与(三)中的相机始终关于地面对称的位置,并始终LookAt相机。通过RenderTexture赋值给(一)中最后一个quad,此quad的shader为UI/Unlit/Transparent。调整材质的透明通道使之看起来像倒影,可调整quad的位置,如果需要把Scale的y值改为-1.

(六)通过一个正交相机来获取上述四个quad叠加倒影,并通过像素相乘来更改背景

 

   IEnumerator CaptureEffect()
    {
        //RenderTexture rt = RenderTexture.active;
        RenderTexture rt = new RenderTexture(width, height, 16);
        effectCamera.targetTexture = rt;
        effectCamera.Render();

        yield return new WaitForEndOfFrame();
        RenderTexture.active = rt;
        effectTex = new Texture2D(width, height);
        effectTex.ReadPixels(new Rect(0, 0, effectTex.width, effectTex.height), 0, 0);
        effectTex.Apply();

        //CreateKaleidoscope();//生成万花筒图片

        effectImage.texture = effectTex;
        effectCamera.targetTexture = null;

        int w = effectTex.width;
        int h = effectTex.height;

        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                Color color = effectTex.GetPixel(i, j);
                Color modifiedColor = color * Color.yellow;
                effectTex.SetPixel(i, j, modifiedColor);
            }
        }

        byte[] bytes = effectTex.EncodeToPNG();
        File.WriteAllBytes(@"D:\image.png", bytes);
        //RenderTexture.active = rt;
        //rt = null;
        /*
        byte[] imageBytes = effectTex.EncodeToJPG();
        string picName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

        WWWForm form = new WWWForm();
        form.AddField("resource_name", picName);
        form.AddBinaryData("file", imageBytes, picName, "file/jpg");

        //Dictionary<object, object> dic = new Dictionary<object, object>();
        //dic.Add("resource_name", picName);

        WWWWebRequest.Instance.PostForm(APiInfo.UploadImage, form, OnImagePosted);
        */

        //后续删除
        //cover.SetActive(false);
    }

 

多层相机叠加

标签:ret   int   www   read   cti   nbsp   adp   style   wait   

原文地址:https://www.cnblogs.com/llstart-new0201/p/10207329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!