Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Unity:Material(Standard Shader)のスクリプト設定を全網羅!

More than 1 year has passed since last update.

はじめに

こんにちは、のんびりエンジニアのたっつーです。
Unity:Material(Standard Shader)のスクリプト設定を全網羅! で書いたのですがQiitaでも共有したいと思います。

ソースコード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class materialTest : MonoBehaviour
{
    public Texture texture;

    void Start ()

    {
        SetMaterial();
    }

    void SetMaterial ()
    {
        var material = this.GetComponent<Renderer>().material;

        /////////////////////////////////////////////////////
        // Main Maps
        /////////////////////////////////////////////////////

        // Albedo
        material.mainTexture = texture;
        material.color = Color.red;
        // 以下と同等
        // material.SetTexture("_MainTex", texture);
        // material.SetColor("_Color", Color.red);

        // Metallic
        material.SetTexture("_MetallicGlossMap", texture);
        material.SetFloat("_Metallic", 1.0f);

        // Smoothness
        //   -> Albedo Alpha
        material.SetInt("_SmoothnessTextureChannel", 1);
        material.SetFloat("_GlossMapScale", 0.93f);

        // Smoothness
        //   -> Metallic Alpha
        material.SetInt("_SmoothnessTextureChannel", 0);
        material.SetFloat("_Glossiness", 0.9f);

        // Normal Map
        material.SetTexture("_BumpMap", texture);
        material.SetFloat("_BumpScale", 1.4f);

        // Height Map
        material.SetTexture("_ParallaxMap", texture);
        material.SetFloat("_Parallax", 1.5f);

        // Occlusion
        material.SetTexture("_OcclusionMap", texture);
        material.SetFloat("_OcclusionStrength", 1.6f);

        // Ditail Mask
        material.SetTexture("_DetailMask", texture);

        // Emission
        // インスペクターでは無効に見えるが効いている
        material.EnableKeyword("_EMISSION");
        material.SetColor("_EmissionColor", Color.green);
        material.SetTexture("_EmissionMap", texture);

        // Tiling
        material.mainTextureScale = new Vector2(2, 2);
        // 以下と同等
        // material.SetTextureScale("_MainTex", new Vector2(2, 2));

        // Offset
        material.mainTextureOffset = new Vector2(3, 3);
        // 以下と同等
        // material.SetTextureOffset("_MainTex", new Vector2(3, 3));

        /////////////////////////////////////////////////////
        // Secondary Maps
        /////////////////////////////////////////////////////

        // Detail Albedo x
        material.SetTexture("_DetailAlbedoMap", texture);

        // Normal Map
        material.SetTexture("_DetailNormalMap", texture);
        material.SetFloat("_DetailNormalMapScale", 1.2f);

        // Tiling
        material.SetTextureScale("_DetailAlbedoMap", new Vector2(4, 4));

        // Offset
        material.SetTextureOffset("_DetailAlbedoMap", new Vector2(5, 5));

        // UVSet
        material.SetFloat("_UVSec", 0); // 0:UV0, 1:UV1

        /////////////////////////////////////////////////////
        // Forward Rendering Options
        /////////////////////////////////////////////////////

        // Specular Highlights
        material.SetFloat("_SpecularHighlights", 0); // 0:OFF, 1:ON

        // Reflections
        material.SetFloat("_GlossyReflections", 0); // 0:OFF, 1:ON

        /////////////////////////////////////////////////////
        // Advanced Options
        /////////////////////////////////////////////////////

        //   Enable GPUI Instancing
        material.enableInstancing = true;

        //   Double Sided Global
        material.doubleSidedGI = true;
    }
}

実行結果

WS000002-4.jpg

title-3.jpg

終わりに

よければ ブログ「初心者向けUnity情報サイト」の方にも色々記載しているのでぜひご参照いただければと思います。

kingyo222
Hololens、トマト、ロードバイク を愛している人
http://www.fast-system.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away