Unity3D_改变材质球属性

本篇博客介绍改变材质球的 RenderingModel 属性,及其普通属性的方法。

改变 RenderModel

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

public class JiaoLongHao : MonoBehaviour
{
    public Material mat;
    void Update()
    {
        // 当按下 O 是,设置制定材质球的 RenderingModel 属性为 Opaque
        if (Input.GetKeyDown(KeyCode.O))
        {
            setMaterialRenderingMode(mat, RenderingMode.Opaque);
        }
    }

    private enum RenderingMode
    {
        Opaque,
        Cutout,
        Fade,
        Transparent
    }

    /// <summary>
    /// 设置材质球的 RenderingModel 属性
    /// </summary>
    /// <param name="material">需要改变 RenderingModel 属性的材质球</param>
    /// <param name="renderingMode">将材质球的 RenderingModel 属性设置为该参数类型(该参数为枚举类型)</param>
    private void setMaterialRenderingMode(Material material, RenderingMode renderingMode)
    {
        switch (renderingMode)
        {
            case RenderingMode.Opaque:
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.DisableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = -1;
                break;
            case RenderingMode.Cutout:
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.EnableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 2450;
                break;
            case RenderingMode.Fade:
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 3000;
                break;
            case RenderingMode.Transparent:
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 3000;
                break;
        }
    }
}

改变材质球的普通属性

以Smoothness属性为例:选择一个材质球 - 点击材质球右上角的“齿轮” - 选择“Edit Shader” - 查看带有Smoothness相关字样的属性(名称 及 类型)

   /// <summary>
    /// 改变材质球的 Smoothness 属性
    /// </summary>
    /// <param name="mat">需要改变属性的材质球</param>
    /// <param name="f">需要改变的数值</param>
    private void SetSmoothness(Material mat, float f)
    {
        // 如图蓝色方框所示,Smoothness 为 float 类型,所以使用 SetFloat 方法,其他类型的属性以此类推
        // 参数一:Shader 中 Smothness 属性的全称,参数二:要把该属性设置的目标值
        mat.SetFloat("_SmoothnessTextureChannel", f);
    }

 

posted on 2020-05-13 10:05  考拉宝贝  阅读(4135)  评论(0编辑  收藏  举报

导航