Unity抗锯齿,Unity3D模型花闪烁等效果优化。

15 篇文章 1 订阅

Unity抗锯齿,Unity3D模型花闪烁等效果优化。

最近在做全息投影项目,模型闪烁,锯齿明显,自己总结了一些优化方法。

方法一:在场景内摄像机上挂载如下抗锯齿脚本。摄像机改为Skybox或者SolidColor模式;

using System.Collections;
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();
    }
}

方法二:unity系统设置:Edit-->ProjectSettings------>QualitySettungs-------->Ultra-------->AntiAlisaing-------->8XMulti Sampling,如下图所示:

方法三:

相机设置:

方法四:相机上挂载如下所示脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QualitySetting : MonoBehaviour {
	
	void Start () {
       
        QualitySettings.shadowResolution = ShadowResolution.VeryHigh;
        QualitySettings.antiAliasing = 8;
		
	}
	
	
}

 

好啦,这是最近做全息投影展示项目,模型锯齿的一些心得。

  • 0
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值