「遊戲引擎 淺入淺出」4.1 Unity Shader和OpenGL Shader

「遊戲引擎 淺入淺出」從零編寫遊戲引擎教程,是一本開源電子書,PDF/隨書代碼/資源下載:

//github.com/ThisisGame/cpp-game-engine-book


4.1 Unity Shader和OpenGL Shader

上一節提到,OpenGL Shader是配套出現的, Vertex Shader(頂點着色器)、Fragment Shader(片段着色器/像素着色器),兩者缺一不可。

但是我用了20年Unity都沒有見過類似的代碼,這是咋回事,Unity跳過OpenGL自己實現了圖形庫嗎?

1. Unity Shader類型

打開Unity,新建Shader。

在Unity中,可以創建4種Shader。

  1. Standard Surface Shader
  2. Unlit Shader
  3. Image Effect Shader
  4. Compute Shader

這4種Shader,是對一套特定功能的Vertex Shader、Fragment Shader組合,取的名字。

以C語言為例,我們編寫多個C語言代碼,可以編譯出各種程序,如收銀台程序、聊天程序、遊戲程序,每一種程序都針對特定功能。

用編寫的Vertex Shader、Fragment Shader,也可以編譯得到各種程序,如無光照的GPU程序、有光照的GPU程序、特效程序,每一種程序都針對特定功能。

Unity中可選的4種Shader,每一種都是針對特定功能,每一種都是由不同的Vertex Shader、Fragment Shader組成。

2. Unity Shader代碼

上面說,Unity Shader是由Vertex Shader、Fragment Shader組成,那就是說,Unity Shader裏面的代碼由Vertex Shader、Fragment Shader拼起來咯?

3.繪製簡單圖形 這一章的配套項目里,ShaderSource.h 就是由 Vertex Shader 和 Fragment Shader 拼起來的,那Unity Shader也是如此嗎?

創建一個 Standard Surface Shader,觀察代碼,發現結構完全不一樣,連 入口 main 函數都沒有!

Shader "Custom/NewSurfaceShader" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _Metallic;
		fixed4 _Color;

		// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
		// See //docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// #pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_CBUFFER_START(Props)
			// put more per-instance properties here
		UNITY_INSTANCING_CBUFFER_END

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

這是因為,我們在Unity中寫的Shader代碼,並不是標準的OpenGL Shader,而是NVIDIA開發的Cg語言。

Cg語言可以由工具,轉換成OpenGL Shader,或者DX Shader,畢竟Unity是跨平台引擎,如果沒有Cg語言,那每一種功能的Shader既要寫OpenGL的,又要寫DX的,要累死程序員了。

3.Unity Shader轉OpenGL Shader

Unity中選中創建的Shader,在Inspector里,打開Shader編譯平台設置,修改為 All platforms

點擊Compile and show code 將Unity Cg Shader文件轉為OpenGL Shader。

轉換完成後自動打開,搜索 glcore,定位到轉換後的代碼。

這就是轉換出來的Vertex Shader代碼,看到入口 main 函數了。