@umicat/three-sdk 1.36.0 → 1.38.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/README.md +24 -2
- package/dist/vfx/VfxInstance.d.ts +6 -0
- package/dist/vfx/VfxInstance.js +188 -24
- package/dist/vfx/document.d.ts +45 -1
- package/dist/vfx/document.js +109 -7
- package/dist/vfx/index.d.ts +2 -0
- package/dist/vfx/index.js +2 -0
- package/dist/vfx/material.d.ts +215 -0
- package/dist/vfx/material.js +532 -0
- package/dist/vfx/mesh.d.ts +96 -0
- package/dist/vfx/mesh.js +161 -0
- package/package.json +1 -1
package/dist/vfx/mesh.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mesh layers and mesh-shaped particles (format v3, umicat-design ADR-055
|
|
3
|
+
* step 2). The shapes are Godot's primitive meshes with Godot's parameters,
|
|
4
|
+
* so a rebuild copies the numbers: QuadMesh faces +Z, PlaneMesh faces +Y,
|
|
5
|
+
* Cylinder / Sphere / Torus stand on the Y axis.
|
|
6
|
+
*/
|
|
7
|
+
import * as T from 'three';
|
|
8
|
+
export const MESH_SHAPES = {
|
|
9
|
+
quad: () => ({ type: 'quad', size: [1, 1] }),
|
|
10
|
+
plane: () => ({ type: 'plane', size: [2, 2] }),
|
|
11
|
+
sphere: () => ({ type: 'sphere', radius: .5, height: 1, hemisphere: false }),
|
|
12
|
+
cylinder: () => ({ type: 'cylinder', top: .5, bottom: .5, height: 1, caps: false }),
|
|
13
|
+
torus: () => ({ type: 'torus', inner: .4, outer: .5 }),
|
|
14
|
+
box: () => ({ type: 'box', size: [1, 1, 1] }),
|
|
15
|
+
ring: () => ({ type: 'ring', inner: .8, outer: 1 }),
|
|
16
|
+
};
|
|
17
|
+
const num = (v, min, max) => typeof v === 'number' && Number.isFinite(v) && v >= min && v <= max;
|
|
18
|
+
/** Fills a shape's missing parameters from its defaults and checks them. */
|
|
19
|
+
export function parseMeshShape(raw, at, fail) {
|
|
20
|
+
const type = raw?.type;
|
|
21
|
+
if (!raw || typeof raw !== 'object' || !(type in MESH_SHAPES))
|
|
22
|
+
return fail(`${at}.type must be one of ${Object.keys(MESH_SHAPES).join(', ')}`);
|
|
23
|
+
const base = MESH_SHAPES[type]();
|
|
24
|
+
for (const k of Object.keys(raw))
|
|
25
|
+
if (!(k in base))
|
|
26
|
+
fail(`${at}.${k} is not a ${type} field (expected ${Object.keys(base).join(', ')})`);
|
|
27
|
+
const s = { ...base, ...raw };
|
|
28
|
+
const pos = (k, v, max = 100) => { if (!num(v, 0, max))
|
|
29
|
+
fail(`${at}.${k} must be 0-${max}`); };
|
|
30
|
+
const vec = (k, v, n) => { if (!Array.isArray(v) || v.length !== n || !v.every((x) => num(x, 0, 100)))
|
|
31
|
+
fail(`${at}.${k} must be [${n === 2 ? 'w, h' : 'x, y, z'}], each 0-100`); };
|
|
32
|
+
switch (s.type) {
|
|
33
|
+
case 'quad':
|
|
34
|
+
case 'plane':
|
|
35
|
+
vec('size', s.size, 2);
|
|
36
|
+
break;
|
|
37
|
+
case 'box':
|
|
38
|
+
vec('size', s.size, 3);
|
|
39
|
+
break;
|
|
40
|
+
case 'sphere':
|
|
41
|
+
pos('radius', s.radius);
|
|
42
|
+
pos('height', s.height, 200);
|
|
43
|
+
if (typeof s.hemisphere !== 'boolean')
|
|
44
|
+
fail(`${at}.hemisphere must be a boolean`);
|
|
45
|
+
break;
|
|
46
|
+
case 'cylinder':
|
|
47
|
+
pos('top', s.top);
|
|
48
|
+
pos('bottom', s.bottom);
|
|
49
|
+
pos('height', s.height);
|
|
50
|
+
if (typeof s.caps !== 'boolean')
|
|
51
|
+
fail(`${at}.caps must be a boolean`);
|
|
52
|
+
if (s.top === 0 && s.bottom === 0)
|
|
53
|
+
fail(`${at} needs top or bottom above 0`);
|
|
54
|
+
break;
|
|
55
|
+
case 'torus':
|
|
56
|
+
case 'ring':
|
|
57
|
+
pos('outer', s.outer);
|
|
58
|
+
if (!num(s.inner, 0, s.outer))
|
|
59
|
+
fail(`${at}.inner must be 0-outer (${s.outer})`);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return s;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The geometry, built once per layer. Enough segments for vertex
|
|
66
|
+
* displacement to read as a surface (Binbun subdivides its cards 16x16 for the
|
|
67
|
+
* same reason).
|
|
68
|
+
*/
|
|
69
|
+
export function meshGeometry(s) {
|
|
70
|
+
switch (s.type) {
|
|
71
|
+
case 'quad': return new T.PlaneGeometry(s.size[0], s.size[1], 16, 16);
|
|
72
|
+
case 'plane': return new T.PlaneGeometry(s.size[0], s.size[1], 16, 16).rotateX(-Math.PI / 2);
|
|
73
|
+
case 'sphere': {
|
|
74
|
+
const g = new T.SphereGeometry(Math.max(s.radius, 1e-4), 32, s.hemisphere ? 8 : 16, 0, Math.PI * 2, 0, s.hemisphere ? Math.PI / 2 : Math.PI);
|
|
75
|
+
return s.radius > 0 && Math.abs(s.height - 2 * s.radius) > 1e-6 ? g.scale(1, s.height / (2 * s.radius), 1) : g;
|
|
76
|
+
}
|
|
77
|
+
case 'cylinder': return new T.CylinderGeometry(s.top, s.bottom, s.height, 32, 8, !s.caps);
|
|
78
|
+
case 'torus': return new T.TorusGeometry((s.inner + s.outer) / 2, Math.max(1e-4, (s.outer - s.inner) / 2), 16, 48).rotateX(-Math.PI / 2);
|
|
79
|
+
case 'box': return new T.BoxGeometry(s.size[0], s.size[1], s.size[2]);
|
|
80
|
+
case 'ring': return new T.RingGeometry(s.inner, s.outer, 64, 1).rotateX(-Math.PI / 2);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Ring UVs from three.js are planar; an effect ring wants u around and v across, like a cylinder's. */
|
|
84
|
+
export function fixRingUv(g, s) {
|
|
85
|
+
if (s.type !== 'ring')
|
|
86
|
+
return g;
|
|
87
|
+
const p = g.getAttribute('position'), uv = g.getAttribute('uv');
|
|
88
|
+
for (let i = 0; i < p.count; i++) {
|
|
89
|
+
const x = p.getX(i), z = p.getZ(i), r = Math.hypot(x, z);
|
|
90
|
+
uv.setXY(i, Math.atan2(z, x) / (Math.PI * 2) + .5, s.outer > s.inner ? (r - s.inner) / (s.outer - s.inner) : 0);
|
|
91
|
+
}
|
|
92
|
+
uv.needsUpdate = true;
|
|
93
|
+
return g;
|
|
94
|
+
}
|
|
95
|
+
const DISPLACE = (c, life) => c.displace ? `p += n * vfxDisplace(uv, ${life});` : '';
|
|
96
|
+
/** A mesh layer: one mesh, its colour and life from uniforms. */
|
|
97
|
+
export const meshLayerVertex = (c) => `
|
|
98
|
+
uniform vec4 uLayerColor; uniform vec2 uLayerLife; uniform float uFacing;
|
|
99
|
+
varying vec2 vUv; varying vec4 vColor; varying vec2 vLife; varying vec3 vN; varying vec3 vV;
|
|
100
|
+
${c.code}
|
|
101
|
+
void main(){
|
|
102
|
+
vec3 p = position, n = normal;
|
|
103
|
+
vUv = uv; vColor = uLayerColor; vLife = uLayerLife;
|
|
104
|
+
${DISPLACE(c, 'uLayerLife')}
|
|
105
|
+
if (uFacing > 0.5) {
|
|
106
|
+
// Turned to face the viewer, keeping its position and its own size.
|
|
107
|
+
vec4 c0 = modelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0);
|
|
108
|
+
// uFacing 1: a quad (its XY), 2: a plane (its XZ, z toward the viewer = down on screen).
|
|
109
|
+
c0.xy += uFacing < 1.5 ? vec2(p.x * length(modelMatrix[0].xyz), p.y * length(modelMatrix[1].xyz))
|
|
110
|
+
: vec2(p.x * length(modelMatrix[0].xyz), -p.z * length(modelMatrix[2].xyz));
|
|
111
|
+
vV = -c0.xyz; vN = vec3(0.0, 0.0, 1.0);
|
|
112
|
+
gl_Position = projectionMatrix * c0;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
vec4 mv = modelViewMatrix * vec4(p, 1.0);
|
|
116
|
+
vV = -mv.xyz; vN = normalize(normalMatrix * n);
|
|
117
|
+
gl_Position = projectionMatrix * mv;
|
|
118
|
+
}`;
|
|
119
|
+
/**
|
|
120
|
+
* A mesh particle: the mesh, scaled by the particle's size, stood on a basis
|
|
121
|
+
* whose Y is the particle's velocity (align velocity), the emitter's axis
|
|
122
|
+
* (axis) or the emitter's own up (otherwise), and turned by its angle about
|
|
123
|
+
* that Y -- Godot's transform_align / rotate_y.
|
|
124
|
+
*/
|
|
125
|
+
export const meshParticleVertex = (c) => `
|
|
126
|
+
attribute vec3 iPos; attribute vec4 iColor; attribute vec4 iMisc; attribute vec3 iVel; attribute vec2 iLife; attribute vec3 iAxes;
|
|
127
|
+
uniform float uAlign; uniform mat3 uOrient; uniform vec3 uAxis;
|
|
128
|
+
varying vec2 vUv; varying vec4 vColor; varying vec2 vLife; varying vec3 vN; varying vec3 vV;
|
|
129
|
+
${c.code}
|
|
130
|
+
void main(){
|
|
131
|
+
vec3 p = position, n = normal;
|
|
132
|
+
vUv = uv; vColor = iColor; vLife = iLife;
|
|
133
|
+
${DISPLACE(c, 'iLife')}
|
|
134
|
+
vec3 up = uOrient * vec3(0.0, 1.0, 0.0);
|
|
135
|
+
if (uAlign > 0.5 && uAlign < 1.5 && dot(iVel, iVel) > 1e-10) up = normalize(iVel);
|
|
136
|
+
else if (uAlign > 2.5) up = normalize(uAxis);
|
|
137
|
+
vec3 ref = abs(up.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);
|
|
138
|
+
vec3 side = normalize(cross(ref, up)), fwd = cross(side, up);
|
|
139
|
+
if (uAlign < 0.5 || (uAlign > 1.5 && uAlign < 2.5)) { side = uOrient * vec3(1.0, 0.0, 0.0); fwd = uOrient * vec3(0.0, 0.0, 1.0); }
|
|
140
|
+
float s = sin(iMisc.y), co = cos(iMisc.y);
|
|
141
|
+
vec3 sx = side * co - fwd * s, sz = side * s + fwd * co;
|
|
142
|
+
mat3 basis = mat3(sx, up, sz);
|
|
143
|
+
vec4 mv = modelViewMatrix * vec4(iPos + basis * (p * iMisc.x * iAxes), 1.0);
|
|
144
|
+
vV = -mv.xyz; vN = normalize((modelViewMatrix * vec4(basis * (n / max(iAxes, vec3(1e-4))), 0.0)).xyz);
|
|
145
|
+
gl_Position = projectionMatrix * mv;
|
|
146
|
+
}`;
|
|
147
|
+
/** Shared by mesh layers and mesh particles: the material, times the colour, with the alpha mode. */
|
|
148
|
+
export const meshFragment = (c) => `
|
|
149
|
+
varying vec2 vUv; varying vec4 vColor; varying vec2 vLife; varying vec3 vN; varying vec3 vV;
|
|
150
|
+
${c.code}
|
|
151
|
+
float vfxBayer(vec2 p){ ivec2 q = ivec2(mod(p, 4.0)); int i = q.x + q.y * 4;
|
|
152
|
+
float m[16] = float[16](0.,8.,2.,10.,12.,4.,14.,6.,3.,11.,1.,9.,15.,7.,13.,5.); return (m[i] + 0.5) / 16.0; }
|
|
153
|
+
void main(){
|
|
154
|
+
float facing = abs(dot(normalize(vN), normalize(vV)));
|
|
155
|
+
vec4 m = vfxMaterial(vUv, vLife, facing);
|
|
156
|
+
float a = m.a * vColor.a;
|
|
157
|
+
${c.alphaMode === 'cutout' ? 'if (a < uCutoff) discard; a = 1.0;' : c.alphaMode === 'dither' ? 'if (a < vfxBayer(gl_FragCoord.xy)) discard; a = 1.0;' : 'if (a <= 0.002) discard;'}
|
|
158
|
+
gl_FragColor = vec4(m.rgb * vColor.rgb, a);
|
|
159
|
+
#include <tonemapping_fragment>
|
|
160
|
+
#include <colorspace_fragment>
|
|
161
|
+
}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umicat/three-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.38.0",
|
|
4
4
|
"description": "Three.js runtime for Umicat games: the scene3d design format, its loader with physics, a kinematic character controller, and the Umicat platform via @umicat/platform-sdk.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|