unity3d Antialiasing 抗锯齿效果 之渲染效果提升

废话不多上代码

C# (肯定是绑定在摄像机上面的,你懂的;注意摄像机一定要是Skybox或者SolidColor模式)


1111.png
2222.png
using System;
using System.Collections.Generic;
using UnityEngine;

public class Antialiasing : MonoBehaviour
{
    private static GameObject renderTargetCam;

    private static TextureRenderer textureRenderer;

    private static RenderTexture renderTexture;

    public static float scale = 2;

    private Camera mainCam;

    private int screenX;

    private int screenY;

    private int targetX = 100;

    private int targetY = 100;

    private int hideFlagDontShowSave = 61;

    public bool restart;

    private bool rendering;

    public static RenderTextureFormat Format = RenderTextureFormat.ARGB32;

    public static GameObject RenderTargetCamera
    {
        get
        {
            return Antialiasing.renderTargetCam;
        }
    }

    public static RenderTexture RenderTexture
    {
        get
        {
            return Antialiasing.renderTexture;
        }
    }
    public Camera RenderingCamera
    {
        get
        {
            return this.mainCam;
        }
    }
    private void OnEnable()
    {
        this.mainCam = base.GetComponent<Camera>();
        if (this.mainCam == null)
        {
            Debug.LogError("Missing Camera on GameObject!");
            base.enabled = false;
            return;
        }
        this.hideFlagDontShowSave = 13;
        this.targetX = Screen.width;
        this.targetY = Screen.height;
        new Shader();
        if (Application.isEditor)
        {
            this.restart = true;
            return;
        }
        this.StartAntialiasing();
    }

    private void OnDisable()
    {
        this.StopAntialiasing();
    }

    private void Update()
    {
        if (this.screenX != Screen.width || this.screenY != Screen.height)
        {
            this.restart = true;
        }
        if (this.restart)
        {
            this.Restart();
        }
    }

    private void Restart()
    {
        this.StopAntialiasing();
        this.StartAntialiasing();
        this.restart = false;
    }

    private void OnPreCull()
    {
        if (this.rendering && (this.screenX != Screen.width || this.screenY != Screen.height))
        {
            this.targetX = Screen.width;
            this.targetY = Screen.height;
            this.restart = true;
        }
        if (this.rendering)
        {
            this.mainCam.targetTexture = Antialiasing.renderTexture;
        }
    }

    private void FinishedRendering()
    {
        if (this.rendering)
        {
            this.mainCam.targetTexture = null;
        }
    }
    public void StartAntialiasing()
    {
        if (this.mainCam == null)
        {
            Debug.LogError("Missing Camera on Object!");
            return;
        }
        this.screenX = Screen.width;
        this.screenY = Screen.height;
        int num = (int)((float)Screen.width * Antialiasing.scale);
        int num2 = (int)((float)Screen.height * Antialiasing.scale);
        if (num <= 0)
        {
            num = 100;
        }
        if (num2 <= 0)
        {
            num2 = 100;
        }
        if (Antialiasing.renderTexture == null || Antialiasing.renderTexture.width != num || Antialiasing.renderTexture.height != num2)
        {
            if (Antialiasing.renderTexture != null)
            {
                Antialiasing.renderTexture.Release();
            }
            Antialiasing.renderTexture = new RenderTexture(num, num2, 2, Antialiasing.Format);
            Antialiasing.renderTexture.name = "SSAARenderTarget";
            Antialiasing.renderTexture.hideFlags = (HideFlags)this.hideFlagDontShowSave;
        }

        if (Antialiasing.renderTargetCam == null)
        {
            Antialiasing.renderTargetCam = new GameObject("SSAARenderTargetCamera");
            Antialiasing.renderTargetCam.hideFlags = (HideFlags)this.hideFlagDontShowSave;
            Camera c = Antialiasing.renderTargetCam.AddComponent<Camera>();
            c.CopyFrom(this.mainCam);
            c.cullingMask = 0;
            c.targetTexture = null;
            c.depth = this.mainCam.depth + 0.5f;
            Antialiasing.textureRenderer = Antialiasing.renderTargetCam.AddComponent<TextureRenderer>();
            Antialiasing.textureRenderer.hideFlags = (HideFlags)this.hideFlagDontShowSave;

         
        }
        this.rendering = true;
    }

    public void StopAntialiasing()
    {
        if (this.mainCam != null && this.mainCam.targetTexture != null)
        {
            this.mainCam.targetTexture = null;
        }
        if (renderTargetCam != null)
        {
            GameObject.Destroy(renderTargetCam);
        }
        this.rendering = false;

    }
}


public class TextureRenderer : MonoBehaviour
{
    private Camera c;
    public bool stereoFirstPass = true;
    private void Awake()
    {
        this.c = gameObject.GetComponent<Camera>();
        if (this.c == null)
        {
            Debug.LogError("TextureRenderer init fail! (no Camera)");
            base.enabled = false;
        }
    }
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(Antialiasing.RenderTexture, destination);
        Antialiasing.RenderTexture.DiscardContents();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,814评论 25 708
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,650评论 1 180
  • 星空破灭 语落心伤 前路为何如此迷茫 谁能为之痴狂 爱已不能说出口 恨在心中滞留 为何你无情地离我而去 去寻找那遥...
    闲狐兔阅读 130评论 0 0
  • 我和他谈了一年多的恋爱,彼此熟悉又陌生,不知道从什么时候开始,我们的感情没有那么好了。可能是我爱生气,一生气...
    张小航Eli阅读 302评论 0 0
  • 最好的写作,一定是在恋爱的时候 这是我刚刚从简书上看到的。 我原来有这么宝贵的写作资源,因为我已经恋爱了近九年! ...
    拂袖三叩首阅读 241评论 0 0