Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
n5
URasterizer
提交
22c69985
URasterizer
项目概览
n5
/
URasterizer
通知
21
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
URasterizer
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
22c69985
编写于
2月 06, 2022
作者:
H
happyfire
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
depth buffer visualization
上级
26b8dec7
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
74 addition
and
14 deletion
+74
-14
URasterizer/Assets/Samples/Scenes/URasterizerScene.unity
URasterizer/Assets/Samples/Scenes/URasterizerScene.unity
+24
-4
URasterizer/Assets/URasterizer/Codes/Rasterizer.cs
URasterizer/Assets/URasterizer/Codes/Rasterizer.cs
+39
-7
URasterizer/Assets/URasterizer/Codes/RenderingConfig.cs
URasterizer/Assets/URasterizer/Codes/RenderingConfig.cs
+9
-1
URasterizer/Assets/URasterizer/RenderingConfig.asset
URasterizer/Assets/URasterizer/RenderingConfig.asset
+2
-2
未找到文件。
URasterizer/Assets/Samples/Scenes/URasterizerScene.unity
浏览文件 @
22c69985
...
@@ -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
}
...
...
URasterizer/Assets/URasterizer/Codes/Rasterizer.cs
浏览文件 @
22c69985
...
@@ -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
)
...
...
URasterizer/Assets/URasterizer/Codes/RenderingConfig.cs
浏览文件 @
22c69985
...
@@ -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
}
URasterizer/Assets/URasterizer/RenderingConfig.asset
浏览文件 @
22c69985
...
@@ -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.47
058824
}
ClearColor
:
{
r
:
0
,
g
:
0
,
b
:
0
,
a
:
0.5
058824
}
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录