2013年3月24日日曜日

[Unity]Shader上で深度値を取得する方法


Shaderから深度値を取得する方法
チュートリアルの「Shader Replacement」が良い教材

◆Edge Detection

・カメラ上で以下のコードを追加する
取得できるのはモードにより異なる
  • 深度のみ
  • 深度と法線

void OnEnable() {
    camera.depthTextureMode = DepthTextureMode.Depth;
    camera.depthTextureMode = DepthTextureMode.DepthNormals;
}



・Shader上の処理
深度バッファ値を格納してあるテクスチャー情報を取得する。
そのためには以下のようにShader用のグローバルな固定変数がある
sampler2D _CameraDepthTexture;
sampler2D _CameraDepthNormalsTexture;

デプスバッファはプラットフォーム毎に異なる実装方法なので、
リンクを参照する。
あとは、差異をなるべく少なくするためのマクロも実装されているので活用する。


◆Glowing Things

RenderWithShader -> 特定のレンダリングパスにシェーダーを適応する処理
そのデモが「Glowing Things」である。

・オブジェクトに適応するShaderのレンダリングタイプを設定する
Hierarchy上のCylinderオブジェクトには「VertexLit Funky」というShaderが適応されている。
ポストエフェクト時に演出していることがわかる。


以下、Unity Script からの抜粋
OnPreCull is called before a camera culls the scene.
OnPreRender is called before a camera starts rendering the scene.
OnPostRender is called after a camera finished rendering the scene.
OnRenderObject is called after camera has rendered the scene.
OnWillRenderObject is called once for each camera if the object is visible.
OnGUI is called for rendering and handling GUI events.
OnRenderImage is called after all rendering is complete to render image






0 件のコメント:

コメントを投稿