合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
CG Kit提供的动态材质功能支持在shader里除已有的uniform GlobalUniform和uniform ModelUniform,可自定义uniform buffer,并能够在渲染过程中动态更新自定义uniform buffer的值,实现材质的颜色和效果动态变化。
layout(set=0, binding=10) uniform ColorInfo { vec2 diffvec2; vec3 diffvec3; vec2 diffvec4; vec2 diffvec5; vec4 color; } colorInfo;
CG Kit仅支持shader中uniform结构的变量类型:int、float、vec2、vec3、vec4、mat4。
"resource": {
"texture": [
{
"uri": "cubemaps/02/cubemap_02_nomip.ktx",
"name": "envTexture",
"samplerIndex": 0
}
],
"sampler": [{ }],
"constants": [
{
"name": "ColorInfo",
"type": "UNIFORM",
"value": [
{
"name": "diffvec2",
"type": "vec2",
"value": [
1,
0
]
},
{
"name": "diffvec3",
"type": "vec3",
"value": [
1,
0,
1
]
},
{
"name": "diffvec4",
"type": "vec2",
"value": [
1,
0
]
},
{
"name": "diffvec5",
"type": "vec2",
"value": [
1,
0
]
},
{
"name": "color",
"type": "color",
"value": [
255,
0,
0,
255
]
}
]
}
],
"shader": [
{
"uri": "shaders/color.frag",
"type": "SHADER_STAGE_TYPE_FRAGMENT"
},
{
"uri": "shaders/static.vert",
"type": "SHADER_STAGE_TYPE_VERTEX"
}
]
}
CG Kit仅支持cgmat配置文件中uniform结构的变量类型:uint、float、vec2、vec3、vec4、mat4、color(即Color类,对应shader里的vec4)。
MeshRenderer* meshRenderer = m_cubeObject->GetComponent<MeshRenderer>();
for (auto materialInstance : meshRenderer->GetMaterialInstances()) {
const String unionName = "ColorInfo";
const String varName = "color";
uint32_t index = materialInstance->GetConstantValueIndex(unionName, varName);
switch (m_colorFlag) {
case 0: {
// Red
materialInstance->SetConstantValue(index, Color::RED);
break;
}
case 1: {
// Green
materialInstance->SetConstantValue(index, Color::GREEN);
break;
}
case 2: {
// Blue
materialInstance->SetConstantValue(index, Color::BLUE);
break;
}
default:
break;
}