@mirage-engine/painter 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @mirage-engine/painter
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ba0576d: provides Painter API
8
+
9
+ ## 0.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 14a8264: provides Painter API
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 siwoo lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # @mirage-engine/painter
2
+
3
+ [![npm](https://img.shields.io/npm/v/@mirage-engine/painter.svg?color=black)](https://www.npmjs.com/package/@mirage-engine/painter)
4
+
5
+ A standalone text & style texture generator for Three.js.
6
+
7
+ ## Usage
8
+
9
+ ```typescript
10
+ import { Painter } from "@mirage-engine/painter";
11
+ const geometry = new THREE.PlaneGeometry(1, 1);
12
+
13
+ const styles = {
14
+ backgroundColor: "#ff0000",
15
+ borderColor: "#000000",
16
+ borderWidth: 2,
17
+ borderRadius: 5,
18
+ }
19
+
20
+ const material = Painter.create(
21
+ "BOX",
22
+ styles,
23
+ "",
24
+ 50, //width
25
+ 50, //height
26
+ );
27
+
28
+ mesh = new THREE.Mesh(geometry, material);
29
+ ```
@@ -0,0 +1,170 @@
1
+ import * as l from "three";
2
+ function b(o, e, t) {
3
+ const n = e.split(`
4
+ `), r = [];
5
+ return n.forEach((i) => {
6
+ const a = i.split(" ");
7
+ let u = a[0];
8
+ for (let c = 1; c < a.length; c++) {
9
+ const d = a[c];
10
+ o.measureText(u + " " + d).width < t ? u += " " + d : (r.push(u), u = d);
11
+ }
12
+ r.push(u);
13
+ }), r;
14
+ }
15
+ function m(o, e, t, n, r = 2) {
16
+ const i = document.createElement("canvas"), a = i.getContext("2d");
17
+ if (!a)
18
+ throw new Error("[Mirage] Failed to create canvas context");
19
+ const c = (window.devicePixelRatio || 1) * r;
20
+ i.width = t * c, i.height = n * c, a.scale(c, c), a.font = e.font, a.fillStyle = e.color, a.textBaseline = "top", a.globalAlpha = 1;
21
+ const d = b(a, o, t), v = e.lineHeight;
22
+ d.forEach((x, g) => {
23
+ const B = g * v + 2;
24
+ let h = 0;
25
+ e.textAlign === "center" ? h = t / 2 : e.textAlign === "right" && (h = t), a.textAlign = e.textAlign, a.fillText(x, h, B);
26
+ });
27
+ const s = new l.CanvasTexture(i);
28
+ return s.colorSpace = l.SRGBColorSpace, s.minFilter = l.LinearFilter, s.magFilter = l.LinearFilter, s.needsUpdate = !0, s;
29
+ }
30
+ function p(o) {
31
+ return typeof o == "number" ? o : parseFloat(o) || 0;
32
+ }
33
+ const C = `
34
+ varying vec2 vUv;
35
+ void main() {
36
+ vUv = uv;
37
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
38
+ }
39
+ `, w = `
40
+ varying vec2 vUv;
41
+
42
+ uniform vec2 uSize;
43
+ uniform float uRadius;
44
+ uniform float uBorderWidth;
45
+ uniform vec3 uColor;
46
+ uniform vec3 uBorderColor;
47
+ uniform float uOpacity;
48
+ uniform float uBgOpacity;
49
+
50
+ // SDF 박스 함수
51
+ float sdRoundedBox(vec2 p, vec2 b, float r) {
52
+ vec2 q = abs(p) - b + r;
53
+ return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r;
54
+ }
55
+
56
+ void main() {
57
+ vec2 p = (vUv - 0.5) * uSize;
58
+ vec2 halfSize = uSize * 0.5;
59
+
60
+ float d = sdRoundedBox(p, halfSize, uRadius);
61
+
62
+ float smoothEdge = 1.0;
63
+
64
+ float fillAlpha = 1.0 - smoothstep(-uBorderWidth - smoothEdge, -uBorderWidth, d);
65
+
66
+ float borderAlpha = 0.0;
67
+
68
+ if (uBorderWidth > 0.01) {
69
+ borderAlpha = (1.0 - smoothstep(0.0, smoothEdge, d)) - fillAlpha;
70
+ }
71
+
72
+ vec3 color = uColor;
73
+ float totalAlpha = borderAlpha + fillAlpha;
74
+
75
+ if (totalAlpha > 0.001) {
76
+ color = mix(uColor, uBorderColor, borderAlpha / totalAlpha);
77
+ }
78
+
79
+ float shapeAlpha = borderAlpha + (fillAlpha * uBgOpacity);
80
+ float finalOpacity = shapeAlpha * uOpacity;
81
+
82
+ if (finalOpacity < 0.001) discard;
83
+
84
+ gl_FragColor = vec4(color, finalOpacity);
85
+ }
86
+ `;
87
+ function f(o) {
88
+ if (!o || o === "transparent")
89
+ return { color: new l.Color(16777215), alpha: 0 };
90
+ const e = o.match(
91
+ /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/
92
+ );
93
+ if (e) {
94
+ const t = parseInt(e[1], 10), n = parseInt(e[2], 10), r = parseInt(e[3], 10), i = e[4] !== void 0 ? parseFloat(e[4]) : 1;
95
+ return { color: new l.Color(`rgb(${t}, ${n}, ${r})`), alpha: i };
96
+ }
97
+ return { color: new l.Color(o), alpha: 1 };
98
+ }
99
+ function A(o, e, t) {
100
+ const n = f(o.backgroundColor), r = f(o.borderColor), i = {
101
+ uSize: { value: new l.Vector2(e, t) },
102
+ uRadius: { value: p(o.borderRadius) },
103
+ uBorderWidth: { value: p(o.borderWidth) },
104
+ uColor: { value: n.color },
105
+ uBorderColor: { value: r.color },
106
+ uOpacity: { value: o.opacity ?? 1 },
107
+ uBgOpacity: { value: n.alpha }
108
+ };
109
+ return new l.ShaderMaterial({
110
+ uniforms: i,
111
+ vertexShader: C,
112
+ fragmentShader: w,
113
+ transparent: !0,
114
+ side: l.FrontSide
115
+ // for better performance
116
+ });
117
+ }
118
+ function S(o, e, t, n) {
119
+ const r = f(e.backgroundColor), i = f(e.borderColor);
120
+ o.uniforms.uSize.value.set(t, n), o.uniforms.uRadius.value = p(e.borderRadius), o.uniforms.uBorderWidth.value = p(e.borderWidth), o.uniforms.uColor.value.copy(r.color), o.uniforms.uBorderColor.value.copy(i.color), o.uniforms.uOpacity.value = e.opacity ?? 1, o.uniforms.uBgOpacity.value = r.alpha;
121
+ }
122
+ const O = {
123
+ create(o, e, t, n, r, i = 2) {
124
+ if (o === "BOX")
125
+ return A(e, n, r);
126
+ if (o === "TEXT") {
127
+ const a = m(
128
+ t || "",
129
+ e,
130
+ n,
131
+ r,
132
+ i
133
+ );
134
+ return new l.MeshBasicMaterial({
135
+ map: a,
136
+ transparent: !0,
137
+ side: l.FrontSide,
138
+ color: 16777215
139
+ });
140
+ }
141
+ return new l.MeshBasicMaterial({ visible: !1 });
142
+ },
143
+ update(o, e, t, n, r, i, a = 2) {
144
+ if (e === "BOX")
145
+ S(
146
+ o,
147
+ t,
148
+ r,
149
+ i
150
+ );
151
+ else if (e === "TEXT") {
152
+ const u = o;
153
+ u.map && u.map.dispose();
154
+ const c = m(
155
+ n || "",
156
+ t,
157
+ r,
158
+ i,
159
+ a
160
+ );
161
+ u.map = c, u.needsUpdate = !0;
162
+ }
163
+ }
164
+ };
165
+ export {
166
+ O as Painter,
167
+ A as createBoxMaterial,
168
+ m as createTextTexture,
169
+ S as updateBoxMaterial
170
+ };
@@ -0,0 +1,55 @@
1
+ (function(c,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("three")):typeof define=="function"&&define.amd?define(["exports","three"],s):(c=typeof globalThis<"u"?globalThis:c||self,s(c.MiragePainter={},c.THREE))})(this,function(c,s){"use strict";function C(e){const o=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const t=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(o,r,t.get?t:{enumerable:!0,get:()=>e[r]})}}return o.default=e,Object.freeze(o)}const l=C(s);function w(e,o,r){const t=o.split(`
2
+ `),a=[];return t.forEach(i=>{const n=i.split(" ");let u=n[0];for(let d=1;d<n.length;d++){const f=n[d];e.measureText(u+" "+f).width<r?u+=" "+f:(a.push(u),u=f)}a.push(u)}),a}function m(e,o,r,t,a=2){const i=document.createElement("canvas"),n=i.getContext("2d");if(!n)throw new Error("[Mirage] Failed to create canvas context");const d=(window.devicePixelRatio||1)*a;i.width=r*d,i.height=t*d,n.scale(d,d),n.font=o.font,n.fillStyle=o.color,n.textBaseline="top",n.globalAlpha=1;const f=w(n,e,r),B=o.lineHeight;f.forEach((S,M)=>{const R=M*B+2;let g=0;o.textAlign==="center"?g=r/2:o.textAlign==="right"&&(g=r),n.textAlign=o.textAlign,n.fillText(S,g,R)});const p=new l.CanvasTexture(i);return p.colorSpace=l.SRGBColorSpace,p.minFilter=l.LinearFilter,p.magFilter=l.LinearFilter,p.needsUpdate=!0,p}function h(e){return typeof e=="number"?e:parseFloat(e)||0}const A=`
3
+ varying vec2 vUv;
4
+ void main() {
5
+ vUv = uv;
6
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
7
+ }
8
+ `,O=`
9
+ varying vec2 vUv;
10
+
11
+ uniform vec2 uSize;
12
+ uniform float uRadius;
13
+ uniform float uBorderWidth;
14
+ uniform vec3 uColor;
15
+ uniform vec3 uBorderColor;
16
+ uniform float uOpacity;
17
+ uniform float uBgOpacity;
18
+
19
+ // SDF 박스 함수
20
+ float sdRoundedBox(vec2 p, vec2 b, float r) {
21
+ vec2 q = abs(p) - b + r;
22
+ return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r;
23
+ }
24
+
25
+ void main() {
26
+ vec2 p = (vUv - 0.5) * uSize;
27
+ vec2 halfSize = uSize * 0.5;
28
+
29
+ float d = sdRoundedBox(p, halfSize, uRadius);
30
+
31
+ float smoothEdge = 1.0;
32
+
33
+ float fillAlpha = 1.0 - smoothstep(-uBorderWidth - smoothEdge, -uBorderWidth, d);
34
+
35
+ float borderAlpha = 0.0;
36
+
37
+ if (uBorderWidth > 0.01) {
38
+ borderAlpha = (1.0 - smoothstep(0.0, smoothEdge, d)) - fillAlpha;
39
+ }
40
+
41
+ vec3 color = uColor;
42
+ float totalAlpha = borderAlpha + fillAlpha;
43
+
44
+ if (totalAlpha > 0.001) {
45
+ color = mix(uColor, uBorderColor, borderAlpha / totalAlpha);
46
+ }
47
+
48
+ float shapeAlpha = borderAlpha + (fillAlpha * uBgOpacity);
49
+ float finalOpacity = shapeAlpha * uOpacity;
50
+
51
+ if (finalOpacity < 0.001) discard;
52
+
53
+ gl_FragColor = vec4(color, finalOpacity);
54
+ }
55
+ `;function v(e){if(!e||e==="transparent")return{color:new l.Color(16777215),alpha:0};const o=e.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);if(o){const r=parseInt(o[1],10),t=parseInt(o[2],10),a=parseInt(o[3],10),i=o[4]!==void 0?parseFloat(o[4]):1;return{color:new l.Color(`rgb(${r}, ${t}, ${a})`),alpha:i}}return{color:new l.Color(e),alpha:1}}function x(e,o,r){const t=v(e.backgroundColor),a=v(e.borderColor),i={uSize:{value:new l.Vector2(o,r)},uRadius:{value:h(e.borderRadius)},uBorderWidth:{value:h(e.borderWidth)},uColor:{value:t.color},uBorderColor:{value:a.color},uOpacity:{value:e.opacity??1},uBgOpacity:{value:t.alpha}};return new l.ShaderMaterial({uniforms:i,vertexShader:A,fragmentShader:O,transparent:!0,side:l.FrontSide})}function b(e,o,r,t){const a=v(o.backgroundColor),i=v(o.borderColor);e.uniforms.uSize.value.set(r,t),e.uniforms.uRadius.value=h(o.borderRadius),e.uniforms.uBorderWidth.value=h(o.borderWidth),e.uniforms.uColor.value.copy(a.color),e.uniforms.uBorderColor.value.copy(i.color),e.uniforms.uOpacity.value=o.opacity??1,e.uniforms.uBgOpacity.value=a.alpha}const T={create(e,o,r,t,a,i=2){if(e==="BOX")return x(o,t,a);if(e==="TEXT"){const n=m(r||"",o,t,a,i);return new l.MeshBasicMaterial({map:n,transparent:!0,side:l.FrontSide,color:16777215})}return new l.MeshBasicMaterial({visible:!1})},update(e,o,r,t,a,i,n=2){if(o==="BOX")b(e,r,a,i);else if(o==="TEXT"){const u=e;u.map&&u.map.dispose();const d=m(t||"",r,a,i,n);u.map=d,u.needsUpdate=!0}}};c.Painter=T,c.createBoxMaterial=x,c.createTextTexture=m,c.updateBoxMaterial=b,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,4 @@
1
+ import { BoxStyles } from './types';
2
+ import * as THREE from "three";
3
+ export declare function createBoxMaterial(styles: BoxStyles, width: number, height: number): THREE.ShaderMaterial;
4
+ export declare function updateBoxMaterial(material: THREE.ShaderMaterial, styles: BoxStyles, width: number, height: number): void;
@@ -0,0 +1,5 @@
1
+ import * as THREE from "three";
2
+ export declare const Painter: {
3
+ create(type: "BOX" | "TEXT", styles: any, content: string, width: number, height: number, quality?: number): THREE.Material;
4
+ update(material: THREE.Material, type: "BOX" | "TEXT", styles: any, content: string, width: number, height: number, quality?: number): void;
5
+ };
@@ -0,0 +1,3 @@
1
+ import { TextStyles } from './types';
2
+ import * as THREE from "three";
3
+ export declare function createTextTexture(text: string, styles: TextStyles, rectWidth: number, rectHeight: number, qualityFactor?: number): THREE.CanvasTexture;
@@ -0,0 +1,4 @@
1
+ export * from './TextGenerator';
2
+ export * from './types';
3
+ export * from './BoxGenerator';
4
+ export * from './Painter';
@@ -0,0 +1,17 @@
1
+ export interface TextStyles {
2
+ font: string;
3
+ color: string;
4
+ textAlign: CanvasTextAlign;
5
+ textBaseline: CanvasTextBaseline;
6
+ direction: CanvasDirection;
7
+ lineHeight: number;
8
+ letterSpacing: number;
9
+ }
10
+ export interface BoxStyles {
11
+ backgroundColor: string;
12
+ opacity: number;
13
+ zIndex: number;
14
+ borderRadius: string;
15
+ borderColor: string;
16
+ borderWidth: string;
17
+ }
@@ -0,0 +1 @@
1
+ export * from './common';
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;
package/package.json CHANGED
@@ -1,11 +1,24 @@
1
1
  {
2
2
  "name": "@mirage-engine/painter",
3
- "version": "0.0.1",
3
+ "version": "0.2.0",
4
4
  "main": "src/index.ts",
5
5
  "types": "src/index.ts",
6
6
  "peerDependencies": {
7
7
  "three": "^0.160.0"
8
8
  },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/dltldn333/MirageEngine",
12
+ "directory": "packages/painter"
13
+ },
14
+ "dependencies": {
15
+ "@types/three": "^0.181.0"
16
+ },
17
+ "private": false,
18
+ "publishConfig": {
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.org/"
21
+ },
9
22
  "scripts": {
10
23
  "build": "vite build"
11
24
  }
@@ -0,0 +1,135 @@
1
+ import * as THREE from "three";
2
+ import { BoxStyles } from "./types";
3
+
4
+ function parsePixelValue(value: string | number): number {
5
+ if (typeof value === "number") return value;
6
+ return parseFloat(value) || 0;
7
+ }
8
+
9
+ const vertexShader = `
10
+ varying vec2 vUv;
11
+ void main() {
12
+ vUv = uv;
13
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
14
+ }
15
+ `;
16
+
17
+ const fragmentShader = `
18
+ varying vec2 vUv;
19
+
20
+ uniform vec2 uSize;
21
+ uniform float uRadius;
22
+ uniform float uBorderWidth;
23
+ uniform vec3 uColor;
24
+ uniform vec3 uBorderColor;
25
+ uniform float uOpacity;
26
+ uniform float uBgOpacity;
27
+
28
+ // SDF 박스 함수
29
+ float sdRoundedBox(vec2 p, vec2 b, float r) {
30
+ vec2 q = abs(p) - b + r;
31
+ return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r;
32
+ }
33
+
34
+ void main() {
35
+ vec2 p = (vUv - 0.5) * uSize;
36
+ vec2 halfSize = uSize * 0.5;
37
+
38
+ float d = sdRoundedBox(p, halfSize, uRadius);
39
+
40
+ float smoothEdge = 1.0;
41
+
42
+ float fillAlpha = 1.0 - smoothstep(-uBorderWidth - smoothEdge, -uBorderWidth, d);
43
+
44
+ float borderAlpha = 0.0;
45
+
46
+ if (uBorderWidth > 0.01) {
47
+ borderAlpha = (1.0 - smoothstep(0.0, smoothEdge, d)) - fillAlpha;
48
+ }
49
+
50
+ vec3 color = uColor;
51
+ float totalAlpha = borderAlpha + fillAlpha;
52
+
53
+ if (totalAlpha > 0.001) {
54
+ color = mix(uColor, uBorderColor, borderAlpha / totalAlpha);
55
+ }
56
+
57
+ float shapeAlpha = borderAlpha + (fillAlpha * uBgOpacity);
58
+ float finalOpacity = shapeAlpha * uOpacity;
59
+
60
+ if (finalOpacity < 0.001) discard;
61
+
62
+ gl_FragColor = vec4(color, finalOpacity);
63
+ }
64
+ `;
65
+
66
+ function parseColor(colorStr: string) {
67
+ if (!colorStr || colorStr === "transparent") {
68
+ return { color: new THREE.Color(0xffffff), alpha: 0.0 };
69
+ }
70
+
71
+ const rgbaMatch = colorStr.match(
72
+ /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/,
73
+ );
74
+
75
+ if (rgbaMatch) {
76
+ const r = parseInt(rgbaMatch[1], 10);
77
+ const g = parseInt(rgbaMatch[2], 10);
78
+ const b = parseInt(rgbaMatch[3], 10);
79
+ const a = rgbaMatch[4] !== undefined ? parseFloat(rgbaMatch[4]) : 1.0;
80
+
81
+ return { color: new THREE.Color(`rgb(${r}, ${g}, ${b})`), alpha: a };
82
+ }
83
+
84
+ return { color: new THREE.Color(colorStr), alpha: 1.0 };
85
+ }
86
+
87
+ export function createBoxMaterial(
88
+ styles: BoxStyles,
89
+ width: number,
90
+ height: number,
91
+ ): THREE.ShaderMaterial {
92
+ const parsedBg = parseColor(styles.backgroundColor);
93
+ const parsedBorder = parseColor(styles.borderColor);
94
+
95
+ const uniforms = {
96
+ uSize: { value: new THREE.Vector2(width, height) },
97
+ uRadius: { value: parsePixelValue(styles.borderRadius) },
98
+ uBorderWidth: { value: parsePixelValue(styles.borderWidth) },
99
+ uColor: { value: parsedBg.color },
100
+ uBorderColor: { value: parsedBorder.color },
101
+ uOpacity: { value: styles.opacity ?? 1.0 },
102
+ uBgOpacity: { value: parsedBg.alpha },
103
+ };
104
+
105
+ const material = new THREE.ShaderMaterial({
106
+ uniforms: uniforms,
107
+ vertexShader: vertexShader,
108
+ fragmentShader: fragmentShader,
109
+ transparent: true,
110
+ side: THREE.FrontSide, // for better performance
111
+ });
112
+
113
+ return material;
114
+ }
115
+
116
+ export function updateBoxMaterial(
117
+ material: THREE.ShaderMaterial,
118
+ styles: BoxStyles,
119
+ width: number,
120
+ height: number,
121
+ ) {
122
+ const parsedBg = parseColor(styles.backgroundColor);
123
+ const parsedBorder = parseColor(styles.borderColor);
124
+
125
+ material.uniforms.uSize.value.set(width, height);
126
+
127
+ material.uniforms.uRadius.value = parsePixelValue(styles.borderRadius);
128
+ material.uniforms.uBorderWidth.value = parsePixelValue(styles.borderWidth);
129
+
130
+ material.uniforms.uColor.value.copy(parsedBg.color);
131
+ material.uniforms.uBorderColor.value.copy(parsedBorder.color);
132
+
133
+ material.uniforms.uOpacity.value = styles.opacity ?? 1.0;
134
+ material.uniforms.uBgOpacity.value = parsedBg.alpha;
135
+ }
package/src/Painter.ts ADDED
@@ -0,0 +1,72 @@
1
+ import * as THREE from "three";
2
+ import { BoxStyles, TextStyles } from "./types";
3
+ import { createBoxMaterial, updateBoxMaterial } from "./BoxGenerator";
4
+ import { createTextTexture } from "./TextGenerator";
5
+
6
+ export const Painter = {
7
+ create(
8
+ type: "BOX" | "TEXT",
9
+ styles: any,
10
+ content: string,
11
+ width: number,
12
+ height: number,
13
+ quality: number = 2,
14
+ ): THREE.Material {
15
+ if (type === "BOX") {
16
+ return createBoxMaterial(styles as BoxStyles, width, height);
17
+ } else if (type === "TEXT") {
18
+ const texture = createTextTexture(
19
+ content || "",
20
+ styles as TextStyles,
21
+ width,
22
+ height,
23
+ quality,
24
+ );
25
+
26
+ return new THREE.MeshBasicMaterial({
27
+ map: texture,
28
+ transparent: true,
29
+ side: THREE.FrontSide,
30
+ color: 0xffffff,
31
+ });
32
+ }
33
+
34
+ return new THREE.MeshBasicMaterial({ visible: false });
35
+ },
36
+
37
+ update(
38
+ material: THREE.Material,
39
+ type: "BOX" | "TEXT",
40
+ styles: any,
41
+ content: string,
42
+ width: number,
43
+ height: number,
44
+ quality: number = 2,
45
+ ) {
46
+ if (type === "BOX") {
47
+ updateBoxMaterial(
48
+ material as THREE.ShaderMaterial,
49
+ styles as BoxStyles,
50
+ width,
51
+ height,
52
+ );
53
+ } else if (type === "TEXT") {
54
+ const basicMat = material as THREE.MeshBasicMaterial;
55
+
56
+ if (basicMat.map) {
57
+ basicMat.map.dispose();
58
+ }
59
+
60
+ const newTexture = createTextTexture(
61
+ content || "",
62
+ styles as TextStyles,
63
+ width,
64
+ height,
65
+ quality,
66
+ );
67
+
68
+ basicMat.map = newTexture;
69
+ basicMat.needsUpdate = true;
70
+ }
71
+ },
72
+ };
@@ -1,5 +1,5 @@
1
1
  import * as THREE from "three";
2
- import { TextStyles } from "../../mirage-engine/src/types";
2
+ import { TextStyles } from "./types";
3
3
 
4
4
  function wrapText(
5
5
  ctx: CanvasRenderingContext2D,
package/src/index.ts CHANGED
@@ -1 +1,4 @@
1
- export * from './TextGenerator';
1
+ export * from "./TextGenerator";
2
+ export * from "./types";
3
+ export * from "./BoxGenerator";
4
+ export * from "./Painter";
@@ -0,0 +1,20 @@
1
+ // For CanvasRenderingContext2D
2
+ export interface TextStyles {
3
+ font: string;
4
+ color: string;
5
+ textAlign: CanvasTextAlign;
6
+ textBaseline: CanvasTextBaseline;
7
+ direction: CanvasDirection;
8
+ lineHeight: number; // px
9
+ letterSpacing: number; // px
10
+ }
11
+
12
+ export interface BoxStyles {
13
+ backgroundColor: string;
14
+ opacity: number;
15
+ zIndex: number;
16
+
17
+ borderRadius: string;
18
+ borderColor: string;
19
+ borderWidth: string;
20
+ }
@@ -0,0 +1 @@
1
+ export * from './common';
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "paths": {
6
+ "@/*": ["src/*"]
7
+ },
8
+ "outDir": "./dist"
9
+ },
10
+ "include": [
11
+ "src",
12
+ "vite.config.ts"
13
+ ]
14
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { defineConfig } from "vite";
2
+ import dts from "vite-plugin-dts";
3
+ import path from "path";
4
+
5
+ export default defineConfig({
6
+ build: {
7
+ lib: {
8
+ entry: path.resolve(__dirname, "src/index.ts"),
9
+ name: "MiragePainter",
10
+ fileName: (format) =>
11
+ `mirage-painter.${format === "es" ? "js" : "umd.js"}`,
12
+ },
13
+ rollupOptions: {
14
+ external: ["three"],
15
+ output: {
16
+ globals: {
17
+ three: "THREE",
18
+ },
19
+ },
20
+ },
21
+ },
22
+ plugins: [
23
+ dts({
24
+ insertTypesEntry: true,
25
+ }),
26
+ ],
27
+ resolve: {
28
+ alias: {
29
+ "@": path.resolve(__dirname, "./src"),
30
+ },
31
+ },
32
+ });