Warning: Declaration of action_plugin_linebreak::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/action.php on line 41

Warning: Declaration of action_plugin_markdownextra::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/action.php on line 16

Warning: Declaration of action_plugin_syntaxhighlighter3_action::register(Doku_Event_Handler &$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/action/action.php on line 28
080 スクリプトからShaderの種類を変更する [stepism@UE4メモ]

ユーザ用ツール

サイト用ツール


wiki:unity:tips:080

以前のリビジョンの文書です —-


Warning: Declaration of syntax_plugin_linebreak::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line 52

Warning: Declaration of syntax_plugin_linebreak::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line 74

Warning: Declaration of syntax_plugin_markdownextra::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line 38

Warning: Declaration of syntax_plugin_markdownextra::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line 47

Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line 53

Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line 82

080 スクリプトからShaderの種類を変更する

概要

Unity におけるシェーダは、マテリアルを通して実装されています。
マテリアルをゲームオブジェクトにアタッチすることで見た目を変化させたりできます。

Unityに標準搭載のシェーダは、マテリアルの設置画面から選択できます。

プログラムからシェーダを変化させるには以下の様にします。

gameObject.renderer.material.shader = Shader.Find( "Self-Illumin/Diffuse" ); 

この状態で再生ボタンを押すとシェーダーが変更され、見た目が変わると思います。

Androidなどの端末に出力すると上手く表示されない

~Shader.Find 関数でシェーダを取得し設定しているのだから問題ない様に見えます。
しかし端末に転送するとShader.Findで返ってくる値はnullです。なぜ?

これは、内臓シェーダが全てパッケージに含まれる訳ではないためです。Unityでは、ビルド時に必要なファイルをかき集めてパッケージを作成しますが、スクリプトで文字列指定されているファイルまでは流石に見ません。なので、“Self-Illumin/Diffuse”シェーダは使われていないものとしてパッケージが作成されたわけです。それで、これを解決するにはどうすれば良いのか?それは冒頭にも述べた様にシェーダはマテリアルを通して実装されています。従って、シェーダ毎にマテリアルを分けてあげれば良いのです。

マテリアルを複数個作りResourcesフォルダに入れます。
シェーダの設定を別々にしておきます。

Material diffuseBlockMaterial = Resources.Load<Material>( "diffuseBlockMaterial" ); 
Material illuminBlockMaterial = Resources.Load<Material>( "Self-IlluminBlockMaterial" ); 

マテリアルを事前にロードしておいて…

gameObject.renderer.material.shader = illuminBlockMaterial.shader; 

必要なタイミングで入れ替えます。これできちんと反映される様になりました。

ちなみに、現時点(2014/07/18)でUnity内臓のシェーダはこれだけあります。

 
 "Bumped Diffuse" 
 "Bumped Specular" 
 "Decal" 
 "Diffuse" 
 "Diffuse Detail" 
 "Parallax Diffuse" 
 "Parallax Specular" 
 "Specular" 
 "VertexLit" 
  
 "FX/Flare" 
  
 "GUI/Text Shader" 
  
 "Mobile/Bumped Diffuse" 
 "Mobile/Bumped Specular" 
 "Mobile/Bumped Specular (1 Directional Light)" 
 "Mobile/Diffuse" 
 "Mobile/Particles/Additive" 
 "Mobile/Particles/Alpha Blended" 
 "Mobile/Particles/Multiply" 
 "Mobile/Particles/VertexLit Blended" 
 "Mobile/Skybox" 
 "Mobile/Unlit (Supports Lightmap 
 "Mobile/VertexLit (Only Directional Lights)" 
  
 "Nature/Terrain/Bumped Specular" 
 "Nature/Terrain/Diffuse" 
 "Nature/Tree Creator Bark" 
 "Nature/Tree Creator Leaves" 
 "Nature/Tree Creator Leaves Fast" 
 "Nature/Tree Soft Occlusion Bark" 
 "Nature/Tree Soft Occlusion Leaves" 
  
 "Particles/Additive" 
 "Particles/Additive (Soft)" 
 "Particles/Alpha Blended" 
 "Particles/Alpha Blended Premultiply" 
 "Particles/Multiply" 
 "Particles/Multiply (Double)" 
 "Particles/VertexLit Blended" 
 "Particles/~Additive-Multiply" 
  
 "Reflective/Bumped Diffuse" 
 "Reflective/Bumped Specular" 
 "Reflective/Bumped Unlit" 
 "Reflective/Bumped VertexLit" 
 "Reflective/Diffuse" 
 "Reflective/Parallax Diffuse" 
 "Reflective/Parallax Specular" 
 "Reflective/Specular" 
 "Reflective/VertexLit" 
  
 "RenderFX/Skybox" 
 "RenderFX/Skybox Cubed" 
  
 "Self-Illumin/Bumped Diffuse" 
 "Self-Illumin/Bumped Specular" 
 "Self-Illumin/Diffuse" 
 "Self-Illumin/Parallax Diffuse" 
 "Self-Illumin/Parallax Specular" 
 "Self-Illumin/Specular" 
 "Self-Illumin/VertexLit" 
  
 "Sprites/Default" 
 "Sprites/Diffuse" 
  
 "Transparent/Bumped Diffuse" 
 "Transparent/Bumped Specular" 
 "Transparent/Cutout/Bumped Diffuse" 
 "Transparent/Cutout/Bumped Specular" 
 "Transparent/Cutout/Diffuse" 
 "Transparent/Cutout/Soft Edge Unlit" 
 "Transparent/Cutout/Specular" 
 "Transparent/Cutout/VertexLit" 
 "Transparent/Diffuse" 
 "Transparent/Parallax Diffuse" 
 "Transparent/Parallax Specular" 
 "Transparent/Specular" 
 "Transparent/VertexLit" 
  
 "Unlit/Texture" 
 "Unlit/Transparent" 
 "Unlit/Transparent Cutout" 
wiki/unity/tips/080.1415870737.txt.gz · 最終更新: 2015/02/14 12:19 (外部編集)