refine activate material (#2685)

* refine label activateMaterial

* refine sprite activateMaterial

* refine motion streak activateMaterial
This commit is contained in:
yinkaile
2018-05-15 18:44:45 +08:00
committed by pandamicro
parent a7e15e80d7
commit ec176ce336
13 changed files with 58 additions and 60 deletions
+17 -14
View File
@@ -379,11 +379,11 @@ var Label = cc.Class({
if (this.font === value) return;
//if delete the font, we should change isSystemFontUsed to true
if(!value) {
if (!value) {
this._isSystemFontUsed = true;
}
if(CC_EDITOR && value) {
if (CC_EDITOR && value) {
this._userDefinedFont = value;
}
@@ -405,9 +405,8 @@ var Label = cc.Class({
this._renderData = null;
}
this._fontAtlas = null;
this._material = null;
this._updateAssembler();
this._activateMaterial();
this._activateMaterial(true);
this._updateRenderData();
},
type: cc.Font,
@@ -536,39 +535,43 @@ var Label = cc.Class({
}
},
_activateMaterial () {
if (this._material) return;
_activateMaterial (force) {
let material = this._material;
if (!material) {
material = this._material = new SpriteMaterial();
}
else if (!force) {
return;
}
this._resetAssemblerData();
let material;
let font = this.font;
if (font instanceof cc.BitmapFont) {
let spriteFrame = this.font.spriteFrame;
let spriteFrame = font.spriteFrame;
// cannot be activated if texture not loaded yet
if (!spriteFrame.textureLoaded()) {
return;
}
material = new SpriteMaterial();
// TODO: old texture in material have been released by loader
material.texture = this._texture = spriteFrame._texture;
this._texture = spriteFrame._texture;
}
else {
this._assemblerData = _canvasPool.get();
this._texture = new cc.Texture2D();
this._texture.initWithElement(this._assemblerData._canvas);
material = new SpriteMaterial();
material.texture = this._texture;
}
material.texture = this._texture;
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
this._texture.url = this.uuid + '_texture';
}
material.color = this.node.color;
this.setMaterial(material);
},
@@ -581,7 +584,7 @@ var Label = cc.Class({
}
if (CC_EDITOR || force) {
this._activateMaterial();
this._activateMaterial(force);
this._updateAssembler();
this._assembler.updateRenderData(this);
}
+24 -21
View File
@@ -89,10 +89,10 @@ var MotionStreak = cc.Class({
*/
_fadeTime: 1,
fadeTime: {
get: function () {
get () {
return this._fadeTime;
},
set: function (value) {
set (value) {
this._fadeTime = value;
this.reset();
},
@@ -110,10 +110,10 @@ var MotionStreak = cc.Class({
*/
_minSeg: 1,
minSeg: {
get: function () {
get () {
return this._minSeg;
},
set: function (value) {
set (value) {
this._minSeg = value;
},
animatable: false,
@@ -130,10 +130,10 @@ var MotionStreak = cc.Class({
*/
_stroke: 64,
stroke: {
get: function () {
get () {
return this._stroke;
},
set: function (value) {
set (value) {
this._stroke = value;
},
animatable: false,
@@ -153,13 +153,12 @@ var MotionStreak = cc.Class({
type: cc.Texture2D
},
texture: {
get: function () {
get () {
return this._texture;
},
set: function (value) {
set (value) {
this._texture = value;
this._material = null;
this._activateMaterial();
this._activateMaterial(true);
},
type: cc.Texture2D,
animatable: false,
@@ -177,10 +176,10 @@ var MotionStreak = cc.Class({
*/
_color: cc.Color.WHITE,
color: {
get: function () {
get () {
return this._color;
},
set: function (value) {
set (value) {
this._color = value;
},
tooltip: CC_DEV && 'i18n:COMPONENT.motionStreak.color'
@@ -197,10 +196,10 @@ var MotionStreak = cc.Class({
*/
_fastMode: false,
fastMode: {
get: function () {
get () {
return this._fastMode;
},
set: function (value) {
set (value) {
this._fastMode = value;
},
animatable: false,
@@ -208,19 +207,23 @@ var MotionStreak = cc.Class({
}
},
onEnable: function () {
onEnable () {
this._super();
this._activateMaterial();
this.reset();
},
_activateMaterial: function () {
if (this._material) return;
_activateMaterial (force) {
let material = this._material;
if (!material) {
material = this._material = new SpriteMaterial();
material.useColor = false;
}
else if (!force) {
return;
}
let material = new SpriteMaterial();
// TODO: old texture in material have been released by loader
material.texture = this._texture;
material.useColor = false;
this.setMaterial(material);
},
@@ -244,7 +247,7 @@ var MotionStreak = cc.Class({
* // Remove all living segments of the ribbon.
* myMotionStreak.reset();
*/
reset: function () {
reset () {
this._points.length = 0;
let renderData = this._renderData;
if (renderData) {
+15 -11
View File
@@ -158,6 +158,8 @@ var Sprite = cc.Class({
ctor () {
this._assembler = null;
this._graySpriteMaterial = null;
this._spriteMaterial = null;
},
editor: CC_EDITOR && {
@@ -216,8 +218,7 @@ var Sprite = cc.Class({
}
this._spriteFrame = value;
if ((this._material && this._material._texture) !== (value && value._texture)) {
// Drop previous material, because texture have changed
this._material = null;
// disable render flow util texture is loaded
this.node._renderFlag &= ~RenderFlow.FLAG_RENDER;
}
// render & update render data flag will be triggered while applying new sprite frame
@@ -466,7 +467,6 @@ var Sprite = cc.Class({
setState: function (state) {
if (this._state === state) return;
this._state = state;
this._material = null;
this._activateMaterial();
},
@@ -522,29 +522,34 @@ var Sprite = cc.Class({
},
_activateMaterial: function () {
this.node._renderFlag &= ~RenderFlow.FLAG_RENDER;
if (!this.enabledInHierarchy) {
return;
}
if (this._material) {
this.node._renderFlag |= RenderFlow.FLAG_RENDER;
return;
}
let spriteFrame = this._spriteFrame;
// cannot be activated if texture not loaded yet
if (!spriteFrame || !spriteFrame.textureLoaded()) {
this.node._renderFlag &= ~RenderFlow.FLAG_RENDER;
return;
}
this.markUpdateRenderData();
// Get material
let texture = spriteFrame.getTexture();
let material;
if (this._state === State.GRAY) {
material = new GraySpriteMaterial();
if (!this._graySpriteMaterial) {
this._graySpriteMaterial = new GraySpriteMaterial();
}
material = this._graySpriteMaterial;
}
else {
material = new SpriteMaterial();
if (!this._spriteMaterial) {
this._spriteMaterial = new SpriteMaterial();
}
material = this._spriteMaterial;
material.color = this.node.color;
}
// TODO: old texture in material have been released by loader
@@ -612,7 +617,6 @@ var Sprite = cc.Class({
this.node.setContentSize(rect.width, rect.height);
}
this.markUpdateRenderData();
this._activateMaterial();
}
},
@@ -36,8 +36,6 @@ module.exports = js.addon({
return comp.requestRenderData();
},
updateRenderData: ttfAssembler.updateRenderData,
fillBuffers (comp, renderer) {
let node = comp.node,
renderData = comp._renderData,
@@ -50,10 +50,6 @@ module.exports = js.addon({
return renderData;
},
updateRenderData (comp) {
this.update(comp);
},
fillBuffers (comp, renderer) {
let data = comp._renderData._data,
node = comp.node,
@@ -40,7 +40,6 @@ module.exports = {
dynamicAtlasManager.insertSpriteFrame(frame);
}
if (!sprite._material || sprite._material._texture !== frame._texture) {
sprite._material = null;
sprite._activateMaterial();
}
}
@@ -42,7 +42,6 @@ const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
dynamicAtlasManager.insertSpriteFrame(frame);
}
if (!sprite._material || sprite._material._texture !== frame._texture) {
sprite._material = null;
sprite._activateMaterial();
}
}
@@ -52,7 +52,6 @@ module.exports = {
dynamicAtlasManager.insertSpriteFrame(frame);
}
if (!sprite._material || sprite._material._texture !== frame._texture) {
sprite._material = null;
sprite._activateMaterial();
}
}
@@ -39,7 +39,6 @@ module.exports = {
dynamicAtlasManager.insertSpriteFrame(frame);
}
if (!sprite._material || sprite._material._texture !== frame._texture) {
sprite._material = null;
sprite._activateMaterial();
}
}
@@ -51,7 +51,6 @@ module.exports = {
dynamicAtlasManager.insertSpriteFrame(frame);
}
if (!sprite._material || sprite._material._texture !== frame._texture) {
sprite._material = null;
sprite._activateMaterial();
}
}
@@ -42,7 +42,6 @@ module.exports = {
dynamicAtlasManager.insertSpriteFrame(frame);
}
if (!sprite._material || sprite._material._texture !== frame._texture) {
sprite._material = null;
sprite._activateMaterial();
}
}
+1 -1
View File
@@ -134,7 +134,7 @@ let _labelHeight = 0;
let _maxLineWidth = 0;
module.exports = {
update (comp) {
updateRenderData (comp) {
if (!comp._renderData.vertDirty) return;
_comp = comp;
+1 -1
View File
@@ -70,7 +70,7 @@ let _isItalic = false;
let _isUnderline = false;
module.exports = {
update (comp) {
updateRenderData (comp) {
if (!comp._renderData.vertDirty) return;
_comp = comp;