From 648bf5554747ecd9461318d397ee18d0ac4d7d17 Mon Sep 17 00:00:00 2001 From: zxx43 Date: Tue, 19 Sep 2023 16:08:43 +0800 Subject: [PATCH 1/2] getProperty to get material uniform --- cocos/asset/assets/material.ts | 50 ++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/cocos/asset/assets/material.ts b/cocos/asset/assets/material.ts index 8bdd53715ba..b8b7673f999 100644 --- a/cocos/asset/assets/material.ts +++ b/cocos/asset/assets/material.ts @@ -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 } from '../../core'; import { SRGBToLinear } from '../../rendering/pipeline-funcs'; import { Renderer } from '../../misc/renderer'; @@ -352,6 +352,52 @@ export class Material extends Asset { 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; } From 0467f361c369b4d3e8569338a0b450e115ece6a3 Mon Sep 17 00:00:00 2001 From: zxx43 Date: Tue, 19 Sep 2023 16:59:50 +0800 Subject: [PATCH 2/2] fix eslint --- cocos/asset/assets/material.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cocos/asset/assets/material.ts b/cocos/asset/assets/material.ts index b8b7673f999..b0d6312ee59 100644 --- a/cocos/asset/assets/material.ts +++ b/cocos/asset/assets/material.ts @@ -29,7 +29,7 @@ import { Texture, Type } from '../../gfx'; import { TextureBase } from './texture-base'; import { IPassInfoFull, Pass, PassOverrides } from '../../render-scene/core/pass'; import { MacroRecord, MaterialProperty, type2reader } from '../../render-scene/core/pass-utils'; -import { Color, warnID, Vec4, cclegacy, Vec2, Vec3, Mat3, Mat4 } from '../../core'; +import { Color, warnID, Vec4, cclegacy, Vec2, Vec3, Mat3, Mat4, warn } from '../../core'; import { SRGBToLinear } from '../../rendering/pipeline-funcs'; import { Renderer } from '../../misc/renderer'; @@ -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.`); } /** @@ -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.`); } /** @@ -312,7 +312,7 @@ 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; @@ -320,7 +320,7 @@ export class Material extends Asset { } } if (!success) { - console.warn(`illegal property name: ${name}.`); + warn(`illegal property name: ${name}.`); } } @@ -348,7 +348,7 @@ 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]; } } @@ -474,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); @@ -515,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) {