diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index a740fad854..3a529ca937 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -213,9 +213,15 @@ export class Material extends Asset { if (bindingType === GFXBindingType.UNIFORM_BUFFER) { pass.setUniform(handle, val); } else if (bindingType === GFXBindingType.SAMPLER) { - const tv = val.getGFXTextureView(); - if (!tv) { console.warn('texture asset incomplete!'); return; } - pass.bindTextureView(Pass.getBindingFromHandle(handle), tv); + const sampler = val.getGFXSampler(); + const textureView = val.getGFXTextureView(); + if (!sampler || !textureView) { + console.warn('texture asset incomplete, missing ' + (sampler ? 'GFXTextureView' : 'GFXSampler')); + return false; + } + const binding = Pass.getBindingFromHandle(handle); + pass.bindSampler(binding, sampler); + pass.bindTextureView(binding, textureView); } return true; } diff --git a/cocos/3d/builtin/init.ts b/cocos/3d/builtin/init.ts index d13175649e..df370cfb56 100644 --- a/cocos/3d/builtin/init.ts +++ b/cocos/3d/builtin/init.ts @@ -17,7 +17,8 @@ class BuiltinResMgr { const canvas = document.createElement('canvas'); const context = canvas.getContext('2d')!; const imgAsset = new ImageAsset(canvas); - canvas.width = canvas.height = 2; + const l = canvas.width = canvas.height = 2; + const hl = l / 2; // ============================ // builtin textures @@ -25,7 +26,7 @@ class BuiltinResMgr { // black texture context.fillStyle = '#000'; - context.fillRect(0, 0, 2, 2); + context.fillRect(0, 0, l, l); const blackTexture = new Texture2D(); blackTexture._uuid = 'black-texture'; blackTexture.image = imgAsset; @@ -33,16 +34,24 @@ class BuiltinResMgr { // grey texture context.fillStyle = '#777'; - context.fillRect(0, 0, 2, 2); + context.fillRect(0, 0, l, l); const greyTexture = new Texture2D(); greyTexture._uuid = 'grey-texture'; greyTexture.image = imgAsset; resources[greyTexture._uuid] = greyTexture; - // grey cube texture - // const greyCubeTexture = new TextureCube(); - // greyCubeTexture._uuid = 'grey-cube-texture'; - // greyCubeTexture.image = { + // white texture + context.fillStyle = '#fff'; + context.fillRect(0, 0, l, l); + const whiteTexture = new Texture2D(); + whiteTexture._uuid = 'white-texture'; + whiteTexture.image = imgAsset; + resources[whiteTexture._uuid] = whiteTexture; + + // white cube texture + // const whiteCubeTexture = new TextureCube(); + // whiteCubeTexture._uuid = 'white-cube-texture'; + // whiteCubeTexture.image = { // front: new ImageAsset(canvas), // back: new ImageAsset(canvas), // left: new ImageAsset(canvas), @@ -50,19 +59,12 @@ class BuiltinResMgr { // top: new ImageAsset(canvas), // bottom: new ImageAsset(canvas), // }; - // resources[greyCubeTexture._uuid] = greyCubeTexture; - - // white texture - context.fillStyle = '#fff'; - context.fillRect(0, 0, 2, 2); - const whiteTexture = new Texture2D(); - whiteTexture._uuid = 'white-texture'; - whiteTexture.image = imgAsset; - resources[whiteTexture._uuid] = whiteTexture; + // whiteCubeTexture.onLoaded(); + // resources[whiteCubeTexture._uuid] = whiteCubeTexture; // normal texture context.fillStyle = '#7f7fff'; - context.fillRect(0, 0, 2, 2); + context.fillRect(0, 0, l, l); const normalTexture = new Texture2D(); normalTexture._uuid = 'normal-texture'; normalTexture.image = imgAsset; @@ -70,12 +72,14 @@ class BuiltinResMgr { // default texture context.fillStyle = '#ddd'; - context.fillRect(0, 0, 2, 2); + context.fillRect(0, 0, l, l); context.fillStyle = '#555'; - context.fillRect(0, 0, 1, 1); + context.fillRect(0, 0, hl, hl); context.fillStyle = '#555'; - context.fillRect(1, 1, 1, 1); + context.fillRect(hl, hl, hl, hl); const defaultTexture = new Texture2D(); + defaultTexture.setFilters(Texture2D.Filter.NEAREST, Texture2D.Filter.NEAREST); + defaultTexture.setMipFilter(Texture2D.Filter.NEAREST); defaultTexture.setWrapMode(Texture2D.WrapMode.REPEAT, Texture2D.WrapMode.REPEAT); defaultTexture._uuid = 'default-texture'; defaultTexture.image = imgAsset; @@ -92,6 +96,7 @@ class BuiltinResMgr { // top: new ImageAsset(canvas), // bottom: new ImageAsset(canvas), // }; + // defaultCubeTexture.onLoaded(); // resources.push(defaultCubeTexture); const spriteFrame = new SpriteFrame(); @@ -101,9 +106,7 @@ class BuiltinResMgr { spriteFrame.setOriginalSize(cc.size(imgAsset.width, imgAsset.height)); spriteFrame.setRect(new Rect(0, 0, imgAsset.width, imgAsset.height)); spriteFrame.image = imgAsset; - if (spriteFrame.onLoaded) { - spriteFrame.onLoaded(); - } + spriteFrame.onLoaded(); resources[spriteFrame._uuid] = spriteFrame; // default material @@ -123,7 +126,7 @@ class BuiltinResMgr { } public get (uuid: string): T { - return this._resources[uuid]; + return this._resources[uuid] as T; } } diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index 550eb22339..87406f1567 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -30,7 +30,7 @@ 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, - GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; + GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; import { GFXDevice } from '../gfx/device'; import { GFXSampler } from '../gfx/sampler'; import { GFXTexture, IGFXTextureInfo } from '../gfx/texture'; @@ -243,7 +243,6 @@ export class TextureBase extends Asset { protected constructor (flipY: boolean = false) { super(); - // @ts-ignore EventTarget.call(this); this._flipY = flipY; @@ -268,6 +267,7 @@ export class TextureBase extends Asset { this._potientialHeight = height; this._format = format; this._mipmapLevel = 1; + this._updateSampler(); this._recreateTexture(); } @@ -558,19 +558,18 @@ export 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, + flags: this._genMipmap ? GFXTextureFlagBit.GEN_MIPMAP : GFXTextureFlagBit.NONE, }; } protected _getTextureViewCreateInfo (): IGFXTextureViewInfo { return { - texture: this._texture, + texture: this._texture!, type: GFXTextureViewType.TV2D, format: this._getGfxFormat(), }; @@ -622,8 +621,7 @@ export class TextureBase extends Asset { } } -/* tslint:disable:no-string-literal */ -cc['TextureBase'] = TextureBase; +cc.TextureBase = TextureBase; /** * !#zh diff --git a/cocos/core/data/object.d.ts b/cocos/core/data/object.d.ts index 698ce0a7a5..96b70896d5 100644 --- a/cocos/core/data/object.d.ts +++ b/cocos/core/data/object.d.ts @@ -41,7 +41,7 @@ export class CCObject { protected _destruct(): void; protected _destroyImmediate(): void; - protected _serialize?(): any; + protected _serialize?(exporting?: any): any; protected _deserialize?(data: any, ctx: any): any; protected _onPreDestroy (): any; protected Flags: object; diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 6d207bd860..247d5cdfb6 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -185,18 +185,11 @@ export class Pass { for (const u of info.samplers) { this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding); - const sampler = device.createSampler({ - name: u.name, - // TODO: specify filter modes in effect - }); - if (sampler) { - this._samplers[u.binding] = sampler; - } else { - console.error('create sampler failed.'); - } const inf = info.properties && info.properties[u.name]; const name = inf && inf.value ? inf.value + '-texture' : _type2default[u.type]; - this._textureViews[u.binding] = builtinResMgr.get(name).getGFXTextureView()!; + const texture = builtinResMgr.get(name); + this._samplers[u.binding] = texture.getGFXSampler()!; + this._textureViews[u.binding] = texture.getGFXTextureView()!; } } diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index bb4f7964d3..2bfc0a874f 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -5,8 +5,8 @@ import { GFXGetTypeSize, GFXShaderType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { GFXShader, GFXUniform, GFXUniformBlock } from '../../gfx/shader'; import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; -import { IDefineMap } from './effect'; import { SkinningUBO } from '../models/model-uniforms'; +import { IDefineMap } from './effect'; function _generateDefines ( device: GFXDevice, @@ -86,7 +86,7 @@ class ProgramLib { def._map = ((value: any) => (value ? (1 << def._offset) : 0)); } if (def.name === 'CC_USE_SKINNING') { - tmpl.blocks = tmpl.blocks.concat(convertToBlockInfo(SkinningUBO.BLOCK)); + tmpl.blocks = tmpl.blocks.concat(skinning); } offset += cnt; def._offset = offset; @@ -149,6 +149,7 @@ class ProgramLib { const globals = convertToBlockInfo(UBOGlobal.BLOCK); const locals = convertToBlockInfo(UBOLocal.BLOCK); +const skinning = convertToBlockInfo(SkinningUBO.BLOCK); function convertToUniformInfo (uniform: GFXUniform): IBlockMember { return { diff --git a/playground/simple.html b/playground/simple.html index b7b956af0a..ed0738541c 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -61,7 +61,7 @@ const texture = createTexture(cc.game.canvas.getElementsByTagName('img')); const material = new cc.Material(); - material.setDefines({ USE_COLOR: true }); + material.setDefines({ USE_COLOR: true, USE_TEXTURE: true }); // parsed effect file is embedded in cocos/3d/builtin/effects.js material.effectName = 'builtin-effect-unlit'; @@ -76,6 +76,7 @@ modelCom.mesh = cc.utils.createMesh(cc.primitives[geomList[i % geomList.length]]()); modelNode.setPosition(0, 0, i * 2); models.push(modelNode); + // material instance initialized here passes.push(modelCom.material.passes[0]); } const handle = passes[0].getHandle('color');