Skip to content

Commit

Permalink
refactor(mask): add e2d
Browse files Browse the repository at this point in the history
  • Loading branch information
singlecoder committed Oct 22, 2024
1 parent 501eb00 commit a010ade
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
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

// 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;
}
});

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 @@ export const E2E_CONFIG = {
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
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.

0 comments on commit a010ade

Please sign in to comment.