トップ 一覧 検索 ヘルプ RSS ログイン

unity_script_create_materialの変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!マテリアルの作成

スクリプト内でマテリアルを作成して形状に割り当てます。
「[[OpenGL描画|unity_script_opengl]]」のページも参照。

ここでは、GameObjectとして事前に配置されている「Plane」という形状に対して新しく生成したマテリアルを割り当てます。

 using UnityEngine;
 using System.Collections;
 
 public class createMaterial : MonoBehaviour {
    private Texture2D m_texture;
    private Material m_material;
    
    // マテリアルの生成.
    void createMyMaterial () {
        // テクスチャの作成.
        m_texture = new Texture2D(32, 32, TextureFormat.ARGB32, false);
        for (int y = 0; y < m_texture.height; y++) {
            for (int x = 0; x < m_texture.width; x++) {
                if ((x & 1) != 0 && (y & 1) == 0) m_texture.SetPixel(x, y, Color.red);
                else m_texture.SetPixel(x, y, Color.white);
            }
        }
        m_texture.Apply();
       
        // マテリアルの作成.
        m_material = new Material(
            "Shader \"myShader\" {" +
            "  Properties {" +
            "    _Color (\"Main Color\", Color) = (1,1,1,0)" + 
            "    _SpecColor (\"Spec Color\", Color) = (1,1,1,1)" +
            "    _Shininess (\"Shininess\", Range (0.01, 1)) = 0.7" +
            "    _MainTex (\"Base (RGB)\", 2D) = \"white\" {}" +
            "  }" +
            "  SubShader {" +
            "    Pass {" +
            "       ZWrite On" +
            "       SeparateSpecular On" +
            "       Lighting On" +
            "       BindChannels {" +
            "         Bind \"vertex\", vertex Bind \"color\", color" +
            "       }" +
            "       Material {" +
            "         Diffuse [_Color]" +
            "         Ambient [_Color]" +
            "         Shininess [_Shininess]" +
            "         Specular [_SpecColor]" +
            "       }" +
            "       SetTexture [_MainTex] {" +
            "         Combine texture * primary DOUBLE, texture * primary" +
            "       }" +
            "    }" +
            "  }" +
            "}"
        );
        m_material.hideFlags = HideFlags.HideAndDontSave;
        m_material.shader.hideFlags = HideFlags.HideAndDontSave;
        m_material.mainTexture = m_texture;
        m_material.color = Color.white;
    }
    
    // Use this for initialization
    void Start () {
        createMyMaterial();
        
        GameObject gobj = GameObject.Find("Plane");
        if (gobj != null) {
            gobj.renderer.material = m_material;
        }
    }
    
    // Update is called once per frame
    void Update () {
    
    }
 }

{{ref_image material_001.jpg}}

new Texture2Dでのテクスチャの生成については「[[テクスチャの作成|unity_script_create_texture2d]]」を参照。

new Materialでのマテリアルの生成内のシェーダーについては「[[単純なシェーダ|unity_shader_basic]]」を参照。

「m_material」に格納されたマテリアルに対して、
 m_material.hideFlags = HideFlags.HideAndDontSave;
 m_material.shader.hideFlags = HideFlags.HideAndDontSave;
 m_material.mainTexture = m_texture;
 m_material.color = Color.white;
としてテクスチャ含むパラメータを指定します。

GameObject として「gobj.renderer.material = m_material」としてマテリアルを割り当て。
マテリアルとしては、デフューズと環境光色、スペキュラ、テクスチャを与えています。

!!法線マップを指定する

既存の「Bumped Specular」シェーダを利用して法線マップ対応のマテリアルを生成します。
 private Texture2D m_texture;
 private Texture2D m_texture_normal;
 private Material m_material;
 
 // マテリアルの生成.
 void createMyMaterial () {
     // テクスチャの作成.
     m_texture = new Texture2D(32, 32, TextureFormat.ARGB32, false);
     for (int y = 0; y < m_texture.height; y++) {
         for (int x = 0; x < m_texture.width; x++) {
             if ((x & 1) != 0 && (y & 1) == 0) m_texture.SetPixel(x, y, Color.red);
             else m_texture.SetPixel(x, y, Color.white);
         }
     }
     m_texture.Apply();
       
     // 法線マップの作成.
     Color col = new Color();
     m_texture_normal = new Texture2D(32, 32, TextureFormat.ARGB32, false);
     for (int y = 0; y < m_texture_normal.height; y++) {
         for (int x = 0; x < m_texture_normal.width; x++) {
             col.r = 0.5f;
             col.g = 0.5f;
             col.b = 1.0f;
             m_texture_normal.SetPixel(x, y, col);
         }
     }
     m_texture_normal.Apply();
        
     // マテリアルの作成.
     m_material = new Material(
         "Shader \"myBumpSpecularShader\" {" +
         "  Properties {" +
         "    _SpecColor (\"Spec Color\", Color) = (1, 1, 1, 1)" +
         "    _Shininess (\"Shininess\", Range (0.01, 1)) = 0.2" +
         "    _MainTex (\"Texture\", 2D) = \"white\" {}" + 
         "    _BumpMap (\"BumpMap\", 2D) = \"bump\" {}" +
         "   }" +
         "   Fallback \"Bumped Specular\"" +
         " }"
     );
     m_material.hideFlags = HideFlags.HideAndDontSave;
     m_material.shader.hideFlags = HideFlags.HideAndDontSave;
     m_material.mainTexture = m_texture;
     m_material.SetTexture("_BumpMap", m_texture_normal);
     m_material.color = Color.white;
 }
「m_texture」に拡散反射のテクスチャを、
「m_texture_normal」に法線マップのテクスチャを格納するとします。
法線マップは「RGB = (0.5, 0.5, 1.0)」が真上を向いたベクトルに相当します。ここから、XとYに±0.5ずつ方向を変えることで各ピクセルの向きを指定します。

「new Material」でのマテリアル生成は「[[既存のシェーダーを利用|unity_shader_use_default]]」も参考。「Bumped Specular」の既存シェーダを利用して、テクスチャだけ変えるようにしています。

  m_material.mainTexture = m_texture;
  m_material.SetTexture("_BumpMap", m_texture_normal);
で、拡散反射のテクスチャと法線マップのテクスチャを指定するようにしています。

!!既存のShaderを割り当てるマテリアルを作成
 Material skyboxMaterial = new Material(Shader.Find("Mobile/Skybox"));
 skyboxMaterial.SetTexture("_FrontTex", m_dstTextureFront);
 skyboxMaterial.SetTexture("_BackTex", m_dstTextureBack);
 ...

Materialのコンストラクタで「Shader.Find(Shader名)」とすることで、
既存のShaderを割り当てたマテリアルを生成します。
SetTexture関数の第一引数でShaderのプロパティ名を指定、第二引数でTexture2Dを渡します。
プロパティ名は、ShaderソースのProperties内を参照。
 Shader "Mobile/Skybox" {
 Properties {
   _FrontTex ("Front (+Z)", 2D) = "white" {}
   _BackTex ("Back (-Z)", 2D) = "white" {}
   _LeftTex ("Left (+X)", 2D) = "white" {}
   _RightTex ("Right (-X)", 2D) = "white" {}
   _UpTex ("Up (+Y)", 2D) = "white" {}
   _DownTex ("down (-Y)", 2D) = "white" {}
 }
としている「_FrontTex」などがプロパティ名です。

ただし「Shader.Find」はUnity Editorでの実行時のみ使用でき、
ビルドして実行する場合は例外が発生します。

!!ビルドして実行する際のShaderを割り当て

プロジェクトでResourcesフォルダを作成し、そこに「Shaders/xxxx.shader」を入れる(Shadersはフォルダとする)。
 Shader shader = Resources.Load<Shader>("Shaders/xxxx");
 Material newMat= new Material(shader );
のようにしてShaderを読み込んでMaterialに割り当てる。

注意点として、Resources.Load<Shader>を使用すると一部の環境でEditorで実行する際に
その都度Shaderのコンパイルがかかる ?(Macでの実行時に確認)
複雑な処理をShaderにやらせている場合は待たされる。

----
{{lastmodified}}