提交 ef056257 编写于 作者: H happyfire

- transform tool

上级 37594523
......@@ -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.backgroundColor;
_rasterizer.ClearColor = ClearColor;
}
......@@ -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();
......
......@@ -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)
{
rasterizeWireframe(t);
RasterizeWireframe(t);
}
else
{
rasterizeTriangle(t);
RasterizeTriangle(t);
}
}
}
#region Wireframe mode
private void drawLine(Vector3 begin, Vector3 end, Color line_color)
private void DrawLine(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 rasterizeWireframe(Triangle t)
private void RasterizeWireframe(Triangle t)
{
drawLine(t.Positions[0], t.Positions[1], t.Colors[0]);
drawLine(t.Positions[1], t.Positions[2], t.Colors[1]);
drawLine(t.Positions[2], t.Positions[0], t.Colors[2]);
DrawLine(t.Positions[0], t.Positions[1], t.Colors[0]);
DrawLine(t.Positions[1], t.Positions[2], t.Colors[1]);
DrawLine(t.Positions[2], t.Positions[0], t.Colors[2]);
}
#endregion
//Screen space rasterization
void rasterizeTriangle(Triangle t)
void RasterizeTriangle(Triangle t)
{
var v = t.Positions;
//Find out the bounding box of current triangle.
......
......@@ -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;
}
}
}
......@@ -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;
......
......@@ -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: 600, y: 600}
m_SizeDelta: {x: 800, 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.
先完成此消息的编辑!
想要评论请 注册