mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
material.setProperty
This commit is contained in:
+28
-13
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user