{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/rendering-paths-light-types/",
  "atlas": "https://www.pystone.net/?node=rendering-paths-light-types#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/rendering-paths-light-types.md",
  "context": "https://www.pystone.net/notes/rendering-paths-light-types.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:rendering-paths-light-types",
  "slug": "rendering-paths-light-types",
  "title": "【光照】渲染路径与光源类型",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "【光照】渲染路径与光源类型",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "40a8e72b1ca4657df5a033c6a9f64943e520511b6978a2cca2a90e5deaf6217c",
  "assets": [
    {
      "reference": "assets/1615550219384.png",
      "url": "/media/ae73ac39e886b7e27850.png",
      "mediaType": "image/png",
      "contentHash": "ae73ac39e886b7e27850dac9ce791a1f6d051a87f49e6052e7f0911deb87ac8c",
      "byteLength": 75155,
      "width": 960,
      "height": 447
    },
    {
      "reference": "assets/1615550226942.png",
      "url": "/media/d936d98091ee5b54ede8.png",
      "mediaType": "image/png",
      "contentHash": "d936d98091ee5b54ede8b695ab5369980a800bb5a2fbd5696ea550856264f581",
      "byteLength": 29495,
      "width": 968,
      "height": 196
    },
    {
      "reference": "assets/1615550370760.png",
      "url": "/media/d5e2bce3c4bda73ec891.png",
      "mediaType": "image/png",
      "contentHash": "d5e2bce3c4bda73ec891c8da3473205e6018926a60d4e568715553785b01acc5",
      "byteLength": 112096,
      "width": 979,
      "height": 498
    },
    {
      "reference": "assets/1615550382678.png",
      "url": "/media/9e130b8548e4e403ec37.png",
      "mediaType": "image/png",
      "contentHash": "9e130b8548e4e403ec375051f2d27e136c2e2e08331bdc90e05cc4111f63d56d",
      "byteLength": 18163,
      "width": 966,
      "height": 87
    },
    {
      "reference": "assets/1615558063917.png",
      "url": "/media/412635250664c91ae5fb.png",
      "mediaType": "image/png",
      "contentHash": "412635250664c91ae5fbb997fe9b3c38d51e3f3a57adfee6d8415e23efcd40fc",
      "byteLength": 26037,
      "width": 419,
      "height": 329
    },
    {
      "reference": "assets/1615553232623.png",
      "url": "/media/df38a620876c54d2432d.png",
      "mediaType": "image/png",
      "contentHash": "df38a620876c54d2432d5a3f90ef84b8f5eb8a97de843a93360a3460616d52ee",
      "byteLength": 23433,
      "width": 1030,
      "height": 161
    },
    {
      "reference": "assets/1615551537223.png",
      "url": "/media/529c2681cd83cc31c767.png",
      "mediaType": "image/png",
      "contentHash": "529c2681cd83cc31c7676ed795859b7054f785455c5e0950b0bab8d7db7ba1cd",
      "byteLength": 80769,
      "width": 985,
      "height": 449
    },
    {
      "reference": "assets/1615551546915.png",
      "url": "/media/6656a93d65d0ed209543.png",
      "mediaType": "image/png",
      "contentHash": "6656a93d65d0ed2095437dacd82b4f46761eb5d8cc6183fc200ad823becc3d09",
      "byteLength": 74508,
      "width": 964,
      "height": 272
    },
    {
      "reference": "assets/1615553346669.png",
      "url": "/media/09d77a997f88674ab0b8.png",
      "mediaType": "image/png",
      "contentHash": "09d77a997f88674ab0b8c7cd1d3e336ebc17c8a2fd9d9e6ed95c6f286cdad773",
      "byteLength": 120613,
      "width": 1501,
      "height": 715
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "【光照】渲染路径与光源类型",
      "anchor": "光照渲染路径与光源类型",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%85%89%E7%85%A7%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84%E4%B8%8E%E5%85%89%E6%BA%90%E7%B1%BB%E5%9E%8B"
    },
    {
      "depth": 2,
      "text": "渲染路径",
      "anchor": "渲染路径",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84"
    },
    {
      "depth": 3,
      "text": "前向渲染路径",
      "anchor": "前向渲染路径",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%89%8D%E5%90%91%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84"
    },
    {
      "depth": 4,
      "text": "Unity的光照处理方式",
      "anchor": "unity的光照处理方式",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#unity%E7%9A%84%E5%85%89%E7%85%A7%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F"
    },
    {
      "depth": 4,
      "text": "RenderMode Tag",
      "anchor": "rendermode-tag",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#rendermode-tag"
    },
    {
      "depth": 5,
      "text": "Bass Pass",
      "anchor": "bass-pass",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#bass-pass"
    },
    {
      "depth": 5,
      "text": "Additional Pass",
      "anchor": "additional-pass",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#additional-pass"
    },
    {
      "depth": 4,
      "text": "可以使用的光照变量和函数",
      "anchor": "可以使用的光照变量和函数",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E7%9A%84%E5%85%89%E7%85%A7%E5%8F%98%E9%87%8F%E5%92%8C%E5%87%BD%E6%95%B0"
    },
    {
      "depth": 4,
      "text": "缺点",
      "anchor": "缺点",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E7%BC%BA%E7%82%B9"
    },
    {
      "depth": 4,
      "text": "实际渲染过程",
      "anchor": "实际渲染过程",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%AE%9E%E9%99%85%E6%B8%B2%E6%9F%93%E8%BF%87%E7%A8%8B"
    },
    {
      "depth": 3,
      "text": "延迟渲染路径",
      "anchor": "延迟渲染路径",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%BB%B6%E8%BF%9F%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84"
    },
    {
      "depth": 4,
      "text": "特点",
      "anchor": "特点",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E7%89%B9%E7%82%B9"
    },
    {
      "depth": 4,
      "text": "局限性",
      "anchor": "局限性",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%B1%80%E9%99%90%E6%80%A7"
    },
    {
      "depth": 4,
      "text": "Unity G-buffer",
      "anchor": "unity-g-buffer",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#unity-g-buffer"
    },
    {
      "depth": 4,
      "text": "可访问变量和函数",
      "anchor": "可访问变量和函数",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%8F%AF%E8%AE%BF%E9%97%AE%E5%8F%98%E9%87%8F%E5%92%8C%E5%87%BD%E6%95%B0"
    },
    {
      "depth": 3,
      "text": "顶点照明渲染路径",
      "anchor": "顶点照明渲染路径",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E9%A1%B6%E7%82%B9%E7%85%A7%E6%98%8E%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84"
    },
    {
      "depth": 3,
      "text": "Comparison",
      "anchor": "comparison",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#comparison"
    },
    {
      "depth": 2,
      "text": "Unity的光源类型",
      "anchor": "unity的光源类型",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#unity%E7%9A%84%E5%85%89%E6%BA%90%E7%B1%BB%E5%9E%8B"
    },
    {
      "depth": 3,
      "text": "平行光",
      "anchor": "平行光",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E5%B9%B3%E8%A1%8C%E5%85%89"
    },
    {
      "depth": 3,
      "text": "点光源",
      "anchor": "点光源",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E7%82%B9%E5%85%89%E6%BA%90"
    },
    {
      "depth": 3,
      "text": "聚光灯",
      "anchor": "聚光灯",
      "citation": "https://www.pystone.net/notes/rendering-paths-light-types/#%E8%81%9A%E5%85%89%E7%81%AF"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:game-graphics-and-runtime",
      "title": "游戏图形与运行时",
      "url": "https://www.pystone.net/notes/game-graphics-and-runtime/",
      "atlas": "https://www.pystone.net/?node=game-graphics-and-runtime#knowledge-atlas",
      "label": "游戏图形与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "渲染基础中的“【光照】渲染路径与光源类型”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#%E6%B8%B2%E6%9F%93%E5%9F%BA%E7%A1%80"
    }
  ],
  "contentMarkdown": "# 【光照】渲染路径与光源类型\n\n> 创建时间：2021/3/12 19:53\n\n  * 【光照】渲染路径与光源类型\n    * 渲染路径\n      * 前向渲染路径\n        * Unity的光照处理方式\n        * RenderMode Tag\n          * Bass Pass\n          * Additional Pass\n        * 可以使用的光照变量和函数\n        * 缺点\n        * 实际渲染过程\n      * 延迟渲染路径\n        * 特点\n        * 局限性\n        * Unity G-buffer\n        * 可访问变量和函数\n      * 顶点照明渲染路径\n      * Comparison\n    * Unity的光源类型\n      * 平行光\n      * 点光源\n      * 聚光灯\n\n## 渲染路径\n\nA rendering path is a series of operations related to lighting and shading.\n\n### 前向渲染路径\n\n```cpp\nstring s = String.Empty;\n\n```\n\n如果物体在n个逐像素光源的影响区域内,那么该物体需要执行n个Pass,每个Pass计算逐像素光源的光照结果,然后在帧缓冲中把这些光照结果混合起来得到最终的颜色值。\n\n#### Unity的光照处理方式\n\n在Unity中前向渲染有三种处理(即照亮物体)的方式： **逐顶点处理** 、 **逐像素处理** 、 **球谐函数(SH)** 处理。\n\n根据光源设置以及光源对物体的影响程度(该物体的远近、光源强度等),按照重要程序对光源进行排序\n\n  * 一定数目的光源进行逐像素方式处理\n\n    * 最亮的平行光\n\n    * Light Component -> 渲染模式(Render Mode) == Important\n\n  * 最多四个光源按逐顶点方式处理\n\n    * Not Important\n\n  * 剩下就使用最差的SH方式处理\n\n    * Not Important\n\n如果以上规则得到的像素光源数量效于Quality Setting中的逐像素光源数量(Pixel Light Count)，会有更多的光源逐像素处理\n\n#### RenderMode Tag\n\n告诉Unity该Pass在渲染路径中的位置，引擎会据此进行一些相关计算并填充一些内置变量。\n\n一个Unity Shader通常会定义一个Base Pass（ Base Pas 也可定义多次， 例如需要双面渲染等情况）以及一个 Additional Pass。\n\n  * 一个 Base Pass仅会执行 一次\n\n  * 每个逐像素光源会执行一次Additional Pass\n\n##### Bass Pass\n\n`# pragma multi_compile_fwdbase`\n\n> 使用编译指令，才可以在Pass得到一些正确的变量，如光照衰减值等\n\n计算——一个 **逐像素** 的 **平行光** \\+ 所有逐顶点和SH光源。只计算一次。\n\n可实现的光照效果：\n\n  * **光照纹理**\n\n  * 环境光\n\n  * 自发光\n\n  * 阴影（平行光的阴影）\n\n##### Additional Pass\n\n`# pragma multi_compile_fwdadd`\n\n计算——其他影响该物体的逐像素光源，每个光源执行一次Pass\n\n  * 默认情况下没有阴影效果\n\n  * 可以使用`#pragma multi_compile_fwdadd_fullshadows`代替`# pragma multi_compile_fwdadd`，为点光源和聚光灯开启阴影效果\n\n  * 设置混合模式`Blend One One` \\- 每个Additional Pass可以与上一次计算的结果在帧缓存中叠加。(若不开启，会覆盖之前的结果)\n\n#### 可以使用的光照变量和函数\n\n![Alt text](assets/1615550219384.png)\n\n![Alt text](assets/1615550226942.png)\n\n![Alt text](assets/1615550370760.png)\n\n![Alt text](assets/1615550382678.png)\n\n#### 缺点\n\n当场景中包含大量实时光源时，前向渲染的性能会急速下降。\n\n每个物体多个Pass，每执行一个Pass都需要重新渲染一遍物体，很多计算是重复的。\n\n#### 实际渲染过程\n\n![Alt text](assets/1615558063917.png)\n\n  1. 清除颜色、深度和模板缓冲，为后面的渲染做准备\n\n  2. 利用Base Pass，将平行光的光照渲染到Frame Buffer中\n\n  3. 使用Additional Pass，依次将4个点光源的光照应用到物体上，得到最后的渲染结果。\n\n渲染点光源的顺序——按照重要程度排序，因素有:\n\n  * 颜色\n\n  * 强度\n\n  * 距离\nUnity具体的做法没有官方说明\n\n### 延迟渲染路径\n\nG-buffer (Geometry buffer) - 存储了我们所关心的表面（离摄像机最近的表面）的其他信息（法线，位置，视角方向，用于光照计算的材质属性等）。\n\n两个Pass：\nPass1 - 不进行光照计算，仅仅计算片元的可见性（深度缓冲技术），可见片元的信息存储到G-buffer。\n\nPass2 - 利用G-buffer中的片元信息进行真正的光照计算。\n\n```cpp\nstring s = String.Empty;\n\n```\n\n#### 特点\n\n效率不依赖于场景的复杂度，而是和屏幕空间的大小有关（决定缓冲区的大小，缓冲区实质上就是一张张2D图像）\n\n适用于场景中光源数目很多，使用前向渲染会造成性能瓶颈的情况下。\n\n#### 局限性\n\n  * 不支持真正的抗锯齿（anti-aliasing）\n\n  * 不能处理半透明物体\n\n  * 显卡要求\n\n    * 必须支持MRT（Multiple Render Targets）\n\n    * Shader Mode 3.0及以上\n\n    * 深度渲染纹理\n\n    * 双面的模板缓冲\n\n#### Unity G-buffer\n\n默认包含以下几个渲染纹理(RenderTexture)\n\n  * RT0 - ARGB32 - 漫反射颜色(A不使用)\n\n  * RT1 - ARGB32 - 高光反射颜色 - A存储高光反射的指数部分\n\n  * RT2 - ARGB2101010 - RGB存储法线(A不用)\n\n  * RT3 - ARGB32(非HDR) or ARGBHalf(HDR) - 存储自发光 + lightmap + 反射探针(reflection probes)\n\n  * 深度缓冲(Z-buffer)\n\n  * 模板缓冲(stencil buffer)\n\n#### 可访问变量和函数\n\nUnityDeferredLibrary.cginc\n\n![Alt text](assets/1615553232623.png)\n\n### 顶点照明渲染路径\n\n  * ForwardPath的一个子集，Unity只会填充一些逐顶点相关的光源变量\n\n  * 最快速\n\n  * 硬件支持最广泛\n\n  * 最多访问到8个逐顶点光源\n\n![Alt text](assets/1615551537223.png)\n\n![Alt text](assets/1615551546915.png)\n\n### Comparison\n\n![Alt text](assets/1615553346669.png)\n\n## Unity的光源类型\n\n  * 平行光(directional light) - 没有位置，没有衰减\n\n  * 点光源(point light)\n\n  * 聚光灯(spot light)\n\n  * 面光源(area light)\n\n> 光源属性\n>\n>   * 位置\n>\n>   * 方向（到某点的方向）\n>\n>   * 颜色\n>\n>   * 强度\n>\n>   * 衰减 (attenuation)到某点的衰减）\n>\n>\n\n### 平行光\n\n  * 方向 - _WorldSpaceLightPos0\n\n  * 颜色和强度 - _LightColor0 (已经是相乘后的结果)\n\n  * 衰减 - 没有衰减(attenuation = 1.0)\n\n### 点光源\n\nRange - 半径\nPosition\n颜色\n强度\n衰减\n\n### 聚光灯\n\n空间中的一块锥形区域定义，表示由一个特定位置出发，向特定方向延伸的光\n\n  * Range\n\n  * Spot Angle\n\n  * Position\n"
}
