From 4d2720d3056b972b7cb5fccc804dcc1d3f79dcf3 Mon Sep 17 00:00:00 2001 From: Matheus Clemente Date: Thu, 11 Apr 2024 15:03:43 -0300 Subject: [PATCH] 3D Canvas module support Fix #83. --- src/module/healthEstimate.js | 1 + src/module/logic.js | 40 +++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/module/healthEstimate.js b/src/module/healthEstimate.js index f3c8531..0b698a4 100644 --- a/src/module/healthEstimate.js +++ b/src/module/healthEstimate.js @@ -13,6 +13,7 @@ Hooks.once("ready", () => game.healthEstimate.ready()); // Canvas Hooks.once("canvasReady", HealthEstimateHooks.onceCanvasReady); Hooks.on("canvasReady", HealthEstimateHooks.onCanvasReady); +Hooks.on("3DCanvasSceneReady", () => setTimeout(HealthEstimateHooks.onCanvasReady, 10)); Hooks.on("createToken", HealthEstimateHooks.onCreateToken); // Actor diff --git a/src/module/logic.js b/src/module/logic.js index 6e81e36..01efbca 100644 --- a/src/module/logic.js +++ b/src/module/logic.js @@ -89,7 +89,7 @@ export class HealthEstimate { * @param {Token} token * @param {Boolean} hovered */ - _handleOverlay(token, hovered) { + async _handleOverlay(token, hovered) { if ( !token?.actor || this.breakOverlayRender(token) @@ -118,9 +118,47 @@ export class HealthEstimate { token.healthEstimate.visible = true; token.healthEstimate.position.set(token.tooltip.x, (token.tooltip.x * position) + yPosition); } + if (game.Levels3DPreview?._active) { + const { tokens, THREE } = game.Levels3DPreview; + const token3d = tokens[token.id]; + + const getThreeSpriteMaterial = async (text) => { + const userTextStyle = this._getUserTextStyle(color, stroke); + text = new PIXI.Text(desc, userTextStyle); + const container = new PIXI.Container(); + container.addChild(text); + const base64 = await canvas.app.renderer.extract.base64(container); + const spriteMaterial = new THREE.SpriteMaterial({ + map: await new THREE.TextureLoader().loadAsync(base64), + transparent: true, + alphaTest: 0.1, + }); + spriteMaterial.pixiText = text; + return spriteMaterial; + }; + + const spriteMaterial = await getThreeSpriteMaterial("text"); + const sprite = new THREE.Sprite(spriteMaterial); + sprite.center.set(0.5, 0.5); + + token3d.mesh.remove(token3d.healthEstimate); + token3d.healthEstimate = sprite; + token3d.healthEstimate.userData.ignoreIntersect = true; + token3d.healthEstimate.userData.ignoreHover = true; + const width = spriteMaterial.pixiText.width / token3d.factor; + const height = spriteMaterial.pixiText.height / token3d.factor; + token3d.healthEstimate.scale.set(width, height, 1); + token3d.healthEstimate.position.set(0, token3d.d + (height / 2) + 0.042, 0); + token3d.mesh.add(token3d.healthEstimate); + } } } else if (token.healthEstimate) { token.healthEstimate.visible = false; + if (game.Levels3DPreview?._active) { + const { tokens } = game.Levels3DPreview; + const token3d = tokens[token.id]; + if (token3d.healthEstimate) token3d.healthEstimate.visible = false; + } } } catch(err) { console.error(