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

Add uniform decorator for improving custom material code #1713

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

HypnosNova
Copy link
Contributor

image

With uniform decorator, it will be easier for create uniform property in material class.

Features:

1. support define glsl variable name

class XXXMaterial extends BaseMaterial {
  // In this case, decorator only know baseColor is a Color type. So in glsl code, the variable name is the same as property name 'baseColor'
  @uniform(ShaderUniformType.Color)
  baseColor: Color = new Color(1, 1, 1, 1);
}

class XXXMaterial2 extends BaseMaterial {
  // In this case, decorator has second param, and varName in glsl is 'material_BaseColor' instead of using the same property name in js
  @uniform(ShaderUniformType.Color, {
    varName: 'material_BaseColor'
  })
  baseColor: Color = new Color(1, 1, 1, 1);
}

2. support macro

class XXXMaterial extends BaseMaterial {
  // In this case, decorator knows if we set the texture, we should also need to enable MATERIAL_HAS_BASETEXTURE macro. If we remove the texture, we should disable the macro.
  @uniform(ShaderUniformType.Texture, {
    varName: "material_BaseColor",
    macroName: "MATERIAL_HAS_BASETEXTURE"
  })
  baseTexture: Texture2D;
}

// the above code logic is the same as following code:
class UnlitMaterial extends BaseMaterial {
  /**
   * Base texture.
   */
  get baseTexture(): Texture2D {
    return <Texture2D>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);
    }
  }
}

3. support keeping reference

class XXXMaterial extends BaseMaterial {
  @uniform(ShaderUniformType.Color, {
    keepRef: true
  })
  baseColor: Color = new Color(1, 1, 1, 1);
}

// If we keepRef, the baseColor will not be changed reference but only copy data just like:
const material = new XXXMaterial();
const newColor = new Color(0,0,0,0);
material.baseColor = newColor; // This code will not change reference
material.baseColor === newColor // false, but the baseColor's data is 0,0,0,0

@codecov
Copy link

codecov bot commented Aug 24, 2023

Codecov Report

Patch coverage: 73.99% and project coverage change: +0.59% 🎉

Comparison is base (ef443a3) 61.37% compared to head (47a8875) 61.97%.
Report is 28 commits behind head on dev/1.1.

❗ Current head 47a8875 differs from pull request most recent head dc6b8e8. Consider uploading reports for the commit dc6b8e8 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.1    #1713      +/-   ##
===========================================
+ Coverage    61.37%   61.97%   +0.59%     
===========================================
  Files          410      432      +22     
  Lines        21834    22447     +613     
  Branches      3140     3190      +50     
===========================================
+ Hits         13401    13911     +510     
- Misses        7244     7315      +71     
- Partials      1189     1221      +32     
Files Changed Coverage Δ
packages/core/src/asset/Loader.ts 83.33% <ø> (ø)
packages/core/src/material/BlinnPhongMaterial.ts 94.11% <ø> (+3.20%) ⬆️
packages/core/src/material/Material.ts 94.59% <0.00%> (-2.63%) ⬇️
packages/core/src/material/PBRBaseMaterial.ts 81.25% <ø> (+0.98%) ⬆️
packages/core/src/material/PBRMaterial.ts 72.72% <ø> (-12.28%) ⬇️
packages/core/src/material/PBRSpecularMaterial.ts 87.50% <ø> (-1.98%) ⬇️
packages/core/src/material/UnlitMaterial.ts 90.90% <ø> (+3.40%) ⬆️
...ckages/core/src/shadow/CascadedShadowCasterPass.ts 31.97% <ø> (ø)
.../loader/src/gltf/extensions/GLTFExtensionParser.ts 55.55% <ø> (-4.45%) ⬇️
...ader/src/gltf/extensions/KHR_materials_variants.ts 20.00% <0.00%> (ø)
... and 33 more

... and 33 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@GuoLei1990 GuoLei1990 changed the base branch from dev/1.1 to main September 15, 2023 09:35
@GuoLei1990 GuoLei1990 added this to the 1.2 milestone Oct 7, 2023
@GuoLei1990 GuoLei1990 added the medium priority Medium priority issue label Oct 7, 2023
@GuoLei1990 GuoLei1990 assigned zhuxudong and unassigned GuoLei1990 Oct 7, 2023
@zhuxudong zhuxudong removed this from the 1.2 milestone Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
material medium priority Medium priority issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants