如何選擇Unity 3d Shader編寫語言?有哪些Shader入門技巧及學習資料?


之前我回答了「Shader的編寫到底應該是美術的事情還是程序的事情?」,有朋友在評論及QQ群里問我如何選擇Unity
3d Shader
的編寫語言,以及如何進行Unity 3d Shader入門學習等等。我打算詳細寫一下我個人的觀點,在5月慢慢進行更新,歡迎大家一起探討。有興趣的朋友還可以加入關於Shader討論的QQ群198647854

What language
should I study for making shaders in Unity3d?

Yesterday I said Cg/HLSL was the best choice for writing
shaders in Unity. A couple of other interesting opinions came out so I did a
bit of reading before making this reply.

Metal API:
Others said that Metal API is the future for iOS. That』s great for you if you
don』t care about Android, PC, Xbox, PS, etc, etc. Anyway, Unity3d doesn』t
support writing in Metal and they said they have no intention to support it in
the near future. They only have support for automatic compiling into Metal API,
so you don』t really get much control over it. So it』s not a choice for a Unity
developer to start learning.

Vulkan:
Somebody said this is the future. Maybe. It』s made from a dead language called
Mantle developed by AMD, which only had a few games ever using it. They took
some ideas from Apple』s Metal and improved it, so people say. Apparently Vulkan
was announced in GDC 2015, but I didn』t see or hear it anywhere. Even if it is
the future, it is a far and distant uncertain future. Vulkan sounds nice, like
a dream come true in some ways, but it isn』t even released yet. Only Valve company
has used it. Do you work at Valve? Does your boss want to support this
technology yet? Too many questions. Plus, John Carmack hinted that Android
might have a similar kinda of Metal API of their own soon, so GPUs of Android
devices might not even need to adopt it. Well, when you consider everything
there is just too much questions surrounding this, too much unknown. In any
case, it』s not a choice for Unity at the moment.

GLSL: You can
write in GLSL in Unity now. Unity will compile this code to whatever platform
you like, though you may have to 「-force-opengl」 on a PC (or any non iOS/Apple)
device to get it to work properly. That will cause the Unity Profiler to be
unable to profile correctly on some GPUs of certain video cards. I』d love to
know if it』s possible to get around this. So, if you use a Mac and everyone in
your team uses a Mac, then sure you should probably use GLSL –it』s the logical
choice.

Surface Shaders:
You can use Unity shaderlab to do some cool stuff, but it won』t be very
efficient for mobile. Shaderlab is like a simplified and more humanly-readable
markup language, but you lose the control and performance options that you need
for mobile games. Since most of the jobs in China will be for making mobile
games, I don』t recommend it.

Cg/HLSL: Some
people said that Cg was a dead language. Well, the fact is that Cg and HLSL are
the same language which is maintained by two different companies, NVidia and
Microsoft. Now Nividia has stopped supporting Cg because there is no need since
Microsoft supports HLSL. People still use the name Cg, but hope nobody gets
confused about it. Cg/HLSL is certainly not dead, and after talking to several
high ups and forum veterans over at Unity, they all think that HLSL is the
future of shader writing in Unity because the company is doing a lot of console
and AAA promotion. Just look at Unity5 features and you will get my meaning.
Cg/HLSL can be compiled to any device you need and since Unity and Microsoft
seem like great friends now, I don』t think this is going to change.

Conclusion:

If you want to use Unity3d and make games for Windows,
Consoles, Android, and iOS, with decent performance on all platforms, you
should use Cg/HLSL.

If you want to use Unity3d and make games for mobile only
(don』t need to support Windows or Consoles), GLSL is a good choice, especially
if you work on Apple Macs.

If you don』t want to use Unity3d and only want to develop
for the newest iOS devices, then Metal is the best, though very few people in
China are using it right now.

If you want to use cocos2d, I am not the expert for
advice, but friends say GLSL is your best choice.

However, also, but, maybe…. In China, the dominant art
style is hand-painted stuff. This doesn』t require very deep shader knowledge,
and is very little work for an experienced guy to achieve. Shader coding is
mainly for trying to go beyond current gen graphics, or create specific
effects, or to get around some graphical issues. So you have to think about if
it』s worth to study, because many kinds of games may not need this.

非常感謝知友@王邢敏 的翻譯,他的微博:One Double(http://weibo.com/laowangba)

昨天我說過,在Unity里寫shader,Cg/ HLSL是最佳選擇。後來冒出了幾個很有意思的觀點,所以我特意查閱了一下資料再來回復。

Metal API:有人說,Metal API是iOS的未來。如果你完全不關心Android,PC,Xbox,PS等平台,那麼你的確可以奉之為圭臬。反正,Unity3d 是不支持用Metal寫的,而且他們說近期也沒有要去支持Metal的打算。他們只支持自動編譯成Metal API,但這樣你能控制的就很少。因此,這不是一個Unity開發者入門的選擇。

Vulkan:有人說這才是未來。大概吧。AMD開發過一個叫做Mantle 的語言,只有很少的幾個遊戲用過它,然後它還死了,現在有人用它搞出了這個。人們說他們從蘋果公司的Metal借鑒了一些理念,用之以完善Vulkan。很顯然,Vulkan是在2015年GDC公布的,但我沒有在其他任何地方聽說或看到。即使它真是未來,那也是一個遠到不著邊際的未來。Vulkan 聽起來不錯,像是某種方式的夢想成真,但它甚至沒有發行。只有Valve公司用過它。什麼?你在Valve工作?你的上司是要支持這個技術了嗎?太多問題了。此外,約翰·卡馬克暗示Android可能不久會有一個類似Metal API的東西,所以Android設備的GPU甚至不需要接受它。那麼,當你考慮所有事情時,就是有太多的問題圍繞著它,太多的未知。無論如何,它不是Unity當下的選擇。

GLSL:現在你可以在Unity里寫GLSL了。Unity會將這段代碼編譯到任何平台,雖然你可能要在PC(或任何非iOS / Apple設備)上執行「-force-opengl」,以使之正常工作。那會導致在某些顯卡GPU上,Unity Profiler不能正確地執行配置。我很想知道,是否有可能解決這問題。所以,如果你用著Mac,團隊里人人用著Mac,那麼相信你應該使用GLSL- 絕對是明智之選。

Surface Shaders:你可以用Unity shader庫做些很酷的東西,但它不一定會在移動設備上有效果。 Shader庫就像一個簡化的,更易於閱讀的標記語言,但你失去了你手機遊戲需要的控制和性能選項。鑒於大多數在中國的工作是做手游,我不推薦這樣做。

Cg/HLSL:有人說,Cg是死的語言。那麼,事實是,Cg和HLSL是同一個語言,只是由兩個不同的公司,NVIDIA 和Microsoft分別維護。現在NIVIDIA已停止支持Cg,沒有必要啊是不,反正Microsoft在維護HLSL。人們仍然使用著Cg的名字,但願沒有人對此困惑了。 Cg/ HLSL當然沒有死,在跟幾個大牛以及論壇老兵聊過Unity,他們都認為HLSL是Unity shader的未來,因為公司為遊戲機做了很多,為3A級提升了許多。只要看看Unity5的特性,你會懂我意思了。Cg/HLSL可以被編譯到你需要的任何設備,同時Unity和微軟已經親密得像兄弟一樣,並且將一直親密下去直到地老天荒。

結論:

如果你想用Unity3d,為Windows,遊戲機,Android和iOS開發遊戲,並且在所有平台上表現不俗,你應該用Cg/HLSL。

如果你想用Unity3d,只做手游(不需要支持Windows或遊戲機),GLSL是上選,尤其是當你用Mac電腦工作。

如果你不想用Unity3d,只希望為最新的iOS設備開發,那麼Metal是最好的,儘管現在中國很少有人用它。

如果你想用cocos2d,我不是專家,但朋友說GLSL是您的最佳選擇。

不過,也有,但,也許......。在中國,佔主導地位的藝術風格是手繪。這並不需要很深的渲染知識,一個經驗豐富的人只要做很少的工作。Shader編碼為主要是為試圖超越目前世代的圖形,或創建特效,或避開某些圖形問題。所以,你要想想是否值得學習,因為很多種遊戲可能不需要這個。


從前是美術出身,做的基本虛擬比較多,對優化的要求不高,有就行。

大多情況需要弄shader都是因為透明效果,反射折射什麼,或者需要頂點色啥的。

這時候,我就掏出shaderforge,一發搞定(不要問我哪裡有download,我用正版的)


對於語言的選擇上 @David Lindsay 已經說的十分詳細了。補充一下推薦以及學習資料和參考書籍。

新手入門推薦CG/HLSL語言。十分簡單,而且Unity官方提供了詳盡的幫助,十分容易上手。一些常用的功能和函數也自帶,直接拿來用就好。

簡單介紹一下Unity的Shader系統。Unity的Shader大體可以分為這麼幾種:

  1. 最容易上手的Surface Shader,他只能用CG/HLSL語言來編寫.
  2. 較為複雜但可以深度定製的Vertex Shader和Fragment Shader,可以選擇Cg/HLSL,GLSL等等。

1 Surface Shader:他可以大概的看成Unity預置的Shader模板,通常只理物體的表面表現(例如高光,反射等等)。正如Unity官方所說:

直接寫一個與照明交互的Shader是複雜的。因為你要考慮不同的光照類型,不同的陰影選項,不同的渲染路徑(Forward和Deffered),直接寫Shader來處理這些東西是十分複雜的過程。

而Surface Shader是一個代碼生成方案,用它編Shader比使用更底層的Vertex Shader/Fragment Shader更容易。但需要注意的是,Surface Shader也不是什麼自定義的語言、魔法或者忍術;它只是自動幫你完成那些需要手寫的、重複的通用代碼。你仍然是用CG/HLSL語言來編寫Surface Shader。

模板中包含了大量的預設置的函數和功能,只要對這個模板進行少量的定製就能達到不錯的效果。舉個例子:如果你需要物體進行環境反射,在Vertex/Fragment Shader編寫中需要進行矩陣的變換和計算,數學渣如我就覺的頭大的不行。而在Surface Shader中你只需要在輸入中添加這麼一行:

float3 worldRefl;
INTERNAL_DATA

然後直接調用

texCUBE (_Cube, IN.worldRefl).rgb

即可。非常方便。

如果你覺得不理解內部的原理是如何操作的。那麼你可以查看Unity把你寫的Surface Shader自動補完成了什麼:

點擊我們寫的Surface Shader,在屬性面板(Inspector)中點擊Show generated code

你會看到Unity自動為你的Surface Shader添加了哪些代碼。比如對於上文提到的

worldRefl

的定義:

方便我們更深一步的理解和學習Shader到底是怎麼工作的。

學習途徑:

參考文檔 Unity - Manual: Writing Surface Shaders

官方例子 Unity - Manual: Surface Shader Examples

推薦參考書籍:Unity Shaders and Effects Cookbook 百度搜索

2 Vertex Shader和Fragment Shader。

如果簡單的模板不能滿足我們對於性能、效果等的要求,我們可以採用Vertex Shader和Fragment Shader,功能十分強大。它可以選擇不同的語言,但有一些小地方需要注意,比如GLSL就需要在PC設備上強制Unity引擎使用Opengl渲染,Metal只能支持iOS平台。

經過優化的Vertex Shader和Fragment Shader性能通常優於Sufrace Shader,而且定製的代碼可以實現Surface Shader模板中不能達到的效果。例如下面這個帶有假折射的玻璃效果,Sufrace Shader不容易完成,而Vertex Shader和Fragment Shader則比較輕鬆搞定。

如何選擇一個合適的語言David說的十分詳盡。而使用的不同語言,學習途徑有很多,例如:

針對GLSL開發的書籍:《Glsl Essentials》【摘要 書評 試讀】。

官方文檔1:Unity - Manual: Writing vertex and fragment shaders

官方文檔2:Unity - Manual: GLSL Shader Programs

如果你理解了Shader的工作原理,轉換語言不過就是API的改變而已。影響是有的,但不會太難。

以上是對於題主問題後半部分「有哪些Shader入門技巧及學習資料」的補充。

PS:Unity對於GLSL的態度:

However, use of raw GLSL is only recommended for testing,
or when you know you will only target Mac OS X, OpenGL ES 2.0 mobile
devices or Linux. In all normal cases, Unity will cross-compile Cg/HLSL
into optimized GLSL when needed.

-。- 粗體不是我加的,人家自己加粗的啊摔 _(:з」∠)_


shader對於一直開發服務端的人來說是一個利器,用來自殺的。。

又是一個很有意思的東西。。


做了八年美工,去年轉的程序,最近需要一個犯罪現場指紋的shader,就是把菲涅爾照應轉移乘到自身顏色上,在把Z和自身透明度乘一下。可能很多程序的人不懂,但是感覺曾經是美工的我竟然寫出了這個shader這個是我人生第一個shader,當然藉助於shaderforge起了個底子,但是效果還是自己整合出來的。


有沒有大牛告訴我float half fixed分別該什麼時候用 說的通俗點 我是新手 比如時間該用哪個 uv流動速度該用哪種類型 fixed的意思是不是數據只能是 1235這種整數 看推薦color要用fixed 那豈不是顏色只能全是純色?求大佬解惑


就一個語言,你還有的選?學下語法結構,就能搞創作了


推薦閱讀:

遊戲中的哪個角色觸動到你的心靈?
如何評價戰艦少女R 2017年11月2日發布的更新預告?
蛇蛇爭霸有哪些技巧(solo賽和團戰)?
如何評價 LoveLive! 繪凜雙人活動瘋狂抬線?
遊戲中氪金大佬與爆肝大佬誰更重要?

TAG:手機遊戲 | Unity遊戲引擎 | 學習資料 |