Merge pull request #10 from YunHsiao/gfx

material.setProperty
This commit is contained in:
Weiliang Wang
2019-01-08 09:52:12 +08:00
committed by GitHub
3 changed files with 44 additions and 24 deletions
+28 -13
View File
@@ -51,6 +51,8 @@ export class Material extends Asset {
protected _defines: Record<string, number | boolean> = {};
@property
protected _props: Array<Record<string, any>> = [];
@property
protected _textures: Array<Record<string, any>> = [];
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 {
@@ -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 = <GFXCommandBuffer>this._cmdBuff;
+11 -7
View File
@@ -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();
}