初始化

This commit is contained in:
SmallMain
2022-06-25 00:23:03 +08:00
commit ef0589e8e5
2264 changed files with 617829 additions and 0 deletions

View File

@@ -0,0 +1,323 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
var CCObject = require('../platform/CCObject');
/**
* !#en
* Base class for handling assets used in Creator.<br/>
*
* You may want to override:<br/>
* - createNode<br/>
* - getset functions of _nativeAsset<br/>
* - cc.Object._serialize<br/>
* - cc.Object._deserialize<br/>
* !#zh
* Creator 中的资源基类。<br/>
*
* 您可能需要重写:<br/>
* - createNode <br/>
* - _nativeAsset 的 getset 方法<br/>
* - cc.Object._serialize<br/>
* - cc.Object._deserialize<br/>
*
* @class Asset
* @extends Object
*/
cc.Asset = cc.Class({
name: 'cc.Asset', extends: CCObject,
ctor () {
/**
* @property {String} _uuid
* @private
*/
// enumerable is false by default, to avoid uuid being assigned to empty string during destroy
Object.defineProperty(this, '_uuid', {
value: '',
writable: true,
});
/**
* !#en
* Whether the asset is loaded or not.
* !#zh
* 该资源是否已经成功加载。
*
* @property loaded
* @type {Boolean}
*/
this.loaded = true;
this._nativeUrl = '';
this._ref = 0;
},
properties: {
/**
* !#en
* Returns the url of this asset's native object, if none it will returns an empty string.
* !#zh
* 返回该资源对应的目标平台资源的 URL如果没有将返回一个空字符串。
* @property nativeUrl
* @type {String}
* @readOnly
*/
nativeUrl: {
get: function () {
if (!this._nativeUrl) {
if (this._native) {
var name = this._native;
if (name.charCodeAt(0) === 47) { // '/'
// remove library tag
// not imported in library, just created on-the-fly
return name.slice(1);
}
if (name.charCodeAt(0) === 46) { // '.'
// imported in dir where json exist
this._nativeUrl = cc.assetManager.utils.getUrlWithUuid(this._uuid, {nativeExt: name, isNative: true });
}
else {
// imported in an independent dir
this._nativeUrl = cc.assetManager.utils.getUrlWithUuid(this._uuid, {__nativeName__: name, nativeExt: cc.path.extname(name), isNative: true});
}
}
}
return this._nativeUrl;
},
visible: false
},
/**
* !#en
* The number of reference
*
* !#zh
* 引用的数量
*
* @property refCount
* @type {Number}
*/
refCount: {
get () {
return this._ref;
}
},
/**
* !#en
* Serializable url for native asset.
* !#zh
* 保存原生资源的 URL。
* @property {String} _native
* @default undefined
* @private
*/
_native: "",
/**
* !#en
* The underlying native asset of this asset if one is available.
* This property can be used to access additional details or functionality releated to the asset.
* This property will be initialized by the loader if `_native` is available.
* !#zh
* 此资源依赖的底层原生资源(如果有的话)。
* 此属性可用于访问与资源相关的其他详细信息或功能。
* 如果 `_native` 可用,则此属性将由加载器初始化。
* @property {Object} _nativeAsset
* @default null
* @private
*/
_nativeAsset: {
get () {
return this._$nativeAsset;
},
set (obj) {
this._$nativeAsset = obj;
}
},
_nativeDep: {
get () {
if (this._native) {
return {__isNative__: true, uuid: this._uuid, ext: this._native};
}
}
}
},
statics: {
/**
* !#en
* Provide this method at the request of AssetDB.
* !#zh
* 应 AssetDB 要求提供这个方法。
*
* @method deserialize
* @param {String} data
* @return {Asset}
* @static
* @private
*/
deserialize: CC_EDITOR && function (data) {
return cc.deserialize(data);
},
/**
* !#en Indicates whether its dependent raw assets can support deferred load if the owner scene (or prefab) is marked as `asyncLoadAssets`.
* !#zh 当场景或 Prefab 被标记为 `asyncLoadAssets`,禁止延迟加载该资源所依赖的其它原始资源。
*
* @property {Boolean} preventDeferredLoadDependents
* @default false
* @static
*/
preventDeferredLoadDependents: false,
/**
* !#en Indicates whether its native object should be preloaded from native url.
* !#zh 禁止预加载原生对象。
*
* @property {Boolean} preventPreloadNativeObject
* @default false
* @static
*/
preventPreloadNativeObject: false
},
/**
* !#en
* Returns the asset's url.
* The `Asset` object overrides the `toString()` method of the `Object` object.
* For `Asset` objects, the `toString()` method returns a string representation of the object.
* JavaScript calls the `toString()` method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation.
* !#zh
* 返回资源的 URL。
*
* Asset 对象将会重写 Object 对象的 `toString()` 方法。
* 对于 Asset 对象,`toString()` 方法返回该对象的字符串表示形式。
* 当资源要表示为文本值时或在字符串连接时引用时JavaScript 会自动调用 `toString()` 方法。
* @method toString
* @return {String}
*/
toString () {
return this.nativeUrl;
},
/**
* !#en
* Provide this method at the request of AssetDB.
* !#zh
* 应 AssetDB 要求提供这个方法。
*
* @method serialize
* @return {String}
* @private
*/
serialize: CC_EDITOR && function () {
return Editor.serialize(this);
},
/**
* !#en
* Create a new node using this asset in the scene.<br/>
* If this type of asset dont have its corresponding node type, this method should be null.
* !#zh
* 使用该资源在场景中创建一个新节点。<br/>
* 如果这类资源没有相应的节点类型,该方法应该是空的。
*
* @method createNode
* @param {Function} callback
* @param {String} callback.error - null or the error info
* @param {Object} callback.node - the created node or null
*/
createNode: null,
/**
* !#en
* Set native file name for this asset.
* !#zh
* 为此资源设置原生文件名。
*
* @seealso nativeUrl
*
* @method _setRawAsset
* @param {String} filename
* @param {Boolean} [inLibrary=true]
* @private
*/
_setRawAsset: function (filename, inLibrary) {
if (inLibrary !== false) {
this._native = filename || undefined;
}
else {
this._native = '/' + filename; // simply use '/' to tag location where is not in the library
}
},
/**
* !#en
* Add references of asset
*
* !#zh
* 增加资源的引用
*
* @method addRef
* @return {Asset} itself
*
* @typescript
* addRef(): cc.Asset
*/
addRef () {
this._ref++;
return this;
},
/**
* !#en
* Reduce references of asset and it will be auto released when refCount equals 0.
*
* !#zh
* 减少资源的引用并尝试进行自动释放。
*
* @method decRef
* @return {Asset} itself
*
* @typescript
* decRef(): cc.Asset
*/
decRef (autoRelease) {
this._ref > 0 && this._ref--;
autoRelease !== false && cc.assetManager._releaseManager.tryRelease(this);
return this;
},
destroy () {
this.loaded = false;
return this._super();
}
});
module.exports = cc.Asset;

View File

@@ -0,0 +1,151 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
const Asset = require('./CCAsset');
const EventTarget = require('../event/event-target');
var LoadMode = cc.Enum({
WEB_AUDIO: 0,
DOM_AUDIO: 1,
});
/**
* !#en Class for audio data handling.
* !#zh 音频资源类。
* @class AudioClip
* @extends Asset
* @uses EventTarget
*/
var AudioClip = cc.Class({
name: 'cc.AudioClip',
extends: Asset,
mixins: [EventTarget],
ctor () {
this._loading = false;
this.loaded = false;
// the web audio buffer or <audio> element
this._audio = null;
},
properties: {
/**
* !#en Get the audio clip duration
* !#zh 获取音频剪辑的长度
* @property duration
* @type {Number}
*/
duration: 0,
loadMode: {
default: LoadMode.WEB_AUDIO,
type: LoadMode
},
_nativeAsset: {
get () {
return this._audio;
},
set (value) {
// HACK: fix load mp3 as audioClip, _nativeAsset is set as audioClip.
// Should load mp3 as audioBuffer indeed.
if (value instanceof cc.AudioClip) {
this._audio = value._nativeAsset;
}
else {
this._audio = value;
}
if (this._audio) {
this.loaded = true;
this.emit('load');
}
},
override: true
},
_nativeDep: {
get () {
return { uuid: this._uuid, audioLoadMode: this.loadMode, ext: cc.path.extname(this._native), __isNative__: true };
},
override: true
}
},
statics: {
LoadMode: LoadMode,
_loadByUrl: function (url, callback) {
var audioClip = cc.assetManager.assets.get(url);
if (!audioClip) {
cc.assetManager.loadRemote(url, function (error, data) {
if (error) {
return callback(error);
}
callback(null, data);
});
}
else {
callback(null, audioClip);
}
}
},
_ensureLoaded (onComplete) {
if (!this.isValid) {
return;
}
if (this.loaded) {
return onComplete && onComplete();
}
else {
if (onComplete) {
this.once('load', onComplete);
}
if (!this._loading) {
this._loading = true;
let self = this;
cc.assetManager.postLoadNative(this, function (err) {
self._loading = false;
});
}
}
},
destroy () {
cc.audioEngine.uncache(this);
this._super();
}
});
/**
* !#zh
* 当该资源加载成功后触发该事件
* !#en
* This event is emitted when the asset is loaded
*
* @event load
*/
cc.AudioClip = AudioClip;
module.exports = AudioClip;

View File

@@ -0,0 +1,149 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
let FontLetterDefinition = function() {
this.u = 0;
this.v = 0;
this.w = 0;
this.h = 0;
this.offsetX = 0;
this.offsetY = 0;
this.textureID = 0;
this.valid = false;
this.xAdvance = 0;
};
let FontAtlas = function (texture) {
this._letterDefinitions = {};
this._texture = texture;
};
FontAtlas.prototype = {
constructor: FontAtlas,
addLetterDefinitions (letter, letterDefinition) {
this._letterDefinitions[letter] = letterDefinition;
},
cloneLetterDefinition () {
let copyLetterDefinitions = {};
for (let key in this._letterDefinitions) {
let value = new FontLetterDefinition();
cc.js.mixin(value, this._letterDefinitions[key]);
copyLetterDefinitions[key] = value;
}
return copyLetterDefinitions;
},
getTexture () {
return this._texture;
},
getLetter (key) {
return this._letterDefinitions[key];
},
getLetterDefinitionForChar (char) {
let key = char.charCodeAt(0);
let hasKey = this._letterDefinitions.hasOwnProperty(key);
let letter;
if (hasKey) {
letter = this._letterDefinitions[key];
} else {
letter = null;
}
return letter;
},
clear () {
this._letterDefinitions = {};
}
};
/**
* @module cc
*/
/**
* !#en Class for BitmapFont handling.
* !#zh 位图字体资源类。
* @class BitmapFont
* @extends Font
*
*/
var BitmapFont = cc.Class({
name: 'cc.BitmapFont',
extends: cc.Font,
properties: {
fntDataStr: {
default: ''
},
spriteFrame: {
default: null,
type: cc.SpriteFrame
},
fontSize: {
default: -1
},
//用来缓存 BitmapFont 解析之后的数据
_fntConfig: null,
_fontDefDictionary: null
},
onLoad () {
let spriteFrame = this.spriteFrame;
if (!this._fontDefDictionary) {
this._fontDefDictionary = new FontAtlas();
if (spriteFrame) {
this._fontDefDictionary._texture = spriteFrame._texture;
}
}
let fntConfig = this._fntConfig;
if (!fntConfig) {
return;
}
let fontDict = fntConfig.fontDefDictionary;
for (let fontDef in fontDict) {
let letter = new FontLetterDefinition();
let rect = fontDict[fontDef].rect;
letter.offsetX = fontDict[fontDef].xOffset;
letter.offsetY = fontDict[fontDef].yOffset;
letter.w = rect.width;
letter.h = rect.height;
letter.u = rect.x;
letter.v = rect.y;
//FIXME: only one texture supported for now
letter.textureID = 0;
letter.valid = true;
letter.xAdvance = fontDict[fontDef].xAdvance;
this._fontDefDictionary.addLetterDefinitions(fontDef, letter);
}
}
});
cc.BitmapFont = BitmapFont;
cc.BitmapFont.FontLetterDefinition = FontLetterDefinition;
cc.BitmapFont.FontAtlas = FontAtlas;
module.exports = BitmapFont;

View File

@@ -0,0 +1,55 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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.
****************************************************************************/
/**
* @class BufferAsset
* @extends Asset
*/
var BufferAsset = cc.Class({
name: 'cc.BufferAsset',
extends: cc.Asset,
ctor () {
this._buffer = null;
},
properties: {
_nativeAsset: {
get () {
return this._buffer;
},
set (bin) {
this._buffer = bin.buffer || bin;
},
override: true
},
buffer () {
return this._buffer;
}
}
});
cc.BufferAsset = module.exports = BufferAsset;

View File

@@ -0,0 +1,38 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#en Class for Font handling.
* !#zh 字体资源类。
* @class Font
* @extends Asset
*/
var Font = cc.Class({
name: 'cc.Font',
extends: cc.Asset
});
cc.Font = module.exports = Font;

View File

@@ -0,0 +1,51 @@
/****************************************************************************
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#en
* Class for JSON file. When the JSON file is loaded, this object is returned.
* The parsed JSON object can be accessed through the `json` attribute in it.<br>
* If you want to get the original JSON text, you should modify the extname to `.txt`
* so that it is loaded as a `TextAsset` instead of a `JsonAsset`.
*
* !#zh
* JSON 资源类。JSON 文件加载后,将会返回该对象。可以通过其中的 `json` 属性访问解析后的 JSON 对象。<br>
* 如果你想要获得 JSON 的原始文本,那么应该修改源文件的后缀为 `.txt`,这样就会加载为一个 `TextAsset` 而不是 `JsonAsset`。
*
* @class JsonAsset
* @extends Asset
*/
var JsonAsset = cc.Class({
name: 'cc.JsonAsset',
extends: cc.Asset,
properties: {
/**
* @property {Object} json - The loaded JSON object.
*/
json: null,
},
});
module.exports = cc.JsonAsset = JsonAsset;

View File

@@ -0,0 +1,56 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* @module cc
*/
/**
* !#en Class for LabelAtlas handling.
* !#zh 艺术数字字体资源类。
* @class LabelAtlas
* @extends BitmapFont
*
*/
var LabelAtlas = cc.Class({
name: 'cc.LabelAtlas',
extends: cc.BitmapFont,
onLoad () {
if (!this.spriteFrame) {
cc.warnID(9100, this.name);
return;
}
if (!this._fntConfig) {
cc.warnID(9101, this.name);
return;
}
this._super();
}
});
cc.LabelAtlas = LabelAtlas;
module.exports = LabelAtlas;

View File

@@ -0,0 +1,209 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#zh
* Prefab 创建实例所用的优化策略,配合 {{#crossLink "Prefab.optimizationPolicy"}}cc.Prefab#optimizationPolicy{{/crossLink}} 使用。
* !#en
* An enumeration used with the {{#crossLink "Prefab.optimizationPolicy"}}cc.Prefab#optimizationPolicy{{/crossLink}}
* to specify how to optimize the instantiate operation.
*
* @enum Prefab.OptimizationPolicy
* @since 1.10.0
*/
var OptimizationPolicy = cc.Enum({
/**
* !#zh
* 根据创建次数自动调整优化策略。初次创建实例时,行为等同 SINGLE_INSTANCE多次创建后将自动采用 MULTI_INSTANCE。
* !#en
* The optimization policy is automatically chosen based on the number of instantiations.
* When you first create an instance, the behavior is the same as SINGLE_INSTANCE. MULTI_INSTANCE will be automatically used after multiple creation.
* @property {Number} AUTO
*/
AUTO: 0,
/**
* !#zh
* 优化单次创建性能。<br>
* 该选项会跳过针对这个 prefab 的代码生成优化操作。当该 prefab 加载后,一般只会创建一个实例时,请选择此项。
* !#en
* Optimize for single instance creation.<br>
* This option skips code generation for this prefab.
* When this prefab will usually create only one instances, please select this option.
* @property {Number} SINGLE_INSTANCE
*/
SINGLE_INSTANCE: 1,
/**
* !#zh
* 优化多次创建性能。<br>
* 该选项会启用针对这个 prefab 的代码生成优化操作。当该 prefab 加载后,一般会创建多个实例时,请选择此项。如果该 prefab 在场景中的节点启用了自动关联,并且在场景中有多份实例,也建议选择此项。
* !#en
* Optimize for creating instances multiple times.<br>
* This option enables code generation for this prefab.
* When this prefab will usually create multiple instances, please select this option.
* It is also recommended to select this option if the prefab instance in the scene has Auto Sync enabled and there are multiple instances in the scene.
* @property {Number} MULTI_INSTANCE
*/
MULTI_INSTANCE: 2,
});
/**
* !#en Class for prefab handling.
* !#zh 预制资源类。
* @class Prefab
* @extends Asset
*/
var Prefab = cc.Class({
name: 'cc.Prefab',
extends: cc.Asset,
ctor () {
/**
* Cache function to optimize instance creaton.
* @property {Function} _createFunction
* @private
*/
this._createFunction = null;
this._instantiatedTimes = 0;
},
properties: {
/**
* @property {Node} data - the main cc.Node in the prefab
*/
data: null,
/**
* !#zh
* 设置实例化这个 prefab 时所用的优化策略。根据使用情况设置为合适的值,能优化该 prefab 实例化所用的时间。
* !#en
* Indicates the optimization policy for instantiating this prefab.
* Set to a suitable value based on usage, can optimize the time it takes to instantiate this prefab.
*
* @property {Prefab.OptimizationPolicy} optimizationPolicy
* @default Prefab.OptimizationPolicy.AUTO
* @since 1.10.0
* @example
* prefab.optimizationPolicy = cc.Prefab.OptimizationPolicy.MULTI_INSTANCE;
*/
optimizationPolicy: OptimizationPolicy.AUTO,
/**
* !#en Indicates the raw assets of this prefab can be load after prefab loaded.
* !#zh 指示该 Prefab 依赖的资源可否在 Prefab 加载后再延迟加载。
* @property {Boolean} asyncLoadAssets
* @default false
*/
asyncLoadAssets: false,
/**
* @property {Boolean} readonly
* @default false
*/
readonly: {
default: false,
editorOnly: true
}
},
statics: {
OptimizationPolicy,
OptimizationPolicyThreshold: 3,
},
createNode: CC_EDITOR && function (cb) {
var node = cc.instantiate(this);
node.name = this.name;
cb(null, node);
},
/**
* Dynamically translation prefab data into minimized code.<br/>
* This method will be called automatically before the first time the prefab being instantiated,
* but you can re-call to refresh the create function once you modified the original prefab data in script.
* @method compileCreateFunction
*/
compileCreateFunction: function () {
var jit = require('../platform/instantiate-jit');
this._createFunction = jit.compile(this.data);
},
// just instantiate, will not initialize the Node, this will be called during Node's initialization.
// @param {Node} [rootToRedirect] - specify an instantiated prefabRoot that all references to prefabRoot in prefab
// will redirect to
_doInstantiate: function (rootToRedirect) {
if (!this.data._prefab) {
// temp guard code
cc.warnID(3700);
}
if (!this._createFunction) {
this.compileCreateFunction();
}
return this._createFunction(rootToRedirect); // this.data._instantiate();
},
_instantiate: function () {
var node, useJit = false;
if (CC_SUPPORT_JIT) {
if (this.optimizationPolicy === OptimizationPolicy.SINGLE_INSTANCE) {
useJit = false;
}
else if (this.optimizationPolicy === OptimizationPolicy.MULTI_INSTANCE) {
useJit = true;
}
else {
// auto
useJit = (this._instantiatedTimes + 1) >= Prefab.OptimizationPolicyThreshold;
}
}
if (useJit) {
// instantiate node
node = this._doInstantiate();
// initialize node
this.data._instantiate(node);
}
else {
// instantiate node
node = this.data._instantiate();
}
++this._instantiatedTimes;
// link prefab in editor
if (CC_EDITOR || CC_TEST) {
var PrefabUtils = Editor.require('scene://utils/prefab');
// This operation is not necessary, but some old prefab asset may not contain complete data.
PrefabUtils.linkPrefab(this, node);
}
return node;
},
destroy () {
this.data && this.data.destroy();
this._super();
},
});
cc.Prefab = module.exports = Prefab;
cc.js.obsolete(cc, 'cc._Prefab', 'Prefab');

View File

@@ -0,0 +1,175 @@
const renderer = require('../renderer');
const Texture2D = require('./CCTexture2D');
import gfx from '../../renderer/gfx';
/**
* !#en The depth buffer and stencil buffer format for RenderTexture.
* !#zh RenderTexture 的深度缓冲以及模板缓冲格式。
* @enum RenderTexture.DepthStencilFormat
*/
let DepthStencilFormat = cc.Enum({
/**
* !#en 24 bit depth buffer and 8 bit stencil buffer
* !#zh 24 位深度缓冲和 8 位模板缓冲
* @property RB_FMT_D24S8
* @readonly
* @type {number}
*/
RB_FMT_D24S8: gfx.RB_FMT_D24S8,
/**
* !#en Only 8 bit stencil buffer
* !#zh 只申请 8 位模板缓冲
* @property RB_FMT_S8
* @readonly
* @type {number}
*/
RB_FMT_S8: gfx.RB_FMT_S8,
/**
* !#en Only 16 bit depth buffer
* !#zh 只申请 16 位深度缓冲
* @property RB_FMT_D16
* @readonly
* @type {number}
*/
RB_FMT_D16: gfx.RB_FMT_D16
})
/**
* Render textures are textures that can be rendered to.
* @class RenderTexture
* @extends Texture2D
*/
let RenderTexture = cc.Class({
name: 'cc.RenderTexture',
extends: Texture2D,
statics: {
DepthStencilFormat
},
ctor () {
this._framebuffer = null;
},
/**
* !#en
* Init the render texture with size.
* !#zh
* 初始化 render texture
* @param {Number} [width]
* @param {Number} [height]
* @param {Number} [depthStencilFormat]
* @method initWithSize
*/
initWithSize (width, height, depthStencilFormat) {
this.width = Math.floor(width || cc.visibleRect.width);
this.height = Math.floor(height || cc.visibleRect.height);
this._resetUnderlyingMipmaps();
let opts = {
colors: [ this._texture ],
};
if (this._depthStencilBuffer) this._depthStencilBuffer.destroy();
let depthStencilBuffer;
if (depthStencilFormat) {
depthStencilBuffer = new gfx.RenderBuffer(renderer.device, depthStencilFormat, width, height);
if (depthStencilFormat === gfx.RB_FMT_D24S8) {
opts.depthStencil = depthStencilBuffer;
}
else if (depthStencilFormat === gfx.RB_FMT_S8) {
opts.stencil = depthStencilBuffer;
}
else if (depthStencilFormat === gfx.RB_FMT_D16) {
opts.depth = depthStencilBuffer;
}
}
this._depthStencilBuffer = depthStencilBuffer;
if (this._framebuffer) this._framebuffer.destroy();
this._framebuffer = new gfx.FrameBuffer(renderer.device, width, height, opts);
this._packable = false;
this.loaded = true;
this.emit("load");
},
updateSize(width, height) {
this.width = Math.floor(width || cc.visibleRect.width);
this.height = Math.floor(height || cc.visibleRect.height);
this._resetUnderlyingMipmaps();
let rbo = this._depthStencilBuffer;
if (rbo) rbo.update(this.width, this.height);
this._framebuffer._width = width;
this._framebuffer._height = height;
},
/**
* !#en Draw a texture to the specified position
* !#zh 将指定的图片渲染到指定的位置上
* @param {Texture2D} texture
* @param {Number} x
* @param {Number} y
*/
drawTextureAt (texture, x, y) {
if (!texture._image || texture._image.width === 0) return;
this._texture.updateSubImage({
x, y,
image: texture._image,
width: texture.width,
height: texture.height,
level: 0,
flipY: false,
premultiplyAlpha: texture._premultiplyAlpha
})
},
/**
* !#en
* Get pixels from render texture, the pixels data stores in a RGBA Uint8Array.
* It will return a new (width * height * 4) length Uint8Array by default。
* You can specify a data to store the pixels to reuse the data,
* you and can specify other params to specify the texture region to read.
* !#zh
* 从 render texture 读取像素数据,数据类型为 RGBA 格式的 Uint8Array 数组。
* 默认每次调用此函数会生成一个大小为 (长 x 高 x 4 的 Uint8Array。
* 你可以通过传入 data 来接收像素数据,也可以通过传参来指定需要读取的区域的像素。
* @method readPixels
* @param {Uint8Array} [data]
* @param {Number} [x]
* @param {Number} [y]
* @param {Number} [w]
* @param {Number} [h]
* @return {Uint8Array}
*/
readPixels (data, x, y, w, h) {
if (!this._framebuffer || !this._texture) return data;
x = x || 0;
y = y || 0;
let width = w || this.width;
let height = h || this.height
data = data || new Uint8Array(width * height * 4);
let gl = cc.game._renderContext;
let oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING);
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer.getHandle());
gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
gl.bindFramebuffer(gl.FRAMEBUFFER, oldFBO);
return data;
},
destroy () {
this._super();
if (this._framebuffer) {
this._framebuffer.destroy();
this._framebuffer = null;
}
}
});
cc.RenderTexture = module.exports = RenderTexture;

View File

@@ -0,0 +1,64 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#en Class for scene handling.
* !#zh 场景资源类。
* @class SceneAsset
* @extends Asset
*
*/
var Scene = cc.Class({
name: 'cc.SceneAsset',
extends: cc.Asset,
properties: {
/**
* @property {Scene} scene
* @default null
*/
scene: null,
/**
* !#en Indicates the raw assets of this scene can be load after scene launched.
* !#zh 指示该场景依赖的资源可否在场景切换后再延迟加载。
* @property {Boolean} asyncLoadAssets
* @default false
*/
asyncLoadAssets: undefined,
//// backup prefab assets in editor
//// {string} assetUuid: {cc.Node} rootInPrefab
//_prefabDatas: {
// default: null,
// editorOnly: true
//}
},
});
cc.SceneAsset = Scene;
module.exports = Scene;

View File

@@ -0,0 +1,69 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#en Class for script handling.
* !#zh Script 资源类。
* @class _Script
* @extends Asset
*
* @private
*/
var Script = cc.Class({
name: 'cc.Script',
extends: cc.Asset,
});
cc._Script = Script;
/**
* !#en Class for JavaScript handling.
* !#zh JavaScript 资源类。
* @class _JavaScript
* @extends Asset
* @private
*
*/
var JavaScript = cc.Class({
name: 'cc.JavaScript',
extends: Script,
});
cc._JavaScript = JavaScript;
/**
* !#en Class for TypeScript handling.
* !#zh TypeScript 资源类。
* @class TypeScript
* @extends Asset
*
*/
var TypeScript = cc.Class({
name: 'cc.TypeScript',
extends: Script,
});
cc._TypeScript = TypeScript;

View File

@@ -0,0 +1,93 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#en Class for sprite atlas handling.
* !#zh 精灵图集资源类。
* @class SpriteAtlas
* @extends Asset
*/
var SpriteAtlas = cc.Class({
name: 'cc.SpriteAtlas',
extends: cc.Asset,
properties: {
_spriteFrames: {
default: {}
},
},
/**
* Returns the texture of the sprite atlas
* @method getTexture
* @returns {Texture2D}
*/
getTexture: function () {
var keys = Object.keys(this._spriteFrames);
if (keys.length > 0) {
var spriteFrame = this._spriteFrames[keys[0]];
return spriteFrame ? spriteFrame.getTexture() : null;
}
else {
return null;
}
},
/**
* Returns the sprite frame correspond to the given key in sprite atlas.
* @method getSpriteFrame
* @param {String} key
* @returns {SpriteFrame}
*/
getSpriteFrame: function (key) {
let sf = this._spriteFrames[key];
if (!sf) {
return null;
}
if (!sf.name) {
sf.name = key;
}
return sf;
},
/**
* Returns the sprite frames in sprite atlas.
* @method getSpriteFrames
* @returns {[SpriteFrame]}
*/
getSpriteFrames: function () {
var frames = [];
var spriteFrames = this._spriteFrames;
for (var key in spriteFrames) {
frames.push(this.getSpriteFrame(key));
}
return frames;
}
});
cc.SpriteAtlas = SpriteAtlas;
module.exports = SpriteAtlas;

View File

@@ -0,0 +1,842 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
const EventTarget = require("../event/event-target");
const INSET_LEFT = 0;
const INSET_TOP = 1;
const INSET_RIGHT = 2;
const INSET_BOTTOM = 3;
let temp_uvs = [{u: 0, v: 0}, {u: 0, v: 0}, {u: 0, v: 0}, {u: 0, v: 0}];
/**
* !#en
* A cc.SpriteFrame has:<br/>
* - texture: A cc.Texture2D that will be used by render components<br/>
* - rectangle: A rectangle of the texture
*
* !#zh
* 一个 SpriteFrame 包含:<br/>
* - 纹理:会被渲染组件使用的 Texture2D 对象。<br/>
* - 矩形:在纹理中的矩形区域。
*
* @class SpriteFrame
* @extends Asset
* @uses EventTarget
* @example
* // load a cc.SpriteFrame with image path (Recommend)
* var self = this;
* var url = "test assets/PurpleMonster";
* cc.resources.load(url, cc.SpriteFrame, null, function (err, spriteFrame) {
* var node = new cc.Node("New Sprite");
* var sprite = node.addComponent(cc.Sprite);
* sprite.spriteFrame = spriteFrame;
* node.parent = self.node
* });
*/
let SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
name: 'cc.SpriteFrame',
extends: require('../assets/CCAsset'),
mixins: [EventTarget],
properties: {
// Use this property to set texture when loading dependency
_textureSetter: {
set: function (texture) {
if (texture) {
if (CC_EDITOR && Editor.isBuilder) {
// just building
this._texture = texture;
return;
}
if (this._texture !== texture) {
this._refreshTexture(texture);
}
}
}
},
/**
* !#en Top border of the sprite
* !#zh sprite 的顶部边框
* @property insetTop
* @type {Number}
* @default 0
*/
insetTop: {
get: function () {
return this._capInsets[INSET_TOP];
},
set: function (value) {
this._capInsets[INSET_TOP] = value;
if (this._texture) {
this._calculateSlicedUV();
}
}
},
/**
* !#en Bottom border of the sprite
* !#zh sprite 的底部边框
* @property insetBottom
* @type {Number}
* @default 0
*/
insetBottom: {
get: function () {
return this._capInsets[INSET_BOTTOM];
},
set: function (value) {
this._capInsets[INSET_BOTTOM] = value;
if (this._texture) {
this._calculateSlicedUV();
}
}
},
/**
* !#en Left border of the sprite
* !#zh sprite 的左边边框
* @property insetLeft
* @type {Number}
* @default 0
*/
insetLeft: {
get: function () {
return this._capInsets[INSET_LEFT];
},
set: function (value) {
this._capInsets[INSET_LEFT] = value;
if (this._texture) {
this._calculateSlicedUV();
}
}
},
/**
* !#en Right border of the sprite
* !#zh sprite 的左边边框
* @property insetRight
* @type {Number}
* @default 0
*/
insetRight: {
get: function () {
return this._capInsets[INSET_RIGHT];
},
set: function (value) {
this._capInsets[INSET_RIGHT] = value;
if (this._texture) {
this._calculateSlicedUV();
}
}
},
},
/**
* !#en
* Constructor of SpriteFrame class.
* !#zh
* SpriteFrame 类的构造函数。
* @method constructor
* @param {String|Texture2D} [filename]
* @param {Rect} [rect]
* @param {Boolean} [rotated] - Whether the frame is rotated in the texture
* @param {Vec2} [offset] - The offset of the frame in the texture
* @param {Size} [originalSize] - The size of the frame in the texture
*/
ctor: function () {
// Init EventTarget data
EventTarget.call(this);
let filename = arguments[0];
let rect = arguments[1];
let rotated = arguments[2];
let offset = arguments[3];
let originalSize = arguments[4];
// the location of the sprite on rendering texture
this._rect = null;
// uv data of frame
this.uv = [];
// texture of frame
this._texture = null;
// store original info before packed to dynamic atlas
this._original = null;
// for trimming
this._offset = null;
// for trimming
this._originalSize = null;
this._rotated = false;
this._flipX = false;
this._flipY = false;
this.vertices = null;
this._capInsets = [0, 0, 0, 0];
this.uvSliced = [];
if (CC_EDITOR) {
// Atlas asset uuid
this._atlasUuid = '';
}
if (filename !== undefined) {
this.setTexture(filename, rect, rotated, offset, originalSize);
} else {
//todo log Error
}
},
/**
* !#en Returns whether the texture have been loaded
* !#zh 返回是否已加载纹理
* @method textureLoaded
* @returns {boolean}
*/
textureLoaded: function () {
return this._texture && this._texture.loaded;
},
onTextureLoaded (callback, target) {
if (this.textureLoaded()) {
callback.call(target);
}
else {
this.once('load', callback, target);
this.ensureLoadTexture();
return false;
}
return true;
},
/**
* !#en Returns whether the sprite frame is rotated in the texture.
* !#zh 获取 SpriteFrame 是否旋转
* @method isRotated
* @return {Boolean}
*/
isRotated: function () {
return this._rotated;
},
/**
* !#en Set whether the sprite frame is rotated in the texture.
* !#zh 设置 SpriteFrame 是否旋转
* @method setRotated
* @param {Boolean} bRotated
*/
setRotated: function (bRotated) {
this._rotated = bRotated;
if (this._texture)
this._calculateUV();
},
/**
* !#en Returns whether the sprite frame is flip x axis in the texture.
* !#zh 获取 SpriteFrame 是否反转 x 轴
* @method isFlipX
* @return {Boolean}
*/
isFlipX: function () {
return this._flipX;
},
/**
* !#en Returns whether the sprite frame is flip y axis in the texture.
* !#zh 获取 SpriteFrame 是否反转 y 轴
* @method isFlipY
* @return {Boolean}
*/
isFlipY: function () {
return this._flipY;
},
/**
* !#en Set whether the sprite frame is flip x axis in the texture.
* !#zh 设置 SpriteFrame 是否翻转 x 轴
* @method setFlipX
* @param {Boolean} flipX
*/
setFlipX: function (flipX) {
this._flipX = flipX;
if (this._texture) {
this._calculateUV();
}
},
/**
* !#en Set whether the sprite frame is flip y axis in the texture.
* !#zh 设置 SpriteFrame 是否翻转 y 轴
* @method setFlipY
* @param {Boolean} flipY
*/
setFlipY: function (flipY) {
this._flipY = flipY;
if (this._texture) {
this._calculateUV();
}
},
/**
* !#en Returns the rect of the sprite frame in the texture.
* !#zh 获取 SpriteFrame 的纹理矩形区域
* @method getRect
* @return {Rect}
*/
getRect: function () {
return cc.rect(this._rect);
},
/**
* !#en Sets the rect of the sprite frame in the texture.
* !#zh 设置 SpriteFrame 的纹理矩形区域
* @method setRect
* @param {Rect} rect
*/
setRect: function (rect) {
this._rect = rect;
if (this._texture)
this._calculateUV();
},
/**
* !#en Returns the original size of the trimmed image.
* !#zh 获取修剪前的原始大小
* @method getOriginalSize
* @return {Size}
*/
getOriginalSize: function () {
return cc.size(this._originalSize);
},
/**
* !#en Sets the original size of the trimmed image.
* !#zh 设置修剪前的原始大小
* @method setOriginalSize
* @param {Size} size
*/
setOriginalSize: function (size) {
if (!this._originalSize) {
this._originalSize = cc.size(size);
} else {
this._originalSize.width = size.width;
this._originalSize.height = size.height;
}
},
/**
* !#en Returns the texture of the frame.
* !#zh 获取使用的纹理实例
* @method getTexture
* @return {Texture2D}
*/
getTexture: function () {
return this._texture;
},
_textureLoadedCallback () {
let self = this;
let texture = this._texture;
if (!texture) {
// clearTexture called while loading texture...
return;
}
let w = texture.width, h = texture.height;
if (self._rect) {
self._checkRect(self._texture);
}
else {
self._rect = cc.rect(0, 0, w, h);
}
if (!self._originalSize) {
self.setOriginalSize(cc.size(w, h));
}
if (!self._offset) {
self.setOffset(cc.v2(0, 0));
}
self._calculateUV();
// dispatch 'load' event of cc.SpriteFrame
self.emit("load");
},
/*
* !#en Sets the texture of the frame.
* !#zh 设置使用的纹理实例。
* @method _refreshTexture
* @param {Texture2D} texture
*/
_refreshTexture: function (texture) {
this._texture = texture;
if (texture.loaded) {
this._textureLoadedCallback();
}
else {
texture.once('load', this._textureLoadedCallback, this);
}
},
/**
* !#en Returns the offset of the frame in the texture.
* !#zh 获取偏移量
* @method getOffset
* @return {Vec2}
*/
getOffset: function () {
return cc.v2(this._offset);
},
/**
* !#en Sets the offset of the frame in the texture.
* !#zh 设置偏移量
* @method setOffset
* @param {Vec2} offsets
*/
setOffset: function (offsets) {
this._offset = cc.v2(offsets);
},
/**
* !#en Clone the sprite frame.
* !#zh 克隆 SpriteFrame
* @method clone
* @return {SpriteFrame}
*/
clone: function() {
return new SpriteFrame(this._texture, this.getRect(), this._rotated, this.getOffset(), this.getOriginalSize());
},
/**
* !#en Set SpriteFrame with Texture, rect, rotated, offset and originalSize.<br/>
* !#zh 通过 Texturerectrotatedoffset 和 originalSize 设置 SpriteFrame。
* @method setTexture
* @param {Texture2D} texture
* @param {Rect} [rect=null]
* @param {Boolean} [rotated=false]
* @param {Vec2} [offset=cc.v2(0,0)]
* @param {Size} [originalSize=rect.size]
* @return {Boolean}
*/
setTexture: function (texture, rect, rotated, offset, originalSize) {
if (arguments.length === 1 && texture === this._texture) return;
if (rect) {
this._rect = rect;
}
else {
this._rect = null;
}
if (offset) {
this.setOffset(offset);
}
else {
this._offset = null;
}
if (originalSize) {
this.setOriginalSize(originalSize);
}
else {
this._originalSize = null;
}
this._rotated = rotated || false;
if (typeof texture === 'string') {
cc.errorID(3401);
return;
}
if (texture instanceof cc.Texture2D) {
this._refreshTexture(texture);
}
return true;
},
/**
* !#en If a loading scene (or prefab) is marked as `asyncLoadAssets`, all the textures of the SpriteFrame which
* associated by user's custom Components in the scene, will not preload automatically.
* These textures will be load when Sprite component is going to render the SpriteFrames.
* You can call this method if you want to load the texture early.
* !#zh 当加载中的场景或 Prefab 被标记为 `asyncLoadAssets` 时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。
* 只有当 Sprite 组件要渲染这些 SpriteFrame 时,才会检查贴图是否加载。如果你希望加载过程提前,你可以手工调用这个方法。
*
* @method ensureLoadTexture
* @example
* if (spriteFrame.textureLoaded()) {
* this._onSpriteFrameLoaded();
* }
* else {
* spriteFrame.once('load', this._onSpriteFrameLoaded, this);
* spriteFrame.ensureLoadTexture();
* }
*/
ensureLoadTexture: function () {
if (this._texture) {
if (!this._texture.loaded) {
// load exists texture
this._refreshTexture(this._texture);
cc.assetManager.postLoadNative(this._texture);
}
}
},
/**
* !#en
* If you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected. Then when you need to render the SpriteFrame, you should call `ensureLoadTexture` manually to reload texture.
* !#zh
* 当你暂时不再使用这个 SpriteFrame 时,可以调用这个方法来保证引用的贴图对象能被 GC。然后当你要渲染 SpriteFrame 时,你需要手动调用 `ensureLoadTexture` 来重新加载贴图。
* @method clearTexture
* @deprecated since 2.1
*/
_checkRect: function (texture) {
let rect = this._rect;
let maxX = rect.x, maxY = rect.y;
if (this._rotated) {
maxX += rect.height;
maxY += rect.width;
}
else {
maxX += rect.width;
maxY += rect.height;
}
if (maxX > texture.width) {
cc.errorID(3300, texture.nativeUrl + '/' + this.name, maxX, texture.width);
}
if (maxY > texture.height) {
cc.errorID(3400, texture.nativeUrl + '/' + this.name, maxY, texture.height);
}
},
_flipXY (uvs) {
if (this._flipX) {
let tempVal = uvs[0];
uvs[0] = uvs[1];
uvs[1] = tempVal;
tempVal = uvs[2];
uvs[2] = uvs[3];
uvs[3] = tempVal;
}
if (this._flipY) {
let tempVal = uvs[0];
uvs[0] = uvs[2];
uvs[2] = tempVal;
tempVal = uvs[1];
uvs[1] = uvs[3];
uvs[3] = tempVal;
}
},
_calculateSlicedUV () {
let rect = this._rect;
let atlasWidth = this._texture.width;
let atlasHeight = this._texture.height;
let leftWidth = this._capInsets[INSET_LEFT];
let rightWidth = this._capInsets[INSET_RIGHT];
let centerWidth = rect.width - leftWidth - rightWidth;
let topHeight = this._capInsets[INSET_TOP];
let bottomHeight = this._capInsets[INSET_BOTTOM];
let centerHeight = rect.height - topHeight - bottomHeight;
let uvSliced = this.uvSliced;
uvSliced.length = 0;
if (this._rotated) {
temp_uvs[0].u = (rect.x) / atlasWidth;
temp_uvs[1].u = (rect.x + bottomHeight) / atlasWidth;
temp_uvs[2].u = (rect.x + bottomHeight + centerHeight) / atlasWidth;
temp_uvs[3].u = (rect.x + rect.height) / atlasWidth;
temp_uvs[3].v = (rect.y) / atlasHeight;
temp_uvs[2].v = (rect.y + leftWidth) / atlasHeight;
temp_uvs[1].v = (rect.y + leftWidth + centerWidth) / atlasHeight;
temp_uvs[0].v = (rect.y + rect.width) / atlasHeight;
this._flipXY(temp_uvs);
for (let row = 0; row < 4; ++row) {
let rowD = temp_uvs[row];
for (let col = 0; col < 4; ++col) {
let colD = temp_uvs[3 - col];
uvSliced.push({
u: rowD.u,
v: colD.v
});
}
}
}
else {
temp_uvs[0].u = (rect.x) / atlasWidth;
temp_uvs[1].u = (rect.x + leftWidth) / atlasWidth;
temp_uvs[2].u = (rect.x + leftWidth + centerWidth) / atlasWidth;
temp_uvs[3].u = (rect.x + rect.width) / atlasWidth;
temp_uvs[3].v = (rect.y) / atlasHeight;
temp_uvs[2].v = (rect.y + topHeight) / atlasHeight;
temp_uvs[1].v = (rect.y + topHeight + centerHeight) / atlasHeight;
temp_uvs[0].v = (rect.y + rect.height) / atlasHeight;
this._flipXY(temp_uvs);
for (let row = 0; row < 4; ++row) {
let rowD = temp_uvs[row];
for (let col = 0; col < 4; ++col) {
let colD = temp_uvs[col];
uvSliced.push({
u: colD.u,
v: rowD.v
});
}
}
}
},
_setDynamicAtlasFrame (frame) {
if (!frame) return;
this._original = {
_texture : this._texture,
_x : this._rect.x,
_y : this._rect.y
}
this._texture = frame.texture;
this._rect.x = frame.x;
this._rect.y = frame.y;
this._calculateUV();
},
_resetDynamicAtlasFrame () {
if (!this._original) return;
this._rect.x = this._original._x;
this._rect.y = this._original._y;
this._texture = this._original._texture;
this._original = null;
if (this._texture.loaded) {
this._calculateUV();
} else {
this.ensureLoadTexture()
}
},
_calculateUV () {
let rect = this._rect,
texture = this._texture,
uv = this.uv,
texw = texture.width,
texh = texture.height;
if (this._rotated) {
let l = texw === 0 ? 0 : rect.x / texw;
let r = texw === 0 ? 0 : (rect.x + rect.height) / texw;
let b = texh === 0 ? 0 : (rect.y + rect.width) / texh;
let t = texh === 0 ? 0 : rect.y / texh;
uv[0] = l;
uv[1] = t;
uv[2] = l;
uv[3] = b;
uv[4] = r;
uv[5] = t;
uv[6] = r;
uv[7] = b;
}
else {
let l = texw === 0 ? 0 : rect.x / texw;
let r = texw === 0 ? 0 : (rect.x + rect.width) / texw;
let b = texh === 0 ? 0 : (rect.y + rect.height) / texh;
let t = texh === 0 ? 0 : rect.y / texh;
uv[0] = l;
uv[1] = b;
uv[2] = r;
uv[3] = b;
uv[4] = l;
uv[5] = t;
uv[6] = r;
uv[7] = t;
}
if (this._flipX) {
let tempVal = uv[0];
uv[0] = uv[2];
uv[2] = tempVal;
tempVal = uv[1];
uv[1] = uv[3];
uv[3] = tempVal;
tempVal = uv[4];
uv[4] = uv[6];
uv[6] = tempVal;
tempVal = uv[5];
uv[5] = uv[7];
uv[7] = tempVal;
}
if (this._flipY) {
let tempVal = uv[0];
uv[0] = uv[4];
uv[4] = tempVal;
tempVal = uv[1];
uv[1] = uv[5];
uv[5] = tempVal;
tempVal = uv[2];
uv[2] = uv[6];
uv[6] = tempVal;
tempVal = uv[3];
uv[3] = uv[7];
uv[7] = tempVal;
}
let vertices = this.vertices;
if (vertices) {
vertices.nu.length = 0;
vertices.nv.length = 0;
for (let i = 0; i < vertices.u.length; i++) {
vertices.nu[i] = vertices.u[i]/texw;
vertices.nv[i] = vertices.v[i]/texh;
}
}
this._calculateSlicedUV();
},
// SERIALIZATION
_serialize: (CC_EDITOR || CC_TEST) && function (exporting, ctx) {
let rect = this._rect;
let offset = this._offset;
let size = this._originalSize;
let uuid;
let texture = this._texture;
if (texture) {
uuid = texture._uuid;
}
if (!uuid) {
let url = this._textureFilename;
if (url) {
uuid = Editor.Utils.UuidCache.urlToUuid(url);
}
}
if (uuid && exporting) {
uuid = Editor.Utils.UuidUtils.compressUuid(uuid, true);
ctx.dependsOn('_textureSetter', uuid);
}
let vertices;
if (this.vertices) {
vertices = {
triangles: this.vertices.triangles,
x: this.vertices.x,
y: this.vertices.y,
u: this.vertices.u,
v: this.vertices.v
};
}
return {
name: this._name,
texture: (!exporting && uuid) || undefined,
atlas: exporting ? undefined : this._atlasUuid, // strip from json if exporting
rect: rect ? [rect.x, rect.y, rect.width, rect.height] : undefined,
offset: offset ? [offset.x, offset.y] : undefined,
originalSize: size ? [size.width, size.height] : undefined,
rotated: this._rotated ? 1 : undefined,
capInsets: this._capInsets,
vertices: vertices
};
},
_deserialize: function (data, handle) {
let rect = data.rect;
if (rect) {
this._rect = new cc.Rect(rect[0], rect[1], rect[2], rect[3]);
}
if (data.offset) {
this.setOffset(new cc.Vec2(data.offset[0], data.offset[1]));
}
if (data.originalSize) {
this.setOriginalSize(new cc.Size(data.originalSize[0], data.originalSize[1]));
}
this._rotated = data.rotated === 1;
this._name = data.name;
let capInsets = data.capInsets;
if (capInsets) {
this._capInsets[INSET_LEFT] = capInsets[INSET_LEFT];
this._capInsets[INSET_TOP] = capInsets[INSET_TOP];
this._capInsets[INSET_RIGHT] = capInsets[INSET_RIGHT];
this._capInsets[INSET_BOTTOM] = capInsets[INSET_BOTTOM];
}
if (CC_EDITOR) {
this._atlasUuid = data.atlas;
}
this.vertices = data.vertices;
if (this.vertices) {
// initialize normal uv arrays
this.vertices.nu = [];
this.vertices.nv = [];
}
if (!CC_BUILD) {
// manually load texture via _textureSetter
let textureUuid = data.texture;
if (textureUuid) {
handle.result.push(this, '_textureSetter', textureUuid);
}
}
}
});
let proto = SpriteFrame.prototype;
proto.copyWithZone = proto.clone;
proto.copy = proto.clone;
proto.initWithTexture = proto.setTexture;
cc.SpriteFrame = SpriteFrame;
module.exports = SpriteFrame;

View File

@@ -0,0 +1,65 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
const Font = require('./CCFont');
/**
* @module cc
*/
/**
* !#en Class for TTFFont handling.
* !#zh TTF 字体资源类。
* @class TTFFont
* @extends Font
*
*/
var TTFFont = cc.Class({
name: 'cc.TTFFont',
extends: Font,
properties: {
_fontFamily: null,
_nativeAsset: {
type: cc.String,
get () {
return this._fontFamily;
},
set (value) {
this._fontFamily = value || 'Arial';
},
override: true
},
_nativeDep: {
get () {
return { uuid: this._uuid, __nativeName__: this._native, ext: cc.path.extname(this._native), __isNative__: true };
},
override: true
}
}
});
cc.TTFFont = module.exports = TTFFont;

View File

@@ -0,0 +1,48 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
/**
* !#en Class for text file.
* !#zh 文本资源类。
* @class TextAsset
* @extends Asset
*/
var TextAsset = cc.Class({
name: 'cc.TextAsset',
extends: cc.Asset,
properties: {
/**
* @property {String} text - The text contents of the resource.
*/
text: "",
},
toString () {
return this.text;
},
});
module.exports = cc.TextAsset = TextAsset;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
const Asset = require('./CCAsset');
const EventTarget = require('../event/event-target');
/**
* !#en Class for video data handling.
* !#zh 视频资源类。
* @class VideoClip
* @extends Asset
* @uses EventTarget
*/
var VideoClip = cc.Class({
name: 'cc.VideoClip',
extends: Asset,
mixins: [EventTarget],
});
cc.VideoClip = VideoClip;
module.exports = VideoClip;

View File

@@ -0,0 +1,44 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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.
****************************************************************************/
require('./CCAsset');
require('./CCFont');
require('./CCPrefab');
require('./CCAudioClip');
require('./CCVideoClip');
require('./CCScripts');
require('./CCSceneAsset');
require('./CCSpriteFrame');
require('./CCTexture2D');
require('./CCRenderTexture');
require('./CCTTFFont');
require('./CCSpriteAtlas');
require('./CCBitmapFont');
require('./CCLabelAtlas');
require('./CCTextAsset');
require('./CCJsonAsset');
require('./CCBufferAsset');
require('./material');

View File

@@ -0,0 +1,54 @@
import Asset from '../CCAsset';
import { parseEffect } from './effect-parser';
/**
* !#en Effect Asset.
* !#zh Effect 资源类型。
* @class EffectAsset
* @extends Asset
*/
let EffectAsset = cc.Class({
name: 'cc.EffectAsset',
extends: Asset,
ctor () {
this._effect = null;
},
properties: {
properties: Object,
techniques: [],
shaders: []
},
onLoad () {
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
return;
}
let lib = cc.renderer._forward._programLib;
for (let i = 0; i < this.shaders.length; i++) {
lib.define(this.shaders[i]);
}
this._initEffect();
},
_initEffect () {
if (this._effect) return;
this._effect = parseEffect(this);
Object.freeze(this._effect);
},
getInstantiatedEffect () {
this._initEffect();
return this._effect.clone();
},
getEffect () {
this._initEffect();
return this._effect;
}
});
module.exports = cc.EffectAsset = EffectAsset;

View File

@@ -0,0 +1,408 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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.
****************************************************************************/
const Asset = require('../CCAsset');
const Texture = require('../CCTexture2D');
const PixelFormat = Texture.PixelFormat;
const EffectAsset = require('./CCEffectAsset');
const textureUtil = require('../../utils/texture-util');
const gfx = cc.gfx;
/**
* !#en Material builtin name
* !#zh 内置材质名字
* @enum Material.BUILTIN_NAME
*/
const BUILTIN_NAME = cc.Enum({
/**
* @property SPRITE
* @readonly
* @type {String}
*/
SPRITE: '2d-sprite',
/**
* @property GRAY_SPRITE
* @readonly
* @type {String}
*/
GRAY_SPRITE: '2d-gray-sprite',
/**
* @property UNLIT
* @readonly
* @type {String}
*/
UNLIT: 'unlit',
});
/**
* !#en Material Asset.
* !#zh 材质资源类。
* @class Material
* @extends Asset
*/
let Material = cc.Class({
name: 'cc.Material',
extends: Asset,
ctor () {
this.loaded = false;
this._manualHash = false;
this._dirty = true;
this._effect = null;
},
properties: {
// deprecated
_defines: {
default: undefined,
type: Object
},
// deprecated
_props: {
default: undefined,
type: Object
},
_effectAsset: {
type: EffectAsset,
default: null,
},
_techniqueIndex: 0,
_techniqueData: Object,
effectName: CC_EDITOR ? {
get () {
return this._effectAsset && this._effectAsset.name;
},
set (val) {
let effectAsset = cc.assetManager.builtins.getBuiltin('effect', val);
if (!effectAsset) {
Editor.warn(`no effect named '${val}' found`);
return;
}
this.effectAsset = effectAsset;
}
} : undefined,
effectAsset: {
get () {
return this._effectAsset;
},
set (asset) {
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
return;
}
this._effectAsset = asset;
if (!asset) {
cc.error('Can not set an empty effect asset.');
return;
}
this._effect = this._effectAsset.getInstantiatedEffect();
}
},
effect: {
get () {
return this._effect;
}
},
techniqueIndex: {
get () {
return this._techniqueIndex;
},
set (v) {
this._techniqueIndex = v;
this._effect.switchTechnique(v);
}
}
},
statics: {
/**
* !#en Get built-in materials
* !#zh 获取内置材质
* @static
* @method getBuiltinMaterial
* @param {string} name
* @return {Material}
*/
getBuiltinMaterial (name) {
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
return new cc.Material();
}
return cc.assetManager.builtins.getBuiltin('material', 'builtin-' + name);
},
BUILTIN_NAME,
/**
* !#en Creates a Material with builtin Effect.
* !#zh 使用内建 Effect 创建一个材质。
* @static
* @method createWithBuiltin
* @param {string} effectName
* @param {number} [techniqueIndex]
* @return {Material}
*/
createWithBuiltin (effectName, techniqueIndex = 0) {
let effectAsset = cc.assetManager.builtins.getBuiltin('effect', 'builtin-' + effectName);
return Material.create(effectAsset, techniqueIndex);
},
/**
* !#en Creates a Material.
* !#zh 创建一个材质。
* @static
* @method create
* @param {EffectAsset} effectAsset
* @param {number} [techniqueIndex]
* @return {Material}
*/
create (effectAsset, techniqueIndex = 0) {
if (!effectAsset) return null;
let material = new Material();
material.effectAsset = effectAsset;
material.techniqueIndex = techniqueIndex;
return material;
}
},
/**
* !#en Sets the Material property
* !#zh 设置材质的属性
* @method setProperty
* @param {string} name
* @param {Object} val
* @param {number} [passIdx]
* @param {boolean} [directly]
*/
setProperty (name, val, passIdx, directly) {
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) return;
if (typeof passIdx === 'string') {
passIdx = parseInt(passIdx);
}
if (val instanceof Texture) {
let isAlphaAtlas = val.isAlphaAtlas();
let key = 'CC_USE_ALPHA_ATLAS_' + name;
let def = this.getDefine(key, passIdx);
if (isAlphaAtlas || def) {
this.define(key, isAlphaAtlas);
}
if (!val.loaded) {
cc.assetManager.postLoadNative(val);
}
}
this._effect.setProperty(name, val, passIdx, directly);
},
/**
* !#en Gets the Material property.
* !#zh 获取材质的属性。
* @method getProperty
* @param {string} name
* @param {number} passIdx
* @return {Object}
*/
getProperty (name, passIdx) {
if (typeof passIdx === 'string') {
passIdx = parseInt(passIdx);
}
return this._effect.getProperty(name, passIdx);
},
/**
* !#en Sets the Material define.
* !#zh 设置材质的宏定义。
* @method define
* @param {string} name
* @param {boolean|number} val
* @param {number} [passIdx]
* @param {boolean} [force]
*/
define (name, val, passIdx, force) {
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) return;
if (typeof passIdx === 'string') {
passIdx = parseInt(passIdx);
}
this._effect.define(name, val, passIdx, force);
},
/**
* !#en Gets the Material define.
* !#zh 获取材质的宏定义。
* @method getDefine
* @param {string} name
* @param {number} [passIdx]
* @return {boolean|number}
*/
getDefine (name, passIdx) {
if (typeof passIdx === 'string') {
passIdx = parseInt(passIdx);
}
return this._effect.getDefine(name, passIdx);
},
/**
* !#en Sets the Material cull mode.
* !#zh 设置材质的裁减模式。
* @method setCullMode
* @param {number} cullMode
* @param {number} passIdx
*/
setCullMode (cullMode = gfx.CULL_BACK, passIdx) {
this._effect.setCullMode(cullMode, passIdx);
},
/**
* !#en Sets the Material depth states.
* !#zh 设置材质的深度渲染状态。
* @method setDepth
* @param {boolean} depthTest
* @param {boolean} depthWrite
* @param {number} depthFunc
* @param {number} passIdx
*/
setDepth (
depthTest = false,
depthWrite = false,
depthFunc = gfx.DS_FUNC_LESS,
passIdx
) {
this._effect.setDepth(depthTest, depthWrite, depthFunc, passIdx);
},
/**
* !#en Sets the Material blend states.
* !#zh 设置材质的混合渲染状态。
* @method setBlend
* @param {boolean} enabled
* @param {number} blendEq
* @param {number} blendSrc
* @param {number} blendDst
* @param {number} blendAlphaEq
* @param {number} blendSrcAlpha
* @param {number} blendDstAlpha
* @param {number} blendColor
* @param {number} passIdx
*/
setBlend (
enabled = false,
blendEq = gfx.BLEND_FUNC_ADD,
blendSrc = gfx.BLEND_SRC_ALPHA,
blendDst = gfx.BLEND_ONE_MINUS_SRC_ALPHA,
blendAlphaEq = gfx.BLEND_FUNC_ADD,
blendSrcAlpha = gfx.BLEND_SRC_ALPHA,
blendDstAlpha = gfx.BLEND_ONE_MINUS_SRC_ALPHA,
blendColor = 0xffffffff,
passIdx
) {
this._effect.setBlend(enabled, blendEq, blendSrc, blendDst, blendAlphaEq, blendSrcAlpha, blendDstAlpha, blendColor, passIdx);
},
/**
* !#en Sets whether enable the stencil test.
* !#zh 设置是否开启模板测试。
* @method setStencilEnabled
* @param {number} stencilTest
* @param {number} passIdx
*/
setStencilEnabled (stencilTest = gfx.STENCIL_INHERIT, passIdx) {
this._effect.setStencilEnabled(stencilTest, passIdx);
},
/**
* !#en Sets the Material stencil render states.
* !#zh 设置材质的模板测试渲染参数。
* @method setStencil
* @param {number} stencilTest
* @param {number} stencilFunc
* @param {number} stencilRef
* @param {number} stencilMask
* @param {number} stencilFailOp
* @param {number} stencilZFailOp
* @param {number} stencilZPassOp
* @param {number} stencilWriteMask
* @param {number} passIdx
*/
setStencil (
stencilTest = gfx.STENCIL_INHERIT,
stencilFunc = gfx.DS_FUNC_ALWAYS,
stencilRef = 0,
stencilMask = 0xff,
stencilFailOp = gfx.STENCIL_OP_KEEP,
stencilZFailOp = gfx.STENCIL_OP_KEEP,
stencilZPassOp = gfx.STENCIL_OP_KEEP,
stencilWriteMask = 0xff,
passIdx
) {
this._effect.setStencil(stencilTest, stencilFunc, stencilRef, stencilMask, stencilFailOp, stencilZFailOp, stencilZPassOp, stencilWriteMask, passIdx);
},
updateHash (hash) {
this._manualHash = hash;
this._effect && this._effect.updateHash(hash);
},
getHash () {
return this._manualHash || (this._effect && this._effect.getHash());
},
onLoad () {
this.effectAsset = this._effectAsset;
if (!this._effect) return;
if (this._techniqueIndex) {
this._effect.switchTechnique(this._techniqueIndex);
}
this._techniqueData = this._techniqueData || {};
let passDatas = this._techniqueData;
for (let index in passDatas) {
index = parseInt(index);
let passData = passDatas[index];
if (!passData) continue;
for (let def in passData.defines) {
this.define(def, passData.defines[def], index);
}
for (let prop in passData.props) {
this.setProperty(prop, passData.props[prop], index);
}
}
},
});
export default Material;
cc.Material = Material;

View File

@@ -0,0 +1,202 @@
import Pass from '../../../renderer/core/pass';
import enums from '../../../renderer/enums';
const gfx = cc.gfx;
export default class EffectBase {
_dirty = true;
_name = '';
get name () {
return this._name;
}
_technique = null;
get technique () {
return this._technique;
}
get passes (): Pass[] {
return [];
}
_createPassProp (name, pass) {
let prop = pass._properties[name];
if (!prop) {
return;
}
let uniform = Object.create(null);
uniform.name = name;
uniform.type = prop.type;
if (prop.value instanceof Float32Array) {
uniform.value = new Float32Array(prop.value);
}
else if (prop.value instanceof Float64Array) {
uniform.value = new Float64Array(prop.value);
}
else {
uniform.value = prop.value;
}
pass._properties[name] = uniform;
return uniform;
}
_setPassProperty (name, value, pass, directly) {
let properties = pass._properties;
if (!properties.hasOwnProperty(name)) {
this._createPassProp(name, pass);
}
let prop = properties[name];
let compareValue = value;
if (prop.type === enums.PARAM_TEXTURE_2D) {
compareValue = value && value.getImpl();
}
if (prop.value === compareValue) {
return true;
}
this._dirty = true;
return Pass.prototype.setProperty.call(pass, name, value, directly);
}
setProperty (name, value, passIdx, directly) {
let success = false;
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
if (this._setPassProperty(name, value, passes[i], directly)) {
success = true;
}
}
if (!success) {
cc.warnID(9103, this.name, name);
}
}
getProperty (name, passIdx) {
let passes = this.passes;
if (passIdx >= passes.length) return;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
let value = passes[i].getProperty(name);
if (value !== undefined) {
return value;
}
}
}
define (name, value, passIdx, force) {
let success = false;
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
if (passes[i].define(name, value, force)) {
success = true;
}
}
if (!success) {
cc.warnID(9104, this.name, name);
}
}
getDefine (name, passIdx) {
let passes = this.passes;
if (passIdx >= passes.length) return;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
let value = passes[i].getDefine(name);
if (value !== undefined) {
return value;
}
}
}
setCullMode (cullMode = gfx.CULL_BACK, passIdx) {
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
passes[i].setCullMode(cullMode);
}
this._dirty = true;
}
setDepth (depthTest, depthWrite, depthFunc, passIdx) {
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
passes[i].setDepth(depthTest, depthWrite, depthFunc);
}
this._dirty = true;
}
setBlend (enabled, blendEq, blendSrc, blendDst, blendAlphaEq, blendSrcAlpha, blendDstAlpha, blendColor, passIdx) {
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
passes[i].setBlend(
enabled,
blendEq,
blendSrc, blendDst,
blendAlphaEq,
blendSrcAlpha, blendDstAlpha, blendColor
);
}
this._dirty = true;
}
setStencilEnabled (stencilTest = gfx.STENCIL_INHERIT, passIdx) {
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
passes[i].setStencilEnabled(stencilTest);
}
this._dirty = true;
}
setStencil (enabled, stencilFunc, stencilRef, stencilMask, stencilFailOp, stencilZFailOp, stencilZPassOp, stencilWriteMask, passIdx) {
let passes = this.passes;
let start = 0, end = passes.length;
if (passIdx !== undefined) {
start = passIdx, end = passIdx + 1;
}
for (let i = start; i < end; i++) {
let pass = passes[i];
pass.setStencilFront(enabled, stencilFunc, stencilRef, stencilMask, stencilFailOp, stencilZFailOp, stencilZPassOp, stencilWriteMask);
pass.setStencilBack(enabled, stencilFunc, stencilRef, stencilMask, stencilFailOp, stencilZFailOp, stencilZPassOp, stencilWriteMask);
}
this._dirty = true;
}
}
cc.EffectBase = EffectBase;

View File

@@ -0,0 +1,156 @@
import Pass from '../../../renderer/core/pass';
import { getInspectorProps, enums2default } from '../../../renderer/types';
import enums from '../../../renderer/enums';
import Effect from './effect';
import Technique from '../../../renderer/core/technique';
function getInvolvedProgram (programName) {
let lib = cc.renderer._forward._programLib;
return lib.getTemplate(programName);
}
// extract properties from each passes and check whether properties is defined but not used.
function parseProperties (effectAsset, passJson) {
let propertiesJson = passJson.properties || {};
let program = getInvolvedProgram(passJson.program);
// check whether properties are defined in the shaders
for (let prop in propertiesJson) {
let uniformInfo = program.uniforms.find(u => u.name === prop);
// the property is not defined in all the shaders used in techs
if (!uniformInfo) {
cc.warnID(9107, effectAsset.name, prop);
continue;
}
}
// create properties
let properties = {};
program.uniforms.forEach(u => {
let name = u.name,
prop = properties[name] = Object.assign({}, u),
propInfo = propertiesJson[name];
let value;
if (propInfo) {
if (propInfo.type === enums.PARAM_TEXTURE_2D) {
value = null;
}
else if (propInfo.type === enums.PARAM_INT || propInfo.type === enums.PARAM_FLOAT) {
value = Array.isArray(propInfo.value) ? propInfo.value[0] : propInfo.value;
}
else {
value = new Float32Array(propInfo.value);
}
}
else {
value = enums2default[u.type];
}
if (value === undefined) {
value = null;
}
prop.value = value;
});
return properties;
};
function passDefines (pass) {
let defines = {};
let program = getInvolvedProgram(pass.program);
program.defines.forEach(d => {
defines[d.name] = enums2default[d.type];
})
return defines;
}
function parseTechniques (effectAsset) {
let techNum = effectAsset.techniques.length;
let techniques = new Array(techNum);
for (let j = 0; j < techNum; ++j) {
let tech = effectAsset.techniques[j];
let techName = tech.name || j;
let passNum = tech.passes.length;
let passes = new Array(passNum);
for (let k = 0; k < passNum; ++k) {
let pass = tech.passes[k];
let passName = pass.name || k;
let detailName = `${effectAsset.name}-${techName}-${passName}`;
let stage = pass.stage || 'opaque';
let properties = parseProperties(effectAsset, pass);
let defines = passDefines(pass);
let newPass = passes[k] = new Pass(passName, detailName, pass.program, stage, properties, defines);
// rasterizer state
if (pass.rasterizerState) {
newPass.setCullMode(pass.rasterizerState.cullMode);
}
// blend state
let blendState = pass.blendState && pass.blendState.targets[0];
if (blendState) {
newPass.setBlend(blendState.blend, blendState.blendEq, blendState.blendSrc,
blendState.blendDst, blendState.blendAlphaEq, blendState.blendSrcAlpha, blendState.blendDstAlpha, blendState.blendColor);
}
// depth stencil state
let depthStencilState = pass.depthStencilState;
if (depthStencilState) {
newPass.setDepth(depthStencilState.depthTest, depthStencilState.depthWrite, depthStencilState.depthFunc);
newPass.setStencilFront(depthStencilState.stencilTest, depthStencilState.stencilFuncFront, depthStencilState.stencilRefFront, depthStencilState.stencilMaskFront,
depthStencilState.stencilFailOpFront, depthStencilState.stencilZFailOpFront, depthStencilState.stencilZPassOpFront, depthStencilState.stencilWriteMaskFront);
newPass.setStencilBack(depthStencilState.stencilTest, depthStencilState.stencilFuncBack, depthStencilState.stencilRefBack, depthStencilState.stencilMaskBack,
depthStencilState.stencilFailOpBack, depthStencilState.stencilZFailOpBack, depthStencilState.stencilZPassOpBack, depthStencilState.stencilWriteMaskBack);
}
}
techniques[j] = new Technique(techName, passes);
}
return techniques;
};
export function parseEffect (effect) {
let techniques = parseTechniques(effect);
return new Effect(effect.name, techniques, 0, effect);
};
if (CC_EDITOR) {
// inspector only need properties defined in CCEffect
Effect.parseForInspector = function (effectAsset) {
return effectAsset.techniques.map((tech, techIdx) => {
let passes = tech.passes.map((pass, passIdx) => {
let program = getInvolvedProgram(pass.program);
let newProps = {};
let props = pass.properties;
for (let name in props) {
newProps[name] = getInspectorProps(props[name]);
let u = program.uniforms.find(u => u.name === name);
newProps[name].defines = u.defines || [];
}
let newDefines = {};
program.defines.map(def => {
newDefines[def.name] = getInspectorProps(def);
})
return {
name: pass.name || passIdx,
props: newProps,
defines: newDefines,
};
})
return {
name: tech.name || techIdx,
passes: passes,
};
})
};
}

View File

@@ -0,0 +1,89 @@
import murmurhash2 from '../../../renderer/murmurhash2_gc';
import utils from './utils';
import Pass from '../../../renderer/core/pass';
import Effect from './effect';
import EffectBase from './effect-base';
const gfx = cc.gfx;
export default class EffectVariant extends EffectBase {
_effect: Effect;
_passes: Pass[] = [];
_stagePasses = {};
_hash = 0;
get effect () {
return this._effect;
}
get name () {
return this._effect && (this._effect.name + ' (variant)');
}
get passes () {
return this._passes;
}
get stagePasses () {
return this._stagePasses;
}
constructor (effect: Effect) {
super();
this.init(effect);
}
_onEffectChanged () {
}
init (effect: Effect) {
if (effect instanceof EffectVariant) {
effect = effect.effect;
}
this._effect = effect;
this._dirty = true;
if (effect) {
let passes = effect.passes;
let variantPasses = this._passes;
variantPasses.length = 0;
let stagePasses = this._stagePasses = {};
for (let i = 0; i < passes.length; i++) {
let variant = variantPasses[i] = Object.setPrototypeOf({}, passes[i]);
variant._properties = Object.setPrototypeOf({}, passes[i]._properties);
variant._defines = Object.setPrototypeOf({}, passes[i]._defines);
if (!stagePasses[variant._stage]) {
stagePasses[variant._stage] = [];
}
stagePasses[variant._stage].push(variant);
}
}
}
updateHash (hash: number) {
}
getHash () {
if (!this._dirty) return this._hash;
this._dirty = false;
let hash = '';
hash += utils.serializePasses(this._passes);
let effect = this._effect;
if (effect) {
hash += utils.serializePasses(effect.passes);
}
this._hash = murmurhash2(hash, 666);
this.updateHash(this._hash);
return this._hash;
}
}
cc.EffectVariant = EffectVariant;

View File

@@ -0,0 +1,58 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import Technique from '../../../renderer/core/technique';
import EffectBase from './effect-base';
export default class Effect extends EffectBase {
_techniques: Technique[] = [];
_asset = null;
get technique () {
return this._technique;
}
get passes () {
return this._technique.passes;
}
/**
* @param {Array} techniques
*/
constructor (name, techniques, techniqueIndex, asset) {
super();
this.init(name, techniques, techniqueIndex, asset, true);
}
init (name, techniques, techniqueIndex, asset, createNative) {
this._name = name;
this._techniques = techniques;
this._technique = techniques[techniqueIndex];
this._asset = asset;
}
switchTechnique (index) {
if (index >= this._techniques.length) {
cc.warn(`Can not switch to technique with index [${index}]`);
return;
}
this._technique = this._techniques[index];
}
clear () {
this._techniques = [];
}
clone () {
let techniques = [];
for (let i = 0; i < this._techniques.length; i++) {
techniques.push(this._techniques[i].clone());
}
let techniqueIndex = this._techniques.indexOf(this._technique);
return new Effect(this._name, techniques, techniqueIndex, this._asset);
}
}
cc.Effect = Effect;

View File

@@ -0,0 +1,3 @@
import './CCEffectAsset';
import './CCMaterial';
import './material-variant';

View File

@@ -0,0 +1,95 @@
import utils from './utils';
import Pool from '../../utils/pool';
/**
* {
* effectUuid: {
* defineSerializeKey: []
* }
* }
*/
class MaterialPool extends Pool {
// default disabled material pool
enabled = false;
_pool = {};
get (exampleMat, renderComponent) {
let pool = this._pool;
if (exampleMat instanceof cc.MaterialVariant) {
if (exampleMat._owner) {
if (exampleMat._owner === renderComponent) {
return exampleMat;
}
else {
exampleMat = exampleMat.material;
}
}
else {
exampleMat._owner = renderComponent;
return exampleMat;
}
}
let instance;
if (this.enabled) {
let uuid = exampleMat.effectAsset._uuid;
if (pool[uuid]) {
let key =
utils.serializeDefines(exampleMat._effect._defines) +
utils.serializeTechniques(exampleMat._effect._techniques);
instance = pool[uuid][key] && pool[uuid][key].pop();
}
}
if (!instance) {
instance = new cc.MaterialVariant(exampleMat);
instance._name = exampleMat._name + ' (Instance)';
instance._uuid = exampleMat._uuid;
}
else {
this.count--;
}
instance._owner = renderComponent;
return instance;
}
put (mat) {
if (!this.enabled || !mat._owner) {
return;
}
let pool = this._pool;
let uuid = mat.effectAsset._uuid;
if (!pool[uuid]) {
pool[uuid] = {};
}
let key =
utils.serializeDefines(mat._effect._defines) +
utils.serializeTechniques(mat._effect._techniques);
if (!pool[uuid][key]) {
pool[uuid][key] = [];
}
if (this.count > this.maxSize) return;
this._clean(mat);
pool[uuid][key].push(mat);
this.count++;
}
clear () {
this._pool = {};
this.count = 0;
}
_clean (mat) {
mat._owner = null;
}
}
let materialPool = new MaterialPool();
Pool.register('material', materialPool);
export default materialPool;

View File

@@ -0,0 +1,73 @@
import Material from './CCMaterial';
import EffectVariant from './effect-variant';
import MaterialPool from './material-pool';
let { ccclass, } = cc._decorator;
/**
* !#en
* Material Variant is an extension of the Material Asset.
* Changes to Material Variant do not affect other Material Variant or Material Asset,
* and changes to Material Asset are synchronized to the Material Variant.
* However, when a Material Variant had already modifies a state, the Material Asset state is not synchronized to the Material Variant.
* !#zh
* 材质变体是材质资源的一个延伸。
* 材质变体的修改不会影响到其他的材质变体或者材质资源,而材质资源的修改会同步体现到材质变体上,
* 但是当材质变体对一个状态修改后,材质资源再对这个状态修改是不会同步到材质变体上的。
* @class MaterialVariant
* @extends Material
*/
@ccclass('cc.MaterialVariant')
export default class MaterialVariant extends Material {
_owner: cc.RenderComponent = null;
_material: Material = null;
/**
* @method createWithBuiltin
* @param {Material.BUILTIN_NAME} materialName
* @param {RenderComponent} [owner]
* @typescript
* static createWithBuiltin (materialName: string, owner: cc.RenderComponent): MaterialVariant | null
*/
static createWithBuiltin (materialName: string, owner: cc.RenderComponent): MaterialVariant | null {
return MaterialVariant.create(Material.getBuiltinMaterial(materialName), owner);
}
/**
* @method create
* @param {Material} material
* @param {RenderComponent} [owner]
* @typescript
* static create (material: Material, owner: cc.RenderComponent): MaterialVariant | null
*/
static create (material: Material, owner: cc.RenderComponent): MaterialVariant | null {
if (!material) return null;
return MaterialPool.get(material, owner);
}
get uuid () {
return this._material._uuid;
}
get owner () {
return this._owner;
}
get material () {
return this._material;
}
constructor (material: Material) {
super();
this.init(material);
}
init (material) {
this._effect = new EffectVariant(material.effect);
this._effectAsset = material._effectAsset;
this._material = material;
}
}
cc.MaterialVariant = MaterialVariant;

View File

@@ -0,0 +1,90 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import enums from '../../../renderer/enums';
// function genHashCode (str) {
// var hash = 0;
// if (str.length == 0) {
// return hash;
// }
// for (var i = 0; i < str.length; i++) {
// var char = str.charCodeAt(i);
// hash = ((hash<<5)-hash)+char;
// hash = hash & hash; // Convert to 32bit integer
// }
// return hash;
// }
const hashArray = [];
function serializeDefines (defines, names) {
const len = names.length;
for (let i = 0; i < len; i++) {
const name = names[i];
hashArray[i] = name + defines[name];
}
hashArray.length = len;
return hashArray.join('');
}
function serializePass (pass, excludeProperties) {
let str = pass._programName + pass._cullMode;
if (pass._blend) {
str += pass._blendEq + pass._blendAlphaEq + pass._blendSrc + pass._blendDst
+ pass._blendSrcAlpha + pass._blendDstAlpha + pass._blendColor;
}
if (pass._depthTest) {
str += pass._depthWrite + pass._depthFunc;
}
if (pass._stencilTest) {
str += pass._stencilFuncFront + pass._stencilRefFront + pass._stencilMaskFront
+ pass._stencilFailOpFront + pass._stencilZFailOpFront + pass._stencilZPassOpFront
+ pass._stencilWriteMaskFront
+ pass._stencilFuncBack + pass._stencilRefBack + pass._stencilMaskBack
+ pass._stencilFailOpBack + pass._stencilZFailOpBack + pass._stencilZPassOpBack
+ pass._stencilWriteMaskBack;
}
if (!excludeProperties) {
str += serializeUniforms(pass._properties, pass._propertyNames);
}
str += serializeDefines(pass._defines, pass._defineNames);
return str;
}
function serializePasses (passes) {
let hashData = '';
for (let i = 0; i < passes.length; i++) {
hashData += serializePass(passes[i]);
}
return hashData;
}
function serializeUniforms (uniforms, names) {
let index = 0;
for (let i = 0, len = names.length; i < len; i++) {
let param = uniforms[names[i]];
let prop = param.value;
if (!prop) {
continue;
}
if (param.type === enums.PARAM_TEXTURE_2D || param.type === enums.PARAM_TEXTURE_CUBE) {
hashArray[index] = prop._id;
}
else {
hashArray[index] = prop.toString();
}
index++
}
hashArray.length = index;
return hashArray.join(';');
}
export default {
serializeDefines,
serializePasses,
serializeUniforms
};