Rasterizer.cs 15.0 KB
Newer Older
H
Init  
happyfire 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
using System;
using System.Collections.Generic;
using UnityEngine;

namespace URasterizer
{
    public enum BufferMask
    {
        Color = 1,
        Depth = 2
    }

    public class Rasterizer
    {
        int _width;
        int _height;

        bool _MSAA;

        Matrix4x4 _matModel;
        Matrix4x4 _matView;
        Matrix4x4 _matProjection;

        public Matrix4x4 ModelMatrix
        {
            get
            {
                return _matModel;
            }
            set
            {
                _matModel = value;
            }
        }

        public Matrix4x4 ViewMatrix
        {
            get
            {
                return _matView;
            }
            set
            {
                _matView = value;
            }
        }

        public Matrix4x4 ProjectionMatrix
        {
            get
            {
                return _matProjection;
            }
            set
            {
                _matProjection = value;
            }
        }

        Color[] frame_buf;
        float[] depth_buf;

        public Texture2D texture;

        public Color ClearColor { get; set; }

H
clip  
happyfire 已提交
67 68 69 70
        //Stats
        public int Stats_Triangles;
        public int Stats_Vertices;

H
Init  
happyfire 已提交
71 72
        public Rasterizer(int w, int h)
        {
H
happyfire 已提交
73 74
            Debug.Log($"Rasterizer screen size: {w}x{h}");

H
Init  
happyfire 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
            _width = w;
            _height = h;

            frame_buf = new Color[w * h];
            depth_buf = new float[w * h];

            texture = new Texture2D(w, h);
            texture.filterMode = FilterMode.Point;
        }

        public float Aspect
        {
            get
            {
H
clip  
happyfire 已提交
89
                return (float)_width / _height;
H
Init  
happyfire 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            }
        }

        void FillArray<T>(T[] arr, T value)
        {
            int i = 0;
            if(arr.Length > 16)
            {
                do
                {
                    arr[i++] = value;
                } while (i < arr.Length);

                while( i + 16 < arr.Length)
                {
                    Array.Copy(arr, 0, arr, i, 16);
                    i += 16;
                }
            }
            while (i < arr.Length)
            {
                arr[i++] = value;
            }
        }

        public void Clear(BufferMask mask)
        {
            if((mask & BufferMask.Color) == BufferMask.Color)
            {                
                FillArray(frame_buf, ClearColor);
            }
            if((mask & BufferMask.Depth) == BufferMask.Depth)
            {
                FillArray(depth_buf, float.MaxValue);
            }
        }

        public void SetupViewProjectionMatrix(Camera camera)
        {
            var camPos = camera.transform.position;
            camPos.z *= -1;
            ViewMatrix = TransformTool.GetViewMatrix(camPos);

            if (camera.orthographic)
            {
                float halfOrthSize = camera.orthographicSize;
                float f = -camera.farClipPlane;
                float n = -camera.nearClipPlane;
                ProjectionMatrix = TransformTool.GetOrthographicProjectionMatrix(-halfOrthSize, halfOrthSize, -halfOrthSize, halfOrthSize, f, n);
            }
            else
            {
                ProjectionMatrix = TransformTool.GetProjectionMatrix(camera.fieldOfView, Aspect, camera.nearClipPlane, camera.farClipPlane);
            }
        }

H
happyfire 已提交
146
        public void Draw(RenderingObject ro, Camera camera, bool wireframeMode=false)
H
Init  
happyfire 已提交
147
        {
H
happyfire 已提交
148
            Mesh mesh = ro.mesh;
H
Init  
happyfire 已提交
149 150
            SetupViewProjectionMatrix(camera);

H
happyfire 已提交
151
            ModelMatrix = ro.GetModelMatrix();
H
Init  
happyfire 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            

            float far = camera.farClipPlane;
            float near = camera.nearClipPlane;
            float f1 = (far - near) / 2.0f;
            float f2 = (far + near) / 2.0f;

            Matrix4x4 mvp = _matProjection * _matView * _matModel;

            var indices = mesh.triangles;
            for(int i=0; i< indices.Length; i+=3)
            {
                int idx0 = indices[i];
                int idx1 = indices[i+1];
                int idx2 = indices[i+2];

                //vertex shader

                //world to clip space
                Vector4[] v =
                {
                    mvp * new Vector4(mesh.vertices[idx0].x, mesh.vertices[idx0].y, mesh.vertices[idx0].z, 1),
                    mvp * new Vector4(mesh.vertices[idx1].x, mesh.vertices[idx1].y, mesh.vertices[idx1].z, 1),
                    mvp * new Vector4(mesh.vertices[idx2].x, mesh.vertices[idx2].y, mesh.vertices[idx2].z, 1),
                };
H
clip  
happyfire 已提交
177 178 179 180 181 182 183

                //do clipping
                if (Clipped(v))
                {
                    continue;
                }

H
Init  
happyfire 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                
                //clip space to NDC (Perspective division)                 
                for(int k=0; k<3; k++)
                {
                    v[k].x /= v[k].w;
                    v[k].y /= v[k].w;
                    v[k].z /= v[k].w;                    
                }

                //NDC to screen space
                for (int k = 0; k < 3; k++)
                {
                    var vec = v[k];
                    vec.x = 0.5f * _width * (vec.x + 1.0f);
                    vec.y = 0.5f * _height * (vec.y + 1.0f);
                    vec.z = vec.z * f1 + f2;
                    v[k] = vec;
                }

                //Triangle setup
                Triangle t = new Triangle();
                for(int k=0; k<3; k++)
                {
                    t.SetPosition(k, v[k]);
                }
                t.SetColor(0, Color.red);
                t.SetColor(1, Color.green);
                t.SetColor(2, Color.blue);

                if (wireframeMode)
                {
H
happyfire 已提交
215
                    RasterizeWireframe(t);
H
Init  
happyfire 已提交
216 217 218
                }
                else
                {
H
happyfire 已提交
219
                    RasterizeTriangle(t);
H
Init  
happyfire 已提交
220 221 222 223 224
                }
                
            }
        }

H
clip  
happyfire 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        bool Clipped(Vector4[] v)
        {
            //裁剪(仅整体剔除)            
            for (int i = 0; i < 3; ++i)
            {
                var vertex = v[i];
                var w = -vertex.w;
                bool inside = (vertex.x <= w && vertex.x >= -w
                    && vertex.y <= w && vertex.y >= -w
                    && vertex.z <= w && vertex.z >= -w);
                if (inside)
                {             
                    //不裁剪三角形,只要有任意一点在clip space中则三角形整体保留
                    return false;
                }
            }

            //三个顶点都不在三角形中则剔除
            return true;
        }

H
Init  
happyfire 已提交
246
        #region Wireframe mode
H
happyfire 已提交
247
        private void DrawLine(Vector3 begin, Vector3 end, Color line_color)
H
Init  
happyfire 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        {            
            int x1 = Mathf.FloorToInt(begin.x);
            int y1 = Mathf.FloorToInt(begin.y);
            int x2 = Mathf.FloorToInt(end.x);
            int y2 = Mathf.FloorToInt(end.y);            

            int x, y, dx, dy, dx1, dy1, px, py, xe, ye, i;

            dx = x2 - x1;
            dy = y2 - y1;
            dx1 = Math.Abs(dx);
            dy1 = Math.Abs(dy);
            px = 2 * dy1 - dx1;
            py = 2 * dx1 - dy1;

            if (dy1 <= dx1)
            {
                if (dx >= 0)
                {
                    x = x1;
                    y = y1;
                    xe = x2;
                }
                else
                {
                    x = x2;
                    y = y2;
                    xe = x1;
                }
                Vector3 point = new Vector3(x, y, 1.0f);
                SetPixel(point, line_color);
                for (i = 0; x < xe; i++)
                {
H
happyfire 已提交
281
                    x++;
H
Init  
happyfire 已提交
282 283
                    if (px < 0)
                    {
H
happyfire 已提交
284
                        px += 2 * dy1;
H
Init  
happyfire 已提交
285 286 287 288 289
                    }
                    else
                    {
                        if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
                        {
H
happyfire 已提交
290
                            y++;
H
Init  
happyfire 已提交
291 292 293
                        }
                        else
                        {
H
happyfire 已提交
294
                            y--;
H
Init  
happyfire 已提交
295
                        }
H
happyfire 已提交
296
                        px +=  2 * (dy1 - dx1);
H
Init  
happyfire 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                    }
                    
                    Vector3 pt = new Vector3(x, y, 1.0f);
                    SetPixel(pt, line_color);
                }
            }
            else
            {
                if (dy >= 0)
                {
                    x = x1;
                    y = y1;
                    ye = y2;
                }
                else
                {
                    x = x2;
                    y = y2;
                    ye = y1;
                }
                Vector3 point = new Vector3(x, y, 1.0f);
                SetPixel(point, line_color);
                for (i = 0; y < ye; i++)
                {
H
happyfire 已提交
321
                    y++;
H
Init  
happyfire 已提交
322 323
                    if (py <= 0)
                    {
H
happyfire 已提交
324
                        py += 2 * dx1;
H
Init  
happyfire 已提交
325 326 327 328 329
                    }
                    else
                    {
                        if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
                        {
H
happyfire 已提交
330
                            x++;
H
Init  
happyfire 已提交
331 332 333
                        }
                        else
                        {
H
happyfire 已提交
334
                            x--;
H
Init  
happyfire 已提交
335
                        }
H
happyfire 已提交
336
                        py += 2 * (dx1 - dy1);
H
Init  
happyfire 已提交
337 338 339 340 341 342 343
                    }
                    Vector3 pt = new Vector3(x, y, 1.0f);
                    SetPixel(pt, line_color);
                }
            }
        }

H
happyfire 已提交
344
        private void RasterizeWireframe(Triangle t)
H
Init  
happyfire 已提交
345
        {
H
happyfire 已提交
346 347 348
            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]);
H
Init  
happyfire 已提交
349 350 351 352
        }

        #endregion

H
clip  
happyfire 已提交
353 354
        

H
Init  
happyfire 已提交
355
        //Screen space  rasterization
H
happyfire 已提交
356
        void RasterizeTriangle(Triangle t)
H
Init  
happyfire 已提交
357 358
        {
            var v = t.Positions;
H
clip  
happyfire 已提交
359
            
H
Init  
happyfire 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
            //Find out the bounding box of current triangle.
            float minX = v[0].x;
            float maxX = minX;
            float minY = v[0].y;
            float maxY = minY;

            for(int i=1; i<3; ++i)
            {
                float x = v[i].x;
                if(x < minX)
                {
                    minX = x;
                } else if(x > maxX)
                {
                    maxX = x;
                }
                float y = v[i].y;
                if(y < minY)
                {
                    minY = y;
                }else if(y > maxY)
                {
                    maxY = y;
                }
            }

            int minPX = Mathf.FloorToInt(minX);
            minPX = minPX < 0 ? 0 : minPX;
            int maxPX = Mathf.CeilToInt(maxX);
            maxPX = maxPX > _width ? _width : maxPX;
            int minPY = Mathf.FloorToInt(minY);
            minPY = minPY < 0 ? 0 : minPY;
            int maxPY = Mathf.CeilToInt(maxY);
            maxPY = maxPY > _height ? _height : maxPY;

            if (_MSAA)
            {

            }
            else
H
clip  
happyfire 已提交
400
            {                
H
Init  
happyfire 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
                // 遍历当前三角形包围中的所有像素,判断当前像素是否在三角形中
                // 对于在三角形中的像素,使用重心坐标插值得到深度值,并使用z buffer进行深度测试和写入
                for(int y = minPY; y < maxPY; ++y)
                {
                    for(int x = minPX; x < maxPX; ++x)
                    {
                        if(IsInsideTriangle(x, y, t))
                        {
                            //计算重心坐标
                            var c = ComputeBarycentric2D(x, y, t);
                            float alpha = c.x;
                            float beta = c.y;
                            float gamma = c.z;
                            //透视校正插值
                            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;
                            z_interpolated *= w_reciprocal;
                            //深度测试
                            int index = GetIndex(x, y);
                            if(-z_interpolated < depth_buf[index])
                            {
                                depth_buf[index] = -z_interpolated;
                                Color color_interpolated = alpha * t.Colors[0] / v[0].w + beta * t.Colors[1] / v[1].w + gamma * t.Colors[2] / v[2].w;
                                color_interpolated *= w_reciprocal;
                                frame_buf[index] = color_interpolated;
                            }
                        }                        
                    }
                }
            }
        }

        bool IsInsideTriangle(int x, int y, Triangle t, float offsetX=0.5f, float offsetY=0.5f)
        {
            Vector3[] v = new Vector3[3];
            for(int i=0; i<3; ++i)
            {
                v[i] = new Vector3(t.Positions[i].x, t.Positions[i].y, t.Positions[i].z);
            }

            //当前像素中心位置p
            Vector3 p = new Vector3(x + offsetX, y + offsetY, 0);            
            
            Vector3 v0p = p - v[0]; v0p[2] = 0;
            Vector3 v01 = v[1] - v[0]; v01[2] = 0;
            Vector3 cross0p = Vector3.Cross(v0p, v01);

            Vector3 v1p = p - v[1]; v1p[2] = 0;
            Vector3 v12 = v[2] - v[1]; v12[2] = 0;
            Vector3 cross1p = Vector3.Cross(v1p, v12);

            if(cross0p.z * cross1p.z > 0)
            {
                Vector3 v2p = p - v[2]; v2p[2] = 0;
                Vector3 v20 = v[0] - v[2]; v20[2] = 0;
                Vector3 cross2p = Vector3.Cross(v2p, v20);
                if(cross2p.z * cross1p.z > 0)
                {
                    return true;
                }
            }

            return false;
        }

        Vector3 ComputeBarycentric2D(float x, float y, Triangle t)
        {
            var v = t.Positions;
            float c1 = (x * (v[1].y - v[2].y) + (v[2].x - v[1].x) * y + v[1].x * v[2].y - v[2].x * v[1].y) / (v[0].x * (v[1].y - v[2].y) + (v[2].x - v[1].x) * v[0].y + v[1].x * v[2].y - v[2].x * v[1].y);
            float c2 = (x * (v[2].y - v[0].y) + (v[0].x - v[2].x) * y + v[2].x * v[0].y - v[0].x * v[2].y) / (v[1].x * (v[2].y - v[0].y) + (v[0].x - v[2].x) * v[1].y + v[2].x * v[0].y - v[0].x * v[2].y);
            float c3 = (x * (v[0].y - v[1].y) + (v[1].x - v[0].x) * y + v[0].x * v[1].y - v[1].x * v[0].y) / (v[2].x * (v[0].y - v[1].y) + (v[1].x - v[0].x) * v[2].y + v[0].x * v[1].y - v[1].x * v[0].y);
            return new Vector3(c1, c2, c3);
        }

        public int GetIndex(int x, int y)
        {
            return y * _width + x;
        }

        public void SetPixel(Vector3 point, Color color)
        {
            if(point.x < 0 || point.x >= _width || point.y < 0 || point.y >= _height)
            {
                return;
            }

            int idx = (int)point.y * _width + (int)point.x;
            frame_buf[idx] = color;
        }

        public void UpdateFrame()
        {

            //SetPixel(new Vector3(0,0), Color.red);
            //SetPixel(new Vector3(0, _height-1), Color.green);

            texture.SetPixels(frame_buf);
            texture.Apply();
        }


    }
}