From 2ce7334951519b970f018883cf298ba43f2e996f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 23 Aug 2023 20:27:12 -0700 Subject: [PATCH 01/19] add new export --- packages/core/src/shader/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/shader/index.ts b/packages/core/src/shader/index.ts index adc58156f3..9a7502ab1c 100644 --- a/packages/core/src/shader/index.ts +++ b/packages/core/src/shader/index.ts @@ -13,5 +13,7 @@ export { ShaderMacro } from "./ShaderMacro"; export { ShaderPass } from "./ShaderPass"; export { ShaderProperty } from "./ShaderProperty"; export { ShaderTagKey } from "./ShaderTagKey"; +export { ShaderUniformType } from "./ShaderUniformType"; export { SubShader } from "./SubShader"; +export { uniform } from "./ShaderUniformDecorator"; export * from "./state"; From 42bc5295bcee1c4f41edb186cf54d05fee78d9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 23 Aug 2023 20:28:12 -0700 Subject: [PATCH 02/19] Create ShaderUniformDecorator.ts --- .../core/src/shader/ShaderUniformDecorator.ts | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 packages/core/src/shader/ShaderUniformDecorator.ts diff --git a/packages/core/src/shader/ShaderUniformDecorator.ts b/packages/core/src/shader/ShaderUniformDecorator.ts new file mode 100644 index 0000000000..ae87a84ead --- /dev/null +++ b/packages/core/src/shader/ShaderUniformDecorator.ts @@ -0,0 +1,74 @@ +import { Material } from "../material/Material"; +import { ShaderProperty } from "./ShaderProperty"; +import { ShaderUniformType } from "./ShaderUniformType"; + +export type UniformOptions = { + varName?: string; // the variable name in glsl + macroName?: string; // whether enable or disable a macro in glsl depends on setting value + keepRef?: boolean; // whether setting a value to a property will change reference or just copy value. Only available for vector or matrix type +} + +function handleMacro(value: any, macroName?: string) { + if (macroName) { + if (value !== undefined) { + this.shaderData.enableMacro(macroName); + } else { + this.shaderData.disableMacro(macroName); + } + } +} + +/** + * Shader uniform decorator. + */ +export function uniform(type: ShaderUniformType, options?: UniformOptions) { + return function (target: Material, propertyKey: string) { + const shaderProp = ShaderProperty.getByName(options?.varName || propertyKey); + const get = "get" + type; + const set = "set" + type; + + let setFunc: (value: any) => void; + if (type === ShaderUniformType.Float || type === ShaderUniformType.Int || type === ShaderUniformType.Texture || type === ShaderUniformType.TextureArray) { + setFunc = function (value: any) { + this.shaderData[set](shaderProp, value); + handleMacro(value, options?.macroName); + } + } else if (options?.keepRef) { + if (type === ShaderUniformType.FloatArray || type === ShaderUniformType.IntArray) { + setFunc = function (value: any) { + let data = this.shaderData[get](shaderProp); + if (!data) { + this.shaderData[set](shaderProp, value); + data = value; + } + data.set(value); + handleMacro(value, options?.macroName); + } + } else if (type === ShaderUniformType.Vector2 || type === ShaderUniformType.Vector3 || type === ShaderUniformType.Vector4 || type === ShaderUniformType.Matrix || type === ShaderUniformType.Color) { + setFunc = function (value: any) { + let data = this.shaderData[get](shaderProp); + if (!data) { + this.shaderData[set](shaderProp, value); + data = value; + } + data.copyFrom(value); + handleMacro(value, options?.macroName); + } + } + } else { + setFunc = function (value: any) { + this.shaderData[set](shaderProp, value); + handleMacro(value, options?.macroName); + } + } + + Reflect.defineProperty(target, propertyKey, { + get: function () { + return this.shaderData[get](shaderProp); + }, + set: setFunc!, + enumerable: false, + configurable: true, + }); + }; +} From 767caf8b22951ef68b98045437c5edbb63626033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 23 Aug 2023 20:28:53 -0700 Subject: [PATCH 03/19] Create ShaderUniformType.ts --- packages/core/src/shader/ShaderUniformType.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 packages/core/src/shader/ShaderUniformType.ts diff --git a/packages/core/src/shader/ShaderUniformType.ts b/packages/core/src/shader/ShaderUniformType.ts new file mode 100644 index 0000000000..276a41d941 --- /dev/null +++ b/packages/core/src/shader/ShaderUniformType.ts @@ -0,0 +1,13 @@ +export enum ShaderUniformType { + Float = "Float", + Texture = "Texture", + Int = "Int", + FloatArray = "FloatArray", + IntArray = "IntArray", + TextureArray = "TextureArray", + Vector2 = "Vector2", + Vector3 = "Vector3", + Vector4 = "Vector4", + Color = "Color", + Matrix = "Matrix", +} From 336fe7790ec8a2b1b2d923d9d2b2d37204ce4a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 23 Aug 2023 23:00:00 -0700 Subject: [PATCH 04/19] fix code style --- .../core/src/shader/ShaderUniformDecorator.ts | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/core/src/shader/ShaderUniformDecorator.ts b/packages/core/src/shader/ShaderUniformDecorator.ts index ae87a84ead..7d8be62413 100644 --- a/packages/core/src/shader/ShaderUniformDecorator.ts +++ b/packages/core/src/shader/ShaderUniformDecorator.ts @@ -6,7 +6,7 @@ export type UniformOptions = { varName?: string; // the variable name in glsl macroName?: string; // whether enable or disable a macro in glsl depends on setting value keepRef?: boolean; // whether setting a value to a property will change reference or just copy value. Only available for vector or matrix type -} +}; function handleMacro(value: any, macroName?: string) { if (macroName) { @@ -28,11 +28,16 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { const set = "set" + type; let setFunc: (value: any) => void; - if (type === ShaderUniformType.Float || type === ShaderUniformType.Int || type === ShaderUniformType.Texture || type === ShaderUniformType.TextureArray) { + if ( + type === ShaderUniformType.Float || + type === ShaderUniformType.Int || + type === ShaderUniformType.Texture || + type === ShaderUniformType.TextureArray + ) { setFunc = function (value: any) { this.shaderData[set](shaderProp, value); handleMacro(value, options?.macroName); - } + }; } else if (options?.keepRef) { if (type === ShaderUniformType.FloatArray || type === ShaderUniformType.IntArray) { setFunc = function (value: any) { @@ -43,8 +48,14 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { } data.set(value); handleMacro(value, options?.macroName); - } - } else if (type === ShaderUniformType.Vector2 || type === ShaderUniformType.Vector3 || type === ShaderUniformType.Vector4 || type === ShaderUniformType.Matrix || type === ShaderUniformType.Color) { + }; + } else if ( + type === ShaderUniformType.Vector2 || + type === ShaderUniformType.Vector3 || + type === ShaderUniformType.Vector4 || + type === ShaderUniformType.Matrix || + type === ShaderUniformType.Color) + { setFunc = function (value: any) { let data = this.shaderData[get](shaderProp); if (!data) { @@ -53,13 +64,13 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { } data.copyFrom(value); handleMacro(value, options?.macroName); - } + }; } } else { setFunc = function (value: any) { this.shaderData[set](shaderProp, value); handleMacro(value, options?.macroName); - } + }; } Reflect.defineProperty(target, propertyKey, { From 574bb2ebbc6434802e00a14022f1b6f898c6779b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 23 Aug 2023 23:00:25 -0700 Subject: [PATCH 05/19] code style --- packages/core/src/shader/ShaderUniformType.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/shader/ShaderUniformType.ts b/packages/core/src/shader/ShaderUniformType.ts index 276a41d941..94a36fd332 100644 --- a/packages/core/src/shader/ShaderUniformType.ts +++ b/packages/core/src/shader/ShaderUniformType.ts @@ -9,5 +9,5 @@ export enum ShaderUniformType { Vector3 = "Vector3", Vector4 = "Vector4", Color = "Color", - Matrix = "Matrix", + Matrix = "Matrix" } From 2f93bc7c82e8926dbd9e07762a2c8cfbe20be69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 23 Aug 2023 23:02:46 -0700 Subject: [PATCH 06/19] Update ShaderUniformDecorator.ts --- packages/core/src/shader/ShaderUniformDecorator.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/shader/ShaderUniformDecorator.ts b/packages/core/src/shader/ShaderUniformDecorator.ts index 7d8be62413..f6f553b841 100644 --- a/packages/core/src/shader/ShaderUniformDecorator.ts +++ b/packages/core/src/shader/ShaderUniformDecorator.ts @@ -54,8 +54,8 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { type === ShaderUniformType.Vector3 || type === ShaderUniformType.Vector4 || type === ShaderUniformType.Matrix || - type === ShaderUniformType.Color) - { + type === ShaderUniformType.Color + ) { setFunc = function (value: any) { let data = this.shaderData[get](shaderProp); if (!data) { @@ -79,7 +79,7 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { }, set: setFunc!, enumerable: false, - configurable: true, + configurable: true }); }; } From 0bc733afc3ed2cc0803f90aa501af603895ae75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Thu, 31 Aug 2023 23:52:16 -0700 Subject: [PATCH 07/19] Update ShaderUniformDecorator.ts --- .../core/src/shader/ShaderUniformDecorator.ts | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/packages/core/src/shader/ShaderUniformDecorator.ts b/packages/core/src/shader/ShaderUniformDecorator.ts index f6f553b841..7144bba6e6 100644 --- a/packages/core/src/shader/ShaderUniformDecorator.ts +++ b/packages/core/src/shader/ShaderUniformDecorator.ts @@ -6,7 +6,7 @@ export type UniformOptions = { varName?: string; // the variable name in glsl macroName?: string; // whether enable or disable a macro in glsl depends on setting value keepRef?: boolean; // whether setting a value to a property will change reference or just copy value. Only available for vector or matrix type -}; +} function handleMacro(value: any, macroName?: string) { if (macroName) { @@ -28,16 +28,11 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { const set = "set" + type; let setFunc: (value: any) => void; - if ( - type === ShaderUniformType.Float || - type === ShaderUniformType.Int || - type === ShaderUniformType.Texture || - type === ShaderUniformType.TextureArray - ) { + if (type === ShaderUniformType.Float || type === ShaderUniformType.Int || type === ShaderUniformType.Texture || type === ShaderUniformType.TextureArray) { setFunc = function (value: any) { this.shaderData[set](shaderProp, value); - handleMacro(value, options?.macroName); - }; + handleMacro.call(this, value, options?.macroName); + } } else if (options?.keepRef) { if (type === ShaderUniformType.FloatArray || type === ShaderUniformType.IntArray) { setFunc = function (value: any) { @@ -47,15 +42,9 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { data = value; } data.set(value); - handleMacro(value, options?.macroName); - }; - } else if ( - type === ShaderUniformType.Vector2 || - type === ShaderUniformType.Vector3 || - type === ShaderUniformType.Vector4 || - type === ShaderUniformType.Matrix || - type === ShaderUniformType.Color - ) { + handleMacro.call(this, value, options?.macroName); + } + } else if (type === ShaderUniformType.Vector2 || type === ShaderUniformType.Vector3 || type === ShaderUniformType.Vector4 || type === ShaderUniformType.Matrix || type === ShaderUniformType.Color) { setFunc = function (value: any) { let data = this.shaderData[get](shaderProp); if (!data) { @@ -63,14 +52,14 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { data = value; } data.copyFrom(value); - handleMacro(value, options?.macroName); - }; + handleMacro.call(this, value, options?.macroName); + } } } else { setFunc = function (value: any) { this.shaderData[set](shaderProp, value); - handleMacro(value, options?.macroName); - }; + handleMacro.call(this, value, options?.macroName); + } } Reflect.defineProperty(target, propertyKey, { @@ -79,7 +68,7 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { }, set: setFunc!, enumerable: false, - configurable: true + configurable: true, }); }; } From 94038069ef5ad97b2a6ac6d59b685f3c6719896c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Thu, 31 Aug 2023 23:53:15 -0700 Subject: [PATCH 08/19] Update BlinnPhongMaterial.ts --- .../core/src/material/BlinnPhongMaterial.ts | 162 ++++++------------ 1 file changed, 49 insertions(+), 113 deletions(-) diff --git a/packages/core/src/material/BlinnPhongMaterial.ts b/packages/core/src/material/BlinnPhongMaterial.ts index 7c645d112d..fcf154cd48 100644 --- a/packages/core/src/material/BlinnPhongMaterial.ts +++ b/packages/core/src/material/BlinnPhongMaterial.ts @@ -1,5 +1,6 @@ import { Color, Vector4 } from "@galacean/engine-math"; import { Engine } from "../Engine"; +import { ShaderUniformType, uniform } from "../shader"; import { Shader } from "../shader/Shader"; import { ShaderProperty } from "../shader/ShaderProperty"; import { Texture2D } from "../texture/Texture2D"; @@ -9,151 +10,93 @@ import { BaseMaterial } from "./BaseMaterial"; * Blinn-phong Material. */ export class BlinnPhongMaterial extends BaseMaterial { - private static _specularColorProp = ShaderProperty.getByName("material_SpecularColor"); - private static _shininessProp = ShaderProperty.getByName("material_Shininess"); - private static _specularTextureProp = ShaderProperty.getByName("material_SpecularTexture"); - /** * Base color. */ - get baseColor(): Color { - return this.shaderData.getColor(BlinnPhongMaterial._baseColorProp); - } - - set baseColor(value: Color) { - const baseColor = this.shaderData.getColor(BlinnPhongMaterial._baseColorProp); - if (value !== baseColor) { - baseColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_BaseColor", + keepRef: true + }) + baseColor: Color = new Color(1, 1, 1, 1); /** * Base texture. */ - get baseTexture(): Texture2D { - return this.shaderData.getTexture(BlinnPhongMaterial._baseTextureProp); - } - - set baseTexture(value: Texture2D) { - this.shaderData.setTexture(BlinnPhongMaterial._baseTextureProp, value); - if (value) { - this.shaderData.enableMacro(BlinnPhongMaterial._baseTextureMacro); - } else { - this.shaderData.disableMacro(BlinnPhongMaterial._baseTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_BaseColor", + macroName: "MATERIAL_HAS_BASETEXTURE" + }) + baseTexture: Texture2D; /** * Specular color. */ - get specularColor(): Color { - return this.shaderData.getColor(BlinnPhongMaterial._specularColorProp); - } - - set specularColor(value: Color) { - const specularColor = this.shaderData.getColor(BlinnPhongMaterial._specularColorProp); - if (value !== specularColor) { - specularColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_SpecularColor", + keepRef: true + }) + specularColor: Color = new Color(1, 1, 1, 1); /** * Specular texture. */ - get specularTexture(): Texture2D { - return this.shaderData.getTexture(BlinnPhongMaterial._specularTextureProp); - } - - set specularTexture(value: Texture2D) { - this.shaderData.setTexture(BlinnPhongMaterial._specularTextureProp, value); - if (value) { - this.shaderData.enableMacro("MATERIAL_HAS_SPECULAR_TEXTURE"); - } else { - this.shaderData.disableMacro("MATERIAL_HAS_SPECULAR_TEXTURE"); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_SpecularTexture", + macroName: "MATERIAL_HAS_SPECULAR_TEXTURE" + }) + specularTexture: Texture2D; /** * Emissive color. */ - get emissiveColor(): Color { - return this.shaderData.getColor(BlinnPhongMaterial._emissiveColorProp); - } - - set emissiveColor(value: Color) { - const emissiveColor = this.shaderData.getColor(BlinnPhongMaterial._emissiveColorProp); - if (value !== emissiveColor) { - emissiveColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_EmissiveColor", + keepRef: true + }) + emissiveColor: Color = new Color(0, 0, 0, 1); /** * Emissive texture. */ - get emissiveTexture(): Texture2D { - return this.shaderData.getTexture(BlinnPhongMaterial._emissiveTextureProp); - } - - set emissiveTexture(value: Texture2D) { - this.shaderData.setTexture(BlinnPhongMaterial._emissiveTextureProp, value); - if (value) { - this.shaderData.enableMacro(BlinnPhongMaterial._emissiveTextureMacro); - } else { - this.shaderData.disableMacro(BlinnPhongMaterial._emissiveTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_EmissiveTexture", + macroName: "MATERIAL_HAS_EMISSIVETEXTURE" + }) + emissiveTexture: Texture2D; /** * Normal texture. */ - get normalTexture(): Texture2D { - return this.shaderData.getTexture(BlinnPhongMaterial._normalTextureProp); - } - - set normalTexture(value: Texture2D) { - this.shaderData.setTexture(BlinnPhongMaterial._normalTextureProp, value); - if (value) { - this.shaderData.enableMacro(BlinnPhongMaterial._normalTextureMacro); - } else { - this.shaderData.disableMacro(BlinnPhongMaterial._normalTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_NormalTexture", + macroName: "MATERIAL_HAS_NORMALTEXTURE" + }) + normalTexture: Texture2D; /** * Normal texture intensity. */ - get normalIntensity(): number { - return this.shaderData.getFloat(BlinnPhongMaterial._normalIntensityProp); - } - - set normalIntensity(value: number) { - this.shaderData.setFloat(BlinnPhongMaterial._normalIntensityProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_NormalIntensity" + }) + normalIntensity: number = 1; /** * Set the specular reflection coefficient, the larger the value, the more convergent the specular reflection effect. */ - get shininess(): number { - return this.shaderData.getFloat(BlinnPhongMaterial._shininessProp); - } - - set shininess(value: number) { - this.shaderData.setFloat(BlinnPhongMaterial._shininessProp, Math.max(value, 1e-4)); - } + @uniform(ShaderUniformType.Float, { + varName: "material_Shininess" + }) + shininess: number = 16; /** * Tiling and offset of main textures. */ - get tilingOffset(): Vector4 { - return this.shaderData.getVector4(BlinnPhongMaterial._tilingOffsetProp); - } - - set tilingOffset(value: Vector4) { - const tilingOffset = this.shaderData.getVector4(BlinnPhongMaterial._tilingOffsetProp); - if (value !== tilingOffset) { - tilingOffset.copyFrom(value); - } - } + @uniform(ShaderUniformType.Vector4, { + varName: "material_TilingOffset", + keepRef: true + }) + tilingOffset: Vector4 = new Vector4(1, 1, 0, 0); /** * Create a BlinnPhong material instance. @@ -166,13 +109,6 @@ export class BlinnPhongMaterial extends BaseMaterial { shaderData.enableMacro("MATERIAL_NEED_WORLD_POS"); shaderData.enableMacro("MATERIAL_NEED_TILING_OFFSET"); - - shaderData.setColor(BlinnPhongMaterial._baseColorProp, new Color(1, 1, 1, 1)); - shaderData.setColor(BlinnPhongMaterial._specularColorProp, new Color(1, 1, 1, 1)); - shaderData.setColor(BlinnPhongMaterial._emissiveColorProp, new Color(0, 0, 0, 1)); - shaderData.setVector4(BlinnPhongMaterial._tilingOffsetProp, new Vector4(1, 1, 0, 0)); - shaderData.setFloat(BlinnPhongMaterial._shininessProp, 16); - shaderData.setFloat(BlinnPhongMaterial._normalIntensityProp, 1); } override clone(): BlinnPhongMaterial { From 29cc34c55740602ac167ece7b5c4ba71457f5ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Thu, 31 Aug 2023 23:53:35 -0700 Subject: [PATCH 09/19] Update PBRBaseMaterial.ts --- packages/core/src/material/PBRBaseMaterial.ts | 240 +++++------------- 1 file changed, 68 insertions(+), 172 deletions(-) diff --git a/packages/core/src/material/PBRBaseMaterial.ts b/packages/core/src/material/PBRBaseMaterial.ts index f421a92cb6..8d33473610 100644 --- a/packages/core/src/material/PBRBaseMaterial.ts +++ b/packages/core/src/material/PBRBaseMaterial.ts @@ -1,5 +1,5 @@ import { Color, Vector4 } from "@galacean/engine-math"; -import { Logger, ShaderProperty } from ".."; +import { Logger, ShaderProperty, ShaderUniformType, uniform } from ".."; import { Engine } from "../Engine"; import { Shader } from "../shader/Shader"; import { Texture2D } from "../texture/Texture2D"; @@ -10,129 +10,77 @@ import { TextureCoordinate } from "./enums/TextureCoordinate"; * PBR (Physically-Based Rendering) Material. */ export abstract class PBRBaseMaterial extends BaseMaterial { - private static _occlusionTextureIntensityProp = ShaderProperty.getByName("material_OcclusionIntensity"); private static _occlusionTextureCoordProp = ShaderProperty.getByName("material_OcclusionTextureCoord"); - private static _occlusionTextureProp = ShaderProperty.getByName("material_OcclusionTexture"); - - private static _clearCoatProp = ShaderProperty.getByName("material_ClearCoat"); - private static _clearCoatTextureProp = ShaderProperty.getByName("material_ClearCoatTexture"); - private static _clearCoatRoughnessProp = ShaderProperty.getByName("material_ClearCoatRoughness"); - private static _clearCoatRoughnessTextureProp = ShaderProperty.getByName("material_ClearCoatRoughnessTexture"); - private static _clearCoatNormalTextureProp = ShaderProperty.getByName("material_ClearCoatNormalTexture"); /** * Base color. */ - get baseColor(): Color { - return this.shaderData.getColor(PBRBaseMaterial._baseColorProp); - } - - set baseColor(value: Color) { - const baseColor = this.shaderData.getColor(PBRBaseMaterial._baseColorProp); - if (value !== baseColor) { - baseColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_BaseColor", + keepRef: true + }) + baseColor: Color = new Color(1, 1, 1, 1); /** * Base texture. */ - get baseTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._baseTextureProp); - } - - set baseTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._baseTextureProp, value); - if (value) { - this.shaderData.enableMacro(PBRBaseMaterial._baseTextureMacro); - } else { - this.shaderData.disableMacro(PBRBaseMaterial._baseTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_BaseColor", + macroName: "MATERIAL_HAS_BASETEXTURE" + }) + baseTexture: Texture2D; /** * Normal texture. */ - get normalTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._normalTextureProp); - } - - set normalTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._normalTextureProp, value); - if (value) { - this.shaderData.enableMacro(PBRBaseMaterial._normalTextureMacro); - } else { - this.shaderData.disableMacro(PBRBaseMaterial._normalTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_NormalTexture", + macroName: "MATERIAL_HAS_NORMALTEXTURE" + }) + normalTexture: Texture2D; /** * Normal texture intensity. */ - get normalTextureIntensity(): number { - return this.shaderData.getFloat(PBRBaseMaterial._normalIntensityProp); - } - - set normalTextureIntensity(value: number) { - this.shaderData.setFloat(PBRBaseMaterial._normalIntensityProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_NormalIntensity" + }) + normalTextureIntensity: number = 1; /** * Emissive color. */ - get emissiveColor(): Color { - return this.shaderData.getColor(PBRBaseMaterial._emissiveColorProp); - } - - set emissiveColor(value: Color) { - const emissiveColor = this.shaderData.getColor(PBRBaseMaterial._emissiveColorProp); - if (value !== emissiveColor) { - emissiveColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_EmissiveColor", + keepRef: true + }) + emissiveColor: Color = new Color(0, 0, 0, 1); /** * Emissive texture. */ - get emissiveTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._emissiveTextureProp); - } - - set emissiveTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._emissiveTextureProp, value); - if (value) { - this.shaderData.enableMacro(PBRBaseMaterial._emissiveTextureMacro); - } else { - this.shaderData.disableMacro(PBRBaseMaterial._emissiveTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_EmissiveTexture", + macroName: "MATERIAL_HAS_EMISSIVETEXTURE" + }) + emissiveTexture: Texture2D; /** * Occlusion texture. */ - get occlusionTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._occlusionTextureProp); - } - - set occlusionTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._occlusionTextureProp, value); - if (value) { - this.shaderData.enableMacro("MATERIAL_HAS_OCCLUSION_TEXTURE"); - } else { - this.shaderData.disableMacro("MATERIAL_HAS_OCCLUSION_TEXTURE"); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_OcclusionTexture", + macroName: "MATERIAL_HAS_OCCLUSION_TEXTURE" + }) + occlusionTexture: Texture2D; /** * Occlusion texture intensity. */ - get occlusionTextureIntensity(): number { - return this.shaderData.getFloat(PBRBaseMaterial._occlusionTextureIntensityProp); - } - - set occlusionTextureIntensity(value: number) { - this.shaderData.setFloat(PBRBaseMaterial._occlusionTextureIntensityProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_OcclusionIntensity" + }) + occlusionTextureIntensity: number = 1; /** * Occlusion texture uv coordinate. @@ -152,96 +100,55 @@ export abstract class PBRBaseMaterial extends BaseMaterial { /** * Tiling and offset of main textures. */ - get tilingOffset(): Vector4 { - return this.shaderData.getVector4(PBRBaseMaterial._tilingOffsetProp); - } - - set tilingOffset(value: Vector4) { - const tilingOffset = this.shaderData.getVector4(PBRBaseMaterial._tilingOffsetProp); - if (value !== tilingOffset) { - tilingOffset.copyFrom(value); - } - } + @uniform(ShaderUniformType.Vector4, { + varName: "material_TilingOffset", + keepRef: true + }) + tilingOffset: Vector4 = new Vector4(1, 1, 0, 0); /** * The clearCoat layer intensity, default 0. */ - get clearCoat(): number { - return this.shaderData.getFloat(PBRBaseMaterial._clearCoatProp); - } - - set clearCoat(value: number) { - if (!!this.shaderData.getFloat(PBRBaseMaterial._clearCoatProp) !== !!value) { - if (value === 0) { - this.shaderData.disableMacro("MATERIAL_ENABLE_CLEAR_COAT"); - } else { - this.shaderData.enableMacro("MATERIAL_ENABLE_CLEAR_COAT"); - } - } - this.shaderData.setFloat(PBRBaseMaterial._clearCoatProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_ClearCoat", + macroName: "MATERIAL_ENABLE_CLEAR_COAT" + }) + clearCoat: number = 0; /** * The clearCoat layer intensity texture. */ - get clearCoatTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._clearCoatTextureProp); - } - - set clearCoatTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._clearCoatTextureProp, value); - - if (value) { - this.shaderData.enableMacro("MATERIAL_HAS_CLEAR_COAT_TEXTURE"); - } else { - this.shaderData.disableMacro("MATERIAL_HAS_CLEAR_COAT_TEXTURE"); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_ClearCoatTexture", + macroName: "MATERIAL_HAS_CLEAR_COAT_TEXTURE" + }) + clearCoatTexture: Texture2D; /** * The clearCoat layer roughness, default 0. */ - get clearCoatRoughness(): number { - return this.shaderData.getFloat(PBRBaseMaterial._clearCoatRoughnessProp); - } - - set clearCoatRoughness(value: number) { - this.shaderData.setFloat(PBRBaseMaterial._clearCoatRoughnessProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_ClearCoatRoughness" + }) + clearCoatRoughness: number = 0; /** * The clearCoat layer roughness texture. */ - get clearCoatRoughnessTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._clearCoatRoughnessTextureProp); - } - - set clearCoatRoughnessTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._clearCoatRoughnessTextureProp, value); - - if (value) { - this.shaderData.enableMacro("MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); - } else { - this.shaderData.disableMacro("MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_ClearCoatRoughnessTexture", + macroName: "MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE" + }) + clearCoatRoughnessTexture: Texture2D; /** * The clearCoat normal map texture. */ - get clearCoatNormalTexture(): Texture2D { - return this.shaderData.getTexture(PBRBaseMaterial._clearCoatNormalTextureProp); - } - - set clearCoatNormalTexture(value: Texture2D) { - this.shaderData.setTexture(PBRBaseMaterial._clearCoatNormalTextureProp, value); - - if (value) { - this.shaderData.enableMacro("MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE"); - } else { - this.shaderData.disableMacro("MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE"); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_ClearCoatNormalTexture", + macroName: "MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE" + }) + clearCoatNormalTexture: Texture2D; /** * Create a pbr base material instance. @@ -255,16 +162,5 @@ export abstract class PBRBaseMaterial extends BaseMaterial { shaderData.enableMacro("MATERIAL_NEED_WORLD_POS"); shaderData.enableMacro("MATERIAL_NEED_TILING_OFFSET"); - - shaderData.setColor(PBRBaseMaterial._baseColorProp, new Color(1, 1, 1, 1)); - shaderData.setColor(PBRBaseMaterial._emissiveColorProp, new Color(0, 0, 0, 1)); - shaderData.setVector4(PBRBaseMaterial._tilingOffsetProp, new Vector4(1, 1, 0, 0)); - - shaderData.setFloat(PBRBaseMaterial._normalIntensityProp, 1); - shaderData.setFloat(PBRBaseMaterial._occlusionTextureIntensityProp, 1); - shaderData.setFloat(PBRBaseMaterial._occlusionTextureCoordProp, TextureCoordinate.UV0); - - shaderData.setFloat(PBRBaseMaterial._clearCoatProp, 0); - shaderData.setFloat(PBRBaseMaterial._clearCoatRoughnessProp, 0); } } From 06020200c09914975a554dcf0b85355d7bbbc58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Thu, 31 Aug 2023 23:54:05 -0700 Subject: [PATCH 10/19] Update PBRMaterial.ts --- packages/core/src/material/PBRMaterial.ts | 49 +++++++---------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/packages/core/src/material/PBRMaterial.ts b/packages/core/src/material/PBRMaterial.ts index bc463e1d07..034bd7d8be 100644 --- a/packages/core/src/material/PBRMaterial.ts +++ b/packages/core/src/material/PBRMaterial.ts @@ -1,5 +1,5 @@ import { Engine } from "../Engine"; -import { ShaderProperty } from "../shader"; +import { ShaderProperty, ShaderUniformType, uniform } from "../shader"; import { Shader } from "../shader/Shader"; import { Texture2D } from "../texture/Texture2D"; import { PBRBaseMaterial } from "./PBRBaseMaterial"; @@ -8,11 +8,7 @@ import { PBRBaseMaterial } from "./PBRBaseMaterial"; * PBR (Metallic-Roughness Workflow) Material. */ export class PBRMaterial extends PBRBaseMaterial { - private static _metallicProp = ShaderProperty.getByName("material_Metal"); - private static _roughnessProp = ShaderProperty.getByName("material_Roughness"); - private static _roughnessMetallicTextureProp = ShaderProperty.getByName("material_RoughnessMetallicTexture"); - - private static _iorProp = Shader.getPropertyByName("material_IOR"); + private static _iorProp = ShaderProperty.getByName("material_IOR"); /** * Index Of Refraction. @@ -30,42 +26,29 @@ export class PBRMaterial extends PBRBaseMaterial { * Metallic. * @defaultValue `1.0` */ - get metallic(): number { - return this.shaderData.getFloat(PBRMaterial._metallicProp); - } - - set metallic(value: number) { - this.shaderData.setFloat(PBRMaterial._metallicProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_Metal" + }) + metallic = 1; /** * Roughness. default 1.0. * @defaultValue `1.0` */ - get roughness(): number { - return this.shaderData.getFloat(PBRMaterial._roughnessProp); - } - - set roughness(value: number) { - this.shaderData.setFloat(PBRMaterial._roughnessProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_Roughness" + }) + roughness = 1; /** * Roughness metallic texture. * @remarks G channel is roughness, B channel is metallic */ - get roughnessMetallicTexture(): Texture2D { - return this.shaderData.getTexture(PBRMaterial._roughnessMetallicTextureProp); - } - - set roughnessMetallicTexture(value: Texture2D) { - this.shaderData.setTexture(PBRMaterial._roughnessMetallicTextureProp, value); - if (value) { - this.shaderData.enableMacro("MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE"); - } else { - this.shaderData.disableMacro("MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE"); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_RoughnessMetallicTexture", + macroName: "MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE" + }) + roughnessMetallicTexture: Texture2D; /** * Create a pbr metallic-roughness workflow material instance. @@ -73,8 +56,6 @@ export class PBRMaterial extends PBRBaseMaterial { */ constructor(engine: Engine) { super(engine, Shader.find("pbr")); - this.shaderData.setFloat(PBRMaterial._metallicProp, 1); - this.shaderData.setFloat(PBRMaterial._roughnessProp, 1); this.shaderData.setFloat(PBRMaterial._iorProp, 1.5); } From 4acf69c52c3ba2887bb6e97e3bd918ba22e480f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Thu, 31 Aug 2023 23:54:22 -0700 Subject: [PATCH 11/19] Update PBRSpecularMaterial.ts --- .../core/src/material/PBRSpecularMaterial.ts | 56 +++++-------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/packages/core/src/material/PBRSpecularMaterial.ts b/packages/core/src/material/PBRSpecularMaterial.ts index 8eb54d0e68..e03097402b 100644 --- a/packages/core/src/material/PBRSpecularMaterial.ts +++ b/packages/core/src/material/PBRSpecularMaterial.ts @@ -1,8 +1,7 @@ import { Color } from "@galacean/engine-math"; +import { uniform, ShaderUniformType } from ".."; import { Engine } from "../Engine"; import { Shader } from "../shader/Shader"; -import { ShaderMacro } from "../shader/ShaderMacro"; -import { ShaderProperty } from "../shader/ShaderProperty"; import { Texture2D } from "../texture/Texture2D"; import { PBRBaseMaterial } from "./PBRBaseMaterial"; @@ -10,54 +9,32 @@ import { PBRBaseMaterial } from "./PBRBaseMaterial"; * PBR (Specular-Glossiness Workflow) Material. */ export class PBRSpecularMaterial extends PBRBaseMaterial { - private static _specularColorProp = ShaderProperty.getByName("material_PBRSpecularColor"); - private static _glossinessProp = ShaderProperty.getByName("material_Glossiness"); - private static _specularGlossinessTextureProp = ShaderProperty.getByName("material_SpecularGlossinessTexture"); - private static _specularGlossinessTextureMacro: ShaderMacro = ShaderMacro.getByName( - "MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE" - ); - /** * Specular color. */ - get specularColor(): Color { - return this.shaderData.getColor(PBRSpecularMaterial._specularColorProp); - } - - set specularColor(value: Color) { - const specularColor = this.shaderData.getColor(PBRSpecularMaterial._specularColorProp); - if (value !== specularColor) { - specularColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_PBRSpecularColor", + keepRef: true + }) + specularColor: Color = new Color(1, 1, 1, 1); /** * Glossiness. */ - get glossiness(): number { - return this.shaderData.getFloat(PBRSpecularMaterial._glossinessProp); - } - - set glossiness(value: number) { - this.shaderData.setFloat(PBRSpecularMaterial._glossinessProp, value); - } + @uniform(ShaderUniformType.Float, { + varName: "material_Glossiness" + }) + glossiness: number = 1; /** * Specular glossiness texture. * @remarks RGB is specular, A is glossiness */ - get specularGlossinessTexture(): Texture2D { - return this.shaderData.getTexture(PBRSpecularMaterial._specularGlossinessTextureProp); - } - - set specularGlossinessTexture(value: Texture2D) { - this.shaderData.setTexture(PBRSpecularMaterial._specularGlossinessTextureProp, value); - if (value) { - this.shaderData.enableMacro(PBRSpecularMaterial._specularGlossinessTextureMacro); - } else { - this.shaderData.disableMacro(PBRSpecularMaterial._specularGlossinessTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_SpecularGlossinessTexture", + macroName: "MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE" + }) + specularGlossinessTexture: Texture2D; /** * Create a pbr specular-glossiness workflow material instance. @@ -65,9 +42,6 @@ export class PBRSpecularMaterial extends PBRBaseMaterial { */ constructor(engine: Engine) { super(engine, Shader.find("pbr-specular")); - - this.shaderData.setColor(PBRSpecularMaterial._specularColorProp, new Color(1, 1, 1, 1)); - this.shaderData.setFloat(PBRSpecularMaterial._glossinessProp, 1.0); } /** From 58901ec251f8be4e0a1fb0f63b1d2197d43198b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Thu, 31 Aug 2023 23:54:41 -0700 Subject: [PATCH 12/19] Update UnlitMaterial.ts --- packages/core/src/material/UnlitMaterial.ts | 52 +++++++-------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/packages/core/src/material/UnlitMaterial.ts b/packages/core/src/material/UnlitMaterial.ts index 2e6684e0ab..f784ff3556 100644 --- a/packages/core/src/material/UnlitMaterial.ts +++ b/packages/core/src/material/UnlitMaterial.ts @@ -1,6 +1,8 @@ import { Color, Vector4 } from "@galacean/engine-math"; import { Engine } from "../Engine"; import { Shader } from "../shader/Shader"; +import { uniform } from "../shader/ShaderUniformDecorator"; +import { ShaderUniformType } from "../shader/ShaderUniformType"; import { Texture2D } from "../texture/Texture2D"; import { BaseMaterial } from "./BaseMaterial"; @@ -11,46 +13,29 @@ export class UnlitMaterial extends BaseMaterial { /** * Base color. */ - get baseColor(): Color { - return this.shaderData.getColor(UnlitMaterial._baseColorProp); - } - - set baseColor(value: Color) { - const baseColor = this.shaderData.getColor(UnlitMaterial._baseColorProp); - if (value !== baseColor) { - baseColor.copyFrom(value); - } - } + @uniform(ShaderUniformType.Color, { + varName: "material_BaseColor", + keepRef: true + }) + baseColor: Color = new Color(1, 1, 1, 1); /** * Base texture. */ - get baseTexture(): Texture2D { - return this.shaderData.getTexture(UnlitMaterial._baseTextureProp); - } - - set baseTexture(value: Texture2D) { - this.shaderData.setTexture(UnlitMaterial._baseTextureProp, value); - if (value) { - this.shaderData.enableMacro(UnlitMaterial._baseTextureMacro); - } else { - this.shaderData.disableMacro(UnlitMaterial._baseTextureMacro); - } - } + @uniform(ShaderUniformType.Texture, { + varName: "material_BaseColor", + macroName: "MATERIAL_HAS_BASETEXTURE" + }) + baseTexture: Texture2D; /** * Tiling and offset of main textures. */ - get tilingOffset(): Vector4 { - return this.shaderData.getVector4(UnlitMaterial._tilingOffsetProp); - } - - set tilingOffset(value: Vector4) { - const tilingOffset = this.shaderData.getVector4(UnlitMaterial._tilingOffsetProp); - if (value !== tilingOffset) { - tilingOffset.copyFrom(value); - } - } + @uniform(ShaderUniformType.Vector4, { + varName: "material_TilingOffset", + keepRef: true + }) + tilingOffset: Vector4 = new Vector4(1, 1, 0, 0); /** * Create a unlit material instance. @@ -63,9 +48,6 @@ export class UnlitMaterial extends BaseMaterial { shaderData.enableMacro("MATERIAL_OMIT_NORMAL"); shaderData.enableMacro("MATERIAL_NEED_TILING_OFFSET"); - - shaderData.setColor(UnlitMaterial._baseColorProp, new Color(1, 1, 1, 1)); - shaderData.setVector4(UnlitMaterial._tilingOffsetProp, new Vector4(1, 1, 0, 0)); } /** From b565e7ee0fdc6b492d2c30c3b172806e33cb464c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:01:17 -0700 Subject: [PATCH 13/19] fix code style --- .../core/src/shader/ShaderUniformDecorator.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/core/src/shader/ShaderUniformDecorator.ts b/packages/core/src/shader/ShaderUniformDecorator.ts index 7144bba6e6..65ddafdc7d 100644 --- a/packages/core/src/shader/ShaderUniformDecorator.ts +++ b/packages/core/src/shader/ShaderUniformDecorator.ts @@ -28,11 +28,16 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { const set = "set" + type; let setFunc: (value: any) => void; - if (type === ShaderUniformType.Float || type === ShaderUniformType.Int || type === ShaderUniformType.Texture || type === ShaderUniformType.TextureArray) { + if ( + type === ShaderUniformType.Float || + type === ShaderUniformType.Int || + type === ShaderUniformType.Texture || + type === ShaderUniformType.TextureArray + ) { setFunc = function (value: any) { this.shaderData[set](shaderProp, value); handleMacro.call(this, value, options?.macroName); - } + }; } else if (options?.keepRef) { if (type === ShaderUniformType.FloatArray || type === ShaderUniformType.IntArray) { setFunc = function (value: any) { @@ -43,8 +48,14 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { } data.set(value); handleMacro.call(this, value, options?.macroName); - } - } else if (type === ShaderUniformType.Vector2 || type === ShaderUniformType.Vector3 || type === ShaderUniformType.Vector4 || type === ShaderUniformType.Matrix || type === ShaderUniformType.Color) { + }; + } else if ( + type === ShaderUniformType.Vector2 || + type === ShaderUniformType.Vector3 || + type === ShaderUniformType.Vector4 || + type === ShaderUniformType.Matrix || + type === ShaderUniformType.Color + ) { setFunc = function (value: any) { let data = this.shaderData[get](shaderProp); if (!data) { @@ -53,13 +64,13 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { } data.copyFrom(value); handleMacro.call(this, value, options?.macroName); - } + }; } } else { setFunc = function (value: any) { this.shaderData[set](shaderProp, value); handleMacro.call(this, value, options?.macroName); - } + }; } Reflect.defineProperty(target, propertyKey, { From c6904c5c45068eaf3fb6fd3147156c80fb6a3ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:04:04 -0700 Subject: [PATCH 14/19] fix code style --- packages/core/src/shader/ShaderUniformDecorator.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/shader/ShaderUniformDecorator.ts b/packages/core/src/shader/ShaderUniformDecorator.ts index 65ddafdc7d..bad2874b8e 100644 --- a/packages/core/src/shader/ShaderUniformDecorator.ts +++ b/packages/core/src/shader/ShaderUniformDecorator.ts @@ -6,7 +6,7 @@ export type UniformOptions = { varName?: string; // the variable name in glsl macroName?: string; // whether enable or disable a macro in glsl depends on setting value keepRef?: boolean; // whether setting a value to a property will change reference or just copy value. Only available for vector or matrix type -} +}; function handleMacro(value: any, macroName?: string) { if (macroName) { @@ -79,7 +79,7 @@ export function uniform(type: ShaderUniformType, options?: UniformOptions) { }, set: setFunc!, enumerable: false, - configurable: true, + configurable: true }); }; } From 56560ff886050983573c5b7eeb26a6f68958e4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:16:47 -0700 Subject: [PATCH 15/19] Update BlinnPhongMaterial.ts --- packages/core/src/material/BlinnPhongMaterial.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/material/BlinnPhongMaterial.ts b/packages/core/src/material/BlinnPhongMaterial.ts index fcf154cd48..716606d252 100644 --- a/packages/core/src/material/BlinnPhongMaterial.ts +++ b/packages/core/src/material/BlinnPhongMaterial.ts @@ -23,7 +23,7 @@ export class BlinnPhongMaterial extends BaseMaterial { * Base texture. */ @uniform(ShaderUniformType.Texture, { - varName: "material_BaseColor", + varName: "material_BaseTexture", macroName: "MATERIAL_HAS_BASETEXTURE" }) baseTexture: Texture2D; From 7e3cad1a6e2360a44aad866738140bd603efe527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:17:11 -0700 Subject: [PATCH 16/19] Update UnlitMaterial.ts --- packages/core/src/material/UnlitMaterial.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/material/UnlitMaterial.ts b/packages/core/src/material/UnlitMaterial.ts index f784ff3556..7d5f076512 100644 --- a/packages/core/src/material/UnlitMaterial.ts +++ b/packages/core/src/material/UnlitMaterial.ts @@ -23,7 +23,7 @@ export class UnlitMaterial extends BaseMaterial { * Base texture. */ @uniform(ShaderUniformType.Texture, { - varName: "material_BaseColor", + varName: "material_BaseTexture", macroName: "MATERIAL_HAS_BASETEXTURE" }) baseTexture: Texture2D; From 3e34c9b72331d49ac7564dbe7cc559d83000bbdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:17:42 -0700 Subject: [PATCH 17/19] Update PBRBaseMaterial.ts --- packages/core/src/material/PBRBaseMaterial.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/material/PBRBaseMaterial.ts b/packages/core/src/material/PBRBaseMaterial.ts index 8d33473610..4217c49690 100644 --- a/packages/core/src/material/PBRBaseMaterial.ts +++ b/packages/core/src/material/PBRBaseMaterial.ts @@ -25,7 +25,7 @@ export abstract class PBRBaseMaterial extends BaseMaterial { * Base texture. */ @uniform(ShaderUniformType.Texture, { - varName: "material_BaseColor", + varName: "material_BaseTexture", macroName: "MATERIAL_HAS_BASETEXTURE" }) baseTexture: Texture2D; From 78751aa75285a42915e737aa9b904648dcb6a884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:26:48 -0700 Subject: [PATCH 18/19] Update BlinnPhongMaterial.ts --- packages/core/src/material/BlinnPhongMaterial.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/core/src/material/BlinnPhongMaterial.ts b/packages/core/src/material/BlinnPhongMaterial.ts index 716606d252..759b4171f7 100644 --- a/packages/core/src/material/BlinnPhongMaterial.ts +++ b/packages/core/src/material/BlinnPhongMaterial.ts @@ -10,6 +10,8 @@ import { BaseMaterial } from "./BaseMaterial"; * Blinn-phong Material. */ export class BlinnPhongMaterial extends BaseMaterial { + private static _shininessProp = ShaderProperty.getByName("material_Shininess"); + /** * Base color. */ @@ -84,10 +86,13 @@ export class BlinnPhongMaterial extends BaseMaterial { /** * Set the specular reflection coefficient, the larger the value, the more convergent the specular reflection effect. */ - @uniform(ShaderUniformType.Float, { - varName: "material_Shininess" - }) - shininess: number = 16; + get shininess(): number { + return this.shaderData.getFloat(BlinnPhongMaterial._shininessProp); + } + + set shininess(value: number) { + this.shaderData.setFloat(BlinnPhongMaterial._shininessProp, Math.max(value, 1e-4)); + } /** * Tiling and offset of main textures. From dc6b8e881c5b3d152645d95c545603d7ee790de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Fri, 1 Sep 2023 00:32:14 -0700 Subject: [PATCH 19/19] Update BlinnPhongMaterial.ts --- packages/core/src/material/BlinnPhongMaterial.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/material/BlinnPhongMaterial.ts b/packages/core/src/material/BlinnPhongMaterial.ts index 759b4171f7..8aa073132d 100644 --- a/packages/core/src/material/BlinnPhongMaterial.ts +++ b/packages/core/src/material/BlinnPhongMaterial.ts @@ -114,6 +114,8 @@ export class BlinnPhongMaterial extends BaseMaterial { shaderData.enableMacro("MATERIAL_NEED_WORLD_POS"); shaderData.enableMacro("MATERIAL_NEED_TILING_OFFSET"); + + shaderData.setFloat(BlinnPhongMaterial._shininessProp, 16); } override clone(): BlinnPhongMaterial {