I’m wondering if there is functionality inside PlayCanvas that let’s you query for a specific part on a model. I know how to query for a GraphNode and set rotation etc, but from a GraphNode I can’t seem to get to the meshInstance to replace/clone a specific material.
I wrote this helper method to iterate through the mesh of a given entity, but was wondering if a more efficient way is available.
findPartByName: function(partName){
// Find the given part and if found, return it.
for(var i=0; i<this.entity.model.model.meshInstances.length; i++){
var modelPart = this.entity.model.model.meshInstances[i];
if( modelPart && modelPart.node.name === partName ){
return modelPart;
}
}
return null;
}
That is the way I’d do it. Except maybe slightly more optimized:
findPartByName: function (partName) {
var meshInstances = this.entity.model.model.meshInstances;
var numMeshInstances = meshInstances.length;
// Find the given part and if found, return it.
for (var i = 0; i < numMeshInstances; i++) {
var meshInstance = meshInstances[i];
if (meshInstance.node.name === partName) {
return meshInstance;
}
}
return null;
}
Have you got any tips on integrating nicely this so I don’t have to repeat this code a lot in individual script files? I tried extending the pc.Entity object (as that has the findXXX methods) but I don’t seem to get that to work.
Never mind! After peeking at the sourcecode on Github I found a way to extend the entity object like this:
pc.extend(pc.Entity.prototype, {
findByPartName: function (partName) {
if(!this.model || !this.model.model || !this.model.model.meshInstances) {
return null;
}
var meshInstances = this.model.model.meshInstances;
var numMeshInstances = meshInstances.length;
// Find the given part and if found, return it.
for (var i = 0; i < numMeshInstances; i++) {
var meshInstance = meshInstances[i];
if (meshInstance.node.name === partName) {
return meshInstance;
}
}
return null;
}
});