玻璃质感测试案例:循环生成多排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官方的着色器压缩包里的代码来书写: [