I have an entity with two meshInstances. I create a new material and assign a texture to that material and apply that material to both meshinstances in the same frame. This fails sometimes, not every time, causing the FIRST of the two meshinstances to have no material / the default material WITHOUT the texture.
If I assign the textureasset to each mesinstance in SEPARATE FRAMES, e.g. I wait any amount of time before assigning the second one, this issue is solved and that is my current workaround.
Question: Why would creating a new material, assigning a textureasset, then applying that material to one meshinstance cause this issue, when waiting and applying them in two separate frames resolves it?
I recorded a 5 minute thorough walkthrough to illustrate this issue: https://youtu.be/W_iQCoJwXpo
Here is my code:
function ApplyTextureAssetToMeshInstance(options){
const {meshInstance, textureAsset} = options;
const material = new pc.StandardMaterial();
material.diffuseMap = textureAsset.resource;
material.opacityMap = textureAsset.resource; // Set opacity map to the same texture
material.blendType = pc.BLEND_NORMAL; //pc.BLEND_ADDITIVE_ALPHA;// BLEND_NORMAL;
meshInstance.material = material;
material.update();
return material;
}
Here is me calling the function.
// FAILS: causes meshInstances[0] to have a default material
ApplyTextureAssetToMeshInstance({
meshInstance:hoop.children[0].render.meshInstances[0],
textureAsset:assets.textures.hoop
});
ApplyTextureAssetToMeshInstance({
meshInstance:hoop.children[0].render.meshInstances[1],
textureAsset:assets.textures.hoop
});
and
// WORKS:
ApplyTextureAssetToMeshInstance({
meshInstance:hoop.children[0].render.meshInstances[0],
textureAsset:assets.textures.hoop
});
setTimeout(function(){ ApplyTextureAssetToMeshInstance({
meshInstance:hoop.children[0].render.meshInstances[1],
textureAsset:assets.textures.hoop
});}, 100);
Thanks for any insight!