Skip to content

Commit

Permalink
Merge pull request #449 from playcanvas/feature/load-assets-on-enable
Browse files Browse the repository at this point in the history
Load assets on enable
  • Loading branch information
vkalpias committed Nov 30, 2015
2 parents 9c9258a + 5346d1a commit 07c6d12
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 73 deletions.
3 changes: 2 additions & 1 deletion src/audio/audio_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pc.extend(pc, function () {
*/
unpause: function () {
if (this.source || !this.paused) {
throw new Error('Call pause() before unpausing.');
console.warn('Call pause() before unpausing.');
return;
}

this._createSource();
Expand Down
35 changes: 28 additions & 7 deletions src/framework/components/animation/animation_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ pc.extend(pc, function () {
asset.off('remove', self.onAssetRemoved, self);
asset.on('remove', self.onAssetRemoved, self);

asset.ready(onAssetReady);
assets.load(asset);
if (asset.resource) {
onAssetReady(asset);
} else {
asset.once('load', onAssetReady, self);
if (self.enabled && self.entity.enabled) {
assets.load(asset);
}
}
};

for(i = 0; i < l; i++) {
Expand Down Expand Up @@ -174,17 +180,17 @@ pc.extend(pc, function () {
var modelComponent = this.entity.model;
if (modelComponent) {
var m = modelComponent.model;
if (m) {
if (m && m !== data.model) {
this.entity.animation.setModel(m);
}
}

for (var animName in data.animations) {
// Set the first loaded animation as the current
if (data.activate && data.enabled && this.entity.enabled && !this.system._inTools) {
if (! data.currAnim && data.activate && data.enabled && this.entity.enabled && !this.system._inTools) {
for (var animName in data.animations) {
// Set the first loaded animation as the current
this.play(animName, 0);
break;
}
break;
}
},

Expand Down Expand Up @@ -230,6 +236,21 @@ pc.extend(pc, function () {

onEnable: function () {
AnimationComponent._super.onEnable.call(this);

// load assets if they're not loaded
var assets = this.data.assets;
var registry = this.system.app.assets;
if (assets) {
for (var i = 0, len = assets.length; i < len; i++) {
var asset = assets[i];
if (! (asset instanceof pc.Asset))
asset = registry.get(asset);

if (asset && !asset.resource)
registry.load(asset);
}
}

if ( this.data.activate &&
!this.data.currAnim &&
!this.system._inTools) {
Expand Down
2 changes: 1 addition & 1 deletion src/framework/components/animation/animation_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pc.extend(pc, function () {

pc.extend(AnimationComponentSystem.prototype, {
initializeComponentData: function (component, data, properties) {
properties = ['activate', 'loop', 'speed', 'assets', 'enabled'];
properties = ['activate', 'enabled', 'loop', 'speed', 'assets'];
AnimationComponentSystem._super.initializeComponentData.call(this, component, data, properties);
},

Expand Down
19 changes: 18 additions & 1 deletion src/framework/components/audiosource/audiosource_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ pc.extend(pc, function () {

onEnable: function () {
AudioSourceComponent._super.onEnable.call(this);

// load assets that haven't been loaded yet
var assets = this.data.assets;
if (assets) {
var registry = this.system.app.assets;

for (var i = 0, len = assets.length; i < len; i++) {
var asset = assets[i];
if (! (asset instanceof pc.Asset))
asset = registry.get(asset);

if (asset && !asset.resource) {
registry.load(asset);
}
}
}

if (this.system.initialized) {
if (this.data.activate && !this.channel) {
this.play(this.currentSource);
Expand Down Expand Up @@ -293,7 +310,7 @@ pc.extend(pc, function () {
}
});

if (! asset.resource)
if (! asset.resource && self.enabled && self.entity.enabled)
this.system.app.assets.load(asset);
} else {
// don't wait for assets that aren't in the registry
Expand Down
111 changes: 85 additions & 26 deletions src/framework/components/model/model_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ pc.extend(pc, function () {
});

this._assetOld = 0;

this._materialEvents = null;
this._dirtyModelAsset = false;
this._dirtyMaterialAsset = false;
};
ModelComponent = pc.inherits(ModelComponent, pc.Component);

Expand Down Expand Up @@ -70,6 +71,8 @@ pc.extend(pc, function () {
var assets = this.system.app.assets;
var asset = id !== null ? assets.get(id) : null;

this._dirtyModelAsset = true;

this._onModelAsset(asset || null);

if (! asset && id !== null)
Expand Down Expand Up @@ -101,10 +104,14 @@ pc.extend(pc, function () {
asset.on('remove', this._onAssetRemove, this);

if (asset.resource) {
this._dirtyModelAsset = false;
this._onModelLoaded(asset.resource.clone());
} else {
} else if (this.enabled && this.entity.enabled) {
this._dirtyModelAsset = false;
assets.load(asset);
}
} else {
this._dirtyModelAsset = false;
}
},

Expand Down Expand Up @@ -269,12 +276,16 @@ pc.extend(pc, function () {

if (asset.resource) {
this.material = asset.resource;
} else {
this._dirtyMaterialAsset = false;
} else if (this.enabled && this.entity.enabled) {
this._dirtyMaterialAsset = false;
assets.load(asset);
}
},

setMaterialAsset: function (value) {
this._dirtyMaterialAsset = true;

// if the type of the value is not a number assume it is an pc.Asset
var id = typeof value === 'number' || !value ? value : value.id;

Expand Down Expand Up @@ -303,6 +314,7 @@ pc.extend(pc, function () {
assets.once('add:' + id, this._onMaterialAsset, this);
} else if (id === null) {
self.material = pc.ModelHandler.DEFAULT_MATERIAL;
self._dirtyMaterialAsset = false;
}

var valueOld = this.data.materialAsset;
Expand Down Expand Up @@ -391,23 +403,42 @@ pc.extend(pc, function () {
this._materialEvents = null;
},

_loadAndSetMeshInstanceMaterial: function (idOrPath, meshInstance, index) {
var self = this;
var asset;
var assets = this.system.app.assets;

_getAssetByIdOrPath: function (idOrPath) {
var asset = null;
var isPath = isNaN(parseInt(idOrPath, 10));

// get asset by id or url
if (!isPath) {
asset = assets.get(idOrPath);
} else if (self.asset) {
var url = self._getMaterialAssetUrl(idOrPath);
if (!url) return;

asset = assets.getByUrl(url);
asset = this.system.app.assets.get(idOrPath);
} else if (this.asset) {
var url = this._getMaterialAssetUrl(idOrPath);
if (url)
asset = this.system.app.assets.getByUrl(url);
}

return asset;
},

_getMaterialAssetUrl: function (path) {
if (!this.asset) return null;

var modelAsset = this.system.app.assets.get(this.asset);
if (!modelAsset) return null;

var fileUrl = modelAsset.getFileUrl();
var dirUrl = pc.path.getDirectory(fileUrl);
return pc.path.join(dirUrl, path);
},

_loadAndSetMeshInstanceMaterial: function (idOrPath, meshInstance, index) {
var self = this;
var assets = this.system.app.assets;

// get asset by id or url
var asset = this._getAssetByIdOrPath(idOrPath);
if (! asset)
return;

var handleMaterial = function (asset) {
if (asset.resource) {
meshInstance.material = asset.resource;
Expand All @@ -424,29 +455,21 @@ pc.extend(pc, function () {
});
});

assets.load(asset);
if (self.enabled && self.entity.enabled)
assets.load(asset);
}
};

if (asset) {
handleMaterial(asset);
} else {
meshInstance.material = pc.ModelHandler.DEFAULT_MATERIAL;

var isPath = isNaN(parseInt(idOrPath, 10));
self._setMaterialEvent(index, isPath ? 'add:url' : 'add', idOrPath, handleMaterial);
}
},

_getMaterialAssetUrl: function (path) {
if (!this.asset) return null;

var modelAsset = this.system.app.assets.get(this.asset);
if (!modelAsset) return null;

var fileUrl = modelAsset.getFileUrl();
var dirUrl = pc.path.getDirectory(fileUrl);
return pc.path.join(dirUrl, path);
},

onSetReceiveShadows: function (name, oldValue, newValue) {
if (newValue !== undefined) {
var componentData = this.data;
Expand All @@ -463,11 +486,47 @@ pc.extend(pc, function () {
ModelComponent._super.onEnable.call(this);

var model = this.data.model;
var isAsset = this.data.type === 'asset';

if (model) {
var inScene = this.system.app.scene.containsModel(model);
if (!inScene) {
this.system.app.scene.addModel(model);
}
} else if (isAsset && this._dirtyModelAsset) {
var asset = this.data.asset;
if (! asset)
return;

asset = this.system.app.assets.get(asset);
if (asset)
this._onModelAsset(asset);
}

// load materialAsset if necessary
if (this._dirtyMaterialAsset) {
var materialAsset = this.data.materialAsset;
if (materialAsset) {
materialAsset = this.system.app.assets.get(materialAsset);
if (materialAsset && !materialAsset.resource) {
this._onMaterialAsset(materialAsset);
}
}
}

// load mapping materials if necessary
if (isAsset) {
var mapping = this.data.mapping;
if (mapping) {
for (var index in mapping) {
if (mapping[index]) {
var asset = this._getAssetByIdOrPath(mapping[index]);
if (asset && !asset.resource) {
this.system.app.assets.load(asset);
}
}
}
}
}
},

Expand Down
2 changes: 1 addition & 1 deletion src/framework/components/model/model_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pc.extend(pc, function () {
data.material = this.defaultMaterial;

// order matters here
properties = ['material', 'materialAsset', 'asset', 'castShadows', 'receiveShadows', 'type', 'enabled', 'mapping'];
properties = ['enabled', 'material', 'materialAsset', 'asset', 'castShadows', 'receiveShadows', 'type', 'mapping'];

ModelComponentSystem._super.initializeComponentData.call(this, component, data, properties);
},
Expand Down
Loading

0 comments on commit 07c6d12

Please sign in to comment.