在之前的文章中,我们实现了URP下的雾效。
我们在这篇文章中,看一下Unity在URP下,计算雾效因子时是怎么统一Z值的。
//D3d with reversed Z => z clip range is [near, 0] -> remapping to [0, far]
//max is required to protect ourselves from near plane not being correct/meaningful in case of oblique matrices.
#define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(((1.0-(coord)/_ProjectionParams.y)*_ProjectionParams.z),0)
n ≤ z ≤ 0 n \leq z \leq0 n≤z≤0
n − n ≤ z − n ≤ 0 − n n - n \leq z - n\leq0 - n n−n≤z−n≤0−n
0 ≤ z − n ≤ − n 0 \leq z - n\leq- n 0≤z−n≤−n
0 ≤ ( z − n ) 1 − n ≤ − n 1 − n 0 \leq (z - n)\frac{1}{-n}\leq- n\frac{1}{-n} 0≤(z−n)−n1≤−n−n1
0 ≤ z − n − n ≤ 1 0 \leq \frac{z - n}{-n}\leq1 0≤−nz−n≤1
0 ≤ z − n − n f ≤ f 0 \leq \frac{z - n}{-n}f\leq f 0≤−nz−nf≤f
0 ≤ n − z n f ≤ f 0 \leq \frac{n - z}{n}f\leq f 0≤nn−zf≤f
0 ≤ ( n n − x n ) f ≤ f 0 \leq (\frac{n}{n}-\frac{x}{n})f\leq f 0≤(nn−nx)f≤f
0 ≤ ( 1 − x n ) f ≤ f 0 \leq (1-\frac{x}{n})f\leq f 0≤(1−nx)f≤f
max(((1.0-(coord)/_ProjectionParams.y)*_ProjectionParams.z),0)
//GL with reversed z => z clip range is [near, -far] -> remapping to [0, far]
#define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max((coord - _ProjectionParams.y)/(-_ProjectionParams.z-_ProjectionParams.y)*_ProjectionParams.z, 0)
n ≤ z ≤ − f n \leq z \leq-f n≤z≤−f
n − n ≤ z − n ≤ − f − n n -n\leq z -n \leq-f - n n−n≤z−n≤−f−n
0 ≤ z − n ≤ − f − n 0\leq z -n \leq-f - n 0≤z−n≤−f−n
0 ≤ ( z − n ) 1 − f − n ≤ − ( f + n ) 1 − f − n 0\leq (z -n) \frac{1}{-f-n}\leq-(f + n)\frac{1}{-f-n} 0≤(z−n)−f−n1≤−(f+n)−f−n1
0 ≤ z − n − f − n ≤ 1 0\leq \frac{z-n}{-f-n}\leq1 0≤−f−nz−n≤1
0 ≤ z − n − f − n f ≤ f 0\leq \frac{z-n}{-f-n}f\leq f 0≤−f−nz−nf≤f
max((coord - _ProjectionParams.y)/(-_ProjectionParams.z-_ProjectionParams.y)*_ProjectionParams.z, 0)
//Opengl => z clip range is [-near, far] -> remapping to [0, far]
#define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(((coord + _ProjectionParams.y)/(_ProjectionParams.z+_ProjectionParams.y))*_ProjectionParams.z, 0)
− n ≤ z ≤ f -n \leq z \leq f −n≤z≤f
− n + n ≤ z + n ≤ f + n -n +n \leq z+n \leq f+n −n+n≤z+n≤f+n
0 ≤ z + n ≤ f + n 0\leq z+n \leq f+n 0≤z+n≤f+n
0 ≤ z + n 1 f + n ≤ ( f + n ) 1 f + n 0\leq z+n\frac{1}{f+n}\leq (f+n)\frac{1}{f+n} 0≤z+nf+n1≤(f+n)f+n1
0 ≤ ( z + n ) 1 f + n ≤ 1 0\leq (z+n)\frac{1}{f+n}\leq 1 0≤(z+n)f+n1≤1
0 ≤ ( z + n ) 1 f + n f ≤ f 0\leq (z+n)\frac{1}{f+n}f\leq f 0≤(z+n)f+n1f≤f
max(((coord + _ProjectionParams.y)/(_ProjectionParams.z+_ProjectionParams.y))*_ProjectionParams.z, 0)