Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify getProperty to get material uniform #16284

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 55 additions & 8 deletions cocos/asset/assets/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { EffectAsset } from './effect-asset';
import { Texture, Type } from '../../gfx';
import { TextureBase } from './texture-base';
import { IPassInfoFull, Pass, PassOverrides } from '../../render-scene/core/pass';
import { MacroRecord, MaterialProperty } from '../../render-scene/core/pass-utils';
import { Color, warnID, Vec4, cclegacy } from '../../core';
import { MacroRecord, MaterialProperty, type2reader } from '../../render-scene/core/pass-utils';
import { Color, warnID, Vec4, cclegacy, Vec2, Vec3, Mat3, Mat4, warn } from '../../core';
import { SRGBToLinear } from '../../rendering/pipeline-funcs';
import { Renderer } from '../../misc/renderer';

Expand Down Expand Up @@ -250,7 +250,7 @@ export class Material extends Asset {
* @param passIdx @en The pass to apply to. Will apply to all passes if not specified. @zh 要重编的 pass 索引,如果没有指定,则重编所有 pass。
*/
public recompileShaders (overrides: MacroRecord, passIdx?: number): void {
console.warn(`Shaders in material asset '${this.name}' cannot be modified at runtime, please instantiate the material first.`);
warn(`Shaders in material asset '${this.name}' cannot be modified at runtime, please instantiate the material first.`);
}

/**
Expand All @@ -260,7 +260,7 @@ export class Material extends Asset {
* @param passIdx The pass to apply to. Will apply to all passes if not specified.
*/
public overridePipelineStates (overrides: PassOverrides, passIdx?: number): void {
console.warn(`Pipeline states in material asset '${this.name}' cannot be modified at runtime, please instantiate the material first.`);
warn(`Pipeline states in material asset '${this.name}' cannot be modified at runtime, please instantiate the material first.`);
}

/**
Expand Down Expand Up @@ -312,15 +312,15 @@ export class Material extends Asset {
}
}
} else {
if (passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return; }
if (passIdx >= this._passes.length) { warn(`illegal pass index: ${passIdx}.`); return; }
const pass = this._passes[passIdx];
if (this._uploadProperty(pass, name, val)) {
this._props[pass.propertyIndex][name] = val;
success = true;
}
}
if (!success) {
console.warn(`illegal property name: ${name}.`);
warn(`illegal property name: ${name}.`);
}
}

Expand Down Expand Up @@ -348,10 +348,56 @@ export class Material extends Asset {
if (name in props) { return props[name]; }
}
} else {
if (passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return null; }
if (passIdx >= this._passes.length) { warn(`illegal pass index: ${passIdx}.`); return null; }
const props = this._props[this._passes[passIdx].propertyIndex];
if (name in props) { return props[name]; }
}
const passid = passIdx;
let pass: Pass | null = null;
if (passid !== undefined) {
pass = this._passes[passid];
} else {
for (let i = 0; i < this._passes.length; ++i) {
const handle = this._passes[i].getHandle(name);
if (handle !== 0) {
pass = this._passes[i];
break;
}
}
}
if (pass) {
const handle = pass.getHandle(name);
if (handle !== 0) {
const type = Pass.getTypeFromHandle(handle);
if (type2reader[type]) {
if (type as Type === Type.INT || type as Type === Type.FLOAT) {
const result: number = 0;
pass.getUniform(handle, result);
return result;
} else if (type as Type === Type.INT2 || type as Type === Type.FLOAT2) {
const result: Vec2 = new Vec2();
pass.getUniform(handle, result);
return result;
} else if (type as Type === Type.INT3 || type as Type === Type.FLOAT3) {
const result: Vec3 = new Vec3();
pass.getUniform(handle, result);
return result;
} else if (type as Type === Type.INT4 || type as Type === Type.FLOAT4) {
const result: Vec4 = new Vec4();
pass.getUniform(handle, result);
return result;
} else if (type as Type === Type.MAT3) {
const result: Mat3 = new Mat3();
pass.getUniform(handle, result);
return result;
} else if (type as Type === Type.MAT4) {
const result: Mat4 = new Mat4();
pass.getUniform(handle, result);
return result;
}
}
}
}
return null;
}

Expand Down Expand Up @@ -428,6 +474,7 @@ export class Material extends Asset {
Object.assign(defines, passInfo.embeddedMacros);
}
if (passInfo.switch && !defines[passInfo.switch]) { continue; }
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const pass = new Pass(cclegacy.director.root);
pass.initialize(passInfo);
passes.push(pass);
Expand Down Expand Up @@ -469,7 +516,7 @@ export class Material extends Asset {
const handle = pass.getHandle(name);
if (!handle) { return false; }
const type = Pass.getTypeFromHandle(handle);
if (type < Type.SAMPLER1D) {
if (type < (Type.SAMPLER1D as number)) {
if (Array.isArray(val)) {
pass.setUniformArray(handle, val as MaterialProperty[]);
} else if (val !== null) {
Expand Down