{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/rendering-basic-lighting/",
  "atlas": "https://www.pystone.net/?node=rendering-basic-lighting#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/rendering-basic-lighting.md",
  "context": "https://www.pystone.net/notes/rendering-basic-lighting.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:rendering-basic-lighting",
  "slug": "rendering-basic-lighting",
  "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": "e00a402e216ef083a1dd6133e5989c745afaa663f51a103e1bfb8d7eb317ec1e",
  "assets": [
    {
      "reference": "assets/1615468873311.png",
      "url": "/media/758e2cb62b11058b471a.png",
      "mediaType": "image/png",
      "contentHash": "758e2cb62b11058b471ac58ec81e692802aed6921349e1f758bcf0437314b9ea",
      "byteLength": 109702,
      "width": 497,
      "height": 260
    },
    {
      "reference": "assets/1615472338403.png",
      "url": "/media/5220b7a0ca3c0b8d5538.png",
      "mediaType": "image/png",
      "contentHash": "5220b7a0ca3c0b8d553875deb24bff9c28218f42aab3eff5b7f0aed52cccc1bf",
      "byteLength": 5711,
      "width": 795,
      "height": 82
    },
    {
      "reference": "assets/1615521215090.png",
      "url": "/media/47eac8b3e71e1e78d0a6.png",
      "mediaType": "image/png",
      "contentHash": "47eac8b3e71e1e78d0a67ddc7c2a6953c61d4685fb0142efffc33b4840eb4d2f",
      "byteLength": 51688,
      "width": 537,
      "height": 245
    },
    {
      "reference": "assets/1615521050254.png",
      "url": "/media/f862116971b55b7e4b7f.png",
      "mediaType": "image/png",
      "contentHash": "f862116971b55b7e4b7fcb2da637188f97351a359dab5a907cdd55bc020448da",
      "byteLength": 11875,
      "width": 546,
      "height": 83
    },
    {
      "reference": "assets/1615521074378.png",
      "url": "/media/db88540cec233a0b2ff2.png",
      "mediaType": "image/png",
      "contentHash": "db88540cec233a0b2ff2ea8ed615334cb63c08a4852460403993e8b3c77f3e2b",
      "byteLength": 4150,
      "width": 252,
      "height": 50
    },
    {
      "reference": "assets/1615521640378.png",
      "url": "/media/8a986c30c4474f216799.png",
      "mediaType": "image/png",
      "contentHash": "8a986c30c4474f2167995bb3e9881697802327874c4fce2ca0ca40fd4d44c07f",
      "byteLength": 47221,
      "width": 514,
      "height": 241
    },
    {
      "reference": "assets/1615521650054.png",
      "url": "/media/013e1ff78ba71c160442.png",
      "mediaType": "image/png",
      "contentHash": "013e1ff78ba71c160442337a4cfbafb592f8a15aa296fab79617c3d03a65f6ff",
      "byteLength": 27155,
      "width": 1066,
      "height": 110
    },
    {
      "reference": "assets/1615521653877.png",
      "url": "/media/3f4cad2c4258fd7b842b.png",
      "mediaType": "image/png",
      "contentHash": "3f4cad2c4258fd7b842b19f59ef293ede1f9df977b1d0c87b7c5fa6a21a9d2cf",
      "byteLength": 6880,
      "width": 513,
      "height": 301
    },
    {
      "reference": "assets/1615520625632.png",
      "url": "/media/d2e23f8a20849b5f5230.png",
      "mediaType": "image/png",
      "contentHash": "d2e23f8a20849b5f5230afb2917897a2adf7cd4fe9d7e8aab7ed328a0b6d14a2",
      "byteLength": 26567,
      "width": 1056,
      "height": 107
    },
    {
      "reference": "assets/1615520645595.png",
      "url": "/media/5ea6f562f9be7f3409d0.png",
      "mediaType": "image/png",
      "contentHash": "5ea6f562f9be7f3409d058ee2d56fb0c65ea1e266486dc0e3e5036d49d16095b",
      "byteLength": 18115,
      "width": 884,
      "height": 48
    },
    {
      "reference": "assets/1615539261972.png",
      "url": "/media/2895498165f70e6422f3.png",
      "mediaType": "image/png",
      "contentHash": "2895498165f70e6422f38ac3861640ab12c0cda7a782d6500a1a3037849f8d83",
      "byteLength": 8295,
      "width": 548,
      "height": 47
    },
    {
      "reference": "assets/1615472332961.png",
      "url": "/media/cc492f6647432b6b06d0.png",
      "mediaType": "image/png",
      "contentHash": "cc492f6647432b6b06d08d2afb1ec8ca9e6b704e20136ec418782875aae8ed79",
      "byteLength": 6059,
      "width": 752,
      "height": 90
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "【光照】基础光照",
      "anchor": "光照基础光照",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E5%85%89%E7%85%A7%E5%9F%BA%E7%A1%80%E5%85%89%E7%85%A7"
    },
    {
      "depth": 2,
      "text": "基础光照",
      "anchor": "基础光照",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E5%9F%BA%E7%A1%80%E5%85%89%E7%85%A7"
    },
    {
      "depth": 3,
      "text": "术语和概念",
      "anchor": "术语和概念",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E6%9C%AF%E8%AF%AD%E5%92%8C%E6%A6%82%E5%BF%B5"
    },
    {
      "depth": 4,
      "text": "辐照度和出射度",
      "anchor": "辐照度和出射度",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E8%BE%90%E7%85%A7%E5%BA%A6%E5%92%8C%E5%87%BA%E5%B0%84%E5%BA%A6"
    },
    {
      "depth": 4,
      "text": "散射(Scattering)和吸收(absorption)",
      "anchor": "散射scattering和吸收absorption",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E6%95%A3%E5%B0%84scattering%E5%92%8C%E5%90%B8%E6%94%B6absorption"
    },
    {
      "depth": 4,
      "text": "高光反射和漫反射",
      "anchor": "高光反射和漫反射",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E9%AB%98%E5%85%89%E5%8F%8D%E5%B0%84%E5%92%8C%E6%BC%AB%E5%8F%8D%E5%B0%84"
    },
    {
      "depth": 4,
      "text": "着色与光照模型",
      "anchor": "着色与光照模型",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E7%9D%80%E8%89%B2%E4%B8%8E%E5%85%89%E7%85%A7%E6%A8%A1%E5%9E%8B"
    },
    {
      "depth": 4,
      "text": "直接光照与间接光照",
      "anchor": "直接光照与间接光照",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E7%9B%B4%E6%8E%A5%E5%85%89%E7%85%A7%E4%B8%8E%E9%97%B4%E6%8E%A5%E5%85%89%E7%85%A7"
    },
    {
      "depth": 4,
      "text": "其他术语",
      "anchor": "其他术语",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E5%85%B6%E4%BB%96%E6%9C%AF%E8%AF%AD"
    },
    {
      "depth": 3,
      "text": "光照模型",
      "anchor": "光照模型",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E5%85%89%E7%85%A7%E6%A8%A1%E5%9E%8B"
    },
    {
      "depth": 4,
      "text": "标准光照模型(Blinn - Phong光照模型)",
      "anchor": "标准光照模型blinn---phong光照模型",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E6%A0%87%E5%87%86%E5%85%89%E7%85%A7%E6%A8%A1%E5%9E%8Bblinn---phong%E5%85%89%E7%85%A7%E6%A8%A1%E5%9E%8B"
    },
    {
      "depth": 5,
      "text": "自发光(emissive)",
      "anchor": "自发光emissive",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E8%87%AA%E5%8F%91%E5%85%89emissive"
    },
    {
      "depth": 5,
      "text": "高光反射(specular)",
      "anchor": "高光反射specular",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E9%AB%98%E5%85%89%E5%8F%8D%E5%B0%84specular"
    },
    {
      "depth": 5,
      "text": "漫反射(diffuse)",
      "anchor": "漫反射diffuse",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E6%BC%AB%E5%8F%8D%E5%B0%84diffuse"
    },
    {
      "depth": 5,
      "text": "环境光(ambient)",
      "anchor": "环境光ambient",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E7%8E%AF%E5%A2%83%E5%85%89ambient"
    },
    {
      "depth": 5,
      "text": "Unity中的应用",
      "anchor": "unity中的应用",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#unity%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8"
    },
    {
      "depth": 4,
      "text": "BRDF",
      "anchor": "brdf",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#brdf"
    },
    {
      "depth": 2,
      "text": "实践应用",
      "anchor": "实践应用",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E5%AE%9E%E8%B7%B5%E5%BA%94%E7%94%A8"
    },
    {
      "depth": 3,
      "text": "逐像素还是逐顶点",
      "anchor": "逐像素还是逐顶点",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E9%80%90%E5%83%8F%E7%B4%A0%E8%BF%98%E6%98%AF%E9%80%90%E9%A1%B6%E7%82%B9"
    },
    {
      "depth": 4,
      "text": "逐像素光照(per-pixel lighting)",
      "anchor": "逐像素光照per-pixel-lighting",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E9%80%90%E5%83%8F%E7%B4%A0%E5%85%89%E7%85%A7per-pixel-lighting"
    },
    {
      "depth": 4,
      "text": "逐顶点光照(per-vertex lighting)",
      "anchor": "逐顶点光照per-vertex-lighting",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E9%80%90%E9%A1%B6%E7%82%B9%E5%85%89%E7%85%A7per-vertex-lighting"
    },
    {
      "depth": 3,
      "text": "游戏场景中灯光照明的构成",
      "anchor": "游戏场景中灯光照明的构成",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E6%B8%B8%E6%88%8F%E5%9C%BA%E6%99%AF%E4%B8%AD%E7%81%AF%E5%85%89%E7%85%A7%E6%98%8E%E7%9A%84%E6%9E%84%E6%88%90"
    },
    {
      "depth": 3,
      "text": "lightmap",
      "anchor": "lightmap",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#lightmap"
    },
    {
      "depth": 4,
      "text": "光照烘焙",
      "anchor": "光照烘焙",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#%E5%85%89%E7%85%A7%E7%83%98%E7%84%99"
    },
    {
      "depth": 5,
      "text": "Unity场景光照烘焙步骤",
      "anchor": "unity场景光照烘焙步骤",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#unity%E5%9C%BA%E6%99%AF%E5%85%89%E7%85%A7%E7%83%98%E7%84%99%E6%AD%A5%E9%AA%A4"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/rendering-basic-lighting/#ref"
    }
  ],
  "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/2/23 17:59\n\n  * 【光照】基础光照\n    * 基础光照\n      * 术语和概念\n        * 辐照度和出射度\n        * 散射(Scattering)和吸收(absorption)\n        * 高光反射和漫反射\n        * 着色与光照模型\n        * 直接光照与间接光照\n        * 其他术语\n      * 光照模型\n        * 标准光照模型(Blinn - Phong光照模型)\n          * 自发光(emissive)\n          * 高光反射(specular)\n          * 漫反射(diffuse)\n          * 环境光(ambient)\n          * Unity中的应用\n        * BRDF\n    * 实践应用\n      * 逐像素还是逐顶点\n        * 逐像素光照(per-pixel lighting)\n        * 逐顶点光照(per-vertex lighting)\n      * 游戏场景中灯光照明的构成\n      * lightmap\n        * 光照烘焙\n          * Unity场景光照烘焙步骤\n    * Ref\n\n## 基础光照\n\n### 术语和概念\n\n#### 辐照度和出射度\n\n辐照度（ irradiance）—— 对于平行光L来说，可以通过计算在垂直于L的单位面积上单位时间内穿过的能量来得到。\n\n![Alt text](assets/1615468873311.png)\n\n辐照度与物体表面光线之间的距离成反比，距离越大，强度越小。即，与d/cos成反比，与cos成正比。\n\n出射度（exitance）——根据入射光线的数量和方向，可以计算出射光线的数量和方向。\n\n辐照度和出射度满足线性关系，他们的比值就是材质的漫反射和高光反射属性\n\n#### 散射(Scattering)和吸收(absorption)\n\n散射：改变方向\n吸收：改变颜色和密度\n\n#### 高光反射和漫反射\n\n  * 高光反射(specular) —— 我们只考虑某一个特定方向的高光反射\n\n  * 漫反射(diffuse)—— 我们假设漫反射部分是没有方向的\n\n#### 着色与光照模型\n\n着色(Shading)： **根据材质属性(漫反射属性等)、光源信息(光源方向、辐照度等)，使用一个等式取计算沿某个观察方向的出射度的过程。**\n\n把这个等式称为 **光照模型** (Lighting Model)。\n\n#### 直接光照与间接光照\n\n  * 直接光照(direct light) - 照射到物体表面，经过一次反射直接进入摄像机的光线。\n\n  * 间接光照(indirect light) - 光线进入摄像机之前，经过了不止一次物体反射\n\n#### 其他术语\n\n环境光（Ambient Lighting）：环境光是来自周围环境整体的散射光，为处于环境中的所有物体提供照明。\n\n阴影（Shadow）：光影通常具有方向性，也指向了光源方向。\n\n明暗变化（Shading）：是特定图像在不同区域展现出来的不同的光的明暗度。举个例子，同一个物体在同一个场景中，表面光的情况也会随着观察者角度的变化、环境光强度的变化而变化。\n\n镜面高光（Specular highlight）：通过直接光源的照射，物体表面会反射高光点。这种物体表面的高光会随着观察者的位置而发生变化。\n\n反射（Reflection）：光的反射会根据物体表面是否具有镜面或漫反射的特性而略有不同。例如，一个金属球将具有较高的镜面反射能力，并映出周围环境中的事物。相反，如果球的表面是磨砂材质，灰色，则照到球表面上的光会被散射。现实生活中的很多物体通常兼具镜面反射和漫反射特点，比如一个磨损的保龄球、一张使用很久的信用卡。\n\n### 光照模型\n\n#### 标准光照模型(Blinn - Phong光照模型)\n\n裴祥风（ Bui Tuong Phong）\n\n只关心直接光照。\n进入摄像机内的光线分为4个部分，每个部分使用一种方法来计算他的贡献度。\n\n##### 自发光(emissive)\n\n如果没有使用全局光照(Global illumination)技术，自发光表面并不会真的照亮周围的物体。\n\n![Alt text](assets/1615472338403.png)\n\n##### 高光反射(specular)\n\n描述当光线从光源照射到模型表面时， 该表面会在完全镜面反射方向散射多少辐射量。\n经验模型，不完全符合真实世界\n\n* * *\n\nPhong模型\n\n![Alt text](assets/1615521215090.png)\n\n![Alt text](assets/1615521050254.png)\n\n  * Mgloss —— 材质的光泽度(gloss), 反光度(shininess)\n\n  * Mspecular —— 材质的高光反射颜色\n\n反射方向可用表面法线n和光照方向l计算：\n\n![Alt text](assets/1615521074378.png)\n\n* * *\n\nBlinn模型\n避免计算反射方向 r。\n\n![Alt text](assets/1615521640378.png)\n\n![Alt text](assets/1615521650054.png)\n\n![Alt text](assets/1615521653877.png)\n\n  * 如果摄像机和光源距离模型足够远的话 Blinn 模型 会快于Phong 模型 —— v和l都是定值，h是一个常量\n\n  * 如果v或者l不是定值，Phong模型可能反而更快\n\n两个都是经验模型，不存在哪个“更正确”，一些情况下，Blinn模型更符合结果。\n\n##### 漫反射(diffuse)\n\n描述被物体表面随机散射到各个方向上的辐射度。\n可以认为在任何反射方向上的分布都是一样的，但是入射光线角度很重要。\n\n**兰伯特定律(Lambert’s law)** : 反射光线强度与表面法线和光源方向之间的夹角的余弦值成正比。\n\n![Alt text](assets/1615520625632.png)\n\n![Alt text](assets/1615520645595.png)\n\n**半兰伯特光照(HalfLambert)** （没有物理依据，只是一个视觉加强技术）\n\n![Alt text](assets/1615539261972.png)\n\n把cos的值从[-1, 1]映射到[0, 1]。\n\n##### 环境光(ambient)\n\n描述其他所有的间接光照\n\n![Alt text](assets/1615472332961.png)\n\n##### Unity中的应用\n\n环境光： 场景的环境光 - Lighting设置中，Ambient Source/ Ambient Color/ Ambient Intensity中控制，Shader内置变量 `UNITY_LIGHTMODEL_AMBIENT`获取\n\n自发光：在fragment Shader输出最后颜色之前，把材质的自发光颜色添加到输出颜色上即可\n\n（”Lighting.cginc”中）_LightColor.0 - 该Pass处理的光源的颜色和强度信息\n_WorldSpaceLightPos0.xyz - 单一平行光的方向\n\n#### BRDF\n\nBRDF(Bidirectional Reflectance Distribution Function) - 是一个经验模型。\n\n为了更加真实的模拟光和物体的交互，出现了基于物理的BRDF模型。\n\n## 实践应用\n\n### 逐像素还是逐顶点\n\n#### 逐像素光照(per-pixel lighting)\n\n在片元着色器中计算，以像素为基础，得到法线。\n\n  * 顶点法线插值 —— Phong插值着色(Phong shading)技术，法线插值着色技术\n\n  * 法线贴图采样\n\n#### 逐顶点光照(per-vertex lighting)\n\n在顶点着色器中计算\n高洛德着色(Gouraud shading) - 顶点上计算光照后，在渲染图元内部进行线性插值，输出成像素颜色。\n\n  * 如果光照模型中有非线性的计算（高光反射）时，逐顶点光照就会出问题。\n\n  * 渲染图元内部进行插值，会导致渲染图元内部的颜色总是暗于顶点处的最高颜色值，在某些情况下会产生明显的棱角现象。\n\n### 游戏场景中灯光照明的构成\n\n光照计算方案分类：\n\n  1. 直接模拟光线从被光源发出到最终被物体完全吸收的正向过程，也就是常说的GI（Global Illumination）；\n\n  2. 不直接模拟光线，而是反向搜集物体表面特定点的受光照强度来模拟现实照明效果，也就是常说的FG（Final Gathering）;\n\n  3. 完全不考虑光线的行为，单纯基于“物体上与其他物体越接近的区域，受到反射光线的照明越弱”这一现象来模拟模拟现实照明（的一部分）效果，也就是常说的AO（Ambient Occlusion）；\n\n  4. 将场景光照结果完全烘焙到模型贴图上，从而完完全全的假冒现实光照效果，也就是我们所说的Lightmap。\n\n不论是GI还是FG，计算量都是非常大的，一帧图片需要几十分钟甚至几十小时来渲染，所以很难被应用在游戏设计领域。因此在游戏设计领域，光照贴图技术依然是目前的主流方式。\n\n由于光照贴图需要事先烘焙（baking）出来，且仅支持静态物体（Static Object），而我们的游戏场景中几乎不可能全都是静态物体，所以通常游戏场景中的灯光照明是多种照明方式的混合作用。\n\n  * 对于静态物体来说，大多使用光照贴图来模拟间接光的照明效果，然后加上直接光源的动态照明效果；\n\n  * 对于运动物体来说，则仅用直接光源的动态照明效果，或者使用光照探针来模拟间接光的照明效果。\n\n### lightmap\n\nA lightmap is a data structure used in lightmapping, a form of surface caching in which the brightness of surfaces in a virtual scene is pre-calculated and stored in texture maps for later use.\n\n#### 光照烘焙\n\n当我们进行游戏时，有些场面避免不了使用大量灯光渲染。大量的灯光渲染对于计算机来说无疑是个负担，为了尽量节省计算机的显卡资源，对于一些静态的不动的物体，可以采用烘培的方法，把灯光的效果，制作成游戏物体纹理的一部分，这样即使没有灯光的渲染。该游戏物品也会呈现一样的视觉效果。\n\n##### Unity场景光照烘焙步骤\n\n  1. 创建物体和灯光\n\n  2. 把所有要渲染的游戏对象全选，在Inspecto面板里面单击Static选项，选择Lightmap static（新版本为Contribute GI）\n\n  3. 将需要烘焙的光源的Mode模式改为Baked\n\n  4. window -> RenderingLighting -> Lighting Settings，Generate Lighting。\n\n## Ref\n\n<https://www.jianshu.com/p/7594b044e6dc>\n"
}
