The material exported by the editor is only the base class Material, while through code you can create the engine's pre-packaged PBRMaterial, UnlitMaterial, and BlinnPhongMaterial.
// 获取想要修改的 renderer
const renderer = entity.getComponent(MeshRenderer);
// 或者获取所有 renderer
const renderers = [];
entity.getComponentsIncludeChildren(MeshRenderer, renderers);
// 通过 `getMaterial` 获取当前 renderer 的第 i 个材质, 默认第 0 个。
const material = renderer.getMaterial();
We can also directly replace the material type, for example, reassigning a PBR material to the model:
// 获取想要修改的 renderer
const renderer = entity.getComponent(MeshRenderer);
// 创建材质
const material = new PBRMaterial(engine);
// 通过 `setMaterial` 设置当前 renderer 的第 i 个材质, 默认第 0 个。
const material = renderer.setMaterial(material);
const pbrMaterial = new PBRMaterial(engine);
const bpMaterial = new BlinnPhongMaterial(engine);
const unlitMaterial = new UnlitMaterial(engine);
// Refer to the shader tutorial for the specific steps of Shader.create
const customMaterial = new Material(engine, Shader.find("***"));
// 设置透明,引擎已经封装好对应渲染状态的设置
pbrMaterial.isTransparent = true;
// 设置透明度
pbrMaterial.baseColor.a = 0.5;
// 金属、粗糙度等其他配置
pbrMaterial.metallic = 1;
pbrMaterial.baseTexture = **;
const shaderData = material.shaderData;
// 获取想要设置的着色器数据
const baseColor = shaderData.setFloat("material_BaseColor");
// 修改着色器数据
baseColor.a = 0.5;
shaderData.setTexture("material_BaseTexture", texture);
shaderData.enable("MATERIAL_HAS_BASETEXTURE");
// 更多的着色器操作,详见着色器文档