提交 c265fe7b 编写于 作者: H happyfire

vertex colors

上级 0850a8e6
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<LangVersion>latest</LangVersion> <LangVersion>8.0</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
...@@ -52,14 +52,17 @@ ...@@ -52,14 +52,17 @@
<UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget> <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
<UnityVersion>2020.3.25f1c1</UnityVersion> <UnityVersion>2020.3.25f1c1</UnityVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Analyzer Include="C:\Program Files (x86)\Microsoft Visual Studio Tools for Unity\16.0\Analyzers\Microsoft.Unity.Analyzers.dll" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Assets\URasterizer\Codes\RenderingObject.cs" /> <Compile Include="Assets\URasterizer\Codes\RenderingObject.cs" />
<Compile Include="Assets\URasterizer\Codes\RenderingConfig.cs" />
<Compile Include="Assets\URasterizer\Codes\StatsPanel.cs" /> <Compile Include="Assets\URasterizer\Codes\StatsPanel.cs" />
<Compile Include="Assets\URasterizer\Codes\CameraRenderer.cs" /> <Compile Include="Assets\URasterizer\Codes\CameraRenderer.cs" />
<Compile Include="Assets\URasterizer\Codes\Triangle.cs" /> <Compile Include="Assets\URasterizer\Codes\Triangle.cs" />
<Compile Include="Assets\URasterizer\Codes\Rasterizer.cs" /> <Compile Include="Assets\URasterizer\Codes\Rasterizer.cs" />
<Compile Include="Assets\URasterizer\Codes\TransformTool.cs" /> <Compile Include="Assets\URasterizer\Codes\TransformTool.cs" />
<Compile Include="Assets\URasterizer\Codes\RenderingConfig.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="UnityEngine"> <Reference Include="UnityEngine">
......
...@@ -305,9 +305,6 @@ MonoBehaviour: ...@@ -305,9 +305,6 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mesh: {fileID: 0} mesh: {fileID: 0}
DoubleSideRendering: 0 DoubleSideRendering: 0
Color0: {r: 1, g: 0, b: 0, a: 1}
Color1: {r: 0, g: 1, b: 0, a: 1}
Color2: {r: 0, g: 0, b: 1, a: 1}
--- !u!1 &264331597 --- !u!1 &264331597
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -662,9 +659,6 @@ MonoBehaviour: ...@@ -662,9 +659,6 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mesh: {fileID: 0} mesh: {fileID: 0}
DoubleSideRendering: 0 DoubleSideRendering: 0
Color0: {r: 1, g: 0, b: 0, a: 1}
Color1: {r: 0, g: 1, b: 0, a: 1}
Color2: {r: 0, g: 0, b: 1, a: 1}
--- !u!95 &963210800 --- !u!95 &963210800
Animator: Animator:
serializedVersion: 3 serializedVersion: 3
...@@ -874,9 +868,6 @@ MonoBehaviour: ...@@ -874,9 +868,6 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mesh: {fileID: 0} mesh: {fileID: 0}
DoubleSideRendering: 0 DoubleSideRendering: 0
Color0: {r: 1, g: 0, b: 0, a: 1}
Color1: {r: 0, g: 1, b: 0, a: 1}
Color2: {r: 0, g: 0, b: 1, a: 1}
--- !u!1 &1825649040 --- !u!1 &1825649040
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -993,9 +984,6 @@ MonoBehaviour: ...@@ -993,9 +984,6 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mesh: {fileID: 0} mesh: {fileID: 0}
DoubleSideRendering: 0 DoubleSideRendering: 0
Color0: {r: 1, g: 0, b: 0, a: 1}
Color1: {r: 0, g: 1, b: 0, a: 1}
Color2: {r: 0, g: 0, b: 1, a: 1}
--- !u!1 &1899433433 --- !u!1 &1899433433
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
......
...@@ -143,9 +143,11 @@ namespace URasterizer ...@@ -143,9 +143,11 @@ namespace URasterizer
//1. z轴反转 //1. z轴反转
//2. 三角形顶点环绕方向从顺时针改成逆时针 //2. 三角形顶点环绕方向从顺时针改成逆时针
int idx0 = indices[i]; //注意这儿对调了v0和v1的索引,因为原来的 0,1,2是顺时针的,对调后是 1,0,2是逆时针的
int idx1 = indices[i+2]; //注意这儿对调了v1和v2的索引,因为原来的 0,1,2是顺时针的,对调后是 0,2,1,是逆时针的 //Unity Quard模型的两个三角形索引分别是 0,3,1,3,0,2 转换后为 3,0,1,0,3,2
int idx2 = indices[i+1]; int idx0 = indices[i+1];
int idx1 = indices[i];
int idx2 = indices[i+2];
//vertex shader //vertex shader
...@@ -181,7 +183,7 @@ namespace URasterizer ...@@ -181,7 +183,7 @@ namespace URasterizer
Vector3 e01 = v1 - v0; Vector3 e01 = v1 - v0;
Vector3 e02 = v2 - v0; Vector3 e02 = v2 - v0;
Vector3 cross = Vector3.Cross(e01, e02); Vector3 cross = Vector3.Cross(e01, e02);
if (cross.z <= 0) if (cross.z < 0)
{ {
continue; continue;
} }
...@@ -208,9 +210,21 @@ namespace URasterizer ...@@ -208,9 +210,21 @@ namespace URasterizer
{ {
t.SetPosition(k, v[k]); t.SetPosition(k, v[k]);
} }
t.SetColor(0, ro.Color0);
t.SetColor(1, ro.Color1); //设置顶点色,使用config中的颜色数组循环设置
t.SetColor(2, ro.Color2); int vertexColorCount = _config.VertexColors.Length;
if(vertexColorCount > 0)
{
t.SetColor(0, _config.VertexColors[idx0 % vertexColorCount]);
t.SetColor(1, _config.VertexColors[idx1 % vertexColorCount]);
t.SetColor(2, _config.VertexColors[idx2 % vertexColorCount]);
}
else
{
t.SetColor(0, Color.white);
t.SetColor(1, Color.white);
t.SetColor(2, Color.white);
}
//Rasterization //Rasterization
if (_config.WireframeMode) if (_config.WireframeMode)
...@@ -251,7 +265,7 @@ namespace URasterizer ...@@ -251,7 +265,7 @@ namespace URasterizer
} }
#region Wireframe mode #region Wireframe mode
private void DrawLine(Vector3 begin, Vector3 end, Color line_color) private void DrawLine(Vector3 begin, Vector3 end, Color colorBegin, Color colorEnd)
{ {
int x1 = Mathf.FloorToInt(begin.x); int x1 = Mathf.FloorToInt(begin.x);
int y1 = Mathf.FloorToInt(begin.y); int y1 = Mathf.FloorToInt(begin.y);
...@@ -265,7 +279,7 @@ namespace URasterizer ...@@ -265,7 +279,7 @@ namespace URasterizer
dx1 = Math.Abs(dx); dx1 = Math.Abs(dx);
dy1 = Math.Abs(dy); dy1 = Math.Abs(dy);
px = 2 * dy1 - dx1; px = 2 * dy1 - dx1;
py = 2 * dx1 - dy1; py = 2 * dx1 - dy1;
if (dy1 <= dx1) if (dy1 <= dx1)
{ {
...@@ -282,6 +296,7 @@ namespace URasterizer ...@@ -282,6 +296,7 @@ namespace URasterizer
xe = x1; xe = x1;
} }
Vector3 point = new Vector3(x, y, 1.0f); Vector3 point = new Vector3(x, y, 1.0f);
Color line_color = dx >= 0 ? colorBegin : colorEnd;
SetPixel(point, line_color); SetPixel(point, line_color);
for (i = 0; x < xe; i++) for (i = 0; x < xe; i++)
{ {
...@@ -304,6 +319,8 @@ namespace URasterizer ...@@ -304,6 +319,8 @@ namespace URasterizer
} }
Vector3 pt = new Vector3(x, y, 1.0f); Vector3 pt = new Vector3(x, y, 1.0f);
float t = (float)(xe - x) / dx1;
line_color = Color.Lerp(colorBegin, colorEnd, t);
SetPixel(pt, line_color); SetPixel(pt, line_color);
} }
} }
...@@ -322,7 +339,9 @@ namespace URasterizer ...@@ -322,7 +339,9 @@ namespace URasterizer
ye = y1; ye = y1;
} }
Vector3 point = new Vector3(x, y, 1.0f); Vector3 point = new Vector3(x, y, 1.0f);
Color line_color = dy >= 0 ? colorBegin : colorEnd;
SetPixel(point, line_color); SetPixel(point, line_color);
for (i = 0; y < ye; i++) for (i = 0; y < ye; i++)
{ {
y++; y++;
...@@ -343,6 +362,8 @@ namespace URasterizer ...@@ -343,6 +362,8 @@ namespace URasterizer
py += 2 * (dx1 - dy1); py += 2 * (dx1 - dy1);
} }
Vector3 pt = new Vector3(x, y, 1.0f); Vector3 pt = new Vector3(x, y, 1.0f);
float t = (float)(ye - y) / dy1;
line_color = Color.Lerp(colorBegin, colorEnd, t);
SetPixel(pt, line_color); SetPixel(pt, line_color);
} }
} }
...@@ -350,9 +371,9 @@ namespace URasterizer ...@@ -350,9 +371,9 @@ namespace URasterizer
private void RasterizeWireframe(Triangle t) private void RasterizeWireframe(Triangle t)
{ {
DrawLine(t.Positions[0], t.Positions[1], t.Colors[0]); DrawLine(t.Positions[0], t.Positions[1], t.Colors[0], t.Colors[1]);
DrawLine(t.Positions[1], t.Positions[2], t.Colors[1]); DrawLine(t.Positions[1], t.Positions[2], t.Colors[1], t.Colors[2]);
DrawLine(t.Positions[2], t.Positions[0], t.Colors[2]); DrawLine(t.Positions[2], t.Positions[0], t.Colors[2], t.Colors[0]);
} }
#endregion #endregion
......
...@@ -9,4 +9,7 @@ public class RenderingConfig : ScriptableObject ...@@ -9,4 +9,7 @@ public class RenderingConfig : ScriptableObject
public bool WireframeMode = false; public bool WireframeMode = false;
public bool BackfaceCulling = true; public bool BackfaceCulling = true;
public bool MSAA = false; public bool MSAA = false;
[Header("Vertex Color Setting")]
public Color[] VertexColors;
} }
...@@ -7,9 +7,6 @@ namespace URasterizer ...@@ -7,9 +7,6 @@ namespace URasterizer
{ {
public Mesh mesh; public Mesh mesh;
public bool DoubleSideRendering; public bool DoubleSideRendering;
public Color Color0 = Color.red;
public Color Color1 = Color.green;
public Color Color2 = Color.blue;
private void Awake() private void Awake()
{ {
......
...@@ -16,3 +16,7 @@ MonoBehaviour: ...@@ -16,3 +16,7 @@ MonoBehaviour:
WireframeMode: 0 WireframeMode: 0
BackfaceCulling: 1 BackfaceCulling: 1
MSAA: 0 MSAA: 0
VertexColors:
- {r: 1, g: 0, b: 0, a: 1}
- {r: 0, g: 1, b: 0, a: 1}
- {r: 0, g: 0, b: 1, a: 1}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册