Skip to content

Commit

Permalink
refactor: extract some methods of the 2D renderers from the Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
eyworldwide committed Sep 26, 2024
1 parent 214b8e6 commit ee29a4f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 75 deletions.
14 changes: 14 additions & 0 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { SimpleSpriteAssembler } from "../assembler/SimpleSpriteAssembler";
import { SpriteMaskLayer } from "../enums/SpriteMaskLayer";
import { SpriteModifyFlags } from "../enums/SpriteModifyFlags";
import { Sprite } from "./Sprite";
import { Material } from "../../material";
import { ColorWriteMask, CullMode, RenderQueueType, Shader } from "../../shader";

/**
* A component for masking Sprites.
Expand All @@ -24,6 +26,18 @@ export class SpriteMask extends Renderer {
/** @internal */
static _alphaCutoffProperty: ShaderProperty = ShaderProperty.getByName("renderer_MaskAlphaCutoff");

static _createSpriteMaskMaterial(engine): Material {
const material = new Material(engine, Shader.find("SpriteMask"));
const renderState = material.renderState;
renderState.blendState.targetBlendState.colorWriteMask = ColorWriteMask.None;
renderState.rasterState.cullMode = CullMode.Off;
renderState.stencilState.enabled = true;
renderState.depthState.enabled = false;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}

/** The mask layers the sprite mask influence to. */
@assignmentClone
influenceLayers: number = SpriteMaskLayer.Everything;
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { SpriteMaskInteraction } from "../enums/SpriteMaskInteraction";
import { SpriteModifyFlags } from "../enums/SpriteModifyFlags";
import { SpriteTileMode } from "../enums/SpriteTileMode";
import { Sprite } from "./Sprite";
import { Material } from "../../material";
import { BlendFactor, BlendOperation, CullMode, RenderQueueType, Shader } from "../../shader";

/**
* Renders a Sprite for 2D graphics.
Expand All @@ -26,6 +28,35 @@ export class SpriteRenderer extends Renderer {
/** @internal */
static _textureProperty: ShaderProperty = ShaderProperty.getByName("renderer_SpriteTexture");

static _createSpriteMaterial(engine, maskInteraction: SpriteMaskInteraction): Material {
const material = new Material(engine, Shader.find("Sprite"));
const renderState = material.renderState;
const target = renderState.blendState.targetBlendState;
target.enabled = true;
target.sourceColorBlendFactor = BlendFactor.SourceAlpha;
target.destinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.sourceAlphaBlendFactor = BlendFactor.One;
target.destinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.colorBlendOperation = target.alphaBlendOperation = BlendOperation.Add;
if (maskInteraction !== SpriteMaskInteraction.None) {
const stencilState = renderState.stencilState;
stencilState.enabled = true;
stencilState.writeMask = 0x00;
stencilState.referenceValue = 1;
const compare =
maskInteraction === SpriteMaskInteraction.VisibleInsideMask
? CompareFunction.LessEqual
: CompareFunction.Greater;
stencilState.compareFunctionFront = compare;
stencilState.compareFunctionBack = compare;
}
renderState.depthState.writeEnabled = false;
renderState.rasterState.cullMode = CullMode.Off;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}

/** @internal */
@ignoreClone
_subChunk: SubPrimitiveChunk;
Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/2d/text/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SubRenderElement } from "../../RenderPipeline/SubRenderElement";
import { Renderer } from "../../Renderer";
import { TransformModifyFlags } from "../../Transform";
import { assignmentClone, deepClone, ignoreClone } from "../../clone/CloneManager";
import { ShaderData, ShaderProperty } from "../../shader";
import { BlendFactor, BlendOperation, CullMode, RenderQueueType, Shader, ShaderData, ShaderProperty } from "../../shader";

Check failure on line 12 in packages/core/src/2d/text/TextRenderer.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·BlendFactor,·BlendOperation,·CullMode,·RenderQueueType,·Shader,·ShaderData,·ShaderProperty·` with `⏎··BlendFactor,⏎··BlendOperation,⏎··CullMode,⏎··RenderQueueType,⏎··Shader,⏎··ShaderData,⏎··ShaderProperty⏎`
import { CompareFunction } from "../../shader/enums/CompareFunction";
import { ShaderDataGroup } from "../../shader/enums/ShaderDataGroup";
import { Texture2D } from "../../texture";
Expand All @@ -21,6 +21,7 @@ import { CharRenderInfo } from "./CharRenderInfo";
import { Font } from "./Font";
import { SubFont } from "./SubFont";
import { TextUtils } from "./TextUtils";
import { Material } from "../../material";

/**
* Renders a text for 2D graphics.
Expand All @@ -32,6 +33,23 @@ export class TextRenderer extends Renderer {
private static _worldPositions = [new Vector3(), new Vector3(), new Vector3(), new Vector3()];
private static _charRenderInfos: CharRenderInfo[] = [];

static _createTextMaterial(engine): Material {
const material = new Material(engine, Shader.find("Text"));
const renderState = material.renderState;
const target = renderState.blendState.targetBlendState;
target.enabled = true;
target.sourceColorBlendFactor = BlendFactor.SourceAlpha;
target.destinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.sourceAlphaBlendFactor = BlendFactor.One;
target.destinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.colorBlendOperation = target.alphaBlendOperation = BlendOperation.Add;
renderState.depthState.writeEnabled = false;
renderState.rasterState.cullMode = CullMode.Off;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}

/** @internal */
@ignoreClone
_textChunks = Array<TextChunk>();
Expand Down
80 changes: 6 additions & 74 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IXRDevice
} from "@galacean/engine-design";
import { Color } from "@galacean/engine-math";
import { SpriteMaskInteraction } from "./2d";
import { SpriteMask, SpriteMaskInteraction, SpriteRenderer, TextRenderer } from "./2d";
import { CharRenderInfo } from "./2d/text/CharRenderInfo";
import { Font } from "./2d/text/Font";
import { BasicResources } from "./BasicResources";
Expand All @@ -30,18 +30,12 @@ import { Material } from "./material/Material";
import { ParticleBufferUtils } from "./particle/ParticleBufferUtils";
import { PhysicsScene } from "./physics/PhysicsScene";
import { ColliderShape } from "./physics/shape/ColliderShape";
import { CompareFunction } from "./shader";
import { Shader } from "./shader/Shader";
import { ShaderMacro } from "./shader/ShaderMacro";
import { ShaderMacroCollection } from "./shader/ShaderMacroCollection";
import { ShaderPass } from "./shader/ShaderPass";
import { ShaderPool } from "./shader/ShaderPool";
import { ShaderProgramPool } from "./shader/ShaderProgramPool";
import { BlendFactor } from "./shader/enums/BlendFactor";
import { BlendOperation } from "./shader/enums/BlendOperation";
import { ColorWriteMask } from "./shader/enums/ColorWriteMask";
import { CullMode } from "./shader/enums/CullMode";
import { RenderQueueType } from "./shader/enums/RenderQueueType";
import { RenderState } from "./shader/state/RenderState";
import { Texture2D, TextureFormat } from "./texture";
import { ClearableObjectPool } from "./utils/ClearableObjectPool";
Expand Down Expand Up @@ -240,17 +234,11 @@ export class Engine extends EventDispatcher {
this._canvas = canvas;

const { _spriteDefaultMaterials: spriteDefaultMaterials } = this;
this._spriteDefaultMaterial = spriteDefaultMaterials[SpriteMaskInteraction.None] = this._createSpriteMaterial(
SpriteMaskInteraction.None
);
spriteDefaultMaterials[SpriteMaskInteraction.VisibleInsideMask] = this._createSpriteMaterial(
SpriteMaskInteraction.VisibleInsideMask
);
spriteDefaultMaterials[SpriteMaskInteraction.VisibleOutsideMask] = this._createSpriteMaterial(
SpriteMaskInteraction.VisibleOutsideMask
);
this._textDefaultMaterial = this._createTextMaterial();
this._spriteMaskDefaultMaterial = this._createSpriteMaskMaterial();
this._spriteDefaultMaterial = spriteDefaultMaterials[SpriteMaskInteraction.None] = SpriteRenderer._createSpriteMaterial(this, SpriteMaskInteraction.None);

Check failure on line 237 in packages/core/src/Engine.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·····`
spriteDefaultMaterials[SpriteMaskInteraction.VisibleInsideMask] = SpriteRenderer._createSpriteMaterial(this, SpriteMaskInteraction.VisibleInsideMask);

Check failure on line 238 in packages/core/src/Engine.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `this,·SpriteMaskInteraction.VisibleInsideMask` with `⏎······this,⏎······SpriteMaskInteraction.VisibleInsideMask⏎····`
spriteDefaultMaterials[SpriteMaskInteraction.VisibleOutsideMask] = SpriteRenderer._createSpriteMaterial(this, SpriteMaskInteraction.VisibleOutsideMask);

Check failure on line 239 in packages/core/src/Engine.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `this,·SpriteMaskInteraction.VisibleOutsideMask` with `⏎······this,⏎······SpriteMaskInteraction.VisibleOutsideMask⏎····`
this._textDefaultMaterial = TextRenderer._createTextMaterial(this);
this._spriteMaskDefaultMaterial = SpriteMask._createSpriteMaskMaterial(this);
this._textDefaultFont = Font.createFromOS(this, "Arial");
this._textDefaultFont.isGCIgnored = true;

Expand Down Expand Up @@ -581,63 +569,7 @@ export class Engine extends EventDispatcher {
return Promise.all(initializePromises).then(() => this);
}

Check failure on line 570 in packages/core/src/Engine.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎⏎`

private _createSpriteMaterial(maskInteraction: SpriteMaskInteraction): Material {
const material = new Material(this, Shader.find("Sprite"));
const renderState = material.renderState;
const target = renderState.blendState.targetBlendState;
target.enabled = true;
target.sourceColorBlendFactor = BlendFactor.SourceAlpha;
target.destinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.sourceAlphaBlendFactor = BlendFactor.One;
target.destinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.colorBlendOperation = target.alphaBlendOperation = BlendOperation.Add;
if (maskInteraction !== SpriteMaskInteraction.None) {
const stencilState = renderState.stencilState;
stencilState.enabled = true;
stencilState.writeMask = 0x00;
stencilState.referenceValue = 1;
const compare =
maskInteraction === SpriteMaskInteraction.VisibleInsideMask
? CompareFunction.LessEqual
: CompareFunction.Greater;
stencilState.compareFunctionFront = compare;
stencilState.compareFunctionBack = compare;
}
renderState.depthState.writeEnabled = false;
renderState.rasterState.cullMode = CullMode.Off;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}

private _createSpriteMaskMaterial(): Material {
const material = new Material(this, Shader.find("SpriteMask"));
const renderState = material.renderState;
renderState.blendState.targetBlendState.colorWriteMask = ColorWriteMask.None;
renderState.rasterState.cullMode = CullMode.Off;
renderState.stencilState.enabled = true;
renderState.depthState.enabled = false;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}

private _createTextMaterial(): Material {
const material = new Material(this, Shader.find("Text"));
const renderState = material.renderState;
const target = renderState.blendState.targetBlendState;
target.enabled = true;
target.sourceColorBlendFactor = BlendFactor.SourceAlpha;
target.destinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.sourceAlphaBlendFactor = BlendFactor.One;
target.destinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.colorBlendOperation = target.alphaBlendOperation = BlendOperation.Add;
renderState.depthState.writeEnabled = false;
renderState.rasterState.cullMode = CullMode.Off;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}

private _onDeviceLost(): void {
this._isDeviceLost = true;
Expand Down

0 comments on commit ee29a4f

Please sign in to comment.