Only use cpp version of BuiltinResMgr now. (#10063)

This commit is contained in:
James Chen
2022-02-08 10:45:32 +08:00
committed by GitHub
parent bd4db672cb
commit 17d4957e49
4 changed files with 125 additions and 1 deletions
+1
View File
@@ -69,6 +69,7 @@
"cocos/core/scene-graph/scene-globals.ts": "cocos/core/scene-graph/scene-globals.jsb.ts",
"cocos/core/scene-graph/index.ts": "cocos/core/scene-graph/index.jsb.ts",
"cocos/core/root.ts": "cocos/core/root.jsb.ts",
"cocos/core/builtin/builtin-res-mgr.ts": "cocos/core/builtin/builtin-res-mgr.jsb.ts",
"cocos/core/assets/index.ts": "cocos/core/assets/index.jsb.ts",
"cocos/core/assets/asset.ts": "cocos/core/assets/asset.jsb.ts",
"cocos/core/assets/buffer-asset.ts": "cocos/core/assets/buffer-asset.jsb.ts",
+11 -1
View File
@@ -82,6 +82,7 @@ interface IMaterialInfo {
type MaterialPropertyFull = MaterialProperty | TextureBase | Texture | null;
declare const jsb: any;
const matProto: any = jsb.Material.prototype;
type setProperyCB = (name: string, val: MaterialPropertyFull | MaterialPropertyFull[], passIdx?: number) => void;
@@ -156,7 +157,7 @@ matProto.getProperty = function (name: string, passIdx?: number) {
const val = this._getProperty(name, passIdx);
if (Array.isArray(val)) {
const first = val[0];
const arr = []; // cjh TODO: optimize temporary gc objects being created
const arr: any[] = []; // cjh TODO: optimize temporary gc objects being created
if (first instanceof Vec2) {
for (let i = 0, len = val.length; i < len; ++i) {
const e = val[i];
@@ -236,6 +237,7 @@ matProto.getProperty = function (name: string, passIdx?: number) {
return ret || val;
};
// @ts-ignore
export type Material = jsb.Material;
export const Material = jsb.Material;
legacyCC.Material = Material;
@@ -301,6 +303,7 @@ materialProto._ctor = function () {
// _initializerDefineProperty(_this, "_defines", _descriptor3$7, _assertThisInitialized(_this));
// _initializerDefineProperty(_this, "_states", _descriptor4$6, _assertThisInitialized(_this));
// _initializerDefineProperty(_this, "_props", _descriptor5$4, _assertThisInitialized(_this));
this._isCtorCalled = true;
};
const oldOnLoaded = materialProto.onLoaded;
@@ -317,6 +320,13 @@ Object.defineProperty(materialProto, 'passes', {
enumerable: true,
configurable: true,
get () {
if (!this._isCtorCalled) {
// Builtin materials are created in cpp, the _passes property is not updated when access it in JS.
// So we need to invoke getPasses() to sync _passes property.
this._ctor();
this._passes = this.getPasses();
}
return this._passes;
},
});
+1
View File
@@ -30,6 +30,7 @@ import {
} from '../data/utils/decorator-jsb-utils';
import { legacyCC } from '../global-exports';
import { Filter, PixelFormat, WrapMode } from './asset-enum';
import { EDITOR, TEST } from '../default-constants';
declare const jsb: any;
const renderTextureProto: any = jsb.RenderTexture.prototype;
+112
View File
@@ -0,0 +1,112 @@
/*
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { legacyCC } from '../global-exports';
import { Device } from '../gfx';
import { SpriteFrame } from '../../2d/assets/sprite-frame';
import type { ImageSource } from '../assets/image-asset';
import { AssetManager } from '../asset-manager/asset-manager';
declare const jsb: any;
const Texture2D = jsb.Texture2D;
const ImageAsset = jsb.ImageAsset;
const BuiltinResMgr = jsb.BuiltinResMgr;
const builtinResMgrProto = BuiltinResMgr.prototype;
const oldInitBuiltinRes = builtinResMgrProto.initBuiltinRes;
builtinResMgrProto.initBuiltinRes = function (device: Device): Promise<void> {
this._resources = {};
this._device = device;
const resources = this._resources;
const len = 2;
const numChannels = 4;
const blackValueView = new Uint8Array(len * len * numChannels);
let offset = 0;
for (let i = 0; i < len * len; i++) {
blackValueView[offset] = 0;
blackValueView[offset + 1] = 0;
blackValueView[offset + 2] = 0;
blackValueView[offset + 3] = 255;
}
const blackMemImageSource: ImageSource = {
width: len,
height: len,
_data: blackValueView,
_compressed: false,
format: Texture2D.PixelFormat.RGBA8888,
};
// black texture
const imgAsset = new ImageAsset(blackMemImageSource);
const blackTexture = new Texture2D();
blackTexture._uuid = 'black-texture';
blackTexture.image = imgAsset;
resources[blackTexture._uuid] = blackTexture;
if (legacyCC.SpriteFrame) {
const spriteFrame = new legacyCC.SpriteFrame() as SpriteFrame;
const image = imgAsset;
const texture = new Texture2D();
texture.image = image;
spriteFrame.texture = texture;
spriteFrame._uuid = 'default-spriteframe';
resources[spriteFrame._uuid] = spriteFrame;
}
return Promise.resolve().then(() => {
oldInitBuiltinRes.call(device);
}).then(() => this._preloadAssets());
};
/**
* @internal
*/
builtinResMgrProto._preloadAssets = async function () {
const resources = this._resources;
if (window._CCSettings && window._CCSettings.preloadAssets && window._CCSettings.preloadAssets.length > 0) {
const preloadedAssets = window._CCSettings.preloadAssets as string[];
return new Promise<void>((resolve, reject) => (legacyCC.assetManager as AssetManager).loadAny(preloadedAssets, { __outputAsArray__: true }, (err, assets) => {
if (err) {
reject(err);
} else {
assets.forEach((asset) => resources[asset._uuid] = asset);
resolve();
}
}));
}
}
builtinResMgrProto.get = function (uuid: string) {
const res = this._resources[uuid];
return res || this.getAsset(uuid);
};
const builtinResMgr = legacyCC.builtinResMgr = BuiltinResMgr.getInstance();
export { builtinResMgr };