3

[譯]Unity – Rendering with Replaced Shaders – NaCl's Blog

 2 years ago
source link: https://fredxxx123.wordpress.com/2012/07/09/%e8%ad%afunity-rendering-with-replaced-shaders/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[譯]Unity – Rendering with Replaced Shaders

原文:Rendering with Replaced Shaders

有些會治效果需要使用不同的 shader 設定去繪製場景。例如,優良的 edge detection 會需要含有場景 normal 值的 texture,這樣一來它就可以確認哪些 surface 的方向是不同的。然後其他特效也許會需要場景的 depth 等等。為了實現這個,是可以讓所有物件使用 replaced shaders 繪製場景。

Shader 的更換可用 script 呼叫 Camera.RenderWithShaderCamera.SetReplacementShader 兩個方法完成。兩者都需要一個 shader 以及 replacementTag

它的運作如下:該 camera 如同往常繪製場景。物件仍然使用它們的 materials,but the actual shader that ends up being used is changed:

  • 如果 replacementTag 是空的,那麼所有的物件都會使用給予的 replacement shader 繪製]。
  • 如果 replacementTag 不是空的,那麼每個物件將會繪製成:
    ○查詢原本在使用的 shader 的 tag value
    ○如果沒有擁有對應的 tag,那麼這個物件將不會被繪製
    ○搜尋在 replacement shader 擁有查詢到的 tag 值的 subshader。如果沒有這樣的 subshader 被找到,那麼這個物件不會被繪製
    ○現在,使用那個 subshader 繪製這個物件。

所以如果所有的 shaders 都存在,例如,內容為"Opaque", "Transparent", "Background", "Overlay" 的 "RenderType" 的 tag。你寫一個帶有 RenderType=Solid tag 的 replacement shader,用一個 subshader 只會繪製 solid 物件。其他種類 tag 的不會在這個 replacement shader 裡面找到,所以物件將不會被繪製。或是你可以寫數種 subshaders 給不同的 "RenderType" tag值使用。順帶一提,所有內建的 Unity shaders 都會設定一個 "RenderType" tag。

Shader replacement tags in built-in Unity shaders

所有內建的 Unity shader 都有一個 "RenderType" 的 tag,那個可以再使用 replaced shaders 時使用。Tag 值會是如下:

  • Opaque: 大多數的 shaders (Normal, Self Illuminated, Reflective, terrain shaders).
  • Transparent: 大多數的半透明 shaders (Transparent, Particle, Font, terrain additive pass shaders).
  • TransparentCutout: 隱藏透明的 shaders (Transparent Cutout, two pass vegetation shaders).
  • Background: Skybox shaders.
  • Overlay: GUITexture, Halo, Flare shaders.
  • TreeOpaque: terrain engine tree bark.
  • TreeTransparentCutout: terrain engine tree leaves.
  • TreeBillboard: terrain engine billboarded trees.
  • Grass: terrain engine grass.
  • GrassBillboard: terrain engine billboarded grass.

Built-in scene depth/normals texture

一個 camera 會有一個內建的功能去繪製 depth 或 depth+normals texures,如果你需要在某些特效上使用它們。參考 Camera Depth Texture 頁面。要注意在某些情況(根據硬體不同),depth 和 depth+normals textures 會自己使用 shader replacement。所以在你的 shader 裡面設定正確的"RenderType"是很重要的。

發佈於 2012/07/092012/07/09作者 fredxxx123分類 程 - 式 - 設 - 計, Unity標籤 shader, Unity


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK