提交 22c69985 编写于 作者: H happyfire

depth buffer visualization

上级 26b8dec7
...@@ -361,7 +361,7 @@ Camera: ...@@ -361,7 +361,7 @@ Camera:
y: 0 y: 0
width: 1 width: 1
height: 1 height: 1
near clip plane: 0.3 near clip plane: 0.8
far clip plane: 1000 far clip plane: 1000
field of view: 60 field of view: 60
orthographic: 0 orthographic: 0
...@@ -864,6 +864,7 @@ GameObject: ...@@ -864,6 +864,7 @@ GameObject:
- component: {fileID: 1297680261} - component: {fileID: 1297680261}
- component: {fileID: 1297680260} - component: {fileID: 1297680260}
- component: {fileID: 1297680264} - component: {fileID: 1297680264}
- component: {fileID: 1297680265}
m_Layer: 0 m_Layer: 0
m_Name: Quad m_Name: Quad
m_TagString: Untagged m_TagString: Untagged
...@@ -942,7 +943,7 @@ Transform: ...@@ -942,7 +943,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1297680259} m_GameObject: {fileID: 1297680259}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1.278, y: -0.616, z: 0.637} m_LocalPosition: {x: 1.278, y: -0.616, z: -2.31}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
...@@ -962,6 +963,25 @@ MonoBehaviour: ...@@ -962,6 +963,25 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mesh: {fileID: 0} mesh: {fileID: 0}
DoubleSideRendering: 0 DoubleSideRendering: 0
--- !u!95 &1297680265
Animator:
serializedVersion: 3
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1297680259}
m_Enabled: 0
m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: 5a61133bba3c7154e9648343d9a914a0, type: 2}
m_CullingMode: 0
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0
m_WarningMessage:
m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1
m_KeepAnimatorControllerStateOnDisable: 0
--- !u!1 &1825649040 --- !u!1 &1825649040
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -990,7 +1010,7 @@ Animator: ...@@ -990,7 +1010,7 @@ Animator:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1825649040} m_GameObject: {fileID: 1825649040}
m_Enabled: 1 m_Enabled: 0
m_Avatar: {fileID: 0} m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: 5a61133bba3c7154e9648343d9a914a0, type: 2} m_Controller: {fileID: 9100000, guid: 5a61133bba3c7154e9648343d9a914a0, type: 2}
m_CullingMode: 0 m_CullingMode: 0
...@@ -1058,7 +1078,7 @@ Transform: ...@@ -1058,7 +1078,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1825649040} m_GameObject: {fileID: 1825649040}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -2.43, y: 0, z: 0} m_LocalPosition: {x: -2.43, y: 0, z: 3.63}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 963210798} m_Father: {fileID: 963210798}
......
...@@ -43,6 +43,7 @@ namespace URasterizer ...@@ -43,6 +43,7 @@ namespace URasterizer
Color[] frame_buf; Color[] frame_buf;
float[] depth_buf; float[] depth_buf;
Color[] temp_buf;
public Texture2D texture; public Texture2D texture;
...@@ -64,6 +65,7 @@ namespace URasterizer ...@@ -64,6 +65,7 @@ namespace URasterizer
frame_buf = new Color[w * h]; frame_buf = new Color[w * h];
depth_buf = new float[w * h]; depth_buf = new float[w * h];
temp_buf = new Color[w * h];
texture = new Texture2D(w, h); texture = new Texture2D(w, h);
texture.filterMode = FilterMode.Point; texture.filterMode = FilterMode.Point;
...@@ -107,7 +109,7 @@ namespace URasterizer ...@@ -107,7 +109,7 @@ namespace URasterizer
} }
if((mask & BufferMask.Depth) == BufferMask.Depth) if((mask & BufferMask.Depth) == BufferMask.Depth)
{ {
FillArray(depth_buf, float.MaxValue); FillArray(depth_buf, 0f);
} }
_trianglesAll = _trianglesRendered = 0; _trianglesAll = _trianglesRendered = 0;
...@@ -222,9 +224,16 @@ namespace URasterizer ...@@ -222,9 +224,16 @@ namespace URasterizer
vec.x = 0.5f * _width * (vec.x + 1.0f); vec.x = 0.5f * _width * (vec.x + 1.0f);
vec.y = 0.5f * _height * (vec.y + 1.0f); vec.y = 0.5f * _height * (vec.y + 1.0f);
//GAMES101约定的NDC是右手坐标系,z值范围是[-1,1],但n为1,f为-1,因此值越大越靠近n。 //在硬件渲染中,NDC的z值经过硬件的透视除法之后就直接写入到depth buffer了,如果要调整需要在投影矩阵中调整
//为了符合深度值越小离camera越近的惯例,将View space的Z值反转一下,这样后面深度测试的时候就可以使用LESS_EQUAL测试了 //由于我们是软件渲染,所以可以在这里调整z值。
vec.z = -vec.z;
//GAMES101约定的NDC是右手坐标系,z值范围是[-1,1],但n为1,f为-1,因此值越大越靠近n。
//为了可视化Depth buffer,将最终的z值从[-1,1]映射到[0,1]的范围,因此最终n为1, f为0。离n越近,深度值越大。
//由于远处的z值为0,因此clear时深度要清除为0,然后深度测试时,使用GREATER测试。
//(当然我们也可以在这儿反转z值,然后clear时使用1清除,并且深度测试时使用LESS_EQUAL测试)
//注意:这儿的z值调整并不是必要的,只是为了可视化时便于映射为颜色值。其实也可以在可视化的地方调整。
//但是这么调整后,正好和Unity在DirectX平台的Reverse z一样,让near plane附近的z值的浮点数精度提高。
vec.z = vec.z * 0.5f + 0.5f;
v[k] = vec; v[k] = vec;
} }
...@@ -469,9 +478,9 @@ namespace URasterizer ...@@ -469,9 +478,9 @@ namespace URasterizer
float w_reciprocal = 1.0f / (alpha / v[0].w + beta / v[1].w + gamma / v[2].w); float w_reciprocal = 1.0f / (alpha / v[0].w + beta / v[1].w + gamma / v[2].w);
float z_interpolated = alpha * v[0].z / v[0].w + beta * v[1].z / v[1].w + gamma * v[2].z / v[2].w; float z_interpolated = alpha * v[0].z / v[0].w + beta * v[1].z / v[1].w + gamma * v[2].z / v[2].w;
z_interpolated *= w_reciprocal; z_interpolated *= w_reciprocal;
//深度测试 //深度测试(注意我们这儿的z值越大越靠近near plane,因此大值通过测试)
int index = GetIndex(x, y); int index = GetIndex(x, y);
if(z_interpolated <= depth_buf[index]) if(z_interpolated > depth_buf[index])
{ {
depth_buf[index] = z_interpolated; depth_buf[index] = z_interpolated;
...@@ -545,7 +554,30 @@ namespace URasterizer ...@@ -545,7 +554,30 @@ namespace URasterizer
public void UpdateFrame() public void UpdateFrame()
{ {
texture.SetPixels(frame_buf); switch (_config.DisplayBuffer)
{
case DisplayBufferType.Color:
texture.SetPixels(frame_buf);
break;
case DisplayBufferType.DepthRed:
case DisplayBufferType.DepthGray:
for (int i = 0; i < depth_buf.Length; ++i)
{
//depth_buf中的值范围是[0,1],且最近处为1,最远处为0。因此可视化后背景是黑色
float c = depth_buf[i];
if(_config.DisplayBuffer == DisplayBufferType.DepthRed)
{
temp_buf[i] = new Color(c, 0, 0);
}
else
{
temp_buf[i] = new Color(c, c, c);
}
}
texture.SetPixels(temp_buf);
break;
}
texture.Apply(); texture.Apply();
if (StatDelegate != null) if (StatDelegate != null)
......
...@@ -8,8 +8,16 @@ public class RenderingConfig : ScriptableObject ...@@ -8,8 +8,16 @@ public class RenderingConfig : ScriptableObject
public Color ClearColor = Color.black; public Color ClearColor = Color.black;
public bool WireframeMode = false; public bool WireframeMode = false;
public bool BackfaceCulling = true; public bool BackfaceCulling = true;
public bool MSAA = false; public DisplayBufferType DisplayBuffer = DisplayBufferType.Color;
[Header("Vertex Color Setting")] [Header("Vertex Color Setting")]
public Color[] VertexColors; public Color[] VertexColors;
} }
public enum DisplayBufferType
{
Color,
DepthRed,
DepthGray
}
...@@ -12,10 +12,10 @@ MonoBehaviour: ...@@ -12,10 +12,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d5fa123a60a768444af03a384cb34a74, type: 3} m_Script: {fileID: 11500000, guid: d5fa123a60a768444af03a384cb34a74, type: 3}
m_Name: RenderingConfig m_Name: RenderingConfig
m_EditorClassIdentifier: m_EditorClassIdentifier:
ClearColor: {r: 0, g: 0.7450409, b: 1, a: 0.47058824} ClearColor: {r: 0, g: 0, b: 0, a: 0.5058824}
WireframeMode: 0 WireframeMode: 0
BackfaceCulling: 1 BackfaceCulling: 1
MSAA: 0 DisplayBuffer: 0
VertexColors: VertexColors:
- {r: 1, g: 0, b: 0, a: 1} - {r: 1, g: 0, b: 0, a: 1}
- {r: 0, g: 1, b: 0, a: 1} - {r: 0, g: 1, b: 0, a: 1}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册