From 29576e8935a727e89f58272d547a885eae6a9502 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 7 Jan 2019 22:30:32 +0800 Subject: [PATCH] material.setProperty --- cocos/3d/assets/material.ts | 41 +++++++++++++------ cocos/pipeline/forward/test-material-stage.ts | 9 ++-- cocos/renderer/core/pass.ts | 18 ++++---- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index 0b14306c92..e401a4f50f 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -51,6 +51,8 @@ export class Material extends Asset { protected _defines: Record = {}; @property protected _props: Array> = []; + @property + protected _textures: Array> = []; protected _passes: Pass[] = []; protected _owner: any = null; @@ -88,26 +90,33 @@ export class Material extends Asset { this.update(); } - public setProperty(handle: number, val: any, passIdx = 0) { - if (this._props.length <= passIdx) { - console.warn('illegal pass index'); + public setProperty(name: string, val: any, passIdx = 0) { + if (passIdx >= this._passes.length) { + console.warn('illegal pass index.'); return; } this._props[passIdx][name] = val; const pass = this._passes[passIdx]; - if (pass) { - if (val instanceof TextureBase) { - pass.setProperty(handle, val._texture); - } else { - pass.setProperty(handle, val); - } + const handle = pass.getHandleFromName(name); + pass.setBlockMember(handle, val); + } + + public setTexture(name: string, val: TextureBase, passIdx = 0) { + if (passIdx >= this._passes.length) { + console.warn('illegal pass index.'); + return; } + this._textures[passIdx][name] = val; + const pass = this._passes[passIdx]; + const handle = pass.getHandleFromName(name); + pass.setTextureView(handle, val._texView); } public copy(mat: Material) { - this._props.length = mat._props.length; + this._props.length = this._textures.length = mat._props.length; for (let i = 0; i < mat._props.length; i++) { Object.assign(this._props[i], mat._props[i]); + Object.assign(this._textures[i], mat._textures[i]); } Object.assign(this._defines, mat._defines); this.update(mat.effectAsset); @@ -136,14 +145,20 @@ export class Material extends Asset { defines: this._defines, }); // handle property values - this._props.length = this._passes.length; + this._props.length = this._textures.length = this._passes.length; if (keepProps) { this._passes.forEach((pass, i) => { let props = this._props[i]; if (!props) { props = this._props[i] = {}; } for (const p of Object.keys(props)) { - const handle = pass.getPropertyHandle(p); - pass.setProperty(handle, props[p]); + const handle = pass.getHandleFromName(p); + pass.setBlockMember(handle, props[p]); + } + let textures = this._textures[i]; + if (!textures) { textures = this._textures[i] = {}; } + for (const t of Object.keys(textures)) { + const handle = pass.getHandleFromName(t); + pass.setTextureView(handle, textures[t]._texView); } }); } else { diff --git a/cocos/pipeline/forward/test-material-stage.ts b/cocos/pipeline/forward/test-material-stage.ts index bece72a16a..a3a645be25 100644 --- a/cocos/pipeline/forward/test-material-stage.ts +++ b/cocos/pipeline/forward/test-material-stage.ts @@ -73,10 +73,11 @@ export class TestMaterialStage extends RenderStage { if (!this._material.inited) { // material this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js - const handle1 = this._material.passes[0].getPropertyHandle('u_sampler'); - this._material.setProperty(handle1, this._texView); - const handle2 = this._material.passes[0].getPropertyHandle('u_color'); - this._material.setProperty(handle2, cc.color('#ffffff')); + const pass = this._material.passes[0]; + const handle1 = pass.getHandleFromName('u_sampler'); + pass.setTextureView(handle1, this._texView); + const handle2 = pass.getHandleFromName('u_color'); + pass.setBlockMember(handle2, cc.color('#ffffff')); this._material.inited = true; } const cmdBuff = this._cmdBuff; diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 602d16a917..2642e1d224 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -145,20 +145,24 @@ export class Pass { console.error('create sampler failed.'); } } + this._bindingLayout.update(); } - public getPropertyHandle(name: string) { + public getHandleFromName(name: string) { return this._handleMap[name]; } - public setProperty(handle: number, value: any) { + public setBlockMember(handle: number, value: any) { const [ binding, idx ] = parseHandle(handle); const block = this._blocks[binding]; - if (block) { - const type = this._typeMap[handle]; - _type2fn[type](block.views[idx], value); - this._buffers[binding].update(block.buffer); - } else if (this._bindingLayout) { + const type = this._typeMap[handle]; + _type2fn[type](block.views[idx], value); + this._buffers[binding].update(block.buffer); + } + + public setTextureView(handle: number, value: any) { + const [ binding ] = parseHandle(handle); + if (this._bindingLayout) { this._bindingLayout.bindTextureView(binding, value); this._bindingLayout.update(); }