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

Refactor sprite mask system #2369

Merged
merged 55 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3ff8a7b
refactor(mask): refactor mask
singlecoder Sep 2, 2024
4afe44b
refactor(mask): opt stencil state
singlecoder Sep 3, 2024
603ba2a
refactor(mask): move sprite mask layer to root
singlecoder Sep 3, 2024
0ae6484
refactor(mask): opt code
singlecoder Sep 5, 2024
1a89b5d
refactor(mask): clear stencil when shadow
singlecoder Sep 9, 2024
a5f1ab9
refactor(mask): fix test error
singlecoder Sep 9, 2024
7ad02b4
refactor(mask): delete render queue check in RenderQueue
singlecoder Sep 10, 2024
e97c4aa
refactor(mask): rename api _isCulledByCamera to _isFilteredByLayer
singlecoder Sep 11, 2024
2f2615b
refactor(mask): when need mask, not change stencil state, upload to g…
singlecoder Sep 11, 2024
47c9c7e
refactor(mask): delete unless code
singlecoder Sep 11, 2024
e52b60a
refactor(mask): mask should be filtered by camera culling mask
singlecoder Sep 12, 2024
8a48797
refactor(mask): opt code
singlecoder Sep 13, 2024
a311f2d
refactor(mask): clear stencil by mask when current render queue has done
singlecoder Sep 14, 2024
9a06859
refactor(mask): opt code
singlecoder Sep 18, 2024
f6f373b
refactor(mask): opt code
singlecoder Sep 18, 2024
f6b35a9
refactor(mask): opt code
singlecoder Sep 18, 2024
47d670a
refactor(mask): opt code
singlecoder Sep 18, 2024
8c0fc37
refactor(mask): opt code
singlecoder Sep 19, 2024
e71b5d6
refactor(mask): delete useless stencil clear
singlecoder Sep 19, 2024
5df85bc
refactor(mask): opt custom stencil states cache
singlecoder Sep 19, 2024
0a09fee
refactor(mask): opt code
singlecoder Sep 19, 2024
82f7f82
refactor(mask): fix conflicts from dev/1.4
singlecoder Sep 19, 2024
12d40f3
refactor(mask): opt custom stencil states
singlecoder Sep 19, 2024
028e4b0
refactor(mask): opt code
singlecoder Sep 19, 2024
2b92fe1
refactor(mask): opt code for clear mask
singlecoder Sep 19, 2024
3d10522
Refactor: extract some methods of the 2D renderers from the Engine (#15)
eyworldwide Sep 29, 2024
5f50e58
refactor(mask): fix perttier
singlecoder Sep 29, 2024
f96f891
refactor(mask): opt code for custom stencil states
singlecoder Oct 9, 2024
ea9e05c
Merge branch 'dev/1.4' into refactor/sprite-mask
singlecoder Oct 12, 2024
6e6eab5
refactor(mask): support developers to customize stencil
singlecoder Oct 12, 2024
ce3efed
refactor(mask): opt code for resume stencil
singlecoder Oct 13, 2024
bd38b58
refactor(mask): migrate 2d default material to basic resources
singlecoder Oct 14, 2024
18ea656
refactor(mask): opt code
singlecoder Oct 14, 2024
894dff9
refactor(mask): opt code
singlecoder Oct 14, 2024
f0c6d4b
refactor(mask): opt mask and stencil clear time
singlecoder Oct 15, 2024
4f4218d
refactor(mask): opt color write mask set
singlecoder Oct 15, 2024
397ac0e
refactor(mask): perttier code
singlecoder Oct 15, 2024
d29b823
refactor(mask): opt code
singlecoder Oct 17, 2024
8a34428
refactor(mask): opt code
singlecoder Oct 17, 2024
61558b1
refactor(mask): opt code
singlecoder Oct 17, 2024
3fd4e4f
refactor(mask): opt code
singlecoder Oct 17, 2024
50a7e7e
refactor(mask): unified update render state
singlecoder Oct 18, 2024
324c9e5
refactor(mask): rename notWriteStencil to hasWrittenToStencil
singlecoder Oct 18, 2024
27e6bf7
refactor(mask): opt code
singlecoder Oct 18, 2024
1b337e7
refactor(mask): opt code
singlecoder Oct 18, 2024
a44e230
refactor(mask): opt code
singlecoder Oct 18, 2024
98efec7
refactor(mask): opt code
singlecoder Oct 18, 2024
c8be7f1
refactor(mask): clear mask when custom shader want to read stencil
singlecoder Oct 18, 2024
1f190f0
refactor(mask): opt code
singlecoder Oct 18, 2024
a5c0445
refactor(mask): opt code
singlecoder Oct 18, 2024
bc21358
refactor(mask): let sprite mask compare funtion always pass
singlecoder Oct 18, 2024
e2efa9d
refactor(mask): opt code
singlecoder Oct 18, 2024
c320b31
refactor(mask): add render queue type check
singlecoder Oct 18, 2024
501eb00
Merge branch 'dev/1.4' into refactor/sprite-mask
singlecoder Oct 21, 2024
a010ade
refactor(mask): add e2d
singlecoder Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions e2e/case/spriteMask-customStencil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* @title SpriteMaskCustomStencil
* @category SpriteMask
*/

import {
AssetType,
Camera,
CompareFunction,
Layer,
Script,
Sprite,
SpriteMask,
SpriteMaskInteraction,
SpriteRenderer,
StencilOperation,
Texture2D,
Vector3,
WebGLEngine
} from "@galacean/engine";
import { initScreenshot, updateForE2E } from "./.mockForE2E";

Check warning on line 21 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L20-L21

Added lines #L20 - L21 were not covered by tests

// Create engine
WebGLEngine.create({ canvas: "canvas" }).then((engine) => {
engine.canvas.resizeByClientSize();

Check warning on line 25 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L24-L25

Added lines #L24 - L25 were not covered by tests

// Create root entity
const rootEntity = engine.sceneManager.activeScene.createRootEntity();

Check warning on line 28 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L28

Added line #L28 was not covered by tests

// Create camera
const cameraEntity = rootEntity.createChild("Camera");
cameraEntity.transform.setPosition(0, 0, 50);
const camera = cameraEntity.addComponent(Camera);
camera.cullingMask = Layer.Layer0;

Check warning on line 34 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L31-L34

Added lines #L31 - L34 were not covered by tests

// Create sprite and mask
engine.resourceManager
.load([
{

Check warning on line 39 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L37-L39

Added lines #L37 - L39 were not covered by tests
// Sprite texture
url: "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*rgNGR4Vb7lQAAAAAAAAAAAAAARQnAQ",
type: AssetType.Texture2D
},
{

Check warning on line 44 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L41-L44

Added lines #L41 - L44 were not covered by tests
// Mask texture
url: "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*qyhFT5Un5AgAAAAAAAAAAAAAARQnAQ",
type: AssetType.Texture2D
}
])
.then((textures: Texture2D[]) => {
const pos = new Vector3();
const scale = new Vector3();

Check warning on line 52 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L46-L52

Added lines #L46 - L52 were not covered by tests

Comment on lines +52 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid reusing 'pos' and 'scale' variables to prevent unintended side effects

The variables pos and scale are reused when setting the position and scale of multiple entities. Since transform.position and transform.scale assign the object references, subsequent modifications to pos and scale will unintentionally affect the previously assigned entities. This can lead to unexpected behavior in the scene.

Consider creating new Vector3 instances for each entity to avoid this issue. Apply the following changes:

In the main block after line 52:

 const pos1 = new Vector3(0, 0, 0);
 const scale1 = new Vector3(5, 5, 5);

 // Use pos1 and scale1 for the first sprite renderer

Repeat for each position and scale:

 const pos2 = new Vector3(3, 3, 0);
 const scale2 = new Vector3(5, 5, 5);

 // Use pos2 and scale2 for the second sprite renderer

In the addSpriteRenderer function, modify lines 155-156:

-  transform.position = pos;
-  transform.scale = scale;
+  transform.position = pos.clone();
+  transform.scale = scale.clone();

In the addMask function, modify line 174:

-  entity.transform.position = pos;
+  entity.transform.position = pos.clone();

Also applies to: 155-156, 174-174

// Create sprites.
const sprite = new Sprite(engine, textures[0]);
const maskSprite = new Sprite(engine, textures[1]);

Check warning on line 56 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L55-L56

Added lines #L55 - L56 were not covered by tests

// create a sprite renderer, and write stencil
pos.set(0, 0, 0);
scale.set(5, 5, 5);
const writeStencilSR = addSpriteRenderer(
pos,
scale,
sprite,
SpriteMaskInteraction.None,
Layer.Layer0,
Layer.Layer0,
0
);
const writeStencilMaterial = writeStencilSR.getInstanceMaterial();
const writeStencilState = writeStencilMaterial.renderState.stencilState;
writeStencilState.enabled = true;
writeStencilState.writeMask = 0xff;
writeStencilState.passOperationFront = StencilOperation.IncrementSaturate;

Check warning on line 74 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L59-L74

Added lines #L59 - L74 were not covered by tests

// create a sprite renderer, mask interaction is none, and read stencil
pos.set(3, 3, 0);
const readStencilSR = addSpriteRenderer(
pos,
scale,
sprite,
SpriteMaskInteraction.None,
Layer.Layer0,
Layer.Layer0,
1
);
readStencilSR.color.set(1, 0, 0, 1);
const readStencilMaterial = readStencilSR.getInstanceMaterial();
const readStencilState = readStencilMaterial.renderState.stencilState;
readStencilState.enabled = true;
readStencilState.referenceValue = 1;
readStencilState.compareFunctionFront = CompareFunction.LessEqual;
readStencilState.compareFunctionBack = CompareFunction.LessEqual;

Check warning on line 93 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L77-L93

Added lines #L77 - L93 were not covered by tests

// create a sprite renderer, mask interaction is not none
pos.set(5, -3, 0);
const maskSR = addSpriteRenderer(
pos,
scale,
sprite,
SpriteMaskInteraction.VisibleOutsideMask,
Layer.Layer0,
Layer.Layer0,
2
);
maskSR.color.set(0, 1, 0, 1);

Check warning on line 106 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L96-L106

Added lines #L96 - L106 were not covered by tests

// create a sprite mask
pos.set(20, 0, 0);
addMask(pos, maskSprite, Layer.Layer0, Layer.Layer0);

Check warning on line 110 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L109-L110

Added lines #L109 - L110 were not covered by tests

// create a sprite renderer, and read stencil
pos.set(20, 10, 0);
scale.set(3, 3, 3);
const readStencilSR2 = addSpriteRenderer(
pos,
scale,
sprite,
SpriteMaskInteraction.None,
Layer.Layer0,
Layer.Layer0,
4
);
readStencilSR2.color.set(1, 0.5, 0.8, 1);
const readStencilMaterial2 = readStencilSR2.getInstanceMaterial();
const readStencilState2 = readStencilMaterial2.renderState.stencilState;
readStencilState2.enabled = true;
readStencilState2.referenceValue = 1;
readStencilState2.compareFunctionFront = CompareFunction.Greater;
readStencilState2.compareFunctionBack = CompareFunction.Greater;

Check warning on line 130 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L113-L130

Added lines #L113 - L130 were not covered by tests

updateForE2E(engine, 100, 100);
initScreenshot(engine, camera);
});

Check warning on line 134 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L132-L134

Added lines #L132 - L134 were not covered by tests

engine.run();

Check warning on line 136 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L136

Added line #L136 was not covered by tests

/**
* Add sprite renderer and set mask interaction and layer.
*/
function addSpriteRenderer(
pos: Vector3,
scale: Vector3,
sprite: Sprite,
maskInteraction: SpriteMaskInteraction,
maskLayer: number,
layer: number,
priority: number
): SpriteRenderer {
const entity = rootEntity.createChild("Sprite");
entity.layer = layer;
const renderer = entity.addComponent(SpriteRenderer);
const { transform } = entity;

Check warning on line 153 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L141-L153

Added lines #L141 - L153 were not covered by tests

transform.position = pos;
transform.scale = scale;
renderer.sprite = sprite;
renderer.maskInteraction = maskInteraction;
renderer.maskLayer = maskLayer;
renderer.priority = priority;

Check warning on line 160 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L155-L160

Added lines #L155 - L160 were not covered by tests

return renderer;
}

Check warning on line 163 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L162-L163

Added lines #L162 - L163 were not covered by tests

/**
* Add sprite mask and set influence layers, include mask animation script.
*/
function addMask<T extends Script>(pos: Vector3, sprite: Sprite, layer: number, influenceLayers: number): void {
const entity = rootEntity.createChild(`Mask`);
entity.layer = layer;
const mask = entity.addComponent(SpriteMask);

Check warning on line 171 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L168-L171

Added lines #L168 - L171 were not covered by tests

// entity.addComponent(scriptType);
entity.transform.position = pos;
mask.sprite = sprite;
mask.influenceLayers = influenceLayers;
}
Comment on lines +168 to +177
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove unused generic type parameter 'T extends Script' from 'addMask' function

The generic type parameter T extends Script in the addMask function is not used within the function body. This may cause confusion and is unnecessary. Consider removing it to simplify the function signature.

Apply this diff to remove the unused generic type parameter:

- function addMask<T extends Script>(pos: Vector3, sprite: Sprite, layer: number, influenceLayers: number): void {
+ function addMask(pos: Vector3, sprite: Sprite, layer: number, influenceLayers: number): void {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function addMask<T extends Script>(pos: Vector3, sprite: Sprite, layer: number, influenceLayers: number): void {
const entity = rootEntity.createChild(`Mask`);
entity.layer = layer;
const mask = entity.addComponent(SpriteMask);
// entity.addComponent(scriptType);
entity.transform.position = pos;
mask.sprite = sprite;
mask.influenceLayers = influenceLayers;
}
function addMask(pos: Vector3, sprite: Sprite, layer: number, influenceLayers: number): void {
const entity = rootEntity.createChild(`Mask`);
entity.layer = layer;
const mask = entity.addComponent(SpriteMask);
// entity.addComponent(scriptType);
entity.transform.position = pos;
mask.sprite = sprite;
mask.influenceLayers = influenceLayers;
}
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 168-171: e2e/case/spriteMask-customStencil.ts#L168-L171
Added lines #L168 - L171 were not covered by tests

});

Check warning on line 178 in e2e/case/spriteMask-customStencil.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/spriteMask-customStencil.ts#L174-L178

Added lines #L174 - L178 were not covered by tests
7 changes: 7 additions & 0 deletions e2e/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@
threshold: 0.2
}
},
SpriteMask: {
CustomStencil: {
category: "SpriteMask",
caseFileName: "spriteMask-customStencil",
threshold: 0.3
}
},

Check warning on line 222 in e2e/config.ts

View check run for this annotation

Codecov / codecov/patch

e2e/config.ts#L216-L222

Added lines #L216 - L222 were not covered by tests
Comment on lines +216 to +222
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Add test coverage for the new SpriteMask configuration.

There are no existing tests covering the newly added E2E_CONFIG.SpriteMask.CustomStencil configuration. Please add appropriate tests to ensure reliability and correctness.

🔗 Analysis chain

Consider adding test coverage for the new configuration.

The static analysis tool indicates that the newly added lines are not covered by tests. To ensure the reliability and correctness of the configuration, it's recommended to add appropriate test coverage for these new lines.

To verify the current test coverage and identify areas that need additional tests, you can run the following command:

Would you like assistance in generating test cases for the new SpriteMask category in the E2E_CONFIG?

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test coverage for the E2E_CONFIG object

# Test: Search for tests related to E2E_CONFIG
rg -i "E2E_CONFIG" --type ts --glob "**/*test*.ts"

# Test: Check if there are any existing tests for SpriteMask category
rg -i "SpriteMask" --type ts --glob "**/*test*.ts"

Length of output: 10608

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 216-222: e2e/config.ts#L216-L222
Added lines #L216 - L222 were not covered by tests

Text: {
TypedText: {
category: "Text",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions examples/sprite-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
// Create sprites.
const sprite = new Sprite(engine, textures[0]);
const maskSprite0 = new Sprite(engine, textures[1]);
const maksSprite1 = new Sprite(engine, textures[2]);
const maskSprite1 = new Sprite(engine, textures[2]);

Check warning on line 61 in examples/sprite-mask.ts

View check run for this annotation

Codecov / codecov/patch

examples/sprite-mask.ts#L61

Added line #L61 was not covered by tests

// Show inside mask.
pos.set(-5, 0, 0);
Expand All @@ -82,7 +82,7 @@
SpriteMaskInteraction.VisibleOutsideMask,
SpriteMaskLayer.Layer1
);
addMask(pos, maksSprite1, SpriteMaskLayer.Layer1, RotationScript);
addMask(pos, maskSprite1, SpriteMaskLayer.Layer1, RotationScript);

Check warning on line 85 in examples/sprite-mask.ts

View check run for this annotation

Codecov / codecov/patch

examples/sprite-mask.ts#L85

Added line #L85 was not covered by tests
});

engine.run();
Expand Down Expand Up @@ -114,7 +114,7 @@
function addMask<T extends Script>(
pos: Vector3,
sprite: Sprite,
influenceLayers: number,
influenceLayers: SpriteMaskLayer,

Check warning on line 117 in examples/sprite-mask.ts

View check run for this annotation

Codecov / codecov/patch

examples/sprite-mask.ts#L117

Added line #L117 was not covered by tests
scriptType: new (entity: Entity) => T
): void {
const entity = rootEntity.createChild("Mask");
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/2d/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { SpriteMaskInteraction } from "./enums/SpriteMaskInteraction";
export { SpriteMaskLayer } from "./enums/SpriteMaskLayer";
export { TextHorizontalAlignment, TextVerticalAlignment } from "./enums/TextAlignment";
export { OverflowMode } from "./enums/TextOverflow";
export { FontStyle } from "./enums/FontStyle";
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import { SubRenderElement } from "../../RenderPipeline/SubRenderElement";
import { Renderer, RendererUpdateFlags } from "../../Renderer";
import { assignmentClone, ignoreClone } from "../../clone/CloneManager";
import { SpriteMaskLayer } from "../../enums/SpriteMaskLayer";
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
import { ShaderProperty } from "../../shader/ShaderProperty";
import { SimpleSpriteAssembler } from "../assembler/SimpleSpriteAssembler";
import { SpriteMaskLayer } from "../enums/SpriteMaskLayer";
import { SpriteModifyFlags } from "../enums/SpriteModifyFlags";
import { Sprite } from "./Sprite";

Expand All @@ -26,7 +26,7 @@

/** The mask layers the sprite mask influence to. */
@assignmentClone
influenceLayers: number = SpriteMaskLayer.Everything;
influenceLayers: SpriteMaskLayer = SpriteMaskLayer.Everything;
/** @internal */
@ignoreClone
_renderElement: RenderElement;
Expand Down Expand Up @@ -177,7 +177,7 @@
constructor(entity: Entity) {
super(entity);
SimpleSpriteAssembler.resetData(this);
this.setMaterial(this._engine._spriteMaskDefaultMaterial);
this.setMaterial(this._engine._basicResources.spriteMaskDefaultMaterial);
this.shaderData.setFloat(SpriteMask._alphaCutoffProperty, this._alphaCutoff);
this._renderElement = new RenderElement();
this._renderElement.addSubRenderElement(new SubRenderElement());
Expand Down Expand Up @@ -261,7 +261,7 @@
const { _engine: engine } = this;
// @todo: This question needs to be raised rather than hidden.
if (material.destroyed) {
material = engine._spriteMaskDefaultMaterial;
material = engine._basicResources.spriteMaskDefaultMaterial;

Check warning on line 264 in packages/core/src/2d/sprite/SpriteMask.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/2d/sprite/SpriteMask.ts#L264

Added line #L264 was not covered by tests
}

// Update position
Expand Down
28 changes: 2 additions & 26 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { Renderer, RendererUpdateFlags } from "../../Renderer";
import { assignmentClone, deepClone, ignoreClone } from "../../clone/CloneManager";
import { ShaderProperty } from "../../shader/ShaderProperty";
import { CompareFunction } from "../../shader/enums/CompareFunction";
import { ISpriteAssembler } from "../assembler/ISpriteAssembler";
import { SimpleSpriteAssembler } from "../assembler/SimpleSpriteAssembler";
import { SlicedSpriteAssembler } from "../assembler/SlicedSpriteAssembler";
Expand Down Expand Up @@ -257,7 +256,6 @@

set maskInteraction(value: SpriteMaskInteraction) {
if (this._maskInteraction !== value) {
this._updateStencilState(this._maskInteraction, value);
this._maskInteraction = value;
}
}
Expand All @@ -269,7 +267,7 @@
super(entity);
this.drawMode = SpriteDrawMode.Simple;
this._dirtyUpdateFlag |= SpriteRendererUpdateFlags.Color;
this.setMaterial(this._engine._spriteDefaultMaterial);
this.setMaterial(this._engine._basicResources.spriteDefaultMaterial);
this._onSpriteChange = this._onSpriteChange.bind(this);
//@ts-ignore
this._color._onValueChanged = this._onColorChanged.bind(this);
Expand Down Expand Up @@ -334,7 +332,7 @@
}
// @todo: This question needs to be raised rather than hidden.
if (material.destroyed) {
material = this._engine._spriteDefaultMaterials[this._maskInteraction];
material = this._engine._basicResources.spriteDefaultMaterial;

Check warning on line 335 in packages/core/src/2d/sprite/SpriteRenderer.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/2d/sprite/SpriteRenderer.ts#L335

Added line #L335 was not covered by tests
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add test coverage for material fallback logic

The line material = this._engine._basicResources.spriteDefaultMaterial; introduces new logic to handle cases where the material has been destroyed. However, static analysis indicates that this line is not covered by existing tests.

Consider adding a unit test to cover this scenario to ensure that the fallback to the default material behaves as expected when the original material is destroyed.

Would you like assistance in creating a test case for this?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 335-335: packages/core/src/2d/sprite/SpriteRenderer.ts#L335
Added line #L335 was not covered by tests

}

// Update position
Expand Down Expand Up @@ -395,28 +393,6 @@
this._dirtyUpdateFlag &= ~SpriteRendererUpdateFlags.AutomaticSize;
}

private _updateStencilState(from: SpriteMaskInteraction, to: SpriteMaskInteraction): void {
const material = this.getMaterial();
const { _spriteDefaultMaterials: spriteDefaultMaterials } = this._engine;
if (material === spriteDefaultMaterials[from]) {
this.setMaterial(spriteDefaultMaterials[to]);
} else {
const { stencilState } = material.renderState;
if (to === SpriteMaskInteraction.None) {
stencilState.enabled = false;
stencilState.writeMask = 0xff;
stencilState.referenceValue = 0;
stencilState.compareFunctionFront = stencilState.compareFunctionBack = CompareFunction.Always;
} else {
stencilState.enabled = true;
stencilState.writeMask = 0x00;
stencilState.referenceValue = 1;
stencilState.compareFunctionFront = stencilState.compareFunctionBack =
to === SpriteMaskInteraction.VisibleInsideMask ? CompareFunction.LessEqual : CompareFunction.Greater;
}
}
}

@ignoreClone
private _onSpriteChange(type: SpriteModifyFlags): void {
switch (type) {
Expand Down
Loading
Loading