当前位置:网站首页>Unity solves the problem of Z-fighting caused by overlapping objects with the same material
Unity solves the problem of Z-fighting caused by overlapping objects with the same material
2022-07-19 03:33:00 【n5】
problem
stay Unity Draw multiple objects that may partially or completely overlap , These objects may use the same material , At this time, the probability will be generated Z-Fighting
Solutions
Z-Fighting The reason is that the depth value calculated by the fragment is too close , Therefore, it is impossible to determine which one is closer to the camera , Competition leads to flickering . Then the solution is to let them Z Values are slightly different . The more common solution is Polgon Offset, namely Unity Shader Lab Of Offset attribute . But for objects with the same material , This method is invalid . Because when such objects overlap each other, even if the material is set Offset, Because it is the same material , Calculated depth It's still the same , It cannot be distinguished . So how can we distinguish the depth value of the same object ? I think of it. GPU Instancing draw , Each object has a instance ID, Use this ID You can distinguish objects , So as to further find a way to distinguish the depth value . But the material Offset It cannot be set for objects , Fortunately, there are alternative methods , Because our ultimate goal is to offset the depth value , Then you can offset directly when calculating the depth value . I adopted the modification clip space Coordinate value method .
Concrete realization
notes : The development environment I use is Unity2021+URP, The principle of built-in assembly line is the same .
First, we need to customize shader, stay Vertex Shader Just offset directly in :
#if UNITY_ANY_INSTANCING_ENABLED
vertexInput.positionCS += _ClipSpaceOffset * unity_InstanceID;
#else
vertexInput.positionCS += _ClipSpaceOffset;
#endif
With the help of Instancing Supported unity_InstanceID, We use an offset value pair clip space The coordinates are offset .
here _ClipSpaceOffset Defined as a material attribute :
properties
{
_ClipSpaceOffset("ClipsSpaceOffset", Float) = 0.0000002
}
It should be noted that , Yours shader Must be incompatible SRP Batcher, Otherwise, it cannot be enabled GPU Instancing, Here because I didn't _ClipSpaceOffset Put it in UnityPerMaterial Of CBuffer in , So nature destroys SRP Batcher.
Finally, open the material Enable GPU Instancing that will do .
A small problem
I found that after using this method , stay Play Mode Running , Dynamically added objects can be displayed normally and stably , No flicker . But in Unity Of Scene View Every time I refresh , The order of these objects will change , It could be Instance ID Changed . In fact, it is also very simple to solve this , Use MaterialProperty Bind each object with a unique ID, Use this ID Instead of Instance ID that will do .
Reference resources
About solving Z-Fighting, May refer to Unity This post of the Forum
边栏推荐
- 論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
- ES6 learning notes - brother Ma at station B
- SwiftUI 考试题库项目之支持题库和考试题库数量(教程含源码)
- Wechat applet
- Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
- Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
- Bisenetv2 face segmentation ncnn reasoning
- leetcode162. 寻找峰值
- oracle 查询 主机名和对应的IP地址
- 未知宽高元素实现水平垂直居中的方法
猜你喜欢

CorelDRAW cannot be installed. Solution

箭头函数与this指向详解

代理模式——B站动力节点

Game theory of catching lice

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?

Polynomial interpolation fitting (II)

Using gatekeeper to restrict kubernetes to create specific types of resources

Leetcode: subsequence problem in dynamic programming

通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动

基于MFC如何实现单个文档的文件读写
随机推荐
上班摸鱼打卡模拟器微信小程序源码
04_ Service registration Eureka
oracle 查询非自增长分区的最大分区
Ubuntu clear CUDA cache
SwiftUI 考试题库项目之支持题库和考试题库数量(教程含源码)
Configure high availability using virtual ip+kept
About 1000base-t1 1000Base-TX and 100base-t1
Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
神经网络学习笔记2.2 ——用Matlab写一个简单的卷积神将网络图像分类器
Latest installation tutorial of VMware Tools (rhel8)
Leetcode: multiple knapsack problem in dynamic programming [one template solves all ~]
CorelDRAW cannot be installed. Solution
Chengxin University envi_ IDL second week homework: extract aerosol thickness at n points + detailed analysis
Chengxin University envi_ IDL second week class content: open hdf4 file and read the file, as well as simple data processing and saving + detailed analysis
We should increase revenue and reduce expenditure
ncnn DataReader&Extractor&blob
无线用的鉴权代码
D. Permutation restoration (greedy / double pointer /set)
Basic IDL content of note 1: common data types_ Create array_ Type conversion_ Print output_ Basic operation_ Relational operation
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)