Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
n5
URasterizer
提交
ef056257
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 搜索 >>
提交
ef056257
编写于
1月 07, 2022
作者:
H
happyfire
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
- transform tool
上级
37594523
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
122 addition
and
44 deletion
+122
-44
URasterizer/Assets/URasterizer/Codes/CameraRenderer.cs
URasterizer/Assets/URasterizer/Codes/CameraRenderer.cs
+11
-10
URasterizer/Assets/URasterizer/Codes/Rasterizer.cs
URasterizer/Assets/URasterizer/Codes/Rasterizer.cs
+23
-20
URasterizer/Assets/URasterizer/Codes/RenderingObject.cs
URasterizer/Assets/URasterizer/Codes/RenderingObject.cs
+22
-1
URasterizer/Assets/URasterizer/Codes/TransformTool.cs
URasterizer/Assets/URasterizer/Codes/TransformTool.cs
+53
-1
URasterizer/Assets/URasterizer/URasterizerScene.unity
URasterizer/Assets/URasterizer/URasterizerScene.unity
+13
-12
未找到文件。
URasterizer/Assets/URasterizer/Codes/CameraRenderer.cs
浏览文件 @
ef056257
...
...
@@ -11,14 +11,12 @@ namespace URasterizer
public
RawImage
rawImg
;
public
Color
ClearColor
=
Color
.
black
;
public
Transform
[]
objNodes
;
private
RenderingObject
[]
renderingObjects
;
private
Camera
_camera
;
public
float
Angle
;
public
bool
WireframeMode
;
private
void
Start
()
...
...
@@ -44,21 +42,24 @@ namespace URasterizer
if
(
objNodes
.
Length
==
0
)
{
renderingObjects
=
new
RenderingObject
[
1
];
var
_mesh
=
new
Mesh
();
_mesh
.
vertices
=
new
Vector3
[]
{
new
Vector3
(
1f
,
0f
,
-
2f
),
new
Vector3
(
0f
,
2f
,
-
2f
),
new
Vector3
(-
1f
,
0f
,
-
2f
)
};
_mesh
.
triangles
=
new
int
[]
{
0
,
1
,
2
};
renderingObjects
=
new
RenderingObject
[
1
];
var
_mesh
=
new
Mesh
{
vertices
=
new
Vector3
[]
{
new
Vector3
(
1f
,
0f
,
-
2f
),
new
Vector3
(
0f
,
2f
,
-
2f
),
new
Vector3
(-
1f
,
0f
,
-
2f
)
},
triangles
=
new
int
[]
{
0
,
1
,
2
}
};
renderingObjects
[
0
]
=
new
RenderingObject
(
_mesh
);
}
RectTransform
rect
=
rawImg
.
GetComponent
<
RectTransform
>();
rect
.
sizeDelta
=
new
Vector2
(
Screen
.
width
,
Screen
.
height
);
int
w
=
Mathf
.
FloorToInt
(
rect
.
rect
.
width
);
int
h
=
Mathf
.
FloorToInt
(
rect
.
rect
.
height
);
Debug
.
Log
(
$"screen size:
{
w
}
x
{
h
}
"
);
_rasterizer
=
new
Rasterizer
(
w
,
h
);
rawImg
.
texture
=
_rasterizer
.
texture
;
_rasterizer
.
ClearColor
=
_camera
.
background
Color
;
_rasterizer
.
ClearColor
=
Clear
Color
;
}
...
...
@@ -69,7 +70,7 @@ namespace URasterizer
for
(
int
i
=
0
;
i
<
renderingObjects
.
Length
;
++
i
)
{
r
.
Draw
(
renderingObjects
[
i
]
.
mesh
,
_camera
,
WireframeMode
);
r
.
Draw
(
renderingObjects
[
i
],
_camera
,
WireframeMode
);
}
r
.
UpdateFrame
();
...
...
URasterizer/Assets/URasterizer/Codes/Rasterizer.cs
浏览文件 @
ef056257
...
...
@@ -66,6 +66,8 @@ namespace URasterizer
public
Rasterizer
(
int
w
,
int
h
)
{
Debug
.
Log
(
$"Rasterizer screen size:
{
w
}
x
{
h
}
"
);
_width
=
w
;
_height
=
h
;
...
...
@@ -137,11 +139,12 @@ namespace URasterizer
}
}
public
void
Draw
(
Mesh
mesh
,
Camera
camera
,
bool
wireframeMode
=
false
)
public
void
Draw
(
RenderingObject
ro
,
Camera
camera
,
bool
wireframeMode
=
false
)
{
Mesh
mesh
=
ro
.
mesh
;
SetupViewProjectionMatrix
(
camera
);
ModelMatrix
=
TransformTool
.
GetModelMatrix
(
0
);
ModelMatrix
=
ro
.
GetModelMatrix
(
);
float
far
=
camera
.
farClipPlane
;
...
...
@@ -198,18 +201,18 @@ namespace URasterizer
if
(
wireframeMode
)
{
r
asterizeWireframe
(
t
);
R
asterizeWireframe
(
t
);
}
else
{
r
asterizeTriangle
(
t
);
R
asterizeTriangle
(
t
);
}
}
}
#
region
Wireframe
mode
private
void
d
rawLine
(
Vector3
begin
,
Vector3
end
,
Color
line_color
)
private
void
D
rawLine
(
Vector3
begin
,
Vector3
end
,
Color
line_color
)
{
int
x1
=
Mathf
.
FloorToInt
(
begin
.
x
);
int
y1
=
Mathf
.
FloorToInt
(
begin
.
y
);
...
...
@@ -243,22 +246,22 @@ namespace URasterizer
SetPixel
(
point
,
line_color
);
for
(
i
=
0
;
x
<
xe
;
i
++)
{
x
=
x
+
1
;
x
++
;
if
(
px
<
0
)
{
px
=
px
+
2
*
dy1
;
px
+=
2
*
dy1
;
}
else
{
if
((
dx
<
0
&&
dy
<
0
)
||
(
dx
>
0
&&
dy
>
0
))
{
y
=
y
+
1
;
y
++
;
}
else
{
y
=
y
-
1
;
y
--
;
}
px
=
px
+
2
*
(
dy1
-
dx1
);
px
+=
2
*
(
dy1
-
dx1
);
}
Vector3
pt
=
new
Vector3
(
x
,
y
,
1.0f
);
...
...
@@ -283,22 +286,22 @@ namespace URasterizer
SetPixel
(
point
,
line_color
);
for
(
i
=
0
;
y
<
ye
;
i
++)
{
y
=
y
+
1
;
y
++
;
if
(
py
<=
0
)
{
py
=
py
+
2
*
dx1
;
py
+=
2
*
dx1
;
}
else
{
if
((
dx
<
0
&&
dy
<
0
)
||
(
dx
>
0
&&
dy
>
0
))
{
x
=
x
+
1
;
x
++
;
}
else
{
x
=
x
-
1
;
x
--
;
}
py
=
py
+
2
*
(
dx1
-
dy1
);
py
+=
2
*
(
dx1
-
dy1
);
}
Vector3
pt
=
new
Vector3
(
x
,
y
,
1.0f
);
SetPixel
(
pt
,
line_color
);
...
...
@@ -306,17 +309,17 @@ namespace URasterizer
}
}
private
void
r
asterizeWireframe
(
Triangle
t
)
private
void
R
asterizeWireframe
(
Triangle
t
)
{
d
rawLine
(
t
.
Positions
[
0
],
t
.
Positions
[
1
],
t
.
Colors
[
0
]);
d
rawLine
(
t
.
Positions
[
1
],
t
.
Positions
[
2
],
t
.
Colors
[
1
]);
d
rawLine
(
t
.
Positions
[
2
],
t
.
Positions
[
0
],
t
.
Colors
[
2
]);
D
rawLine
(
t
.
Positions
[
0
],
t
.
Positions
[
1
],
t
.
Colors
[
0
]);
D
rawLine
(
t
.
Positions
[
1
],
t
.
Positions
[
2
],
t
.
Colors
[
1
]);
D
rawLine
(
t
.
Positions
[
2
],
t
.
Positions
[
0
],
t
.
Colors
[
2
]);
}
#
endregion
//Screen space rasterization
void
r
asterizeTriangle
(
Triangle
t
)
void
R
asterizeTriangle
(
Triangle
t
)
{
var
v
=
t
.
Positions
;
//Find out the bounding box of current triangle.
...
...
URasterizer/Assets/URasterizer/Codes/RenderingObject.cs
浏览文件 @
ef056257
...
...
@@ -17,7 +17,28 @@ namespace URasterizer
public
RenderingObject
(
Mesh
mesh
)
{
this
.
mesh
=
mesh
;
this
.
mesh
=
mesh
;
}
// TRS
public
Matrix4x4
GetModelMatrix
()
{
if
(
transform
==
null
)
{
return
TransformTool
.
GetRotZMatrix
(
0
);
}
var
matScale
=
TransformTool
.
GetScaleMatrix
(
transform
.
lossyScale
);
var
rotation
=
transform
.
rotation
.
eulerAngles
;
var
rotX
=
TransformTool
.
GetRotationMatrix
(
Vector3
.
right
,
-
rotation
.
x
);
var
rotY
=
TransformTool
.
GetRotationMatrix
(
Vector3
.
up
,
rotation
.
y
);
var
rotZ
=
TransformTool
.
GetRotationMatrix
(
Vector3
.
forward
,
rotation
.
z
);
var
matRot
=
rotY
*
rotX
*
rotZ
;
// rotation apply order: z, x, y
var
matTranslation
=
TransformTool
.
GetTranslationMatrix
(
transform
.
position
);
return
matTranslation
*
matRot
*
matScale
;
}
}
}
URasterizer/Assets/URasterizer/Codes/TransformTool.cs
浏览文件 @
ef056257
...
...
@@ -28,7 +28,59 @@ namespace URasterizer
return
view
;
}
public
static
Matrix4x4
GetModelMatrix
(
float
rotation_angle
)
//Get the result of rotate a vector around axis with angle radius.
//axis must be normalized.
public
static
Vector3
RotateVector
(
Vector3
axis
,
Vector3
v
,
float
radius
)
{
Vector3
v_parallel
=
Vector3
.
Dot
(
axis
,
v
)
*
axis
;
Vector3
v_vertical
=
v
-
v_parallel
;
float
v_vertical_len
=
v_vertical
.
magnitude
;
Vector3
a
=
axis
;
Vector3
b
=
v_vertical
.
normalized
;
Vector3
c
=
Vector3
.
Cross
(
a
,
b
);
Vector3
v_vertical_rot
=
v_vertical_len
*
(
Mathf
.
Cos
(
radius
)
*
b
+
Mathf
.
Sin
(
radius
)
*
c
);
return
v_parallel
+
v_vertical_rot
;
}
public
static
Matrix4x4
GetRotationMatrix
(
Vector3
axis
,
float
angle
)
{
Vector3
vx
=
new
Vector3
(
1
,
0
,
0
);
Vector3
vy
=
new
Vector3
(
0
,
1
,
0
);
Vector3
vz
=
new
Vector3
(
0
,
0
,
1
);
axis
.
Normalize
();
float
radius
=
angle
*
D2R
;
var
tx
=
RotateVector
(
axis
,
vx
,
radius
);
var
ty
=
RotateVector
(
axis
,
vy
,
radius
);
var
tz
=
RotateVector
(
axis
,
vz
,
radius
);
Matrix4x4
rotMat
=
Matrix4x4
.
identity
;
rotMat
.
SetColumn
(
0
,
tx
);
rotMat
.
SetColumn
(
1
,
ty
);
rotMat
.
SetColumn
(
2
,
tz
);
return
rotMat
;
}
public
static
Matrix4x4
GetTranslationMatrix
(
Vector3
translate
)
{
Matrix4x4
translationMat
=
Matrix4x4
.
identity
;
translationMat
.
SetColumn
(
3
,
new
Vector4
(
translate
.
x
,
translate
.
y
,
-
translate
.
z
,
1
));
return
translationMat
;
}
public
static
Matrix4x4
GetScaleMatrix
(
Vector3
scale
)
{
Matrix4x4
scaleMat
=
Matrix4x4
.
identity
;
scaleMat
[
0
,
0
]
=
scale
.
x
;
scaleMat
[
1
,
1
]
=
scale
.
y
;
scaleMat
[
2
,
2
]
=
scale
.
z
;
return
scaleMat
;
}
public
static
Matrix4x4
GetRotZMatrix
(
float
rotation_angle
)
{
Matrix4x4
model
=
Matrix4x4
.
identity
;
...
...
URasterizer/Assets/URasterizer/URasterizerScene.unity
浏览文件 @
ef056257
...
...
@@ -158,7 +158,7 @@ RectTransform:
m_AnchorMin
:
{
x
:
0.5
,
y
:
0.5
}
m_AnchorMax
:
{
x
:
0.5
,
y
:
0.5
}
m_AnchoredPosition
:
{
x
:
0
,
y
:
0
}
m_SizeDelta
:
{
x
:
6
00
,
y
:
600
}
m_SizeDelta
:
{
x
:
8
00
,
y
:
600
}
m_Pivot
:
{
x
:
0.5
,
y
:
0.5
}
---
!u!114
&129304943
MonoBehaviour
:
...
...
@@ -288,7 +288,7 @@ Transform:
m_LocalScale
:
{
x
:
1
,
y
:
1
,
z
:
1
}
m_Children
:
[]
m_Father
:
{
fileID
:
0
}
m_RootOrder
:
5
m_RootOrder
:
4
m_LocalEulerAnglesHint
:
{
x
:
0
,
y
:
0
,
z
:
0
}
---
!u!1
&264331597
GameObject
:
...
...
@@ -318,7 +318,7 @@ Camera:
m_Enabled
:
1
serializedVersion
:
2
m_ClearFlags
:
2
m_BackGroundColor
:
{
r
:
0
,
g
:
0
,
b
:
0
,
a
:
0
}
m_BackGroundColor
:
{
r
:
0
.24528301
,
g
:
0.24528301
,
b
:
0.24528301
,
a
:
0
}
m_projectionMatrixMode
:
1
m_GateFitMode
:
2
m_FOVAxisMode
:
0
...
...
@@ -339,7 +339,7 @@ Camera:
m_Depth
:
-1
m_CullingMask
:
serializedVersion
:
2
m_Bits
:
0
m_Bits
:
4294967295
m_RenderingPath
:
-1
m_TargetTexture
:
{
fileID
:
0
}
m_TargetDisplay
:
0
...
...
@@ -378,8 +378,8 @@ MonoBehaviour:
m_Name
:
m_EditorClassIdentifier
:
rawImg
:
{
fileID
:
129304943
}
ClearColor
:
{
r
:
0
,
g
:
0
,
b
:
0
,
a
:
0.41960785
}
objNodes
:
[]
Angle
:
0
WireframeMode
:
0
---
!u!1
&870169031
GameObject
:
...
...
@@ -638,9 +638,10 @@ Transform:
m_LocalRotation
:
{
x
:
0
,
y
:
0
,
z
:
0
,
w
:
1
}
m_LocalPosition
:
{
x
:
0
,
y
:
1
,
z
:
-9.2
}
m_LocalScale
:
{
x
:
1
,
y
:
1
,
z
:
1
}
m_Children
:
[]
m_Children
:
-
{
fileID
:
1825649044
}
m_Father
:
{
fileID
:
0
}
m_RootOrder
:
7
m_RootOrder
:
6
m_LocalEulerAnglesHint
:
{
x
:
0
,
y
:
0
,
z
:
0
}
---
!u!1
&1297680259
GameObject
:
...
...
@@ -736,7 +737,7 @@ Transform:
m_LocalScale
:
{
x
:
1
,
y
:
1
,
z
:
1
}
m_Children
:
[]
m_Father
:
{
fileID
:
0
}
m_RootOrder
:
6
m_RootOrder
:
5
m_LocalEulerAnglesHint
:
{
x
:
0
,
y
:
0
,
z
:
0
}
---
!u!1
&1825649040
GameObject
:
...
...
@@ -827,12 +828,12 @@ Transform:
m_PrefabInstance
:
{
fileID
:
0
}
m_PrefabAsset
:
{
fileID
:
0
}
m_GameObject
:
{
fileID
:
1825649040
}
m_LocalRotation
:
{
x
:
0
,
y
:
0
,
z
:
0
,
w
:
1
}
m_LocalPosition
:
{
x
:
0
,
y
:
0
,
z
:
0
}
m_LocalRotation
:
{
x
:
-0
,
y
:
-0
,
z
:
-
0
,
w
:
1
}
m_LocalPosition
:
{
x
:
1.75
,
y
:
-1
,
z
:
9.2
}
m_LocalScale
:
{
x
:
1
,
y
:
1
,
z
:
1
}
m_Children
:
[]
m_Father
:
{
fileID
:
0
}
m_RootOrder
:
4
m_Father
:
{
fileID
:
963210798
}
m_RootOrder
:
0
m_LocalEulerAnglesHint
:
{
x
:
0
,
y
:
0
,
z
:
0
}
---
!u!1
&1933767904
GameObject
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录