CesiumJS 原始碼雜談 - 從光到 Uniform

嶺南燈火發表於2023-04-16

之前對實時渲染(RealTimeRendering)的殿堂就十分嚮往,也有簡單瞭解過實時渲染中的光,無奈一直沒能系統學習。鑑於筆者已經有一點 CesiumJS 原始碼基礎,所以就抽了一個週末跟了跟 CesiumJS 中的光照初步,在簡單的程式碼追蹤後,發現想系統學習光照材質,仍然是需要 RTR 知識的,這次僅僅瞭解了光在 CesiumJS 底層中是如何從 API 傳遞到 WebGL 著色器中去的,為之後深入研究打下基礎。

1. 有什麼光

CesiumJS 支援的光的型別比較少,預設場景光就一個太陽光:

// Scene 類建構函式中

this.light = new SunLight();

從上面這程式碼可知,CesiumJS 目前場景中只支援加入一個光源。

查閱 API,可得知除了 SubLight 之外,還有一個 DirectionalLight,即方向光。

官方示例程式碼《Lighting》中就使用了方向光來模擬手電筒效果(flashLight)、月光效果(moonLight)、自定義光效果。

方向光比太陽光多出來一個必選的方向屬性:

const flashLight = new DirectionalLight({
  direction: scene.camera.directionWC // 每幀都不一樣,手電筒一直沿著相機視線照射
})

這個 direction 屬性是一個單位向量即可(模長是 1)。

說起來歸一化、規範化、標準化好像都能在網上找到與單位向量類似的意思,都是向量除以模長。

可見,CesiumJS 並沒有內建點光源、聚光燈,需要自己寫著色過程(請參考 Primitive API 或 CustomShader API)。

2. 光如何轉換成 Uniform 以及何時被呼叫

既然 CesiumJS 支援的光只有一個,那麼調查起來就簡單了。先給結論:

光是作為 Uniform 值傳遞到著色器中的。 先查清楚光是如何從 Scene.light 轉至 Renderer 中的 uniform 的。

2.1. 統一值狀態物件(UniformState)

在 Scene 渲染一幀的過程中,幾乎就在最頂部,Scene.js 模組內的函式 render 就每幀更新著 Context 物件的 uniformState 屬性:

function render(scene) {
  const frameState = scene._frameState;

  const context = scene.context;
  const us = context.uniformState;

  // ...

  us.update(frameState);

  // ...
}

這個 uniformState 物件就是 CesiumJS 絕大多數統一值(Uniform)的封裝集合,它的更新方法就會更新來自幀狀態物件(FrameState)的光引數:

UniformState.prototype.update = function (frameState) {
  // ...
  const light = defaultValue(frameState.light, defaultLight);
  if (light instanceof SunLight) { /**/ }
  else { /**/ }

  const lightColor = light.color;
  // 計算 HDR 光到 this._lightColor 上

  // ...
}

那麼,這個掛在 Context 上的 uniformState 物件包含的光狀態資訊,是什麼時候被使用的呢?下一小節 2.2 就會介紹。

2.2. 上下文(Context)執行 DrawCommand

在 Scene 的更新過程中,最後 DrawCommand 物件被 Context 物件執行:

function continueDraw(context, drawCommand, shaderProgram, uniformMap) {
  // ...
  shaderProgram._setUniforms(
    uniformMap,
    context._us,
    context.validateShaderProgram
  )
  // ...
}

Context.prototype.draw = function (/* ... */) {
  // ...
  shaderProgram = defaultValue(shaderProgram, drawCommand._shaderProgram);
  uniformMap = defaultValue(uniformMap, drawCommand._uniformMap);

  beginDraw(this, framebuffer, passState, shaderProgram, renderState);
  continueDraw(this, drawCommand, shaderProgram, uniformMap);
}

就在 continueDraw 函式中,呼叫了 ShaderProgram 物件的 _setUniforms 方法,所有 Uniform 值在此將傳入 WebGL 狀態機中。

ShaderProgram.prototype._setUniforms = function (/**/) {
  // ...
  const uniforms = this._uniforms;
  len = uniforms.length;
  for (i = 0; i < len; ++i) {
    uniforms[i].set();
  }
  // ...
}

而這每一個 uniforms[i],都是一個沒有公開在 API 檔案中的私有類,也就是接下來 2.3 小節中要介紹的 WebGL Uniform 值封裝物件。

2.3. 對 WebGL Uniform 值的封裝

進入 createUniforms.js 模組:

// createUniforms.js

UniformFloat.prototype.set = function () { /* ... */ }
UniformFloatVec2.prototype.set = function () { /* ... */ }
UniformFloatVec3.prototype.set = function () { /* ... */ }
UniformFloatVec4.prototype.set = function () { /* ... */ }
UniformSampler.prototype.set = function () { /* ... */ }
UniformInt.prototype.set = function () { /* ... */ }
UniformIntVec2.prototype.set = function () { /* ... */ }
UniformIntVec3.prototype.set = function () { /* ... */ }
UniformIntVec4.prototype.set = function () { /* ... */ }
UniformMat2.prototype.set = function () { /* ... */ }
UniformMat3.prototype.set = function () { /* ... */ }
UniformMat4.prototype.set = function () { /* ... */ }

可以說把 WebGL uniform 的型別都封裝了一個私有類。

以表示光方向的 UniformFloatVec3 類為例,看看它的 WebGL 呼叫:

function UniformFloatVec3(gl, activeUniform, uniformName, location) {
  this.name = uniformName

  this.value = undefined
  this._value = undefined

  this._gl = gl
  this._location = location
}

UniformFloatVec3.prototype.set = function () {
  const v = this.value

  if (defined(v.red)) {
    if (!Color.equals(v, this._value)) {
      this._value = Color.clone(v, this._value)
      this._gl.uniform3f(this._location, v.red, v.green, v.blue)
    }
  } else if (defined(v.x)) {
    if (!Cartesian3.equals(v, this._value)) {
      this._value = Cartesian3.clone(v, this._value)
      this._gl.uniform3f(this._location, v.x, v.y, v.z)
    }
  } else {
    throw new DeveloperError(`Invalid vec3 value for uniform "${this.name}".`);
  }
}

2.4. 自動統一值(AutomaticUniforms)

在 2.2 小節中有一個細節沒有詳細說明,即 ShaderProgram_setUniforms 方法中為什麼可以直接呼叫每一個 uniforms[i]set()

回顧一下:

  • Scene.jsrender 函式內,光的資訊被 us.update(frameState) 更新至 UniformState 物件中;
  • ShaderProgram_setUniforms 方法,呼叫 uniforms[i].set() 方法, 更新每一個私有 Uniform 物件上的值到 WebGL 狀態機中

是不是缺少了點什麼?

是的,UniformState 的值是如何賦予給 uniforms[i] 的?

這就不得不提及 ShaderProgram.js 模組中為當前著色器物件的 Uniform 分類過程了,查詢模組中的 reinitialize 函式:

function reinitialize(shader) {
  // ...
  const uniforms = findUniforms(gl, program)
  const partitionedUniforms = partitionUniforms(
    shader,
    uniforms.uniformsByName
  )

  // ...
  shader._uniformsByName = uniforms.uniformsByName
  shader._uniforms = uniforms.uniform
  shader._automaticUniforms = partitionedUniforms.automaticUniforms
  shader._manualUniforms = partitionedUniforms.manualUniforms
  // ...
}

它把著色器物件上的 Uniform 全部找了出來,並分類為:

  • _uniformsByName - 一個字典物件,鍵名是著色器中 uniform 的變數名,值是 Uniform 的封裝物件,例如 UniformFloatVec3
  • _uniforms - 一個陣列,每個元素都是 Uniform 的封裝物件,例如 UniformFloatVec3 等,若同名,則與 _uniformsByName 中的值是同一個引用
  • _manualUniforms - 一個陣列,每個元素都是 Uniform 的封裝物件,例如 UniformFloatVec3 等,若同名,則與 _uniformsByName 中的值是同一個引用
  • _automaticUniforms - 一個陣列,每個元素是一個 object 物件,表示要 CesiumJS 自動更新的 Uniform 的對映關聯關係

舉例,_automaticUniforms[i] 用 TypeScript 來描述,是這麼一個物件:

type AutomaticUniformElement = {
  automaticUniform: AutomaticUniform
  uniform: UniformFloatVec3
}

而這個 _automaticUniforms 就擁有自動更新 CesiumJS 內部狀態的 Uniform 值的功能,例如我們所需的光狀態資訊。

來看 AutomaticUniforms.js 模組的預設匯出物件:

// AutomaticUniforms.js

const AutomaticUniforms = {
  // ...
  czm_sunDirectionEC: new AutomaticUniform({ /**/ }),
  czm_sunDirectionWC: new AutomaticUniform({ /**/ }),
  czm_lightDirectionEC: new AutomaticUniform({ /**/ }),
  czm_lightDirectionWC: new AutomaticUniform({ /**/ }),
  czm_lightColor: new AutomaticUniform({
    size: 1,
    datatype: WebGLConstants.FLOAT_VEC3,
    getValue: function (uniformState) {
      return uniformState.lightColor;
    },
  }),
  czm_lightColorHdr:  new AutomaticUniform({ /**/ }),
  // ...
}
export default AutomaticUniforms

所以,在 ShaderProgram.prototype._setUniforms 執行的時候,其實是對自動統一值有一個賦值的過程,然後才到各個 uniforms[i]set() 過程:

ShaderProgram.prototype._setUniforms = function (
  uniformMap,
  uniformState,
  validate
) {
  let len;
  let i;

  // ...

  const automaticUniforms = this._automaticUniforms;
  len = automaticUniforms.length;
  for (i = 0; i < len; ++i) {
    const au = automaticUniforms[i];
    au.uniform.value = au.automaticUniform.getValue(uniformState);
  }

  // 譯者注:au.uniform 實際上也在 this._uniforms 中
  // 是同一個引用在不同的位置,所以上面呼叫 au.automaticUniform.getValue 
  // 之後,下面 uniforms[i].set() 就會使用的是 “自動更新” 的 uniform 值

  const uniforms = this._uniforms;
  len = uniforms.length;
  for (i = 0; i < len; ++i) {
    uniforms[i].set();
  }

  // ...
}

也許這個過程有些亂七八糟,那就再簡單梳理一次:

  • Scene 的 render 過程中,更新了 uniformState
  • Context 執行 DrawCommand 過程中,ShaderProgram 的 _setUniforms 執行所有 uniforms 的 WebGL 設定,這其中就會對 CesiumJS 內部不需要手動更新的 Uniform 狀態資訊進行自動重新整理
  • 而在 ShaderProgram 繫結前,早就會把這個著色器中的 uniform 進行分組,一組是常規的 uniform 值,另一組則是需要根據 AutomaticUniform(自動統一值)更新的 uniform 值

說到底,光狀態資訊也不過是一種 Uniform,在最原始的 WebGL 學習教材中也是如此,只不過 CesiumJS 是一個更復雜的狀態機器,需要更多邏輯劃分就是了。

3. 在著色器中如何使用

上面介紹完光的型別、在 CesiumJS 原始碼中如何轉化成 Uniform 並刷入 WebGL,那麼這一節就簡單看看光的狀態 Uniform 在著色器程式碼中都有哪些使用之處。

3.1. 點雲

PointCloud.js 使用了 czm_lightColor

找到 createShaders 函式下面這個分支:

// Version 1.104

function createShaders(pointCloud, frameState, style) {
  // ...
  if (usesNormals && normalShading) {
    vs +=
      "    float diffuseStrength = czm_getLambertDiffuse(czm_lightDirectionEC, normalEC); \n" +
      "    diffuseStrength = max(diffuseStrength, 0.4); \n" + // Apply some ambient lighting
      "    color.xyz *= diffuseStrength * czm_lightColor; \n";
  }
  // ...
}

顯然,這段程式碼在拼湊頂點著色器程式碼,在 1.104 版本官方並沒有改變這種拼接著色器程式碼的模式。

著色程式碼的含義也很簡單,將漫反射強度值乘上 czm_lightColor,把結果交給 color 的 xyz 分量。漫反射強度在這裡限制了最大值 0.4。

漫反射強度來自內建 GLSL 函式 czm_getLambertDiffuse(參考 packages/engine/Source/Shaders/Builtin/Functions/getLambertDiffuse.glsl

3.2. 馮氏著色法

Primitive API 材質物件的預設著色方法是 馮氏著色法(Phong),這個在 LearnOpenGL 網站上有詳細介紹。

呼叫鏈:

MaterialAppearance.js
  ┗ TexturedMaterialAppearanceFS.js ← TexturedMaterialAppearanceFS.glsl
    ┗ phong.glsl → vec4 czm_phong()

除了 TexturedMaterialAppearanceFS 外,MaterialAppearance.js 還用了 BasicMaterialAppearanceFSAllMaterialAppearanceFS 兩個片元著色器,這倆也用到了 czm_phong 函式。

看看 czm_phong 函式本體:

// phong.glsl

vec4 czm_phong(vec3 toEye, czm_material material, vec3 lightDirectionEC)
{
    // Diffuse from directional light sources at eye (for top-down)
    float diffuse = czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 0.0, 1.0), material);
    if (czm_sceneMode == czm_sceneMode3D) {
        // (and horizon views in 3D)
        diffuse += czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 1.0, 0.0), material);
    }

    float specular = czm_private_getSpecularOfMaterial(lightDirectionEC, toEye, material);

    // Temporary workaround for adding ambient.
    vec3 materialDiffuse = material.diffuse * 0.5;

    vec3 ambient = materialDiffuse;
    vec3 color = ambient + material.emission;
    color += materialDiffuse * diffuse * czm_lightColor;
    color += material.specular * specular * czm_lightColor;

    return vec4(color, material.alpha);
}

函式內前面的計算步驟是獲取漫反射、高光值,走的是輔助函式,在這個檔案內也能看到。

最後燈光 czm_lightColor 和材質的漫反射、蘭伯特漫反射、材質輝光等因子一起相乘累加,得到最終的顏色值。

除了 phong.glsl 外,參與半透明計算的 czm_translucentPhong 函式(在 translucentPhong.glsl 檔案中)在 OIT.js 模組中用於替換 czm_phong 函式。

3.3. 地球

Globe.js 中使用的 GlobeFS 片元著色器程式碼中使用到了 czm_lightColor,主要是 main 函式中:

void main() {
// ...

#ifdef ENABLE_VERTEX_LIGHTING
    float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_lightDirectionEC, normalize(v_normalEC)) * u_lambertDiffuseMultiplier + u_vertexShadowDarkness, 0.0, 1.0);
    vec4 finalColor = vec4(color.rgb * czm_lightColor * diffuseIntensity, color.a);
#elif defined(ENABLE_DAYNIGHT_SHADING)
    float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_lightDirectionEC, normalEC) * 5.0 + 0.3, 0.0, 1.0);
    diffuseIntensity = mix(1.0, diffuseIntensity, fade);
    vec4 finalColor = vec4(color.rgb * czm_lightColor * diffuseIntensity, color.a);
#else
    vec4 finalColor = color;
#endif

// ...
}

同樣是先獲取蘭伯特漫反射值(使用 clamp 函式釘死在 [0, 1] 區間內),然後將顏色、czm_lightColor、漫反射值和透明度一起計算出 finalColor,把最終顏色值交給下一步計算。

這裡區分了兩個宏分支,受 TerrainProvider 影響,有興趣可以追一下 GlobeSurfaceTileProvider.js 模組中 addDrawCommandsForTile 函式中 hasVertexNormals 引數的獲取。

3.4. 模型架構中的光著色階段

在 1.97 大改的 Model API 中,PBR 著色法使用了 czm_lightColorHdr 變數。czm_lightColorHdr 也是自動統一值(AutomaticUniforms)的一個。

在 Model 的更新過程中,有一個 buildDrawCommands 的步驟,其中有一個函式 ModelRuntimePrimitive.prototype.configurePipeline 會增減 ModelRuntimePrimitive 上的著色階段:

ModelRuntimePrimitive.prototype.configurePipeline = function (frameState) {
  // ...
  pipelineStages.push(LightingPipelineStage);
  // ...
}

上面是其中一個階段 —— LightingPipelineStage,最後在 ModelSceneGraph.prototype.buildDrawCommands 方法內會呼叫每一個 stage 的 process 方法,呼叫 shaderBuilder 構建出著色器物件所需的材料,進而構建出著色器物件。過程比較複雜,直接看其中 LightingPipelineStage.glsl 提供的階段函式:

void lightingStage(inout czm_modelMaterial material, ProcessedAttributes attributes)
{
    // Even though the lighting will only set the diffuse color,
    // pass all other properties so further stages have access to them.
    vec3 color = vec3(0.0);

    #ifdef LIGHTING_PBR
    color = computePbrLighting(material, attributes);
    #else // unlit
    color = material.diffuse;
    #endif

    #ifdef HAS_POINT_CLOUD_COLOR_STYLE
    // The colors resulting from point cloud styles are adjusted differently.
    color = czm_gammaCorrect(color);
    #elif !defined(HDR)
    // If HDR is not enabled, the frame buffer stores sRGB colors rather than
    // linear colors so the linear value must be converted.
    color = czm_linearToSrgb(color);
    #endif

    material.diffuse = color;
}

進入 computePbrLighting 函式(同一個檔案內):

#ifdef LIGHTING_PBR
vec3 computePbrLighting(czm_modelMaterial inputMaterial, ProcessedAttributes attributes)
{
    // ...

    #ifdef USE_CUSTOM_LIGHT_COLOR
    vec3 lightColorHdr = model_lightColorHdr;
    #else
    vec3 lightColorHdr = czm_lightColorHdr;
    #endif

    vec3 color = inputMaterial.diffuse;
    #ifdef HAS_NORMALS
    color = czm_pbrLighting(
        attributes.positionEC,
        inputMaterial.normalEC,
        czm_lightDirectionEC,
        lightColorHdr,
        pbrParameters
    );

        #ifdef USE_IBL_LIGHTING
        color += imageBasedLightingStage(
            attributes.positionEC,
            inputMaterial.normalEC,
            czm_lightDirectionEC,
            lightColorHdr,
            pbrParameters
        );
        #endif
    #endif

   // ...
}
#endif

故,存在 USE_CUSTOM_LIGHT_COLOR 宏時才會使用 czm_lightColorHdr 變數作為燈光顏色,參與函式 czm_pbrLighting 計算出顏色值。

3.5. 後記

除了光顏色本身,我在著色器程式碼中看到被應用的還有光線的方向,主要是 czm_lightDirectionEC 等變數,光照材質仍需一個漫長的學習過程。

相關文章