If I want to change color on the meshinstances within an imported glTF-file, I get issues relating naming from within the blender-file.
The imported file at server-side, is logged as “Untitled”?
I am able to change color on the entire object, but - as such - not the individual parts.
How to access? (as one can see I am trying with a children-approach):
var LoadGltfExternal = pc.createScript('loadGltfExternal');
LoadGltfExternal.attributes.add('baseUrl', {type: 'string'});
LoadGltfExternal.attributes.add('gltfFilename', {type: 'string'});
// initialize code called once per entity
LoadGltfExternal.prototype.initialize = function() {
var app = this.app;
var self = this; self.fr =0;
app.assets.loadFromUrl(self.baseUrl + self.gltfFilename, 'json', function (err, asset) {
var gltf = JSON.parse(asset.resource);
loadGltf(gltf, app.graphicsDevice, function (err, res) {
// Wrap the model as an asset and add to the asset registry
var asset = new pc.Asset('gltf', 'model', {
url: ''
});
asset.resource = res.model;
asset.loaded = true;
app.assets.add(asset);
// Add the loaded scene to the hierarchy
self.entity.addComponent('model', {
asset: asset
});
}, {
basePath: self.baseUrl
});
});
};
// update code called every frame
LoadGltfExternal.prototype.update = function(dt) {
var app = this.app;
var self = this;
self.fr++;
if(self.fr>100 && self.fr<110){
var testMat = self.app.root.findByName('TestMat'); console.log('TestMat @ ModelLoad : ',testMat.name);
var meshInstance = self.app.root.findByName('ModelLoad'); console.log('meshInstance children dmt: ',meshInstance.children[0].name);
var color = meshInstance.model.meshInstances[0].material.diffuse.clone();
// a parallel try:
// console.log('meshInstance.model.meshInstances[1] og [2] : '+ meshInstance.model.meshInstances[0].name + " :: "+ meshInstance.model.meshInstances[1].name );
material = meshInstance.model.meshInstances[0].material;
material.diffuse.set(1, 0, 0);
material.update();
console.log('color update: ', color.r, color.g, color.b);
var color0 = meshInstance.model.meshInstances[1].material.diffuse.clone();
material0 = meshInstance.model.meshInstances[1].material;
material0.diffuse.set(0, 1, 0);
material0.update();
console.log('color update0: ', color0.r, color0.g, color0.b);
}
};
Can’t really share the project here, as it doesn’t make sense localhost/playcanvas-host (due to CORS for the glTF-file).
The naming of the parented objects in blender:
Heirarchy as follows from below:
- Sp is child of Bn (blender)
- Server-object is called “PyraMid_SB3.gltf” (the external loaded)
- Editor-object is top by the name of “ModelLoad” (having the script attached):
Here is the output (“Untitled”):