Creator v2.4.3
我的需求很简单,加载一个 Material,然后作用到两个 sprite,然后设置材质的某个属性 (setDefine),但是不希望两个 sprite 一起变化。
我翻到论坛有人提到过:
https://docs.cocos.com/creator/manual/zh/render/material.html?q=materialvariant
这里有写到共享材质和变体材质 MaterialVariant 的内容,但是我简单的试了下, 怎么写都不对。下面是我的代码。
显示加载材质
cc.loader.loadRes("testMaterial", cc.Material, function(err, data) {
this.loadedMaterial = data;
});
材质很简单:
void main () {
#if DEFINE_TEST
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
#else
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
#endif
}
然后写了个按钮,实际应用
this.materialVariantA = cc.MaterialVariant.create(this.loadedMaterial, this.spriteA.getComponent(cc.Sprite));
this.spriteA.getComponent(cc.Sprite).setMaterial(0, this.materialVariantA);
this.materialVariantB = cc.MaterialVariant.create(this.loadedMaterial, this.spriteB.getComponent(cc.Sprite));
this.spriteB.getComponent(cc.Sprite).setMaterial(0, this.materialVariantB);
这里其实是了很多种写法,按钮点击以后,材质都能生效,但是接下来就不对了。
再写一个按钮
this.materialVariantA.define("DEFINE_TEST", true);
这里点击按钮以后,两个 sprite 还是一起变了颜色。
想请教一下各位大佬,这个想要材质单独变化,究竟应该怎么写猜才对呢?
