@mirage-engine/painter 0.4.0 → 1.0.1
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 +12 -0
- package/dist/mirage-painter.js +491 -191
- package/dist/mirage-painter.umd.js +138 -72
- package/dist/src/Box/BoxGenerator.d.ts +19 -0
- package/dist/src/Painter.d.ts +5 -3
- package/dist/src/Text/FontMetricsManager.d.ts +10 -0
- package/dist/src/Text/TextGenerator.d.ts +11 -0
- package/dist/src/index.d.ts +3 -2
- package/dist/src/shaders/index.d.ts +9 -0
- package/dist/src/tools/parser.d.ts +16 -0
- package/dist/src/types/common.d.ts +5 -0
- package/package.json +1 -1
- package/src/Box/BoxGenerator.ts +350 -0
- package/src/Painter.ts +30 -27
- package/src/Text/FontMetricsManager.ts +62 -0
- package/src/Text/TextGenerator.ts +123 -0
- package/src/env.d.ts +4 -0
- package/src/index.ts +4 -3
- package/src/shaders/base/box-fragment.glsl +129 -0
- package/src/shaders/base/box-vertex.glsl +7 -0
- package/src/shaders/chunk/base-color-chunk.glsl +2 -0
- package/src/shaders/chunk/decl-chunk.glsl +4 -0
- package/src/shaders/chunk/uv-chunk.glsl +3 -0
- package/src/shaders/index.ts +16 -0
- package/src/tools/parser.ts +148 -0
- package/src/types/common.ts +5 -1
- package/dist/src/BoxGenerator.d.ts +0 -4
- package/dist/src/TextGenerator.d.ts +0 -3
- package/dist/src/dev/devShader.d.ts +0 -5
- package/src/BoxGenerator.ts +0 -236
- package/src/TextGenerator.ts +0 -85
- package/src/dev/devShader.ts +0 -114
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
varying vec2 vUv;
|
|
2
|
+
uniform vec2 uSize;
|
|
3
|
+
uniform vec4 uBorderRadius;
|
|
4
|
+
uniform float uBorderWidth;
|
|
5
|
+
uniform vec4 uBgColor;
|
|
6
|
+
uniform vec4 uBorderColor;
|
|
7
|
+
uniform float uOpacity;
|
|
8
|
+
|
|
9
|
+
uniform int uGradientCount;
|
|
10
|
+
uniform float uGradientAngle;
|
|
11
|
+
uniform vec4 uGradientColors[8];
|
|
12
|
+
uniform float uGradientStops[8];
|
|
13
|
+
|
|
14
|
+
#INJECT_DECLARATIONS
|
|
15
|
+
|
|
16
|
+
vec3 linearToSrgb(vec3 linearColor) {
|
|
17
|
+
vec3 linearPart = 12.92 * linearColor;
|
|
18
|
+
vec3 curvePart = 1.055 * pow(linearColor, vec3(1.0 / 2.4)) - 0.055;
|
|
19
|
+
vec3 switchCondition = step(vec3(0.0031308), linearColor);
|
|
20
|
+
return mix(linearPart, curvePart, switchCondition);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
vec3 srgbToLinear(vec3 srgbColor) {
|
|
24
|
+
vec3 linearPart = srgbColor / 12.92;
|
|
25
|
+
vec3 curvePart = pow((srgbColor + vec3(0.055)) / 1.055, vec3(2.4));
|
|
26
|
+
vec3 switchCondition = step(vec3(0.04045), srgbColor);
|
|
27
|
+
return mix(linearPart, curvePart, switchCondition);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
vec4 blendSrcOverInLinear(vec4 front, vec4 back) {
|
|
31
|
+
front.rgb = srgbToLinear(front.rgb);
|
|
32
|
+
back.rgb = srgbToLinear(back.rgb);
|
|
33
|
+
float aOut = front.a + back.a * (1.0 - front.a);
|
|
34
|
+
float safeAlpha = max(aOut, 0.0001);
|
|
35
|
+
vec3 cOut = (front.rgb * front.a + back.rgb * back.a * (1.0 - front.a)) / safeAlpha;
|
|
36
|
+
return vec4(linearToSrgb(cOut), aOut);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
vec4 blendSrcOver(vec4 front, vec4 back) {
|
|
40
|
+
float aOut = front.a + back.a * (1.0 - front.a);
|
|
41
|
+
float safeAlpha = max(aOut, 0.0001);
|
|
42
|
+
vec3 cOut = (front.rgb * front.a + back.rgb * back.a * (1.0 - front.a)) / safeAlpha;
|
|
43
|
+
return vec4(cOut, aOut);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
float sdRoundedBox(vec2 p, vec2 b, float r) {
|
|
47
|
+
vec2 q = abs(p) - b + r;
|
|
48
|
+
return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
float sdVisualBox(vec2 p, vec2 b, float r) {
|
|
52
|
+
vec2 q = abs(p) - b + r;
|
|
53
|
+
float n = 2.01;
|
|
54
|
+
float d = pow(pow(max(q.x, 0.0), n) + pow(max(q.y, 0.0), n), 1.0/n) - r;
|
|
55
|
+
return min(max(q.x, q.y), 0.0) + d;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
vec4 calculateGradientLayer(vec2 uv) {
|
|
59
|
+
if (uGradientCount < 2) return uGradientColors[0];
|
|
60
|
+
|
|
61
|
+
vec2 dir = vec2(sin(uGradientAngle), cos(uGradientAngle));
|
|
62
|
+
vec2 p = (uv - 0.5) * uSize;
|
|
63
|
+
float proj = dot(p, dir);
|
|
64
|
+
float L = abs(uSize.x * dir.x) + abs(uSize.y * dir.y);
|
|
65
|
+
float t = clamp((proj / L) + 0.5, 0.0, 1.0);
|
|
66
|
+
|
|
67
|
+
vec4 color = uGradientColors[0];
|
|
68
|
+
for (int i = 1; i < 8; i++) {
|
|
69
|
+
if (i >= uGradientCount) break;
|
|
70
|
+
float prevStop = uGradientStops[i-1];
|
|
71
|
+
float currStop = uGradientStops[i];
|
|
72
|
+
|
|
73
|
+
float factor = clamp((t - prevStop) / (currStop - prevStop + 0.00001), 0.0, 1.0);
|
|
74
|
+
color = mix(color, uGradientColors[i], factor);
|
|
75
|
+
}
|
|
76
|
+
// return vec4(linearToSrgb(color.rgb), color.a);
|
|
77
|
+
return vec4(color);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
void main() {
|
|
81
|
+
vec2 p = (vUv - 0.5) * uSize;
|
|
82
|
+
vec2 halfSize = uSize * 0.5;
|
|
83
|
+
|
|
84
|
+
#INJECT_UV_MODIFIER
|
|
85
|
+
|
|
86
|
+
// color decision pipeline
|
|
87
|
+
vec4 baseColor = vec4(uBgColor.rgb, uBgColor.a);
|
|
88
|
+
|
|
89
|
+
if (uGradientCount > 0) {
|
|
90
|
+
vec4 gradColor = calculateGradientLayer(vUv);
|
|
91
|
+
baseColor = blendSrcOver(gradColor, baseColor);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#INJECT_BASE_COLOR
|
|
95
|
+
|
|
96
|
+
// Hybrid SDF
|
|
97
|
+
vec2 xRadii = mix(uBorderRadius.xw, uBorderRadius.yz, step(0.0, p.x));
|
|
98
|
+
float r = mix(xRadii.y, xRadii.x, step(0.0, p.y));
|
|
99
|
+
r = min(r, min(halfSize.x, halfSize.y));
|
|
100
|
+
|
|
101
|
+
float d = sdRoundedBox(p, halfSize, r);
|
|
102
|
+
|
|
103
|
+
float d_smooth = sdVisualBox(p, halfSize, r);
|
|
104
|
+
float d_visual = d_smooth / fwidth(d_smooth);
|
|
105
|
+
|
|
106
|
+
// rendering pipeline
|
|
107
|
+
float bgMask = 1.0 - smoothstep(-0.5, 0.5, d_visual);
|
|
108
|
+
|
|
109
|
+
float halfBorder = uBorderWidth * 0.5;
|
|
110
|
+
float borderD = abs(d + halfBorder) - halfBorder;
|
|
111
|
+
float borderAlpha = (1.0 - smoothstep(0.0, 1.0, borderD)) * uBorderColor.a;
|
|
112
|
+
if (uBorderWidth <= 0.01) {
|
|
113
|
+
borderAlpha = 0.0;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// final blending (border + background) using blendSrcOver
|
|
117
|
+
vec4 borderLayer = vec4(uBorderColor.rgb, borderAlpha);
|
|
118
|
+
vec4 finalColor = blendSrcOver(borderLayer, baseColor);
|
|
119
|
+
// final color control (Tint, Noise)
|
|
120
|
+
#INJECT_COLOR_MODIFIER
|
|
121
|
+
|
|
122
|
+
float finalOpacity = finalColor.a * bgMask * uOpacity;
|
|
123
|
+
if (finalOpacity < 0.001) discard;
|
|
124
|
+
|
|
125
|
+
gl_FragColor = vec4(finalColor.rgb, finalOpacity);
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
// #include <colorspace_fragment>
|
|
129
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import vertexShader from "./base/box-vertex.glsl?raw";
|
|
2
|
+
import fragmentShader from "./base/box-fragment.glsl?raw";
|
|
3
|
+
import declChunk from "./chunk/decl-chunk.glsl?raw";
|
|
4
|
+
import uvChunk from "./chunk/uv-chunk.glsl?raw";
|
|
5
|
+
import baseColorChunk from "./chunk/base-color-chunk.glsl?raw";
|
|
6
|
+
|
|
7
|
+
export const BoxShader = {
|
|
8
|
+
vertexShader,
|
|
9
|
+
fragmentShader,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const BoxChunk = {
|
|
13
|
+
declChunk,
|
|
14
|
+
uvChunk,
|
|
15
|
+
baseColorChunk,
|
|
16
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
|
|
3
|
+
export function parsePixelValue(value: string | number): number {
|
|
4
|
+
if (typeof value === "number") return value;
|
|
5
|
+
return parseFloat(value) || 0;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function parseColor(colorStr: string) {
|
|
9
|
+
if (!colorStr || colorStr === "transparent") {
|
|
10
|
+
return { color: new THREE.Color(0xffffff), alpha: 0.0 };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const rgbaMatch = colorStr.match(
|
|
14
|
+
/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
if (rgbaMatch) {
|
|
18
|
+
const r = parseInt(rgbaMatch[1], 10);
|
|
19
|
+
const g = parseInt(rgbaMatch[2], 10);
|
|
20
|
+
const b = parseInt(rgbaMatch[3], 10);
|
|
21
|
+
const a = rgbaMatch[4] !== undefined ? parseFloat(rgbaMatch[4]) : 1.0;
|
|
22
|
+
return { color: new THREE.Color(`rgb(${r}, ${g}, ${b})`), alpha: a };
|
|
23
|
+
}
|
|
24
|
+
return { color: new THREE.Color(colorStr), alpha: 1.0 };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function splitByComma(str: string) {
|
|
28
|
+
const result = [];
|
|
29
|
+
let current = "";
|
|
30
|
+
let depth = 0;
|
|
31
|
+
for (let i = 0; i < str.length; i++) {
|
|
32
|
+
const char = str[i];
|
|
33
|
+
if (char === "(") depth++;
|
|
34
|
+
else if (char === ")") depth--;
|
|
35
|
+
else if (char === "," && depth === 0) {
|
|
36
|
+
result.push(current.trim());
|
|
37
|
+
current = "";
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
current += char;
|
|
41
|
+
}
|
|
42
|
+
if (current) result.push(current.trim());
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function parseLinearGradient(value: string | null | undefined) {
|
|
47
|
+
if (!value || typeof value !== "string" || !value.includes("linear-gradient")) return null;
|
|
48
|
+
|
|
49
|
+
const match = value.match(/linear-gradient\((.*)\)/);
|
|
50
|
+
if (!match) return null;
|
|
51
|
+
|
|
52
|
+
const content = match[1];
|
|
53
|
+
const parts = splitByComma(content);
|
|
54
|
+
|
|
55
|
+
let angle = Math.PI; // default: to bottom
|
|
56
|
+
let firstStopIndex = 0;
|
|
57
|
+
|
|
58
|
+
const firstPart = parts[0].trim();
|
|
59
|
+
if (firstPart.startsWith("to ")) {
|
|
60
|
+
if (firstPart === "to top") angle = 0;
|
|
61
|
+
else if (firstPart === "to right") angle = Math.PI / 2;
|
|
62
|
+
else if (firstPart === "to bottom") angle = Math.PI;
|
|
63
|
+
else if (firstPart === "to left") angle = Math.PI * 1.5;
|
|
64
|
+
else if (firstPart === "to top right" || firstPart === "to right top")
|
|
65
|
+
angle = Math.PI / 4;
|
|
66
|
+
else if (
|
|
67
|
+
firstPart === "to bottom right" ||
|
|
68
|
+
firstPart === "to right bottom"
|
|
69
|
+
)
|
|
70
|
+
angle = Math.PI * 0.75;
|
|
71
|
+
else if (firstPart === "to bottom left" || firstPart === "to left bottom")
|
|
72
|
+
angle = Math.PI * 1.25;
|
|
73
|
+
else if (firstPart === "to top left" || firstPart === "to left top")
|
|
74
|
+
angle = Math.PI * 1.75;
|
|
75
|
+
firstStopIndex = 1;
|
|
76
|
+
} else if (
|
|
77
|
+
firstPart.endsWith("deg") ||
|
|
78
|
+
firstPart.endsWith("rad") ||
|
|
79
|
+
firstPart.endsWith("turn")
|
|
80
|
+
) {
|
|
81
|
+
const num = parseFloat(firstPart);
|
|
82
|
+
if (firstPart.endsWith("deg")) angle = num * (Math.PI / 180);
|
|
83
|
+
else if (firstPart.endsWith("rad")) angle = num;
|
|
84
|
+
else if (firstPart.endsWith("turn")) angle = num * Math.PI * 2;
|
|
85
|
+
firstStopIndex = 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const stops = [];
|
|
89
|
+
for (let i = firstStopIndex; i < parts.length; i++) {
|
|
90
|
+
if (stops.length >= 8) break;
|
|
91
|
+
const part = parts[i].trim();
|
|
92
|
+
const lastSpace = part.lastIndexOf(" ");
|
|
93
|
+
let colorStr = part;
|
|
94
|
+
let stopPos = null;
|
|
95
|
+
|
|
96
|
+
if (lastSpace !== -1 && !part.endsWith(")")) {
|
|
97
|
+
const possiblePos = part.substring(lastSpace + 1);
|
|
98
|
+
if (
|
|
99
|
+
possiblePos.endsWith("%") ||
|
|
100
|
+
possiblePos.endsWith("px") ||
|
|
101
|
+
!isNaN(parseFloat(possiblePos))
|
|
102
|
+
) {
|
|
103
|
+
colorStr = part.substring(0, lastSpace).trim();
|
|
104
|
+
stopPos = possiblePos;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const parsedColor = parseColor(colorStr);
|
|
109
|
+
stops.push({
|
|
110
|
+
color: parsedColor.color,
|
|
111
|
+
alpha: parsedColor.alpha,
|
|
112
|
+
rawStop: stopPos,
|
|
113
|
+
stop: 0,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (stops.length > 0) {
|
|
118
|
+
for (let i = 0; i < stops.length; i++) {
|
|
119
|
+
if (stops[i].rawStop !== null) {
|
|
120
|
+
stops[i].stop = parseFloat(stops[i].rawStop!) / 100;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (stops[0].rawStop === null) stops[0].stop = 0.0;
|
|
125
|
+
|
|
126
|
+
if (stops.length > 1) {
|
|
127
|
+
if (stops[stops.length - 1].rawStop === null) stops[stops.length - 1].stop = 1.0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let lastKnownIndex = 0;
|
|
131
|
+
for (let i = 1; i < stops.length; i++) {
|
|
132
|
+
if (stops[i].rawStop !== null || i === stops.length - 1) {
|
|
133
|
+
const diff = i - lastKnownIndex;
|
|
134
|
+
if (diff > 1) {
|
|
135
|
+
const startVal = stops[lastKnownIndex].stop;
|
|
136
|
+
const endVal = stops[i].stop;
|
|
137
|
+
const step = (endVal - startVal) / diff;
|
|
138
|
+
for (let j = 1; j < diff; j++) {
|
|
139
|
+
stops[lastKnownIndex + j].stop = startVal + step * j;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
lastKnownIndex = i;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { angle, stops };
|
|
148
|
+
}
|
package/src/types/common.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// For CanvasRenderingContext2D
|
|
2
2
|
export interface TextStyles {
|
|
3
3
|
font: string;
|
|
4
|
+
fontSize :string;
|
|
4
5
|
color: string;
|
|
5
6
|
textAlign: CanvasTextAlign;
|
|
6
7
|
textBaseline: CanvasTextBaseline;
|
|
@@ -11,9 +12,11 @@ export interface TextStyles {
|
|
|
11
12
|
|
|
12
13
|
export interface BoxStyles {
|
|
13
14
|
backgroundColor: string;
|
|
15
|
+
backgroundImage: string;
|
|
16
|
+
imageSrc?: string;
|
|
17
|
+
isTraveler?: boolean;
|
|
14
18
|
opacity: number;
|
|
15
19
|
zIndex: number;
|
|
16
|
-
|
|
17
20
|
borderRadius: string;
|
|
18
21
|
borderColor: string;
|
|
19
22
|
borderWidth: string;
|
|
@@ -22,5 +25,6 @@ export interface BoxStyles {
|
|
|
22
25
|
export interface ShaderHooks {
|
|
23
26
|
uvModifier?: string;
|
|
24
27
|
colorModifier?: string;
|
|
28
|
+
uniforms?: Record<string, any>;
|
|
25
29
|
}
|
|
26
30
|
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { BoxStyles, ShaderHooks } from './types';
|
|
2
|
-
import * as THREE from "three";
|
|
3
|
-
export declare function createBoxMaterial(styles: BoxStyles, width: number, height: number, texture?: THREE.Texture | null, hooks?: ShaderHooks): THREE.ShaderMaterial;
|
|
4
|
-
export declare function updateBoxMaterial(material: THREE.ShaderMaterial, styles: BoxStyles, width: number, height: number, texture?: THREE.Texture | null): void;
|
package/src/BoxGenerator.ts
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
import { BoxStyles, ShaderHooks } from "./types";
|
|
3
|
-
// [for dev]
|
|
4
|
-
// import { glassHooks } from "./dev/devShader";
|
|
5
|
-
|
|
6
|
-
function parsePixelValue(value: string | number): number {
|
|
7
|
-
if (typeof value === "number") return value;
|
|
8
|
-
return parseFloat(value) || 0;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function setBorderRadius(target: THREE.Vector4, radius: string) {
|
|
12
|
-
if (!radius) {
|
|
13
|
-
target.set(0, 0, 0, 0);
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const arr = radius.split("/")[0].trim().split(/\s+/);
|
|
17
|
-
const tl = parsePixelValue(arr[0]);
|
|
18
|
-
const tr = parsePixelValue(arr[1] ?? arr[0]);
|
|
19
|
-
const br = parsePixelValue(arr[2] ?? arr[0]);
|
|
20
|
-
const bl = parsePixelValue(arr[3] ?? arr[1] ?? arr[0]);
|
|
21
|
-
|
|
22
|
-
target.set(tl, tr, br, bl);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const vertexShader = /* glsl */ `
|
|
26
|
-
varying vec2 vUv;
|
|
27
|
-
varying vec4 vScreenPos;
|
|
28
|
-
void main() {
|
|
29
|
-
vUv = uv;
|
|
30
|
-
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
31
|
-
vScreenPos = gl_Position;
|
|
32
|
-
}
|
|
33
|
-
`;
|
|
34
|
-
|
|
35
|
-
const fragmentShaderTemplate = /* glsl */ `
|
|
36
|
-
|
|
37
|
-
varying vec2 vUv;
|
|
38
|
-
|
|
39
|
-
uniform vec2 uSize;
|
|
40
|
-
uniform vec4 uBorderRadius;
|
|
41
|
-
uniform float uBorderWidth;
|
|
42
|
-
uniform vec3 uBgColor;
|
|
43
|
-
uniform vec3 uBorderColor;
|
|
44
|
-
uniform float uOpacity;
|
|
45
|
-
uniform float uBgOpacity;
|
|
46
|
-
uniform float uBorderOpacity;
|
|
47
|
-
|
|
48
|
-
#INJECT_DECLARATIONS
|
|
49
|
-
|
|
50
|
-
float sdRoundedBox(vec2 p, vec2 b, float r) {
|
|
51
|
-
vec2 q = abs(p) - b + r;
|
|
52
|
-
|
|
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
|
-
#INJECT_UV_MODIFIER
|
|
61
|
-
|
|
62
|
-
// color decision pipeline
|
|
63
|
-
vec4 baseColor = vec4(uBgColor, uBgOpacity);
|
|
64
|
-
|
|
65
|
-
#INJECT_BASE_COLOR
|
|
66
|
-
|
|
67
|
-
// sdf shape pipeline
|
|
68
|
-
vec2 xRadii = mix(uBorderRadius.xw, uBorderRadius.yz, step(0.0, p.x));
|
|
69
|
-
float r = mix(xRadii.y, xRadii.x, step(0.0, p.y));
|
|
70
|
-
float d = sdRoundedBox(p, halfSize, r);
|
|
71
|
-
|
|
72
|
-
float aa = 1.0;
|
|
73
|
-
float bgMask = 1.0 - smoothstep(0.0, aa, d);
|
|
74
|
-
|
|
75
|
-
float halfBorder = uBorderWidth * 0.5;
|
|
76
|
-
float borderD = abs(d + halfBorder) - halfBorder;
|
|
77
|
-
float borderAlpha = (1.0 - smoothstep(0.0, aa, borderD)) * uBorderOpacity;
|
|
78
|
-
if (uBorderWidth <= 0.01) {
|
|
79
|
-
borderAlpha = 0.0;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// final blending (border + background)
|
|
83
|
-
float aFront = borderAlpha;
|
|
84
|
-
float aBack = baseColor.a;
|
|
85
|
-
float aOut = aFront + aBack * (1.0 - aFront);
|
|
86
|
-
|
|
87
|
-
float safeAlpha = max(aOut, 0.0001);
|
|
88
|
-
vec3 cOut = (uBorderColor * aFront + baseColor.rgb * aBack * (1.0 - aFront)) / safeAlpha;
|
|
89
|
-
vec4 finalColor = vec4(cOut, aOut);
|
|
90
|
-
|
|
91
|
-
// final color control (Tint, Noise)
|
|
92
|
-
#INJECT_COLOR_MODIFIER
|
|
93
|
-
|
|
94
|
-
float finalOpacity = finalColor.a * bgMask * uOpacity;
|
|
95
|
-
if (finalOpacity < 0.001) discard;
|
|
96
|
-
|
|
97
|
-
gl_FragColor = vec4(finalColor.rgb, finalOpacity);
|
|
98
|
-
|
|
99
|
-
#include <colorspace_fragment>
|
|
100
|
-
}
|
|
101
|
-
`;
|
|
102
|
-
|
|
103
|
-
function parseColor(colorStr: string) {
|
|
104
|
-
if (!colorStr || colorStr === "transparent") {
|
|
105
|
-
return { color: new THREE.Color(0xffffff), alpha: 0.0 };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const rgbaMatch = colorStr.match(
|
|
109
|
-
/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
if (rgbaMatch) {
|
|
113
|
-
const r = parseInt(rgbaMatch[1], 10);
|
|
114
|
-
const g = parseInt(rgbaMatch[2], 10);
|
|
115
|
-
const b = parseInt(rgbaMatch[3], 10);
|
|
116
|
-
const a = rgbaMatch[4] !== undefined ? parseFloat(rgbaMatch[4]) : 1.0;
|
|
117
|
-
return { color: new THREE.Color(`rgb(${r}, ${g}, ${b})`), alpha: a };
|
|
118
|
-
}
|
|
119
|
-
return { color: new THREE.Color(colorStr), alpha: 1.0 };
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function createBoxMaterial(
|
|
123
|
-
styles: BoxStyles,
|
|
124
|
-
width: number,
|
|
125
|
-
height: number,
|
|
126
|
-
texture: THREE.Texture | null = null,
|
|
127
|
-
hooks?: ShaderHooks,
|
|
128
|
-
): THREE.ShaderMaterial {
|
|
129
|
-
const hasTexture = texture !== null;
|
|
130
|
-
// [for dev]
|
|
131
|
-
// const activeHooks = hasTexture ? glassHooks : hooks;
|
|
132
|
-
|
|
133
|
-
const declChunk = hasTexture
|
|
134
|
-
? /* glsl */ `
|
|
135
|
-
uniform sampler2D uTexture;
|
|
136
|
-
varying vec4 vScreenPos;
|
|
137
|
-
`
|
|
138
|
-
: "";
|
|
139
|
-
|
|
140
|
-
// [un dev]
|
|
141
|
-
const uvChunk = hasTexture
|
|
142
|
-
? /* glsl */ `
|
|
143
|
-
vec2 screenUv = (vScreenPos.xy / vScreenPos.w) * 0.5 + 0.5;
|
|
144
|
-
vec2 resultUv = screenUv;
|
|
145
|
-
${hooks?.uvModifier || ""}
|
|
146
|
-
`
|
|
147
|
-
: "";
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
// [for dev]
|
|
151
|
-
// const uvChunk = hasTexture
|
|
152
|
-
// ? /* glsl */ `
|
|
153
|
-
// vec2 screenUv = (vScreenPos.xy / vScreenPos.w) * 0.5 + 0.5;
|
|
154
|
-
// vec2 resultUv = screenUv;
|
|
155
|
-
// ${activeHooks?.uvModifier || ""}
|
|
156
|
-
// `
|
|
157
|
-
// : "";
|
|
158
|
-
|
|
159
|
-
const baseColorChunk = hasTexture
|
|
160
|
-
? /* glsl */ `
|
|
161
|
-
baseColor = texture2D(uTexture, resultUv);
|
|
162
|
-
`
|
|
163
|
-
: "";
|
|
164
|
-
|
|
165
|
-
// [un dev]
|
|
166
|
-
const colorModChunk = hooks?.colorModifier || "";
|
|
167
|
-
|
|
168
|
-
// [for dev]
|
|
169
|
-
// const colorModChunk = hasTexture
|
|
170
|
-
// ? activeHooks?.colorModifier || ""
|
|
171
|
-
// : hooks?.colorModifier || "";
|
|
172
|
-
|
|
173
|
-
const fragmentShader = fragmentShaderTemplate
|
|
174
|
-
.replace("#INJECT_DECLARATIONS", declChunk)
|
|
175
|
-
.replace("#INJECT_UV_MODIFIER", uvChunk)
|
|
176
|
-
.replace("#INJECT_BASE_COLOR", baseColorChunk)
|
|
177
|
-
.replace("#INJECT_COLOR_MODIFIER", colorModChunk);
|
|
178
|
-
|
|
179
|
-
// uniform setting
|
|
180
|
-
const parsedBg = parseColor(styles.backgroundColor);
|
|
181
|
-
const parsedBorder = parseColor(styles.borderColor);
|
|
182
|
-
const uniforms = {
|
|
183
|
-
uSize: { value: new THREE.Vector2(width, height) },
|
|
184
|
-
uBorderRadius: { value: new THREE.Vector4(0, 0, 0, 0) },
|
|
185
|
-
uBorderWidth: { value: parsePixelValue(styles.borderWidth) },
|
|
186
|
-
uBgColor: { value: parsedBg.color },
|
|
187
|
-
uBorderColor: { value: parsedBorder.color },
|
|
188
|
-
uOpacity: { value: styles.opacity ?? 1.0 },
|
|
189
|
-
uBgOpacity: { value: parsedBg.alpha },
|
|
190
|
-
uBorderOpacity: { value: parsedBorder.alpha },
|
|
191
|
-
uTexture: { value: null as THREE.Texture | null },
|
|
192
|
-
};
|
|
193
|
-
// border radius value initialize
|
|
194
|
-
setBorderRadius(uniforms.uBorderRadius.value, styles.borderRadius);
|
|
195
|
-
|
|
196
|
-
if (hasTexture) {
|
|
197
|
-
uniforms.uTexture.value = texture;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const material = new THREE.ShaderMaterial({
|
|
201
|
-
uniforms: uniforms,
|
|
202
|
-
vertexShader: vertexShader,
|
|
203
|
-
fragmentShader: fragmentShader,
|
|
204
|
-
transparent: true,
|
|
205
|
-
side: THREE.FrontSide, // for better performance
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
return material;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export function updateBoxMaterial(
|
|
212
|
-
material: THREE.ShaderMaterial,
|
|
213
|
-
styles: BoxStyles,
|
|
214
|
-
width: number,
|
|
215
|
-
height: number,
|
|
216
|
-
texture?: THREE.Texture | null,
|
|
217
|
-
) {
|
|
218
|
-
const parsedBg = parseColor(styles.backgroundColor);
|
|
219
|
-
const parsedBorder = parseColor(styles.borderColor);
|
|
220
|
-
|
|
221
|
-
material.uniforms.uSize.value.set(width, height);
|
|
222
|
-
|
|
223
|
-
// material.uniforms.uBorderRadius.value = parsePixelValue(styles.borderRadius);
|
|
224
|
-
setBorderRadius(material.uniforms.uBorderRadius.value, styles.borderRadius);
|
|
225
|
-
material.uniforms.uBorderWidth.value = parsePixelValue(styles.borderWidth);
|
|
226
|
-
|
|
227
|
-
material.uniforms.uBgColor.value.copy(parsedBg.color);
|
|
228
|
-
material.uniforms.uBorderColor.value.copy(parsedBorder.color);
|
|
229
|
-
|
|
230
|
-
material.uniforms.uOpacity.value = styles.opacity ?? 1.0;
|
|
231
|
-
material.uniforms.uBgOpacity.value = parsedBg.alpha;
|
|
232
|
-
material.uniforms.uBorderOpacity.value = parsedBorder.alpha;
|
|
233
|
-
if (material.uniforms.uTexture && texture !== undefined) {
|
|
234
|
-
material.uniforms.uTexture.value = texture;
|
|
235
|
-
}
|
|
236
|
-
}
|
package/src/TextGenerator.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
import { TextStyles } from "./types";
|
|
3
|
-
|
|
4
|
-
function wrapText(
|
|
5
|
-
ctx: CanvasRenderingContext2D,
|
|
6
|
-
text: string,
|
|
7
|
-
maxWidth: number
|
|
8
|
-
): string[] {
|
|
9
|
-
const forcedLines = text.split("\n");
|
|
10
|
-
const resultLines: string[] = [];
|
|
11
|
-
|
|
12
|
-
forcedLines.forEach((line) => {
|
|
13
|
-
const words = line.split(" ");
|
|
14
|
-
let currentLine = words[0];
|
|
15
|
-
|
|
16
|
-
for (let i = 1; i < words.length; i++) {
|
|
17
|
-
const word = words[i];
|
|
18
|
-
const width = ctx.measureText(currentLine + " " + word).width;
|
|
19
|
-
|
|
20
|
-
if (width < maxWidth) {
|
|
21
|
-
currentLine += " " + word;
|
|
22
|
-
} else {
|
|
23
|
-
resultLines.push(currentLine);
|
|
24
|
-
currentLine = word;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
resultLines.push(currentLine);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return resultLines;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function createTextTexture(
|
|
34
|
-
text: string,
|
|
35
|
-
styles: TextStyles,
|
|
36
|
-
rectWidth: number,
|
|
37
|
-
rectHeight: number,
|
|
38
|
-
qualityFactor: number = 2
|
|
39
|
-
): THREE.CanvasTexture {
|
|
40
|
-
const canvas = document.createElement("canvas");
|
|
41
|
-
const ctx = canvas.getContext("2d");
|
|
42
|
-
|
|
43
|
-
if (!ctx) {
|
|
44
|
-
throw new Error("[Mirage] Failed to create canvas context");
|
|
45
|
-
}
|
|
46
|
-
// text Quality
|
|
47
|
-
|
|
48
|
-
const pixelRatio = window.devicePixelRatio || 1;
|
|
49
|
-
const scale = pixelRatio * qualityFactor;
|
|
50
|
-
|
|
51
|
-
canvas.width = rectWidth * scale;
|
|
52
|
-
canvas.height = rectHeight * scale;
|
|
53
|
-
ctx.scale(scale, scale);
|
|
54
|
-
|
|
55
|
-
ctx.font = styles.font;
|
|
56
|
-
ctx.fillStyle = styles.color;
|
|
57
|
-
ctx.textBaseline = "top";
|
|
58
|
-
|
|
59
|
-
ctx.globalAlpha = 1;
|
|
60
|
-
|
|
61
|
-
const lines = wrapText(ctx, text, rectWidth);
|
|
62
|
-
const lineHeight = styles.lineHeight;
|
|
63
|
-
|
|
64
|
-
lines.forEach((line, index) => {
|
|
65
|
-
const y = index * lineHeight + 2;
|
|
66
|
-
|
|
67
|
-
let x = 0;
|
|
68
|
-
if (styles.textAlign === "center") {
|
|
69
|
-
x = rectWidth / 2;
|
|
70
|
-
} else if (styles.textAlign === "right") {
|
|
71
|
-
x = rectWidth;
|
|
72
|
-
}
|
|
73
|
-
ctx.textAlign = styles.textAlign as CanvasTextAlign;
|
|
74
|
-
|
|
75
|
-
ctx.fillText(line, x, y);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
const texture = new THREE.CanvasTexture(canvas);
|
|
79
|
-
texture.colorSpace = THREE.SRGBColorSpace;
|
|
80
|
-
texture.minFilter = THREE.LinearFilter;
|
|
81
|
-
texture.magFilter = THREE.LinearFilter;
|
|
82
|
-
texture.needsUpdate = true;
|
|
83
|
-
|
|
84
|
-
return texture;
|
|
85
|
-
}
|