From c3be94b6c6c37a938efb10e7809b4fdbf37e9ee5 Mon Sep 17 00:00:00 2001 From: Leslie Date: Mon, 14 Jan 2019 10:45:28 +0800 Subject: [PATCH] adjust texture assets' struct, put most create logic to texture-base --- cocos/3d/assets/texture-cube.ts | 45 ++++++--------- .../3d/framework/skinning-model-component.js | 2 +- cocos/3d/index.js | 2 +- cocos/3d/misc/utils.ts | 39 +++++-------- cocos/assets/texture-2d.ts | 35 ++---------- cocos/assets/texture-base.ts | 55 ++++++++++++++----- 6 files changed, 76 insertions(+), 102 deletions(-) diff --git a/cocos/3d/assets/texture-cube.ts b/cocos/3d/assets/texture-cube.ts index 02468a164b..9b508c941e 100644 --- a/cocos/3d/assets/texture-cube.ts +++ b/cocos/3d/assets/texture-cube.ts @@ -27,8 +27,7 @@ import { Texture2D } from '../../assets'; import ImageAsset from '../../assets/image-asset'; import TextureBase from '../../assets/texture-base'; import { ccclass, property } from '../../core/data/class-decorator'; -import { GFXTextureType, GFXTextureUsageBit, GFXTextureViewType, GFXTextureFlagBit } from '../../gfx/define'; -import { GFXDevice } from '../../gfx/device'; +import { GFXTextureFlagBit, GFXTextureViewType } from '../../gfx/define'; interface ITextureCubeMipmap { front: ImageAsset; @@ -54,9 +53,11 @@ export default class TextureCube extends TextureBase { */ set mipmaps (value) { this._mipmaps = value; - this._potientialWidth = value.length === 0 ? 0 : value[0].front.width; - this._potientialHeight = value.length === 0 ? 0 : value[0].front.height; - this._recreateTexture(); + this.create( + value.length === 0 ? 0 : value[0].front.width, + value.length === 0 ? 0 : value[0].front.height, + undefined, + this._mipmaps.length); this._mipmaps.forEach((mipmap, level) => { _forEachFace(mipmap, (face, faceIndex) => { this._assignImage(face, level, faceIndex); @@ -171,32 +172,18 @@ export default class TextureCube extends TextureBase { } } - protected _createTextureImpl (gfxDevice: GFXDevice) { - this._texture = gfxDevice.createTexture({ - type: GFXTextureType.TEX2D, - /* tslint:disable:no-bitwise */ - usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, - /* tslint:enable:no-bitwise */ - format: this._getGfxFormat(), - width: this._potientialWidth, - height: this._potientialHeight, - mipLevel: this._mipmaps.length, - arrayLayer: 6, - flags: GFXTextureFlagBit.CUBEMAP, - }); + protected _getTextureCreateInfo () { + const result = super._getTextureCreateInfo(); + result.arrayLayer = 6; + result.flags = result.flags | GFXTextureFlagBit.CUBEMAP; + return result; } - protected _createTextureViewImpl (gfxDevice: GFXDevice) { - if (!this._texture) { - return; - } - - this._textureView = gfxDevice.createTextureView({ - texture: this._texture, - type: GFXTextureViewType.CUBE, - format: this._getGfxFormat(), - layerCount: 6, - }); + protected _getTextureViewCreateInfo () { + const result = super._getTextureViewCreateInfo(); + result.type = GFXTextureViewType.CUBE; + result.layerCount = 6; + return result; } } diff --git a/cocos/3d/framework/skinning-model-component.js b/cocos/3d/framework/skinning-model-component.js index c170eb5617..858acd6ddb 100644 --- a/cocos/3d/framework/skinning-model-component.js +++ b/cocos/3d/framework/skinning-model-component.js @@ -28,7 +28,7 @@ import { mat4 } from '../../core/vmath'; import ModelComponent from './model-component'; import { renderer } from '../../renderer/index'; import { ccclass, property, executionOrder, menu,executeInEditMode } from '../../core/data/class-decorator'; -import utils from '../misc/utils'; +import * as utils from '../misc/utils'; import Skeleton from '../assets/skeleton'; import { Node } from '../../scene-graph/node'; diff --git a/cocos/3d/index.js b/cocos/3d/index.js index 8f70aeeed1..32a86b567a 100644 --- a/cocos/3d/index.js +++ b/cocos/3d/index.js @@ -27,7 +27,7 @@ export * from './assets'; export * from './builtin'; export * from './framework'; export * from './memop'; -import utils from './misc/utils'; +import * as utils from './misc/utils'; import * as primitives from './primitive'; import * as geometry from './geom-utils'; diff --git a/cocos/3d/misc/utils.ts b/cocos/3d/misc/utils.ts index 9d1a4b42f0..46c82198a9 100644 --- a/cocos/3d/misc/utils.ts +++ b/cocos/3d/misc/utils.ts @@ -1,21 +1,21 @@ import { GFXPrimitiveMode } from '../../gfx/define'; import * as renderer from '../../renderer'; -import gfx from '../../renderer/gfx/index'; -import find from '../../scene-graph/find'; +export { default as find } from '../../scene-graph/find'; +import Texture2D from '../../assets/texture-2d'; +import { Filter, PixelFormat, WrapMode } from '../../assets/texture-base'; import { Mesh, RenderingMesh } from '../assets/mesh'; /** * - * @param {import("../assets/skeleton").default} skinning */ -function createJointsTexture (skinning) { +export function createJointsTexture (skinning: { joints: any[]; }) { const jointCount = skinning.joints.length; // Set jointsTexture. // A squared texture with side length N(N > 1) multiples of 2 can store // 2 ^ (2 * N - 2) matrices. // We support most 1024 joints. - let size; + let size = 0; if (jointCount > 1024) { throw new Error('To many joints(more than 1024).'); } else if (jointCount > 256) { @@ -30,19 +30,14 @@ function createJointsTexture (skinning) { size = 4; } - return new gfx.Texture2D(cc.game._renderContext, { - width: size, - height: size, - format: gfx.TEXTURE_FMT_RGBA32F, - minFilter: gfx.FILTER_NEAREST, - magFilter: gfx.FILTER_NEAREST, - wrapS: gfx.WRAP_CLAMP, - wrapT: gfx.WRAP_CLAMP, - mipmap: false, - }); + const texture = new Texture2D(); + texture.create(size, size, PixelFormat.RGBA32F); + texture.setFilters(Filter.NEAREST, Filter.NEAREST); + texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); + return texture; } -function createMesh (context, data) { +export function createMesh (context, data) { const ia = renderer.createIA(context, data); const meshAsset = new Mesh(); const primitiveMode = data.primitiveMode === undefined ? GFXPrimitiveMode.TRIANGLE_LIST : data.primitiveMode; @@ -54,16 +49,8 @@ function createMesh (context, data) { } /** - * @param {Uint8Array} buffer + * */ -function toPPM (buffer, w, h) { +export function toPPM (buffer: Uint8Array, w: number, h: number) { return `P3 ${w} ${h} 255\n${buffer.filter((e, i) => i % 4 < 3).toString()}\n`; } - -export default { - createJointsTexture, - createMesh, - - find, - toPPM, -}; diff --git a/cocos/assets/texture-2d.ts b/cocos/assets/texture-2d.ts index 4cc7b76b9c..30c621caef 100644 --- a/cocos/assets/texture-2d.ts +++ b/cocos/assets/texture-2d.ts @@ -25,8 +25,6 @@ ****************************************************************************/ // @ts-check import { ccclass, property } from '../core/data/class-decorator'; -import { GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; -import { GFXDevice } from '../gfx/device'; import ImageAsset from './image-asset'; import TextureBase from './texture-base'; @@ -48,9 +46,11 @@ export default class Texture2D extends TextureBase { */ set mipmaps (value) { this._mipmaps = value; - this._potientialWidth = value.length === 0 ? 0 : value[0].width; - this._potientialHeight = value.length === 0 ? 0 : value[0].height; - this._recreateTexture(); + this.create( + value.length === 0 ? 0 : value[0].width, + value.length === 0 ? 0 : value[0].height, + undefined, + this._mipmaps.length); this._mipmaps.forEach((mipmap, level) => { this._assignImage(mipmap, level); }); @@ -180,31 +180,6 @@ export default class Texture2D extends TextureBase { handle.result.push(this._mipmaps, `${i}`, mipmapUUID); } } - - protected _createTextureImpl (gfxDevice: GFXDevice) { - this._texture = gfxDevice.createTexture({ - type: GFXTextureType.TEX2D, - /* tslint:disable:no-bitwise */ - usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, - /* tslint:enable:no-bitwise */ - format: this._getGfxFormat(), - width: this._potientialWidth, - height: this._potientialHeight, - mipLevel: this._mipmaps.length, - }); - } - - protected _createTextureViewImpl (gfxDevice: GFXDevice) { - if (!this._texture) { - return; - } - - this._textureView = gfxDevice.createTextureView({ - texture: this._texture, - type: GFXTextureViewType.TV2D, - format: this._getGfxFormat(), - }); - } } /* tslint:disable:no-string-literal */ diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index 2b4ab8881b..33450e9cff 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -29,11 +29,12 @@ import EventTarget from '../core/event/event-target'; import IDGenerator from '../core/utils/id-generator'; import {addon} from '../core/utils/js'; import { ccenum } from '../core/value-types/enum'; -import { GFXAddress, GFXBufferTextureCopy, GFXFilter, GFXFormat } from '../gfx/define'; +import { GFXAddress, GFXBufferTextureCopy, GFXFilter, GFXFormat, + GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; import { GFXDevice } from '../gfx/device'; import { GFXSampler } from '../gfx/sampler'; -import { GFXTexture } from '../gfx/texture'; -import { GFXTextureView } from '../gfx/texture-view'; +import { GFXTexture, IGFXTextureInfo } from '../gfx/texture'; +import { GFXTextureView, IGFXTextureViewInfo } from '../gfx/texture-view'; import { Asset } from './asset'; import ImageAsset from './image-asset'; @@ -200,9 +201,11 @@ export default class TextureBase extends Asset { protected _textureView: GFXTextureView | null = null; - protected _potientialWidth: number = 0; + private _potientialWidth: number = 0; - protected _potientialHeight: number = 0; + private _potientialHeight: number = 0; + + private _mipmapLevel: number = 1; @property private _genMipmap = false; @@ -260,6 +263,14 @@ export default class TextureBase extends Asset { this.loaded = false; } + public create (width: number, height: number, format = PixelFormat.RGBA8888, mipmapLevel = 1) { + this._potientialWidth = width; + this._potientialHeight = height; + this._format = format; + this._mipmapLevel = 1; + this._recreateTexture(); + } + /** * Gets the underlying texture object. * @deprecated Use getGfxTexture() instead. @@ -475,6 +486,7 @@ export default class TextureBase extends Asset { } protected _getGlobalDevice (): GFXDevice | null { + // @ts-ignore return cc.director.root.device; } @@ -543,6 +555,27 @@ export default class TextureBase extends Asset { } } + protected _getTextureCreateInfo (): IGFXTextureInfo { + return { + type: GFXTextureType.TEX2D, + /* tslint:disable:no-bitwise */ + usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, + /* tslint:enable:no-bitwise */ + format: toGfxFormat(this._format), + width: this._potientialWidth, + height: this._potientialHeight, + mipLevel: this._mipmapLevel, + }; + } + + protected _getTextureViewCreateInfo (): IGFXTextureViewInfo { + return { + texture: this._texture, + type: GFXTextureViewType.TV2D, + format: this._getGfxFormat(), + }; + } + protected _recreateTexture () { this._destroyTexture(); @@ -551,17 +584,9 @@ export default class TextureBase extends Asset { return; } - this._createTextureImpl(gfxDevice); + this._texture = gfxDevice.createTexture(this._getTextureCreateInfo()); - this._createTextureViewImpl(gfxDevice); - } - - protected _createTextureImpl (gfxDevice: GFXDevice) { - return; - } - - protected _createTextureViewImpl (gfxDevice: GFXDevice) { - return; + this._textureView = gfxDevice.createTextureView(this._getTextureViewCreateInfo()); } private _destroyTexture () {