[开发者心得] 【027】材质接口变更

[复制链接]
667 |0
喵喵哭唧唧 发表于 2023-3-22 20:15:19 | 显示全部楼层 |阅读模式
本帖最后由 哭唧唧的细狗 于 2024-1-4 14:23 编辑

什么是材质
材质主要是用来表现物体对光的交互(反射、折射等)性质。譬如金属对光的反射和毛毯对光的反射性质完全不一样,对与口袋方舟编辑器来说,这样的差别就通过材质这个属性来计算出不同的颜色。

口袋方舟编辑器提供了大量的材质资源,供开发者使用。

027后接口是什么样的?

设置颜色

GameObject.asyncFindGameObjectById("xxx").then(go=>{
    let model = go as Model
    // getMaterialInstance()[0]这里我是去获取对象的第一个材质实例
    let mat = model.getMaterialInstance()[0]

    // 设置对象的
Main_Diffuse_ColorLinearColor(1,0,0)
    //
Main_Diffuse_Color是设置漫反射相关的参数名,具体可以参见后边章节的介绍
    mat.setVectorParameterValue("Main_Diffuse_Color", new LinearColor(1,0,0))
})


image.png

设置前

image.png
设置后

设置自发光



GameObject.asyncFindGameObjectById("xxx").then(go=>{
   let model = go as Model
   // getMaterialInstance()[0]这里我是去获取对象的第一个材质实例
   let mat = model.getMaterialInstance()[0]

   // 设置对象的BreathingLamp_Color为new LinearColor(1500,0,0)
   // BreathingLamp_Color是设置呼吸灯颜色相关的参数名,具体可以参见后边章节的介绍
   mat.setVectorParameterValue("BreathingLamp_Color", new LinearColor(1500,0,0))
})

image.png



设置贴图


GameObject.asyncFindGameObjectById("xxx").then(go=>{
    let model = go as Model
    // getMaterialInstance()[0]这里我是去获取对象的第一个材质实例
    let mat = model.getMaterialInstance()[0]

    // 设置对象的Main_Color_Texture为46816的贴图
    // Main_Color_Texture是设置主贴图相关的参数名,具体可以参见后边章节的介绍
    mat.setTextureParameterValue("Main_Color_Texture", "46816")
})


image.png

设置平铺


GameObject.asyncFindGameObjectById("xxx").then(go=>{
    let model = go as Model
    // getMaterialInstance()[0]这里我是去获取对象的第一个材质实例
    let mat = model.getMaterialInstance()[0]

    // 设置对象的Main_TileX为10
    //Main_TileX是设置纹理坐标X方向平铺率相关的参数名,具体可以参见后边章节的介绍
    mat.setScalarParameterValue("Main_TileX", 10)
})



image.png
修改前


image.png
修改后

材质怎么获取参数?有哪些参数需要我们去设置?




当打开对应的mat文件后,查看里边的内容,ParamName相关即是需要的参数名称
image.png

在开始代码设置材质参数那儿,有的使用的是setVectorParameterValue,有的使用的是setTextureParameterValue
其区别在于查看mat文件里对应参数的ParamType,下边给一个对应的表:
ParamType 为 Vector4 对应 setVectorParameterValue
ParamType 为 Texture 对应 setTextureParameterValue
ParamType 为 float 对应 setScalarParameterValue


列举一些需要设置的参数包括:
Tone_Saturation,饱和度
Main_TileX,主贴图X轴的平铺值
Main_TileZ,主贴图Z轴的平铺值
Main_TileY,主贴图Y轴的平铺值
Decal_Color_Texture,贴花颜色纹理
Detail_Normal_Texture,细节法线纹理
Main_Color_Texture,颜色纹理(主纹理)
Main_MRAE_Texture,MRAE纹理
Main_Normal_Texture,法线纹理
Main_Anisotropy_Texture,各项异性纹理
BreathingLamp_Color,呼吸灯颜色
Main_Color_Texture,漫反射颜色



替换材质的方法


GameObject.asyncFindGameObjectById("xxx").then(go=>{
    let model = go as Model
    model.setMaterial("93277")
})


更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

热门版块
快速回复 返回顶部 返回列表