From 2ec7b94b36a58c7344c156891e2b2b9e1edee0e6 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Fri, 12 Apr 2019 14:21:25 +0800 Subject: [PATCH] fix error for rendering components that are not initialized due to built-in resource (#4204) --- cocos/3d/builtin/init.ts | 10 ++++- cocos/3d/ui/assembler/mask/stencil-manager.ts | 2 +- cocos/3d/ui/components/graphics-component.ts | 36 ++++++++-------- cocos/3d/ui/components/label-component.ts | 22 +--------- cocos/3d/ui/components/mask-component.ts | 41 ++++-------------- cocos/3d/ui/components/sprite-component.ts | 22 +--------- cocos/3d/ui/components/ui-render-component.ts | 43 +++++++++++-------- cocos/core/platform/CCView.js | 3 -- 8 files changed, 63 insertions(+), 116 deletions(-) diff --git a/cocos/3d/builtin/init.ts b/cocos/3d/builtin/init.ts index 1ab299858f..9f3bb6bef2 100644 --- a/cocos/3d/builtin/init.ts +++ b/cocos/3d/builtin/init.ts @@ -158,10 +158,16 @@ class BuiltinResMgr { // sprite material const spriteMtl = new cc.Material(); - spriteMtl._uuid = 'ui-sprite-material'; - spriteMtl.initialize({ defines: { USE_TEXTURE: true }, effectName: 'builtin-sprite' }); + spriteMtl._uuid = 'ui-base-material'; + spriteMtl.initialize({ defines: { USE_TEXTURE: false }, effectName: 'builtin-sprite' }); resources[spriteMtl._uuid] = spriteMtl; + // sprite material + const spriteColorMtl = new cc.Material(); + spriteColorMtl._uuid = 'ui-sprite-material'; + spriteColorMtl.initialize({ defines: { USE_TEXTURE: true }, effectName: 'builtin-sprite' }); + resources[spriteColorMtl._uuid] = spriteColorMtl; + // default particle material const defaultParticleMtl = new cc.Material(); defaultParticleMtl._uuid = 'default-particle-material'; diff --git a/cocos/3d/ui/assembler/mask/stencil-manager.ts b/cocos/3d/ui/assembler/mask/stencil-manager.ts index 74c1f98ac1..b1d4fc4e76 100644 --- a/cocos/3d/ui/assembler/mask/stencil-manager.ts +++ b/cocos/3d/ui/assembler/mask/stencil-manager.ts @@ -139,9 +139,9 @@ export class StencilManager { } } - const state = this._stencilPattern; const pass = mat.passes[0]; if (this._changed(pass)){ + const state = this._stencilPattern; this._defaultPipelineState.depthStencilState = { stencilTestFront: state.stencilTest, stencilFuncFront: state.func, diff --git a/cocos/3d/ui/components/graphics-component.ts b/cocos/3d/ui/components/graphics-component.ts index bee0ee1367..4e020bc646 100644 --- a/cocos/3d/ui/components/graphics-component.ts +++ b/cocos/3d/ui/components/graphics-component.ts @@ -27,12 +27,10 @@ import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { Color } from '../../../core/value-types'; import { UI } from '../../../renderer/ui/ui'; -import { Material } from '../../assets'; -import { RenderableComponent } from '../../framework/renderable-component'; import { IAssembler } from '../assembler/assembler'; import { LineCap, LineJoin } from '../assembler/graphics/types'; import { Impl } from '../assembler/graphics/webgl/impl'; -import { UIRenderComponent } from '../components/ui-render-component'; +import { InstanceMaterialType, UIRenderComponent } from '../components/ui-render-component'; /** * @class Graphics @@ -197,7 +195,7 @@ export class GraphicsComponent extends UIRenderComponent { constructor (){ super(); - this.impl = this._assembler && (this._assembler as IAssembler).createImpl!(this); + this._instanceMaterialType = InstanceMaterialType.ADDCOLOR; } public onRestore () { @@ -206,10 +204,20 @@ export class GraphicsComponent extends UIRenderComponent { } } + public __preload (){ + if (super.__preload){ + super.__preload(); + } + + this._flushAssembler(); + this.impl = this._assembler && (this._assembler as IAssembler).createImpl!(this); + } + public onEnable () { if (super.onEnable) { super.onEnable(); } + this._activateMaterial(); } @@ -457,22 +465,12 @@ export class GraphicsComponent extends UIRenderComponent { return false; } - protected _instanceMaterial () { - let mat: Material | null = null; - if (this._sharedMaterial) { - mat = Material.getInstantiatedMaterial(this._sharedMaterial, new RenderableComponent(), CC_EDITOR ? true : false); - } else { - if (UIRenderComponent.addColorMat) { - mat = new Material(); - mat.copy(UIRenderComponent.addColorMat); - } else { - mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-sprite-material'), new RenderableComponent(), CC_EDITOR ? true : false); - mat.initialize({ defines: { USE_TEXTURE: false } }); - UIRenderComponent.addColorMat = mat; - } + public helpInstanceMaterial () { + this._instanceMaterial(); + if (!this.impl){ + this._flushAssembler(); + this.impl = this._assembler && (this._assembler as IAssembler).createImpl!(this); } - - this._updateMaterial(mat); } protected _flushAssembler (){ diff --git a/cocos/3d/ui/components/label-component.ts b/cocos/3d/ui/components/label-component.ts index 9d29962799..ac916e429e 100644 --- a/cocos/3d/ui/components/label-component.ts +++ b/cocos/3d/ui/components/label-component.ts @@ -31,11 +31,9 @@ import { ImageAsset } from '../../../assets/image-asset'; import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { ccenum } from '../../../core/value-types/enum'; import { UI } from '../../../renderer/ui/ui'; -import { Material } from '../../assets'; -import { RenderableComponent } from '../../framework/renderable-component'; import { FontAtlas } from '../assembler/label/bmfontUtils'; import { ISharedLabelData } from '../assembler/label/ttfUtils'; -import { UIRenderComponent } from './ui-render-component'; +import { InstanceMaterialType, UIRenderComponent } from './ui-render-component'; /** * !#en Enum for vertical text alignment. @@ -629,24 +627,6 @@ export class LabelComponent extends UIRenderComponent { return true; } - protected _instanceMaterial () { - let mat: Material | null = null; - if (this._sharedMaterial) { - mat = Material.getInstantiatedMaterial(this._sharedMaterial, new RenderableComponent(), CC_EDITOR ? true : false); - } else { - if (UIRenderComponent.addTextureMat) { - mat = new Material(); - mat.copy(UIRenderComponent.addTextureMat); - } else { - mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-sprite-material'), new RenderableComponent(), CC_EDITOR ? true : false); - mat.initialize({ defines: { USE_TEXTURE: true } }); - UIRenderComponent.addColorMat = mat; - } - } - - this._updateMaterial(mat); - } - protected _flushAssembler () { const assembler = LabelComponent.Assembler!.getAssembler(this); diff --git a/cocos/3d/ui/components/mask-component.ts b/cocos/3d/ui/components/mask-component.ts index 03440142d7..527c4bf884 100644 --- a/cocos/3d/ui/components/mask-component.ts +++ b/cocos/3d/ui/components/mask-component.ts @@ -31,10 +31,8 @@ import { ccenum } from '../../../core/value-types/enum'; import * as vmath from '../../../core/vmath'; import { UI } from '../../../renderer/ui/ui'; import { Node } from '../../../scene-graph'; -import { Material } from '../../assets/material'; -import { RenderableComponent } from '../../framework/renderable-component'; import { GraphicsComponent } from './graphics-component'; -import { UIRenderComponent } from './ui-render-component'; +import { InstanceMaterialType, UIRenderComponent } from './ui-render-component'; const _worldMatrix = new Mat4(); const _vec2_temp = new Vec2(); @@ -247,7 +245,13 @@ export class MaskComponent extends UIRenderComponent { private _graphics: GraphicsComponent | null = null; private _clearGraphics: GraphicsComponent | null = null; + constructor (){ + super(); + this._instanceMaterialType = InstanceMaterialType.ADDCOLOR; + } + public onLoad (){ + this._flushAssembler(); this._createGraphics(); } @@ -364,35 +368,6 @@ export class MaskComponent extends UIRenderComponent { return this._clearGraphics !== null && this._graphics !== null && this._renderPermit; } - protected _instanceMaterial () { - let mat: Material | null = null; - if (this._sharedMaterial) { - mat = Material.getInstantiatedMaterial(this._sharedMaterial, new RenderableComponent(), CC_EDITOR ? true : false); - } else { - // if (this._type === MaskType.IMAGE_STENCIL) { - // if (UIRenderComponent.addTextureMat) { - // mat = new Material(); - // mat.copy(UIRenderComponent.addTextureMat); - // } else { - // mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-sprite-material'), new RenderableComponent(), CC_EDITOR ? true : false); - // mat.initialize({ defines: { USE_TEXTURE: true } }); - // UIRenderComponent.addTextureMat = mat; - // } - // } else { - if (UIRenderComponent.addColorMat) { - mat = new Material(); - mat.copy(UIRenderComponent.addColorMat); - } else { - mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-sprite-material'), new RenderableComponent(), CC_EDITOR ? true : false); - mat.initialize({ defines: { USE_TEXTURE: false } }); - UIRenderComponent.addColorMat = mat; - } - // } - } - - this._updateMaterial(mat); - } - protected _flushAssembler (){ const assembler = MaskComponent.Assembler!.getAssembler(this); const posAssembler = MaskComponent.PostAssembler!.getAssembler(this); @@ -446,6 +421,7 @@ export class MaskComponent extends UIRenderComponent { if (!this._clearGraphics) { this._clearGraphics = new GraphicsComponent(); this._clearGraphics.node = new Node('clear-graphics'); + this._clearGraphics.helpInstanceMaterial(); this._clearGraphics._activateMaterial(); this._clearGraphics.lineWidth = 0; this._clearGraphics.rect(0, 0, cc.visibleRect.width, cc.visibleRect.height); @@ -455,6 +431,7 @@ export class MaskComponent extends UIRenderComponent { if (!this._graphics) { this._graphics = new GraphicsComponent(); this._graphics.node = this.node; + this._graphics.helpInstanceMaterial(); this._graphics.lineWidth = 0; this._graphics.strokeColor = cc.color(0, 0, 0, 0); } diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index 3d15b6d596..2d8f216588 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -32,9 +32,7 @@ import { clampf } from '../../../core/utils/misc'; import { Vec2 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import { UI } from '../../../renderer/ui/ui'; -import { Material } from '../../assets'; -import { RenderableComponent } from '../../framework/renderable-component'; -import { UIRenderComponent } from './ui-render-component'; +import { InstanceMaterialType, UIRenderComponent } from './ui-render-component'; /** * !#en Enum for sprite type. @@ -518,24 +516,6 @@ export class SpriteComponent extends UIRenderComponent { return true; } - protected _instanceMaterial () { - let mat: Material | null = null; - if (this._sharedMaterial) { - mat = Material.getInstantiatedMaterial(this._sharedMaterial, new RenderableComponent(), CC_EDITOR ? true : false); - } else { - if (UIRenderComponent.addTextureMat) { - mat = new Material(); - mat.copy(UIRenderComponent.addTextureMat); - } else { - mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-sprite-material'), new RenderableComponent(), CC_EDITOR ? true : false); - mat.initialize({ defines: { USE_TEXTURE: true}}); - UIRenderComponent.addTextureMat = mat; - } - } - - this._updateMaterial(mat); - } - protected _flushAssembler () { const assembler = SpriteComponent.Assembler!.getAssembler(this); diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 1ae9e7b2bf..266de385dd 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -37,6 +37,7 @@ import { GFXBlendFactor } from '../../../gfx/define'; import { RenderData } from '../../../renderer/ui/renderData'; import { UI } from '../../../renderer/ui/ui'; import { Material } from '../../assets/material'; +import { RenderableComponent } from '../../framework/renderable-component'; import { IAssembler, IAssemblerManager } from '../assembler/assembler'; import { CanvasComponent } from './canvas-component'; import { UIComponent } from './ui-component'; @@ -45,6 +46,11 @@ import { UITransformComponent } from './ui-transfrom-component'; // hack ccenum(GFXBlendFactor); +export enum InstanceMaterialType { + ADDCOLOR = 0, + ADDCOLORANDTEXTURE = 1, +} + /** * !#en * Base class for components which supports rendering features. @@ -168,10 +174,6 @@ export class UIRenderComponent extends UIComponent { public static Assembler: IAssemblerManager | null = null; public static PostAssembler: IAssemblerManager | null = null; - // 预先定制两份 UI 常用材质,也许之后需要修改 - public static addColorMat: Material | null = null; - public static addTextureMat: Material | null = null; - @property protected _srcBlendFactor = GFXBlendFactor.SRC_ALPHA; @property @@ -189,6 +191,7 @@ export class UIRenderComponent extends UIComponent { // 特殊渲染标记,在可渲染情况下,因为自身某个原因不给予渲染 protected _renderPermit = true; protected _material: Material | null = null; + protected _instanceMaterialType = InstanceMaterialType.ADDCOLORANDTEXTURE; protected _blendTemplate = { blendState: { targets: [ @@ -202,19 +205,8 @@ export class UIRenderComponent extends UIComponent { rasterizerState: {}, }; - constructor (){ - super(); - if (this._instanceMaterial) { - this._instanceMaterial(); - } - - if (this._flushAssembler) { - this._flushAssembler(); - } - } - public __preload (){ - + this._instanceMaterial(); } public onEnable () { @@ -367,7 +359,24 @@ export class UIRenderComponent extends UIComponent { } } - protected _instanceMaterial? (): void; + protected _instanceMaterial () { + let mat: Material | null = null; + if (this._sharedMaterial) { + mat = Material.getInstantiatedMaterial(this._sharedMaterial, new RenderableComponent(), CC_EDITOR ? true : false); + } else { + switch (this._instanceMaterialType){ + case InstanceMaterialType.ADDCOLOR: + mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-base-material'), new RenderableComponent(), CC_EDITOR ? true : false); + break; + case InstanceMaterialType.ADDCOLORANDTEXTURE: + mat = Material.getInstantiatedMaterial(cc.builtinResMgr.get('ui-sprite-material'), new RenderableComponent(), CC_EDITOR ? true : false); + break; + } + } + + this._updateMaterial(mat); + } + protected _flushAssembler? (): void; } diff --git a/cocos/core/platform/CCView.js b/cocos/core/platform/CCView.js index 8345a41d70..b3f1111507 100644 --- a/cocos/core/platform/CCView.js +++ b/cocos/core/platform/CCView.js @@ -320,9 +320,6 @@ class View extends EventTarget { orientation = orientation & cc.macro.ORIENTATION_AUTO; if (orientation && this._orientation !== orientation) { this._orientation = orientation; - var designWidth = this._originalDesignResolutionSize.width; - var designHeight = this._originalDesignResolutionSize.height; - this.setDesignResolutionSize(designWidth, designHeight, this._resolutionPolicy); } }