MEL によるシェーダー 3

surfaceLuminance を使ったシェーダー

surfaceLuminance ノードを使うことによって、 オブジェクトの表面における輝度を使ったシェーダーを作ることができます。  これによって、様々な特殊効果を実現することができます。

シェーダーの例 3

surfaceLuminance ノードを使用したシェーダーの例として、 輝度の明るいところだけ別のマテリアルになるシェーダーを作ってみましょう。
以下の lightShader1 は、輝度が 0.5 以上のところは blinn、 0.5 以下のところは lambert になるシェーダーをエクスプレッションによって作成するプロシージャです。

  1. 以下の MEL スクリプトを lightShader1.mel という名前で作ります。
    global proc lightShader1()
    {
    	string $shname1 = `shadingNode -asShader lambert`;
    	string $sgname1 = `sets -renderable true -noSurfaceShader true -empty`;
    	connectAttr -f ($shname1 + ".outColor") ($sgname1 + ".surfaceShader");
    
    	string $shname2 = `shadingNode -asShader blinn`;
    	string $sgname2 = `sets -renderable true -noSurfaceShader true -empty`;
    	connectAttr -f ($shname2 + ".outColor") ($sgname2 + ".surfaceShader");
    
    	string $shname3 = `shadingNode -asShader surfaceShader`;
    	string $sgname3 = `sets -renderable true -noSurfaceShader true -empty`;
    	connectAttr -f ($shname3 + ".outColor") ($sgname3 + ".surfaceShader");
    
    	string $slname = `shadingNode -asUtility surfaceLuminance`;
    
    	expression -o $shname3 -s
    		("if(" + $slname + ".o > 0.5)"
    		+ "{"
    		+ "ocr = " + $shname2 + ".ocr;"
    		+ "ocg = " + $shname2 + ".ocg;"
    		+ "ocb = " + $shname2 + ".ocb;"
    		+ "}"
    		+ "else"
    		+ "{"
    		+ "ocr = " + $shname1 + ".ocr;"
    		+ "ocg = " + $shname1 + ".ocg;"
    		+ "ocb = " + $shname1 + ".ocb;"
    		+ "}");
    }
    
  2. Script EditorFile → Source Script によって lightShader1.mel を読み込みます。
  3. Script Editor のインプットウインドウで、lightShader1() を実行します。
    (lightShader1() 実行後の HyperShade の図)
    [lightShader1() 実行後の HyperShade]
  4. 結果をわかりやすく表示するために lambert と blinn の diffuse の色を変えます。
    (HyperShade の図)
    [色を変更後の HyperShade]
  5. オブジェクトとライトを適当に作って、オブジェクトに surfaceShader をアサインした後にレンダリングしてみます。
    以下の図は NURBS の球と Point Light を 2 個配置してレンダリングしています。
    輝度が 0.5 以上のところが赤(blinn)、 0.5 以下のところが緑(lambert)になっています。
    [レンダリング実行図]

スクリプトの解説

global proc lightShader1()
lightShader1 というプロシージャの宣言です。
string $shname1 = `shadingNode -asShader lambert`;
lambert マテリアルを作ります。
shadingNode コマンドによってノードを作ることによって、 HyperGraph などでマテリアルとして表示できるようになります。
string $sgname1 = `sets -renderable true -noSurfaceShader true -empty`;
マテリアルをコネクトするためのセットを作ります。
-renderable true
作られたセットはシェーディンググループになって、 renderPartition ノードにコネクトされます。
-noSurfaceShader true
デフォールトのシェーダーにコネクトしません。
上で作ったマテリアル $shname1 にコネクトするからです。
-empty
空のセットを作ります。
セレクトされているオブジェクトがあっても、 このセットに加えません。
connectAttr -f ($shname1 + ".outColor") ($sgname1 + ".surfaceShader");
マテリアルが $shname1 出力する色情報を表す outColor アトリビュートを、 セット $sgname1 の surfaceShader アトリビュートにコネクトします。
これで、マテリアル $shname1 がレンダリング可能になります。
string $shname2 = `shadingNode -asShader blinn`;
string $sgname2 = `sets -renderable true -noSurfaceShader true -empty`;
connectAttr -f ($shname2 + ".outColor") ($sgname2 + ".surfaceShader");
blinn マテリアルを作ること以外は lambert の場合と同じ処理をしています。
string $shname3 = `shadingNode -asShader surfaceShader`;
string $sgname3 = `sets -renderable true -noSurfaceShader true -empty`;
connectAttr -f ($shname3 + ".outColor") ($sgname3 + ".surfaceShader");
surfaceShader マテリアルを作ること以外は lambert, blinn の場合と同じ処理をしています。
string $slname = `shadingNode -asUtility surfaceLuminance`;
surfaceLuminance ノードを作ります。
expression -o $shname3 -s
("if(" + $slname + ".o > 0.5)"
+ "{"
+ "ocr = " + $shname2 + ".ocr;"
+ "ocg = " + $shname2 + ".ocg;"
+ "ocb = " + $shname2 + ".ocb;"
+ "}"
+ "else"
+ "{"
+ "ocr = " + $shname1 + ".ocr;"
+ "ocg = " + $shname1 + ".ocg;"
+ "ocb = " + $shname1 + ".ocb;"
+ "}");
surfaceShader ノード($shname3)にエクスプレッションを定義して、 色の計算をしています。
以下に式の部分を解説します。
("if(" + $slname + ".o > 0.5)"
surfaceLuminance ノードの o (outValue) アトリビュートの値が 0.5 より大きいかどうかを調べています。
+ "ocr = " + $shname2 + ".ocr;"
+ "ocg = " + $shname2 + ".ocg;"
+ "ocb = " + $shname2 + ".ocb;"
0.5 より大きい場合は blinn ($shname2)の色を surfaceShader の色に設定します。
+ "ocr = " + $shname1 + ".ocr;"
+ "ocg = " + $shname1 + ".ocg;"
+ "ocb = " + $shname1 + ".ocb;"
0.5 より小さい場合は lambert ($shname1)の色を surfaceShader の色に設定します。

練習

練習課題

参考


Prev | Next
Home | Contents
Mail