diff --git a/cocos/3d/assets/texture-cube.ts b/cocos/3d/assets/texture-cube.ts index 9a9ce7ccb3..43f5f423db 100644 --- a/cocos/3d/assets/texture-cube.ts +++ b/cocos/3d/assets/texture-cube.ts @@ -28,6 +28,7 @@ import ImageAsset from '../../assets/image-asset'; import TextureBase from '../../assets/texture-base'; import { ccclass, property } from '../../core/data/class-decorator'; import { GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../../gfx/define'; +import { GFXDevice } from '../../gfx/device'; interface ITextureCubeMipmap { front: ImageAsset; @@ -53,7 +54,14 @@ 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._mipmaps.forEach((mipmap, level) => { + _forEachFace(mipmap, (face, faceIndex) => { + this._assignImage(face, level, faceIndex); + }); + }); } /** @@ -163,45 +171,25 @@ export default class TextureCube extends TextureBase { } } - protected _createTexture () { - if (this._mipmaps.length === 0) { - return; - } - - const gfxDevice = this._getGlobalDevice(); - if (!gfxDevice) { - return; - } - + 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.width, - height: this.height, + width: this._potientialWidth, + height: this._potientialHeight, mipLevel: this._mipmaps.length, arrayLayer: 6, }); - - this._mipmaps.forEach((mipmap, level) => { - _forEachFace(mipmap, (face, faceIndex) => { - this._assignImage(face, level, faceIndex); - }); - }); } - protected _createTextureView () { + protected _createTextureViewImpl (gfxDevice: GFXDevice) { if (!this._texture) { return; } - const gfxDevice = this._getGlobalDevice(); - if (!gfxDevice) { - return; - } - this._textureView = gfxDevice.createTextureView({ texture: this._texture, type: GFXTextureViewType.TV2D_ARRAY, diff --git a/cocos/assets/texture-2d.ts b/cocos/assets/texture-2d.ts index faf0eff38d..4cc7b76b9c 100644 --- a/cocos/assets/texture-2d.ts +++ b/cocos/assets/texture-2d.ts @@ -26,6 +26,7 @@ // @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'; @@ -47,7 +48,12 @@ 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._mipmaps.forEach((mipmap, level) => { + this._assignImage(mipmap, level); + }); } /** @@ -175,42 +181,24 @@ export default class Texture2D extends TextureBase { } } - protected _createTexture () { - if (this._mipmaps.length === 0) { - return; - } - - const gfxDevice = this._getGlobalDevice(); - if (!gfxDevice) { - return; - } - + 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.width, - height: this.height, + width: this._potientialWidth, + height: this._potientialHeight, mipLevel: this._mipmaps.length, }); - - this._mipmaps.forEach((mipmap, level) => { - this._assignImage(mipmap, level); - }); } - protected _createTextureView () { + protected _createTextureViewImpl (gfxDevice: GFXDevice) { if (!this._texture) { return; } - const gfxDevice = this._getGlobalDevice(); - if (!gfxDevice) { - return; - } - this._textureView = gfxDevice.createTextureView({ texture: this._texture, type: GFXTextureViewType.TV2D, diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index fb0a4cdf93..2b4ab8881b 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -63,11 +63,11 @@ export enum PixelFormat { /** * 24-bit texture: RGB888 */ - RGB888 = GFXFormat.RGB8UI, + RGB888 = GFXFormat.RGB8, /** * 32-bit texture: RGBA8888 */ - RGBA8888 = GFXFormat.RGBA8UI, + RGBA8888 = GFXFormat.RGBA8, /** * 32-bit float texture: RGBA32F */ @@ -75,15 +75,15 @@ export enum PixelFormat { /** * 8-bit textures used as masks */ - A8 = GFXFormat.R8UI, + A8 = GFXFormat.A8, /** * 8-bit intensity texture */ - I8 = GFXFormat.R8UI, + I8 = GFXFormat.L8, /** * 16-bit textures used as masks */ - AI8 = GFXFormat.RG8UI, + AI8 = GFXFormat.LA8, /** * rgb 2 bpp pvrtc */ @@ -165,11 +165,9 @@ export default class TextureBase extends Asset { * For Cubemap texture, the width of texture is equal to its every sides's very first mipmaps's width. * !#zh * 贴图像素宽度 - * @property width - * @type {Number} */ public get width (): number { - return this._width; + return this._texture ? this._texture.width : 0; } /** @@ -179,11 +177,9 @@ export default class TextureBase extends Asset { * For Cubemap texture, the height of texture is equal to its every sides's very first mipmaps's height. * !#zh * 贴图像素高度 - * @property height - * @type {Number} */ public get height (): number { - return this._height; + return this._texture ? this._texture.height : 0; } public static PixelFormat = PixelFormat; @@ -204,6 +200,10 @@ export default class TextureBase extends Asset { protected _textureView: GFXTextureView | null = null; + protected _potientialWidth: number = 0; + + protected _potientialHeight: number = 0; + @property private _genMipmap = false; @@ -234,10 +234,6 @@ export default class TextureBase extends Asset { @property private _anisotropy = 1; - private _width: number = 0; - - private _height: number = 0; - private _sampler: GFXSampler | null = null; private _id: string; @@ -549,19 +545,22 @@ export default class TextureBase extends Asset { protected _recreateTexture () { this._destroyTexture(); - this._createTexture(); - if (this._texture) { - this._width = this._texture.width; - this._height = this._texture.height; + + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; } - this._createTextureView(); + + this._createTextureImpl(gfxDevice); + + this._createTextureViewImpl(gfxDevice); } - protected _createTexture () { + protected _createTextureImpl (gfxDevice: GFXDevice) { return; } - protected _createTextureView () { + protected _createTextureViewImpl (gfxDevice: GFXDevice) { return; } diff --git a/cocos/pipeline/forward/test-material-stage.ts b/cocos/pipeline/forward/test-material-stage.ts index 56f99511df..68b22e0a0a 100644 --- a/cocos/pipeline/forward/test-material-stage.ts +++ b/cocos/pipeline/forward/test-material-stage.ts @@ -9,6 +9,8 @@ import { GFXTextureView } from '../../gfx/texture-view'; import { RenderFlow } from '../render-flow'; import { RenderStage, RenderStageInfo } from '../render-stage'; import { RenderView } from '../render-view'; +import Texture2D from '../../assets/texture-2d'; +import ImageAsset from '../../assets/image-asset'; export class TestMaterialStage extends RenderStage { @@ -113,40 +115,51 @@ export class TestMaterialStage extends RenderStage { } private loadTexture (image: HTMLImageElement): boolean { - this._texture = this._device.createTexture({ - type: GFXTextureType.TEX2D, - usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, - format: GFXFormat.RGBA8, - width: image.width, - height: image.height, - flags: GFXTextureFlagBit.NONE, - }); + // this._texture = this._device.createTexture({ + // type: GFXTextureType.TEX2D, + // usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, + // format: GFXFormat.RGBA8, + // width: image.width, + // height: image.height, + // flags: GFXTextureFlagBit.NONE, + // }); + // if (!this._texture) { + // return false; + // } + + // this._texView = this._device.createTextureView({ + // texture: this._texture, + // type: GFXTextureViewType.TV2D, + // format: GFXFormat.RGBA8, + // }); + + // if (!this._texView) { + // return false; + // } + + // const region: GFXBufferTextureCopy = { + // buffOffset: 0, + // buffStride: 0, + // buffTexHeight: 0, + // texOffset: { x: 0, y: 0, z: 0 }, + // texExtent: { width: image.width, height: image.height, depth: 1 }, + // texSubres: { baseMipLevel: 0, levelCount: 1, baseArrayLayer: 0, layerCount: 1 }, + // }; + + // this._device.copyImageSourceToTexture(image, this._texture, [region]); + + const textureAsset = new Texture2D(); + textureAsset.image = new ImageAsset(image); + this._texture = textureAsset.getGFXTexture(); if (!this._texture) { return false; } - - this._texView = this._device.createTextureView({ - texture: this._texture, - type: GFXTextureViewType.TV2D, - format: GFXFormat.RGBA8, - }); - + this._texView = textureAsset.getGFXTextureView(); if (!this._texView) { return false; } - const region: GFXBufferTextureCopy = { - buffOffset: 0, - buffStride: 0, - buffTexHeight: 0, - texOffset: { x: 0, y: 0, z: 0 }, - texExtent: { width: image.width, height: image.height, depth: 1 }, - texSubres: { baseMipLevel: 0, levelCount: 1, baseArrayLayer: 0, layerCount: 1 }, - }; - - this._device.copyImageSourceToTexture(image, this._texture, [region]); - return true; } }