Skip to content

Commit

Permalink
add asset place holder and remove deferred loading (cocos#8473)
Browse files Browse the repository at this point in the history
* remove deferred loading and add place holder

* resolve circle referenced

* support most of asset

* refine

* fix bugs

* refine

* should use getClassId

* change name

* change name

* change isPlaceHolder to isDefault
  • Loading branch information
SantyWang authored Apr 19, 2021
1 parent e46a3b0 commit 2b3ab66
Show file tree
Hide file tree
Showing 30 changed files with 284 additions and 171 deletions.
2 changes: 1 addition & 1 deletion cocos/2d/assets/sprite-atlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class SpriteAtlas extends Asset {
const frames = data.spriteFrames;
this.spriteFrames = js.createMap();
for (let i = 0; i < frames.length; i += 2) {
handle.result.push(this.spriteFrames, frames[i], frames[i + 1]);
handle.result.push(this.spriteFrames, frames[i], frames[i + 1], js._getClassId(SpriteFrame));
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion cocos/2d/assets/sprite-frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { ImageAsset, ImageSource } from '../../core/assets/image-asset';
import { Texture2D } from '../../core/assets/texture-2d';
import { errorID } from '../../core/platform/debug';
import { dynamicAtlasManager } from '../utils/dynamic-atlas/atlas-manager';
import { js } from '../../core/utils/js';

const INSET_LEFT = 0;
const INSET_TOP = 1;
Expand Down Expand Up @@ -1213,7 +1214,7 @@ export class SpriteFrame extends Asset {
if (!BUILD) {
// manually load texture via _textureSetter
if (data.texture) {
handle.result.push(this, '_textureSource', data.texture);
handle.result.push(this, '_textureSource', data.texture, js._getClassId(Texture2D));
}
}

Expand Down Expand Up @@ -1291,6 +1292,18 @@ export class SpriteFrame extends Asset {
texture.once('load', this._textureLoaded, this);
}
}

public initDefault (uuid?: string) {
super.initDefault(uuid);
const texture = new Texture2D();
texture.initDefault();
this._refreshTexture(texture);
this._calculateUV();
}

public validate () {
return this._texture && this._rect && this._rect.width !== 0 && this._rect.height !== 0;
}
}

legacyCC.SpriteFrame = SpriteFrame;
14 changes: 8 additions & 6 deletions cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,14 @@ export class Sprite extends Renderable2D {

private _applySpriteSize () {
if (this._spriteFrame) {
if (SizeMode.RAW === this._sizeMode) {
const size = this._spriteFrame.originalSize;
this.node._uiProps.uiTransformComp!.setContentSize(size);
} else if (SizeMode.TRIMMED === this._sizeMode) {
const rect = this._spriteFrame.getRect();
this.node._uiProps.uiTransformComp!.setContentSize(rect.width, rect.height);
if (!this._spriteFrame.isDefault) {
if (SizeMode.RAW === this._sizeMode) {
const size = this._spriteFrame.originalSize;
this.node._uiProps.uiTransformComp!.setContentSize(size);
} else if (SizeMode.TRIMMED === this._sizeMode) {
const rect = this._spriteFrame.getRect();
this.node._uiProps.uiTransformComp!.setContentSize(rect.width, rect.height);
}
}

this._activateMaterial();
Expand Down
15 changes: 15 additions & 0 deletions cocos/3d/assets/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,21 @@ export class Mesh extends Asset {
return vertexBuffer;
});
}

public initDefault (uuid?: string) {
super.initDefault(uuid);
this.reset({
struct: {
vertexBundles: [],
primitives: [],
},
data: globalEmptyMeshBuffer,
});
}

public validate () {
return this.renderingSubMeshes.length > 0 && this.data.byteLength > 0;
}
}
legacyCC.Mesh = Mesh;

Expand Down
4 changes: 4 additions & 0 deletions cocos/3d/assets/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export class Skeleton extends Asset {
(legacyCC.director.root.dataPoolManager as DataPoolManager).releaseSkeleton(this);
return super.destroy();
}

public validate () {
return this.joints.length > 0 && this.bindposes.length > 0;
}
}

legacyCC.Skeleton = Skeleton;
5 changes: 4 additions & 1 deletion cocos/audio/audio-clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export interface AudioMeta {
@ccclass('cc.AudioClip')
export class AudioClip extends Asset {
public static AudioType = AudioType;
public static preventDeferredLoadDependents = true;

@serializable
protected _duration = 0; // we serialize this because it's unavailable at runtime on some platforms
Expand Down Expand Up @@ -102,6 +101,10 @@ export class AudioClip extends Asset {
return this._loadMode;
}

public validate () {
return !!this._meta;
}

public getDuration () { return this._meta ? this._meta.duration : this._duration; }
}

Expand Down
6 changes: 4 additions & 2 deletions cocos/core/animation/animation-clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ export declare namespace AnimationClip {
*/
@ccclass('cc.AnimationClip')
export class AnimationClip extends Asset {
public static preventDeferredLoadDependents = true;

public static WrapMode = AnimationWrapMode;

/**
Expand Down Expand Up @@ -433,6 +431,10 @@ export class AnimationClip extends Asset {
}
}
}

public validate () {
return this.keys.length > 0 && this.curves.length > 0;
}
}

legacyCC.AnimationClip = AnimationClip;
4 changes: 2 additions & 2 deletions cocos/core/asset-manager/asset-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @packageDocumentation
* @module asset-manager
*/
import { BUILD, EDITOR } from 'internal:constants';
import { BUILD, EDITOR, PREVIEW } from 'internal:constants';
import { Asset } from '../assets/asset';
import { legacyCC } from '../global-exports';
import { error } from '../platform/debug';
Expand Down Expand Up @@ -194,7 +194,7 @@ export class AssetManager {
* 是否强制加载资源, 如果为 true ,加载资源将会忽略报错
*
*/
public force = !!EDITOR;
public force = EDITOR || PREVIEW;

/**
* @en
Expand Down
4 changes: 0 additions & 4 deletions cocos/core/asset-manager/depend-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export interface IDependencies {
nativeDep?: Record<string, any>;
deps: string[];
parsedFromExistAsset?: boolean;
preventPreloadNativeObject?: boolean;
preventDeferredLoadDependents?: boolean;
persistDeps?: string[];
}

Expand Down Expand Up @@ -187,8 +185,6 @@ export class DependUtil {
const out: IDependencies = {
deps: [],
parsedFromExistAsset: true,
preventPreloadNativeObject: (asset.constructor as typeof Asset).preventPreloadNativeObject,
preventDeferredLoadDependents: (asset.constructor as typeof Asset).preventDeferredLoadDependents,
};
const deps = asset.__depends__ as IDependProp[];
for (let i = 0, l = deps.length; i < l; i++) {
Expand Down
4 changes: 4 additions & 0 deletions cocos/core/asset-manager/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Asset } from '../assets/asset';
import MissingScript from '../components/missing-script';
import { deserialize, Details } from '../data/deserialize';
import { error } from '../platform/debug';
import { js } from '../utils/js';
import { decodeUuid } from './helper';

const missingClass = EDITOR && EditorExtends.MissingReporter.classInstance;
Expand All @@ -39,6 +40,7 @@ export interface IDependProp {
uuid: string;
owner: any;
prop: string;
type?: Constructor<Asset>;
}

export default function (json: Record<string, any>, options: Record<string, any>): Asset {
Expand Down Expand Up @@ -80,6 +82,7 @@ export default function (json: Record<string, any>, options: Record<string, any>
const uuidList = tdInfo.uuidList! as string[];
const objList = tdInfo.uuidObjList!;
const propList = tdInfo.uuidPropList! as string[];
const typeList = (tdInfo.uuidTypeList || []);
const depends: IDependProp[] = [];

for (let i = 0; i < uuidList.length; i++) {
Expand All @@ -88,6 +91,7 @@ export default function (json: Record<string, any>, options: Record<string, any>
uuid: decodeUuid(dependUuid),
owner: objList[i],
prop: propList[i],
type: js._getClassById(typeList[i]) as Constructor<Asset>,
};
}

Expand Down
4 changes: 2 additions & 2 deletions cocos/core/asset-manager/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @packageDocumentation
* @module asset-manager
*/
import { EDITOR } from 'internal:constants';
import { BUILD, EDITOR } from 'internal:constants';
import { sys } from '../platform/sys';
import { js } from '../utils';
import { callInNextTick } from '../utils/misc';
Expand Down Expand Up @@ -172,7 +172,7 @@ export class Downloader {
* @property maxRetryCount
* @type {Number}
*/
public maxRetryCount = EDITOR ? 0 : 3;
public maxRetryCount = BUILD ? 3 : 0;

public appendTimeStamp = !!EDITOR;

Expand Down
46 changes: 21 additions & 25 deletions cocos/core/asset-manager/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ export default function fetch (task: Task, done: CompleteCallbackNoData) {

const { options, progress } = task;
const depends = [];
const total = progress.total;
options!.__exclude__ = options!.__exclude__ || Object.create(null);
const total = progress.total as number;
const exclude = options!.__exclude__ = options!.__exclude__ || Object.create(null);

task.output = [];

forEach(task.input as RequestItem[], (item, cb) => {
if (!item.isNative && assets.has(item.uuid)) {
const asset = assets.get(item.uuid);
asset!.addRef();
handle(item, task, asset, null, asset!.__asyncLoadAssets__, depends, total);
item.content = asset!.addRef();
task.output.push(item);
if (progress.canInvoke) {
task.dispatch('progress', ++progress.finish, progress.total, item);
}
cb();
return;
}
Expand All @@ -68,11 +71,23 @@ export default function fetch (task: Task, done: CompleteCallbackNoData) {
progress.canInvoke = false;
done(err);
} else {
handle(item, task, null, null, false, depends, total);
task.output.push(item);
if (progress.canInvoke) {
task.dispatch('progress', ++progress.finish, progress.total, item);
}
}
}
} else if (!task.isFinish) {
handle(item, task, null, data, !item.isNative, depends, total);
item.file = data;
task.output.push(item);
if (!item.isNative) {
exclude[item.uuid] = true;
getDepends(item.uuid, data, exclude, depends, item.config!);
progress.total = total + depends.length;
}
if (progress.canInvoke) {
task.dispatch('progress', ++progress.finish, progress.total, item);
}
}
cb();
});
Expand Down Expand Up @@ -115,22 +130,3 @@ function decreaseRef (task: Task) {
}
}
}

function handle (item: RequestItem, task: Task, content: any, file: any, loadDepends: boolean, depends: any[], last: number) {
const exclude = task.options!.__exclude__;
const progress = task.progress;

item.content = content;
item.file = file;
task.output.push(item);

if (loadDepends) {
exclude[item.uuid] = true;
getDepends(item.uuid, file || content, exclude, depends, true, false, item.config!);
progress.total = last + depends.length;
}

if (progress.canInvoke) {
task.dispatch('progress', ++progress.finish, progress.total, item);
}
}
Loading

0 comments on commit 2b3ab66

Please sign in to comment.