From 9d03bf48095b8d712672ce24e70d38f7ade2384f Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 21 Mar 2019 18:18:01 +0800 Subject: [PATCH] generate mipmap depend on mip filter (#4081) --- cocos/3d/builtin/init.ts | 6 +-- cocos/3d/framework/camera-component.ts | 2 +- cocos/assets/texture-base.ts | 52 ++++++-------------------- cocos/renderer/core/sampler-lib.ts | 19 +++++++++- cocos/renderer/scene/planar-shadow.ts | 12 +----- 5 files changed, 36 insertions(+), 55 deletions(-) diff --git a/cocos/3d/builtin/init.ts b/cocos/3d/builtin/init.ts index 26255b294c..99f2fa6750 100644 --- a/cocos/3d/builtin/init.ts +++ b/cocos/3d/builtin/init.ts @@ -35,7 +35,7 @@ class BuiltinResMgr { // black texture const blackCubeTexture = new TextureCube(); blackCubeTexture._uuid = 'black-cube-texture'; - blackCubeTexture.setGenMipmap(true); + blackCubeTexture.setMipFilter(TextureCube.Filter.LINEAR); blackCubeTexture.image = { front: new ImageAsset(canvas), back: new ImageAsset(canvas), @@ -65,7 +65,7 @@ class BuiltinResMgr { // white cube texture const whiteCubeTexture = new TextureCube(); whiteCubeTexture._uuid = 'white-cube-texture'; - whiteCubeTexture.setGenMipmap(true); + whiteCubeTexture.setMipFilter(TextureCube.Filter.LINEAR); whiteCubeTexture.image = { front: new ImageAsset(canvas), back: new ImageAsset(canvas), @@ -99,7 +99,7 @@ class BuiltinResMgr { // default cube texture const defaultCubeTexture = new TextureCube(); - defaultCubeTexture.setGenMipmap(true); + defaultCubeTexture.setMipFilter(TextureCube.Filter.LINEAR); defaultCubeTexture._uuid = 'default-cube-texture'; defaultCubeTexture.image = { front: new ImageAsset(canvas), diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index 3517b22185..dd7e20ddf9 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -103,7 +103,7 @@ export class CameraComponent extends Component { @property protected _screenScale = 1; @property - protected _targetDisplay = -1; + protected _targetDisplay = 0; protected _camera: Camera | null = null; diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index 9b01c98083..635785e39a 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -129,6 +129,7 @@ export enum WrapMode { * @enum {number} */ export enum Filter { + NONE = GFXFilter.NONE, /** * Specifies linear filtering. */ @@ -180,9 +181,6 @@ export class TextureBase extends EventTargetFactory(Asset) { return texture._format >= PixelFormat.RGB_PVRTC_2BPPV1 && texture._format <= PixelFormat.RGBA_PVRTC_4BPPV1; } - @property - protected _genMipmap = false; - @property protected _format: number = PixelFormat.RGBA8888; @@ -199,7 +197,7 @@ export class TextureBase extends EventTargetFactory(Asset) { protected _magFilter: number = Filter.LINEAR; @property - protected _mipFilter: number = Filter.LINEAR; + protected _mipFilter: number = Filter.NONE; @property protected _wrapS: number = WrapMode.REPEAT; @@ -294,16 +292,6 @@ export class TextureBase extends EventTargetFactory(Asset) { return this._anisotropy; } - /** - * !#en - * Whether or not to generate mipmap. - * !#zh 检查纹理在上传 GPU 时是否生成 mipmap。 - * @return - */ - public genMipmap () { - return this._genMipmap || false; - } - /** * !#en Sets the wrap s and wrap t options.
* If the texture size is NPOT (non power of 2), then in can only use gl.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}. @@ -314,10 +302,10 @@ export class TextureBase extends EventTargetFactory(Asset) { * @param wrapR */ public setWrapMode (wrapS: WrapMode, wrapT: WrapMode, wrapR?: WrapMode) { - if (wrapS !== this._wrapS) { this._wrapS = wrapS; this._samplerInfo[SamplerInfoIndex.addressU] = wrapS; } - if (wrapT !== this._wrapT) { this._wrapT = wrapT; this._samplerInfo[SamplerInfoIndex.addressV] = wrapT; } + this._wrapS = wrapS; this._samplerInfo[SamplerInfoIndex.addressU] = wrapS; + this._wrapT = wrapT; this._samplerInfo[SamplerInfoIndex.addressV] = wrapT; if (wrapR === undefined) { return; } - if (wrapR !== this._wrapR) { this._wrapR = wrapR; this._samplerInfo[SamplerInfoIndex.addressW] = wrapR; } + this._wrapR = wrapR; this._samplerInfo[SamplerInfoIndex.addressW] = wrapR; } /** @@ -327,8 +315,8 @@ export class TextureBase extends EventTargetFactory(Asset) { * @param magFilter */ public setFilters (minFilter: Filter, magFilter: Filter) { - if (minFilter !== this._minFilter) { this._minFilter = minFilter; this._samplerInfo[SamplerInfoIndex.minFilter] = minFilter; } - if (magFilter !== this._magFilter) { this._magFilter = magFilter; this._samplerInfo[SamplerInfoIndex.magFilter] = magFilter; } + this._minFilter = minFilter; this._samplerInfo[SamplerInfoIndex.minFilter] = minFilter; + this._magFilter = magFilter; this._samplerInfo[SamplerInfoIndex.magFilter] = magFilter; } /** @@ -337,7 +325,7 @@ export class TextureBase extends EventTargetFactory(Asset) { * @param mipFilter */ public setMipFilter (mipFilter: Filter) { - if (mipFilter !== this._mipFilter) { this._mipFilter = mipFilter; this._samplerInfo[SamplerInfoIndex.mipFilter] = mipFilter; } + this._mipFilter = mipFilter; this._samplerInfo[SamplerInfoIndex.mipFilter] = mipFilter; } /** @@ -366,21 +354,7 @@ export class TextureBase extends EventTargetFactory(Asset) { * @param anisotropy */ public setAnisotropy (anisotropy: number) { - if (anisotropy !== this._anisotropy) { this._anisotropy = anisotropy; this._samplerInfo[SamplerInfoIndex.maxAnisotropy] = anisotropy; } - } - - /** - * !#en - * Sets whether generate mipmaps for the texture - * !#zh 是否为纹理设置生成 mipmaps。 - * @param mipmap - */ - public setGenMipmap (mipmap: boolean) { - if (this._genMipmap !== mipmap) { - this._genMipmap = mipmap; - this._recreateTexture(); - this.updateImage(); - } + this._anisotropy = anisotropy; this._samplerInfo[SamplerInfoIndex.maxAnisotropy] = anisotropy; } public destroy () { @@ -411,8 +385,7 @@ export class TextureBase extends EventTargetFactory(Asset) { this._wrapS + ',' + this._wrapT + ',' + (this._premultiplyAlpha ? 1 : 0) + ',' + this._mipFilter + ',' + this._anisotropy + ',' + - (this._flipY ? 1 : 0) + ',' + - (this._genMipmap ? 1 : 0); + (this._flipY ? 1 : 0); } /** @@ -435,9 +408,8 @@ export class TextureBase extends EventTargetFactory(Asset) { this.setMipFilter(parseInt(fields[6])); this.setAnisotropy(parseInt(fields[7])); } - if (fields.length >= 10) { + if (fields.length >= 9) { this._flipY = fields[8].charCodeAt(0) === CHAR_CODE_1; - this._genMipmap = fields[9].charCodeAt(0) === CHAR_CODE_1; } } @@ -543,7 +515,7 @@ export class TextureBase extends EventTargetFactory(Asset) { width: this._potientialWidth, height: this._potientialHeight, mipLevel: this._mipmapLevel, - flags: this._genMipmap ? GFXTextureFlagBit.GEN_MIPMAP : GFXTextureFlagBit.NONE, + flags: this._mipFilter !== Filter.NONE ? GFXTextureFlagBit.GEN_MIPMAP : GFXTextureFlagBit.NONE, }; } diff --git a/cocos/renderer/core/sampler-lib.ts b/cocos/renderer/core/sampler-lib.ts index 9c387618d9..ce83aab033 100644 --- a/cocos/renderer/core/sampler-lib.ts +++ b/cocos/renderer/core/sampler-lib.ts @@ -1,5 +1,6 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. +import { GFXAddress, GFXComparisonFunc, GFXFilter } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { GFXSampler, IGFXSamplerInfo } from '../../gfx/sampler'; @@ -19,13 +20,29 @@ export enum SamplerInfoIndex { total = borderColor + 4, } +const defaultInfo = [ + GFXFilter.LINEAR, + GFXFilter.LINEAR, + GFXFilter.NONE, + GFXAddress.WRAP, + GFXAddress.WRAP, + GFXAddress.WRAP, + 16, + GFXComparisonFunc.NEVER, + 0, 1000, 0, + 0, 0, 0, 0, +]; + const gfxInfo: IGFXSamplerInfo = {}; class SamplerLib { protected _cache: Record = {}; public getSampler (device: GFXDevice, info: number[]) { - const hash = info.toString(); + let hash = ''; + for (let i = 0; i < defaultInfo.length; i++) { + hash += (info[i] || defaultInfo[i]) + ','; + } const cache = this._cache[hash]; if (cache) { return cache; } gfxInfo.minFilter = info[SamplerInfoIndex.minFilter]; diff --git a/cocos/renderer/scene/planar-shadow.ts b/cocos/renderer/scene/planar-shadow.ts index 267b2f743e..f4c2e12a06 100644 --- a/cocos/renderer/scene/planar-shadow.ts +++ b/cocos/renderer/scene/planar-shadow.ts @@ -25,13 +25,6 @@ export class PlanarShadow { return this._distance; } - set offset (val: number) { - this._offset = val; - } - get offset () { - return this._offset; - } - set shadowColor (color: Color) { color4.array(this._data, color, UBOShadow.SHADOW_COLOR_OFFSET); } @@ -43,7 +36,6 @@ export class PlanarShadow { protected _scene: RenderScene; protected _normal = new Vec3(0, 1, 0); protected _distance = 0; - protected _offset = 0.01; protected _data = Float32Array.from([ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, // matLightPlaneProj 0.3, 0.3, 0.3, 1.0, // shadowColor @@ -56,7 +48,7 @@ export class PlanarShadow { // tslint:disable: one-variable-per-declaration public updateSphereLight (light: SphereLight) { light.node.getWorldPosition(_v3); - const n = this._normal, d = this._distance + this._offset; + const n = this._normal, d = this._distance; const NdL = vec3.dot(n, _v3); const lx = _v3.x, ly = _v3.y, lz = _v3.z; const nx = n.x, ny = n.y, nz = n.z; @@ -82,7 +74,7 @@ export class PlanarShadow { public updateDirLight (light: DirectionalLight) { light.node.getWorldRotation(_qt); vec3.transformQuat(_v3, _forward, _qt); - const n = this._normal, d = this._distance + this._offset; + const n = this._normal, d = this._distance; const NdL = vec3.dot(n, _v3), scale = 1 / NdL; const lx = _v3.x * scale, ly = _v3.y * scale, lz = _v3.z * scale; const nx = n.x, ny = n.y, nz = n.z;