我想在我的游戏引擎中添加一些多材质,我有一个特殊的特性Material: Any
,它可以创建单独的管道。我有收集材料类型(HashMap<TypeId,>)和收集材料对象Vec
pub fn create_material(
&mut self,
material: Arc<(dyn Material + Send + Sync)>,
) -> MaterialHandle
{
let index = self.materials.len();
self.materials.push(material);
return MaterialHandle::new(index);
}
pub fn bind_material<M: Material + Sync + Send>(&mut self){
self.pipelines.insert(TypeId::of::<M>(), M::pipeline(&self));
}
当我处理材料时,我需要绑定一个管道,得到(ECS)所有匹配类型的材料。但是管道的材质和得到的材质的类型id是不同的(即使我只有一个材质类型和实例)
for material_type in self.pipelines.keys() {
let pipeline = self.pipelines.get(&material_type).unwrap();
bind_pipeline(pipeline);
for (_, (mesh, handle, transform)) in &mut world.query::<(
&Mesh, &MaterialHandle, &Transform,
)>(){
let found_material = self.materials.get(handle.get()).unwrap();
println!("Parsed material: {:?}, Object material: {:?}", *material_type, found_material.type_id()); // TypeIds are different
}
}
我认为这是因为我试图比较泛型类型和trait对象的TypeId,这是不正确的。我该怎么办呢?
很抱歉放弃这个问题。事实证明,我试图获得Arc wrapper的TypeId,但不是材料本身。
println!("Parsed material: {:?}, Object material: {:?}", *material_type, (**found_material).type_id());
可以了(https://github.com/konceptosociala/despero/blob/main/src/render/renderer.rs#LL294C15-L294C15)