sampler from texture asset

This commit is contained in:
Yun Hsiao Wu
2019-01-23 13:53:17 +08:00
parent eedffd20dc
commit ba5ed365b2
7 changed files with 50 additions and 48 deletions
+9 -3
View File
@@ -213,9 +213,15 @@ export class Material extends Asset {
if (bindingType === GFXBindingType.UNIFORM_BUFFER) { if (bindingType === GFXBindingType.UNIFORM_BUFFER) {
pass.setUniform(handle, val); pass.setUniform(handle, val);
} else if (bindingType === GFXBindingType.SAMPLER) { } else if (bindingType === GFXBindingType.SAMPLER) {
const tv = val.getGFXTextureView(); const sampler = val.getGFXSampler();
if (!tv) { console.warn('texture asset incomplete!'); return; } const textureView = val.getGFXTextureView();
pass.bindTextureView(Pass.getBindingFromHandle(handle), tv); 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; return true;
} }
+27 -24
View File
@@ -17,7 +17,8 @@ class BuiltinResMgr {
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
const context = canvas.getContext('2d')!; const context = canvas.getContext('2d')!;
const imgAsset = new ImageAsset(canvas); const imgAsset = new ImageAsset(canvas);
canvas.width = canvas.height = 2; const l = canvas.width = canvas.height = 2;
const hl = l / 2;
// ============================ // ============================
// builtin textures // builtin textures
@@ -25,7 +26,7 @@ class BuiltinResMgr {
// black texture // black texture
context.fillStyle = '#000'; context.fillStyle = '#000';
context.fillRect(0, 0, 2, 2); context.fillRect(0, 0, l, l);
const blackTexture = new Texture2D(); const blackTexture = new Texture2D();
blackTexture._uuid = 'black-texture'; blackTexture._uuid = 'black-texture';
blackTexture.image = imgAsset; blackTexture.image = imgAsset;
@@ -33,16 +34,24 @@ class BuiltinResMgr {
// grey texture // grey texture
context.fillStyle = '#777'; context.fillStyle = '#777';
context.fillRect(0, 0, 2, 2); context.fillRect(0, 0, l, l);
const greyTexture = new Texture2D(); const greyTexture = new Texture2D();
greyTexture._uuid = 'grey-texture'; greyTexture._uuid = 'grey-texture';
greyTexture.image = imgAsset; greyTexture.image = imgAsset;
resources[greyTexture._uuid] = greyTexture; resources[greyTexture._uuid] = greyTexture;
// grey cube texture // white texture
// const greyCubeTexture = new TextureCube(); context.fillStyle = '#fff';
// greyCubeTexture._uuid = 'grey-cube-texture'; context.fillRect(0, 0, l, l);
// greyCubeTexture.image = { 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), // front: new ImageAsset(canvas),
// back: new ImageAsset(canvas), // back: new ImageAsset(canvas),
// left: new ImageAsset(canvas), // left: new ImageAsset(canvas),
@@ -50,19 +59,12 @@ class BuiltinResMgr {
// top: new ImageAsset(canvas), // top: new ImageAsset(canvas),
// bottom: new ImageAsset(canvas), // bottom: new ImageAsset(canvas),
// }; // };
// resources[greyCubeTexture._uuid] = greyCubeTexture; // whiteCubeTexture.onLoaded();
// resources[whiteCubeTexture._uuid] = whiteCubeTexture;
// 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;
// normal texture // normal texture
context.fillStyle = '#7f7fff'; context.fillStyle = '#7f7fff';
context.fillRect(0, 0, 2, 2); context.fillRect(0, 0, l, l);
const normalTexture = new Texture2D(); const normalTexture = new Texture2D();
normalTexture._uuid = 'normal-texture'; normalTexture._uuid = 'normal-texture';
normalTexture.image = imgAsset; normalTexture.image = imgAsset;
@@ -70,12 +72,14 @@ class BuiltinResMgr {
// default texture // default texture
context.fillStyle = '#ddd'; context.fillStyle = '#ddd';
context.fillRect(0, 0, 2, 2); context.fillRect(0, 0, l, l);
context.fillStyle = '#555'; context.fillStyle = '#555';
context.fillRect(0, 0, 1, 1); context.fillRect(0, 0, hl, hl);
context.fillStyle = '#555'; context.fillStyle = '#555';
context.fillRect(1, 1, 1, 1); context.fillRect(hl, hl, hl, hl);
const defaultTexture = new Texture2D(); 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.setWrapMode(Texture2D.WrapMode.REPEAT, Texture2D.WrapMode.REPEAT);
defaultTexture._uuid = 'default-texture'; defaultTexture._uuid = 'default-texture';
defaultTexture.image = imgAsset; defaultTexture.image = imgAsset;
@@ -92,6 +96,7 @@ class BuiltinResMgr {
// top: new ImageAsset(canvas), // top: new ImageAsset(canvas),
// bottom: new ImageAsset(canvas), // bottom: new ImageAsset(canvas),
// }; // };
// defaultCubeTexture.onLoaded();
// resources.push(defaultCubeTexture); // resources.push(defaultCubeTexture);
const spriteFrame = new SpriteFrame(); const spriteFrame = new SpriteFrame();
@@ -101,9 +106,7 @@ class BuiltinResMgr {
spriteFrame.setOriginalSize(cc.size(imgAsset.width, imgAsset.height)); spriteFrame.setOriginalSize(cc.size(imgAsset.width, imgAsset.height));
spriteFrame.setRect(new Rect(0, 0, imgAsset.width, imgAsset.height)); spriteFrame.setRect(new Rect(0, 0, imgAsset.width, imgAsset.height));
spriteFrame.image = imgAsset; spriteFrame.image = imgAsset;
if (spriteFrame.onLoaded) { spriteFrame.onLoaded();
spriteFrame.onLoaded();
}
resources[spriteFrame._uuid] = spriteFrame; resources[spriteFrame._uuid] = spriteFrame;
// default material // default material
@@ -123,7 +126,7 @@ class BuiltinResMgr {
} }
public get<T extends Asset> (uuid: string): T { public get<T extends Asset> (uuid: string): T {
return this._resources[uuid]; return this._resources[uuid] as T;
} }
} }
+5 -7
View File
@@ -30,7 +30,7 @@ import IDGenerator from '../core/utils/id-generator';
import {addon} from '../core/utils/js'; import {addon} from '../core/utils/js';
import { ccenum } from '../core/value-types/enum'; import { ccenum } from '../core/value-types/enum';
import { GFXAddress, GFXBufferTextureCopy, GFXFilter, GFXFormat, import { GFXAddress, GFXBufferTextureCopy, GFXFilter, GFXFormat,
GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define';
import { GFXDevice } from '../gfx/device'; import { GFXDevice } from '../gfx/device';
import { GFXSampler } from '../gfx/sampler'; import { GFXSampler } from '../gfx/sampler';
import { GFXTexture, IGFXTextureInfo } from '../gfx/texture'; import { GFXTexture, IGFXTextureInfo } from '../gfx/texture';
@@ -243,7 +243,6 @@ export class TextureBase extends Asset {
protected constructor (flipY: boolean = false) { protected constructor (flipY: boolean = false) {
super(); super();
// @ts-ignore
EventTarget.call(this); EventTarget.call(this);
this._flipY = flipY; this._flipY = flipY;
@@ -268,6 +267,7 @@ export class TextureBase extends Asset {
this._potientialHeight = height; this._potientialHeight = height;
this._format = format; this._format = format;
this._mipmapLevel = 1; this._mipmapLevel = 1;
this._updateSampler();
this._recreateTexture(); this._recreateTexture();
} }
@@ -558,19 +558,18 @@ export class TextureBase extends Asset {
protected _getTextureCreateInfo (): IGFXTextureInfo { protected _getTextureCreateInfo (): IGFXTextureInfo {
return { return {
type: GFXTextureType.TEX2D, type: GFXTextureType.TEX2D,
/* tslint:disable:no-bitwise */
usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST,
/* tslint:enable:no-bitwise */
format: toGfxFormat(this._format), format: toGfxFormat(this._format),
width: this._potientialWidth, width: this._potientialWidth,
height: this._potientialHeight, height: this._potientialHeight,
mipLevel: this._mipmapLevel, mipLevel: this._mipmapLevel,
flags: this._genMipmap ? GFXTextureFlagBit.GEN_MIPMAP : GFXTextureFlagBit.NONE,
}; };
} }
protected _getTextureViewCreateInfo (): IGFXTextureViewInfo { protected _getTextureViewCreateInfo (): IGFXTextureViewInfo {
return { return {
texture: this._texture, texture: this._texture!,
type: GFXTextureViewType.TV2D, type: GFXTextureViewType.TV2D,
format: this._getGfxFormat(), format: this._getGfxFormat(),
}; };
@@ -622,8 +621,7 @@ export class TextureBase extends Asset {
} }
} }
/* tslint:disable:no-string-literal */ cc.TextureBase = TextureBase;
cc['TextureBase'] = TextureBase;
/** /**
* !#zh * !#zh
+1 -1
View File
@@ -41,7 +41,7 @@ export class CCObject {
protected _destruct(): void; protected _destruct(): void;
protected _destroyImmediate(): void; protected _destroyImmediate(): void;
protected _serialize?(): any; protected _serialize?(exporting?: any): any;
protected _deserialize?(data: any, ctx: any): any; protected _deserialize?(data: any, ctx: any): any;
protected _onPreDestroy (): any; protected _onPreDestroy (): any;
protected Flags: object; protected Flags: object;
+3 -10
View File
@@ -185,18 +185,11 @@ export class Pass {
for (const u of info.samplers) { for (const u of info.samplers) {
this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding); 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 inf = info.properties && info.properties[u.name];
const name = inf && inf.value ? inf.value + '-texture' : _type2default[u.type]; const name = inf && inf.value ? inf.value + '-texture' : _type2default[u.type];
this._textureViews[u.binding] = builtinResMgr.get<TextureBase>(name).getGFXTextureView()!; const texture = builtinResMgr.get<TextureBase>(name);
this._samplers[u.binding] = texture.getGFXSampler()!;
this._textureViews[u.binding] = texture.getGFXTextureView()!;
} }
} }
+3 -2
View File
@@ -5,8 +5,8 @@ import { GFXGetTypeSize, GFXShaderType } from '../../gfx/define';
import { GFXDevice } from '../../gfx/device'; import { GFXDevice } from '../../gfx/device';
import { GFXShader, GFXUniform, GFXUniformBlock } from '../../gfx/shader'; import { GFXShader, GFXUniform, GFXUniformBlock } from '../../gfx/shader';
import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline';
import { IDefineMap } from './effect';
import { SkinningUBO } from '../models/model-uniforms'; import { SkinningUBO } from '../models/model-uniforms';
import { IDefineMap } from './effect';
function _generateDefines ( function _generateDefines (
device: GFXDevice, device: GFXDevice,
@@ -86,7 +86,7 @@ class ProgramLib {
def._map = ((value: any) => (value ? (1 << def._offset) : 0)); def._map = ((value: any) => (value ? (1 << def._offset) : 0));
} }
if (def.name === 'CC_USE_SKINNING') { if (def.name === 'CC_USE_SKINNING') {
tmpl.blocks = tmpl.blocks.concat(convertToBlockInfo(SkinningUBO.BLOCK)); tmpl.blocks = tmpl.blocks.concat(skinning);
} }
offset += cnt; offset += cnt;
def._offset = offset; def._offset = offset;
@@ -149,6 +149,7 @@ class ProgramLib {
const globals = convertToBlockInfo(UBOGlobal.BLOCK); const globals = convertToBlockInfo(UBOGlobal.BLOCK);
const locals = convertToBlockInfo(UBOLocal.BLOCK); const locals = convertToBlockInfo(UBOLocal.BLOCK);
const skinning = convertToBlockInfo(SkinningUBO.BLOCK);
function convertToUniformInfo (uniform: GFXUniform): IBlockMember { function convertToUniformInfo (uniform: GFXUniform): IBlockMember {
return { return {
+2 -1
View File
@@ -61,7 +61,7 @@
const texture = createTexture(cc.game.canvas.getElementsByTagName('img')); const texture = createTexture(cc.game.canvas.getElementsByTagName('img'));
const material = new cc.Material(); 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 // parsed effect file is embedded in cocos/3d/builtin/effects.js
material.effectName = 'builtin-effect-unlit'; material.effectName = 'builtin-effect-unlit';
@@ -76,6 +76,7 @@
modelCom.mesh = cc.utils.createMesh(cc.primitives[geomList[i % geomList.length]]()); modelCom.mesh = cc.utils.createMesh(cc.primitives[geomList[i % geomList.length]]());
modelNode.setPosition(0, 0, i * 2); modelNode.setPosition(0, 0, i * 2);
models.push(modelNode); models.push(modelNode);
// material instance initialized here
passes.push(modelCom.material.passes[0]); passes.push(modelCom.material.passes[0]);
} }
const handle = passes[0].getHandle('color'); const handle = passes[0].getHandle('color');