划重点:
1)和其他引擎类似,material是实例化出来的,作用域只针对当前mesh节点;sharedMaterial是共享材质,作用于所有关联的mesh节点。
2)只要通过接口获取过material,之后无论做什么操作都不会生成新的材质实例。(官方说有BUG)
3)替换mesh的material会同时替换其sharedMaterial。
测试脚本如下,做了三次不同的测试:
@property(ModelComponent) model1 : ModelComponent = null;
@property(ModelComponent) model2 : ModelComponent = null;
@property(ModelComponent) model3 : ModelComponent = null;
start ()
{
this.test1();
this.test2();
this.test3();
}
/** 方块1
* 测试结果:修改material属性不会影响其他使用该材质的mesh
* 输出结果:=model1 sharedMaterail 1==1282510404
=model1 materail 1==2452748122
=model1 materail 2==2452748122
=model1 materail 3==2452748122
=model1 sharedMaterail 2==1282510404
方块1颜色:红色方块
分析:3次打印的material的hashcode都是一样的,sharedMaterail前后两次也是一样的
*/
test1()
{
let shared : Material = this.model1.sharedMaterial;
console.log("=model1 sharedMaterail 1==" + shared.hash);
let mat : Material = this.model1.material;
console.log("=model1 materail 1==" + mat.hash);
let mat2 : Material = this.model1.material;
console.log("=model1 materail 2==" + mat2.hash);
//修改材质球属性,不会影响其他使用该材质的物体
this.model1.material.setProperty("mainColor", Color.RED);
let mat3 : Material = this.model1.material;
console.log("=model1 materail 3==" + mat3.hash);
let shared2 : Material = this.model1.sharedMaterial;
console.log("=model1 sharedMaterail 2==" + shared2.hash);
}
/**方块2
* 测试结果:替换material后,material对象只是属性发生改变,对象没变;sharedMaterial对象变为新替换的材质对象
* 输出结果:=model2 sharedMaterail 1==1282510404
=model2 materail 1==2452748122
=model2 materail 2==2452748122
=model2 materail new==2494204105
=model2 materail 3==2452748122
=model2 sharedMaterail 2==2494204105
方块2颜色:蓝色方块
分析:3次获取material对象hashcode都没变,2次获取sharedMaterial对象hashcode变了
*/
test2()
{
let shared : Material = this.model2.sharedMaterial;
console.log("=model2 sharedMaterail 1==" + shared.hash);
let mat : Material = this.model2.material;
console.log("=model2 materail 1==" + mat.hash);
let mat2 : Material = this.model2.material;
console.log("=model2 materail 2==" + mat2.hash);
//替换不同材质球后,材质球变为非共享状态
let newPmtl = new Material();
newPmtl.copy(mat2); //??这种操作有问题??
newPmtl.setProperty("mainColor", Color.BLUE);
this.model2.material = newPmtl;
console.log("=model2 materail new==" + newPmtl.hash);
let mat3 : Material = this.model2.material;
console.log("=model2 materail 3==" + mat3.hash);
let shared2 : Material = this.model2.sharedMaterial;
console.log("=model2 sharedMaterail 2==" + shared2.hash);
}
/**方块3
* 测试结果:调用过material接口后,再修改sharedMaterial属性不会对该mesh产生影响
* 输出结果:=model3 sharedMaterail 1==1282510404
=model3 materail 1==2452748122
=model3 materail 3==2452748122
=model3 sharedMaterail 2==1282510404
方块3颜色:白色方块
*/
test3()
{
let shared : Material = this.model3.sharedMaterial;
console.log("=model3 sharedMaterail 1==" + shared.hash);
let mat : Material = this.model3.material;
console.log("=model3 materail 1==" + mat.hash);
//调用过material或者getMaterial(0)接口后,材质球变为非共享模式,
//修改shareMaterial不会对该模型产生影响
this.model3.sharedMaterial.setProperty("mainColor", Color.GREEN);
let mat3 : Material = this.model3.material;
console.log("=model3 materail 3==" + mat3.hash);
let shared2 : Material = this.model3.sharedMaterial;
console.log("=model3 sharedMaterail 2==" + shared2.hash);
}
测试效果图:
工程超链接