玻璃质感测试案例:循环生成多排3D 球体,通过C#代码修改球体身上材质球相关参数。 渲染模式(Rendering Mode):Transparent Albedo里的Alpha值透明度调为0; 四种情况: 第一排:金属度:00.5,平滑度0.5; 第二排:金属度:00.5,平滑度1; 第三排:金属度:0,平滑度0.51; 第四排:金属度0.5,平滑度0.51。 项目代码:

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

public class GlassDemo : MonoBehaviour {

private Material m\_Material;
public GameObject prefab;

void Start () {

    //金属度:0~0.5,平滑度0.5;
    for (int i = 0; i <= 5; i++)
    {
        Vector3 pos = new Vector3(i, 0, 0);
        GameObject go = GameObject.Instantiate(prefab, pos, Quaternion.identity);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Metallic", i \* 0.1f);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Glossiness",0.5f);
    }

    //金属度:0~0.5,平滑度1;
    for (int i = 0; i <= 5; i++)
    {
        Vector3 pos = new Vector3(i, 0, -1);
        GameObject go = GameObject.Instantiate(prefab, pos, Quaternion.identity);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Metallic", i \* 0.1f);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Glossiness", 1);
    }

    //金属度:0,平滑度0.5~1;
    for (int i = 0; i <= 5; i++)
    {
        Vector3 pos = new Vector3(i, 0, -2);
        GameObject go = GameObject.Instantiate(prefab, pos, Quaternion.identity);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Metallic", 0);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Glossiness", i \* 0.1f);
    }

    //金属度0.5,平滑度0.5~1。
    for (int i = 0; i <= 5; i++)
    {
        Vector3 pos = new Vector3(i, 0, -3);
        GameObject go = GameObject.Instantiate(prefab, pos, Quaternion.identity);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Metallic", 0.5f);
        go.GetComponent<MeshRenderer>().material.SetFloat("\_Glossiness", i \* 0.1f);
    }
}

}

代码中Shader参数的值可以对照Unity官方的着色器压缩包里的代码来书写: [

](http://www.wjgbaby.com/wp-content/uploads/2018/03/18032903-300x178.png)](http://www.wjgbaby.com/wp-content/uploads/2018/03/18032903.png)项目运行效果,未开启光线渲染: [![](http://www.wjgbaby.com/wp-content/uploads/2018/03/18032904-300x183.png)
](http://www.wjgbaby.com/wp-content/uploads/2018/03/18032903-300x178.png)](http://www.wjgbaby.com/wp-content/uploads/2018/03/18032903.png)项目运行效果,未开启光线渲染: [![](http://www.wjgbaby.com/wp-content/uploads/2018/03/18032904-300x183.png)
玻璃质感总结 1.两个参数 Albedo 有两个参数,贴图和颜色面板,颜色面板里面的Alpha 通道是0~255。 Albedo 贴图是一张纯色纹理图,贴图是可以存Alpha 通道的。 颜色面板里面的Alpha 通道,调整的时候,整个模型都是统一的一种效果; 贴图则可以分区域控制透明度的高与低,使得模型透明效果更有层次。