-
-
Notifications
You must be signed in to change notification settings - Fork 395
Add UI Mask && Clean up Mask code #2912
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
Draft
cptbtptpbcptdtptp
wants to merge
12
commits into
galacean:dev/2.0
Choose a base branch
from
cptbtptpbcptdtptp:feat/UIMask
base: dev/2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,344
−273
Draft
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
36dc785
feat: update code
cptbtptpbcptdtptp 11bfc9d
feat: add ui mask
cptbtptpbcptdtptp 6b8b328
feat: add mask
cptbtptpbcptdtptp f4a23c3
feat: update comments
cptbtptpbcptdtptp ececde4
fix: correct ui rect mask clip coordinates
cptbtptpbcptdtptp 03ee15e
feat: add rect mask
cptbtptpbcptdtptp 1922888
fix: lint
cptbtptpbcptdtptp 15a08bf
feat: add e2e for rect mask
cptbtptpbcptdtptp 0139751
Merge remote-tracking branch 'origin/dev/2.0' into feat/UIMask
cptbtptpbcptdtptp 8b6515d
Merge remote-tracking branch 'origin/dev/2.0' into feat/UIMask
cptbtptpbcptdtptp a1c0d6d
feat: update e2e
cptbtptpbcptdtptp 0113170
feat: add ui mask
cptbtptpbcptdtptp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /** | ||
| * @title UI Mask Raycast | ||
| * @category UI | ||
| */ | ||
| import { | ||
| Camera, | ||
| Color, | ||
| Entity, | ||
| PointerEventData, | ||
| Script, | ||
| Sprite, | ||
| SpriteMaskInteraction, | ||
| Texture2D, | ||
| TextureFormat, | ||
| WebGLEngine | ||
| } from "@galacean/engine"; | ||
| import { CanvasRenderMode, Image, Mask, Text, UICanvas, UITransform } from "@galacean/engine-ui"; | ||
|
|
||
| class ClickCounter extends Script { | ||
| label: string = ""; | ||
| counterText: Text = null; | ||
| private _count: number = 0; | ||
|
|
||
| override onPointerClick(_eventData: PointerEventData): void { | ||
| this._count += 1; | ||
| if (this.counterText) { | ||
| this.counterText.text = `${this.label}: ${this._count}`; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| const engine = await WebGLEngine.create({ canvas: "canvas" }); | ||
| engine.canvas.resizeByClientSize(); | ||
| window.addEventListener("resize", () => engine.canvas.resizeByClientSize()); | ||
|
|
||
| const scene = engine.sceneManager.activeScene; | ||
| const root = scene.createRootEntity("Root"); | ||
|
|
||
| const cameraEntity = root.createChild("Camera"); | ||
| cameraEntity.transform.setPosition(0, 0, 10); | ||
| cameraEntity.addComponent(Camera); | ||
|
|
||
| const canvasEntity = root.createChild("UICanvas"); | ||
| const uiCanvas = canvasEntity.addComponent(UICanvas); | ||
| uiCanvas.renderMode = CanvasRenderMode.ScreenSpaceOverlay; | ||
| uiCanvas.referenceResolutionPerUnit = 100; | ||
| uiCanvas.referenceResolution.set(1000, 700); | ||
|
|
||
| const solidSprite = createSolidSprite(engine); | ||
| const circleSprite = createCircleSprite(engine, 256); | ||
|
|
||
| const panelSize = { width: 760, height: 420 }; | ||
|
|
||
| const outsideEntity = canvasEntity.createChild("OutsidePanel"); | ||
| const outsideImage = outsideEntity.addComponent(Image); | ||
| (outsideEntity.transform as UITransform).size.set(panelSize.width, panelSize.height); | ||
| outsideImage.sprite = solidSprite; | ||
| outsideImage.color.set(0.2, 0.56, 0.96, 0.95); | ||
| outsideImage.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask; | ||
|
|
||
| const insideEntity = canvasEntity.createChild("InsidePanel"); | ||
| const insideImage = insideEntity.addComponent(Image); | ||
| (insideEntity.transform as UITransform).size.set(panelSize.width, panelSize.height); | ||
| insideImage.sprite = solidSprite; | ||
| insideImage.color.set(0.96, 0.36, 0.28, 0.95); | ||
| insideImage.maskInteraction = SpriteMaskInteraction.VisibleInsideMask; | ||
|
|
||
| const maskEntity = canvasEntity.createChild("Mask"); | ||
| const maskTransform = maskEntity.transform as UITransform; | ||
| maskTransform.size.set(280, 280); | ||
| const mask = maskEntity.addComponent(Mask); | ||
| mask.sprite = circleSprite; | ||
| mask.alphaCutoff = 0.1; | ||
|
|
||
| const maskPreviewEntity = maskEntity.createChild("MaskPreview"); | ||
| const maskPreview = maskPreviewEntity.addComponent(Image); | ||
| (maskPreviewEntity.transform as UITransform).size.set(280, 280); | ||
| maskPreview.sprite = circleSprite; | ||
| maskPreview.color.set(1, 1, 1, 0.22); | ||
| maskPreview.raycastEnabled = false; | ||
|
|
||
| createLabel(canvasEntity, "Title", "UI Mask + Raycast", 300, 44, new Color(1, 1, 1, 1)); | ||
| createLabel( | ||
| canvasEntity, | ||
| "Hint", | ||
| "Click red center (inside mask) and blue edge (outside mask).", | ||
| 250, | ||
| 24, | ||
| new Color(1, 1, 1, 0.95) | ||
| ); | ||
|
|
||
| const insideCountLabel = createLabel(canvasEntity, "InsideCount", "Inside hits: 0", -260, 30, new Color(1, 0.85, 0.8, 1)); | ||
| const outsideCountLabel = createLabel( | ||
| canvasEntity, | ||
| "OutsideCount", | ||
| "Outside hits: 0", | ||
| -305, | ||
| 30, | ||
| new Color(0.78, 0.9, 1, 1) | ||
| ); | ||
|
|
||
| const insideCounter = insideEntity.addComponent(ClickCounter); | ||
| insideCounter.label = "Inside hits"; | ||
| insideCounter.counterText = insideCountLabel; | ||
|
|
||
| const outsideCounter = outsideEntity.addComponent(ClickCounter); | ||
| outsideCounter.label = "Outside hits"; | ||
| outsideCounter.counterText = outsideCountLabel; | ||
|
|
||
| engine.run(); | ||
| } | ||
|
|
||
| main(); | ||
|
|
||
| function createSolidSprite(engine: WebGLEngine): Sprite { | ||
| const texture = new Texture2D(engine, 1, 1, TextureFormat.R8G8B8A8, false); | ||
| texture.setPixelBuffer(new Uint8Array([255, 255, 255, 255])); | ||
| return new Sprite(engine, texture); | ||
| } | ||
|
|
||
| function createCircleSprite(engine: WebGLEngine, size: number): Sprite { | ||
| const canvas = document.createElement("canvas"); | ||
| canvas.width = size; | ||
| canvas.height = size; | ||
|
|
||
| const context = canvas.getContext("2d"); | ||
| if (!context) { | ||
| throw new Error("Failed to create canvas 2D context."); | ||
| } | ||
|
|
||
| context.clearRect(0, 0, size, size); | ||
| context.fillStyle = "#ffffff"; | ||
| context.beginPath(); | ||
| context.arc(size * 0.5, size * 0.5, size * 0.46, 0, Math.PI * 2); | ||
| context.closePath(); | ||
| context.fill(); | ||
|
|
||
| const texture = new Texture2D(engine, size, size, TextureFormat.R8G8B8A8, false); | ||
| texture.setImageSource(canvas); | ||
| return new Sprite(engine, texture); | ||
| } | ||
|
|
||
| function createLabel( | ||
| parent: Entity, | ||
| name: string, | ||
| content: string, | ||
| y: number, | ||
| fontSize: number, | ||
| color: Color | ||
| ): Text { | ||
| const entity = parent.createChild(name); | ||
| const transform = entity.transform as UITransform; | ||
| transform.size.set(980, 64); | ||
| transform.setPosition(0, y, 0); | ||
|
|
||
| const label = entity.addComponent(Text); | ||
| label.text = content; | ||
| label.fontSize = fontSize; | ||
| label.color = color; | ||
| label.raycastEnabled = false; | ||
|
|
||
| return label; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prettier violations are failing lint in this example file.
Please format Line 93 and Lines 144-151 to match the current prettier config.
🎯 Formatting patch
Also applies to: 144-151
🧰 Tools
🪛 ESLint
[error] 93-93: Replace
canvasEntity,·"InsideCount",·"Inside·hits:·0",·-260,·30,·new·Color(1,·0.85,·0.8,·1)with⏎····canvasEntity,⏎····"InsideCount",⏎····"Inside·hits:·0",⏎····-260,⏎····30,⏎····new·Color(1,·0.85,·0.8,·1)⏎··(prettier/prettier)
🤖 Prompt for AI Agents