@tresjs/core 1.8.0 → 1.8.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/dist/tres.js +206 -206
- package/dist/tres.umd.cjs +2 -2
- package/package.json +1 -1
package/dist/tres.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* name: @tresjs/core
|
|
3
|
-
* version: v1.8.
|
|
3
|
+
* version: v1.8.1
|
|
4
4
|
* (c) 2023
|
|
5
5
|
* description: Declarative ThreeJS using Vue Components
|
|
6
6
|
* author: Alvaro Saburido <hola@alvarosaburido.dev> (https://github.com/alvarosabu/)
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import { useEventListener as
|
|
10
|
-
import * as
|
|
11
|
-
import { PerspectiveCamera as z, OrthographicCamera as ye, Vector3 as
|
|
12
|
-
var
|
|
13
|
-
const
|
|
14
|
-
let
|
|
8
|
+
import { inject as Z, watch as j, toRef as $e, ref as L, defineComponent as q, onUnmounted as ge, shallowRef as Q, shallowReactive as Oe, computed as he, toRefs as we, provide as T, onBeforeUnmount as Ae, h as H } from "vue";
|
|
9
|
+
import { useEventListener as Be, createEventHook as ee, useRafFn as Ie, resolveUnref as w, useWindowSize as Fe, useElementSize as _e, useDevicePixelRatio as Ue, unrefElement as G } from "@vueuse/core";
|
|
10
|
+
import * as ce from "three";
|
|
11
|
+
import { PerspectiveCamera as z, OrthographicCamera as ye, Vector3 as je, Color as K, Mesh as ze, Fog as De, BufferAttribute as We, Clock as ve, Scene as ke, LoadingManager as Ne, TextureLoader as Ve, Raycaster as He, Vector2 as Ge, sRGBEncoding as Ke, ACESFilmicToneMapping as Xe, PCFSoftShadowMap as Ye, PCFShadowMap as Je, LinearEncoding as le, NoToneMapping as ue, WebGLRenderer as Ze } from "three";
|
|
12
|
+
var qe = /* @__PURE__ */ ((e) => (e.Perspective = "Perspective", e.Orthographic = "Orthographic", e))(qe || {});
|
|
13
|
+
const Qe = 45;
|
|
14
|
+
let b;
|
|
15
15
|
function Se() {
|
|
16
|
-
const { state: e, setState: n } =
|
|
17
|
-
function
|
|
18
|
-
var
|
|
19
|
-
if (
|
|
20
|
-
const { near:
|
|
16
|
+
const { state: e, setState: n } = $(), o = Z("aspect-ratio");
|
|
17
|
+
function a(t = "Perspective", r) {
|
|
18
|
+
var l, s, i;
|
|
19
|
+
if (t === "Perspective") {
|
|
20
|
+
const { near: u, far: d, fov: p } = r || {
|
|
21
21
|
near: 0.1,
|
|
22
22
|
far: 1e3,
|
|
23
|
-
fov:
|
|
23
|
+
fov: Qe
|
|
24
24
|
};
|
|
25
|
-
|
|
25
|
+
b = new z(p, ((l = e.aspectRatio) == null ? void 0 : l.value) || window.innerWidth / window.innerHeight, u, d), (s = e.cameras) == null || s.push(b);
|
|
26
26
|
} else {
|
|
27
|
-
const { left:
|
|
27
|
+
const { left: u, right: d, top: p, bottom: m, near: g, far: R } = r || {
|
|
28
28
|
left: -100,
|
|
29
29
|
right: 100,
|
|
30
30
|
top: 100,
|
|
@@ -32,36 +32,36 @@ function Se() {
|
|
|
32
32
|
near: 0.1,
|
|
33
33
|
far: 1e3
|
|
34
34
|
};
|
|
35
|
-
|
|
35
|
+
b = new ye(u, d, p, m, g, R), (i = e.cameras) == null || i.push(b);
|
|
36
36
|
}
|
|
37
|
-
return e.camera =
|
|
37
|
+
return e.camera = b, n("camera", e.camera), b;
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
var u;
|
|
41
|
-
e.camera instanceof z && e.aspectRatio && (e.camera.aspect = e.aspectRatio.value), (u = e.camera) == null || u.updateProjectionMatrix();
|
|
42
|
-
}
|
|
43
|
-
function c(u) {
|
|
39
|
+
function c() {
|
|
44
40
|
var t;
|
|
45
|
-
|
|
41
|
+
e.camera instanceof z && e.aspectRatio && (e.camera.aspect = e.aspectRatio.value), (t = e.camera) == null || t.updateProjectionMatrix();
|
|
42
|
+
}
|
|
43
|
+
function f(t) {
|
|
44
|
+
var r;
|
|
45
|
+
(r = e.cameras) == null || r.push(t), t instanceof z && e.aspectRatio && (t.aspect = e.aspectRatio.value), t.updateProjectionMatrix(), n("camera", t);
|
|
46
46
|
}
|
|
47
|
-
function
|
|
47
|
+
function h() {
|
|
48
48
|
e.cameras = [];
|
|
49
49
|
}
|
|
50
|
-
return
|
|
51
|
-
activeCamera:
|
|
52
|
-
createCamera:
|
|
53
|
-
updateCamera:
|
|
54
|
-
pushCamera:
|
|
55
|
-
clearCameras:
|
|
50
|
+
return o && j(o, c), {
|
|
51
|
+
activeCamera: $e(e, "camera"),
|
|
52
|
+
createCamera: a,
|
|
53
|
+
updateCamera: c,
|
|
54
|
+
pushCamera: f,
|
|
55
|
+
clearCameras: h
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
|
-
const
|
|
59
|
-
function
|
|
58
|
+
const fe = "[TresJS ▲ ■ ●] ";
|
|
59
|
+
function F() {
|
|
60
60
|
function e(a, c) {
|
|
61
|
-
console.error(`${
|
|
61
|
+
console.error(`${fe} ${a}`, c || "");
|
|
62
62
|
}
|
|
63
63
|
function n(a) {
|
|
64
|
-
console.warn(`${
|
|
64
|
+
console.warn(`${fe} ${a}`);
|
|
65
65
|
}
|
|
66
66
|
function o(a, c) {
|
|
67
67
|
}
|
|
@@ -71,73 +71,73 @@ function I() {
|
|
|
71
71
|
logMessage: o
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
|
-
const D =
|
|
74
|
+
const D = L({ ...ce, uuid: ce.MathUtils.generateUUID() });
|
|
75
75
|
delete D.value.Scene;
|
|
76
|
-
let
|
|
77
|
-
function
|
|
78
|
-
const { logError: o } =
|
|
79
|
-
!
|
|
80
|
-
const { createComponentInstances: a } =
|
|
76
|
+
let A;
|
|
77
|
+
function et(e, n = "Tres") {
|
|
78
|
+
const { logError: o } = F();
|
|
79
|
+
!A && e && (A = e);
|
|
80
|
+
const { createComponentInstances: a } = Ce(n);
|
|
81
81
|
return {
|
|
82
|
-
extend: (
|
|
83
|
-
if (!
|
|
82
|
+
extend: (f) => {
|
|
83
|
+
if (!f) {
|
|
84
84
|
o("No objects provided to extend catalogue");
|
|
85
85
|
return;
|
|
86
86
|
}
|
|
87
|
-
D.value = Object.assign(D.value,
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
D.value = Object.assign(D.value, f);
|
|
88
|
+
const h = a(L(f));
|
|
89
|
+
A && h.forEach(([t, r]) => {
|
|
90
|
+
A._context.components[t] || A.component(t, r);
|
|
91
91
|
});
|
|
92
92
|
},
|
|
93
93
|
catalogue: D
|
|
94
94
|
};
|
|
95
95
|
}
|
|
96
|
-
const
|
|
96
|
+
const M = (e) => typeof e < "u", tt = (e) => typeof e == "function", N = (e) => !!e && e.constructor === Array;
|
|
97
97
|
function nt(e) {
|
|
98
|
-
return typeof e == "number" ? [e, e, e] : e instanceof
|
|
98
|
+
return typeof e == "number" ? [e, e, e] : e instanceof je ? [e.x, e.y, e.z] : e;
|
|
99
99
|
}
|
|
100
100
|
function rt(e) {
|
|
101
|
-
return e instanceof
|
|
101
|
+
return e instanceof K ? e : Array.isArray(e) ? new K(...e) : new K(e);
|
|
102
102
|
}
|
|
103
|
-
const
|
|
104
|
-
function
|
|
103
|
+
const pe = ["rotation", "scale", "position"], ot = ["X", "Y", "Z"], at = ["color"], it = ["r", "g", "b"];
|
|
104
|
+
function Ce(e) {
|
|
105
105
|
const {
|
|
106
106
|
/* logMessage, */
|
|
107
107
|
logError: n
|
|
108
|
-
} =
|
|
108
|
+
} = F();
|
|
109
109
|
function o(t, r) {
|
|
110
|
-
|
|
111
|
-
const i = l.replace(/(-\w)/g, (
|
|
112
|
-
r.setAttribute(i, new
|
|
110
|
+
M(t) && M(r) && Object.entries(t).forEach(([l, s]) => {
|
|
111
|
+
const i = l.replace(/(-\w)/g, (u) => u[1].toUpperCase());
|
|
112
|
+
r.setAttribute(i, new We(...s));
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
function a(t, r) {
|
|
116
|
-
|
|
117
|
-
const i = l.replace(/(-\w)/g, (
|
|
118
|
-
let
|
|
116
|
+
M(t) && M(r) && Object.entries(t).forEach(([l, s]) => {
|
|
117
|
+
const i = l.replace(/(-\w)/g, (g) => g[1].toUpperCase());
|
|
118
|
+
let u, d, p, m;
|
|
119
119
|
if (!(i === "args" || s === void 0)) {
|
|
120
|
-
|
|
121
|
-
i.startsWith(
|
|
120
|
+
pe.includes(i) && s ? s = nt(s) : pe.forEach((g) => {
|
|
121
|
+
i.startsWith(g) && i.length === g.length + 1 && (u = g, d = i.substring(g.length), ot.includes(d) || n(
|
|
122
122
|
// eslint-disable-next-line max-len
|
|
123
|
-
`There was an error setting ${l} property, ${
|
|
123
|
+
`There was an error setting ${l} property, ${d} is not a valid axis for ${u}`
|
|
124
124
|
));
|
|
125
|
-
}), at.forEach((
|
|
126
|
-
i.startsWith(
|
|
125
|
+
}), at.forEach((g) => {
|
|
126
|
+
i.startsWith(g) && i.length === g.length + 1 && (p = g, m = i.substring(g.length).toLowerCase(), it.includes(m) || n(`There was an error setting ${l} property , ${m} is not a valid axis for ${p}`));
|
|
127
127
|
}), t.ref && (t.ref = r);
|
|
128
128
|
try {
|
|
129
|
-
if (r[i] &&
|
|
129
|
+
if (r[i] && M(r[i].set))
|
|
130
130
|
r[i].set(...N(s) ? s : [s]);
|
|
131
131
|
else if (
|
|
132
132
|
// Check if the property has a "setAxis" method
|
|
133
|
-
|
|
133
|
+
u && r[u]
|
|
134
134
|
)
|
|
135
|
-
|
|
135
|
+
M(r[u][`set${d}`]) ? r[u][`set${d}`](s) : M(r[`rotate${d}`]) && r[`rotate${d}`](s);
|
|
136
136
|
else if (
|
|
137
137
|
// Check if the instance has a "color" property
|
|
138
|
-
|
|
138
|
+
p && m && r[p] && r[p][m]
|
|
139
139
|
)
|
|
140
|
-
r[
|
|
140
|
+
r[p][m] = s;
|
|
141
141
|
else {
|
|
142
142
|
if (s === "" && (s = !0), tt(r[i])) {
|
|
143
143
|
if (l === "center" && !s)
|
|
@@ -147,71 +147,75 @@ function Ee(e) {
|
|
|
147
147
|
}
|
|
148
148
|
r[i] = s;
|
|
149
149
|
}
|
|
150
|
-
} catch (
|
|
151
|
-
n(`There was an error setting ${i} property`,
|
|
150
|
+
} catch (g) {
|
|
151
|
+
n(`There was an error setting ${i} property`, g);
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
154
|
});
|
|
155
155
|
}
|
|
156
156
|
function c(t) {
|
|
157
|
-
var i,
|
|
157
|
+
var i, u;
|
|
158
158
|
const r = /^Symbol\(Fragment\)$/g, l = /^Symbol\(Text\)$/g, s = /^Symbol\(Comment\)$/g;
|
|
159
159
|
if (r.test(t.type.toString()))
|
|
160
|
-
return t.children.map((
|
|
160
|
+
return t.children.map((d) => c(d));
|
|
161
161
|
if (l.test(t.type.toString()) || s.test(t.type.toString()))
|
|
162
162
|
return;
|
|
163
163
|
{
|
|
164
|
-
const
|
|
164
|
+
const d = t.type.name.replace(e, ""), p = Z("catalogue");
|
|
165
165
|
let m;
|
|
166
166
|
if (p)
|
|
167
167
|
if ((i = t.children) != null && i.default) {
|
|
168
|
-
const
|
|
169
|
-
m = new p.value[
|
|
168
|
+
const g = t.children.default().map((R) => c(R));
|
|
169
|
+
m = new p.value[d](...g.flat().filter(Boolean));
|
|
170
170
|
} else
|
|
171
|
-
(
|
|
172
|
-
return t != null && t.props && (
|
|
171
|
+
(u = t == null ? void 0 : t.props) != null && u.args ? p != null && p.value[d] ? m = new p.value[d](...t.props.args) : n(`There is no ${d} in the catalogue`, p == null ? void 0 : p.value.uuid) : m = new p.value[d]();
|
|
172
|
+
return t != null && t.props && (d === "BufferGeometry" ? o(t.props, m) : a(t.props, m)), m;
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
|
-
function
|
|
175
|
+
function f(t, r, l) {
|
|
176
176
|
if (l.default && (l != null && l.default())) {
|
|
177
177
|
const s = l.default().map((i) => c(i));
|
|
178
178
|
if (t.name === "Group") {
|
|
179
179
|
const i = new t();
|
|
180
|
-
return s.forEach((
|
|
181
|
-
i.add(
|
|
180
|
+
return s.forEach((u) => {
|
|
181
|
+
i.add(u);
|
|
182
182
|
}), i;
|
|
183
183
|
} else
|
|
184
184
|
return new t(...s.flat().filter(Boolean));
|
|
185
185
|
} else
|
|
186
186
|
return r.args ? new t(...r.args) : new t();
|
|
187
187
|
}
|
|
188
|
-
function
|
|
188
|
+
function h(t) {
|
|
189
189
|
return Object.entries(t.value).filter(([r, l]) => {
|
|
190
190
|
var s, i;
|
|
191
191
|
return (i = (s = l == null ? void 0 : l.prototype) == null ? void 0 : s.constructor) == null ? void 0 : i.toString().includes("class");
|
|
192
192
|
}).map(([r, l]) => {
|
|
193
|
-
const s = `${e}${r}`, i =
|
|
193
|
+
const s = `${e}${r}`, i = q({
|
|
194
194
|
name: s,
|
|
195
|
-
setup(
|
|
196
|
-
const { state:
|
|
197
|
-
let
|
|
198
|
-
a(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
195
|
+
setup(u, { slots: d, attrs: p, ...m }) {
|
|
196
|
+
const { state: g } = $(), { onLoop: R } = ne(), v = g.scene, _ = g.raycaster;
|
|
197
|
+
let S = f(l, p, d);
|
|
198
|
+
if (a(p, S), S instanceof z || S instanceof ye) {
|
|
199
|
+
const { pushCamera: O } = Se();
|
|
200
|
+
O(S);
|
|
201
|
+
}
|
|
202
|
+
S.isObject3D && (v == null || v.add(S));
|
|
203
|
+
let C = null, E = null;
|
|
204
|
+
if (S instanceof ze) {
|
|
205
|
+
R(() => {
|
|
206
|
+
if (S && _ && (v != null && v.children)) {
|
|
207
|
+
const x = _.intersectObjects(v == null ? void 0 : v.children);
|
|
208
|
+
x.length > 0 ? (E = x[0], (C === null || C.object.uuid !== (E == null ? void 0 : E.object.uuid)) && m.emit("pointer-enter", E), m.emit("pointer-move", E)) : (E = null, C !== null && m.emit("pointer-leave", C)), C = E;
|
|
205
209
|
}
|
|
206
210
|
});
|
|
207
|
-
const
|
|
208
|
-
|
|
211
|
+
const O = Be(window, "click", () => {
|
|
212
|
+
m.emit("click", C);
|
|
209
213
|
});
|
|
210
214
|
ge(() => {
|
|
211
|
-
|
|
215
|
+
O();
|
|
212
216
|
});
|
|
213
217
|
}
|
|
214
|
-
return
|
|
218
|
+
return v && S instanceof De && (v.fog = S), m.expose(S), () => {
|
|
215
219
|
};
|
|
216
220
|
}
|
|
217
221
|
});
|
|
@@ -219,33 +223,33 @@ function Ee(e) {
|
|
|
219
223
|
});
|
|
220
224
|
}
|
|
221
225
|
return {
|
|
222
|
-
createComponentInstances:
|
|
226
|
+
createComponentInstances: h,
|
|
223
227
|
processProps: a,
|
|
224
228
|
createInstanceFromVNode: c
|
|
225
229
|
};
|
|
226
230
|
}
|
|
227
|
-
const
|
|
231
|
+
const Ee = ee(), Re = ee(), te = ee(), I = new ve();
|
|
228
232
|
let W = 0, k = 0;
|
|
229
|
-
const { pause: st, resume: ct, isActive: lt } =
|
|
233
|
+
const { pause: st, resume: ct, isActive: lt } = Ie(
|
|
230
234
|
() => {
|
|
231
|
-
|
|
235
|
+
Ee.trigger({ delta: W, elapsed: k, clock: I }), Re.trigger({ delta: W, elapsed: k, clock: I }), te.trigger({ delta: W, elapsed: k, clock: I });
|
|
232
236
|
},
|
|
233
237
|
{ immediate: !1 }
|
|
234
238
|
);
|
|
235
|
-
|
|
236
|
-
W =
|
|
239
|
+
te.on(() => {
|
|
240
|
+
W = I.getDelta(), k = I.getElapsedTime();
|
|
237
241
|
});
|
|
238
|
-
function
|
|
242
|
+
function ne() {
|
|
239
243
|
return {
|
|
240
|
-
onBeforeLoop:
|
|
241
|
-
onLoop:
|
|
242
|
-
onAfterLoop:
|
|
244
|
+
onBeforeLoop: Ee.on,
|
|
245
|
+
onLoop: Re.on,
|
|
246
|
+
onAfterLoop: te.on,
|
|
243
247
|
pause: st,
|
|
244
248
|
resume: ct,
|
|
245
249
|
isActive: lt
|
|
246
250
|
};
|
|
247
251
|
}
|
|
248
|
-
const ut =
|
|
252
|
+
const ut = Q(new ke());
|
|
249
253
|
function ft() {
|
|
250
254
|
return {
|
|
251
255
|
scene: ut
|
|
@@ -258,54 +262,54 @@ function pt(e) {
|
|
|
258
262
|
}), n;
|
|
259
263
|
}
|
|
260
264
|
async function Ct(e, n, o, a, c) {
|
|
261
|
-
const { logError:
|
|
262
|
-
c && c(
|
|
265
|
+
const { logError: f } = F(), h = new e();
|
|
266
|
+
c && c(h), o && o(h);
|
|
263
267
|
const r = (Array.isArray(n) ? n : [n]).map(
|
|
264
268
|
(l) => new Promise((s, i) => {
|
|
265
|
-
|
|
269
|
+
h.load(
|
|
266
270
|
l,
|
|
267
|
-
(
|
|
268
|
-
|
|
271
|
+
(u) => {
|
|
272
|
+
u.scene && Object.assign(u, pt(u.scene)), s(u);
|
|
269
273
|
},
|
|
270
274
|
a,
|
|
271
|
-
(
|
|
275
|
+
(u) => i(f("[useLoader] - Failed to load resource", u))
|
|
272
276
|
);
|
|
273
277
|
})
|
|
274
278
|
);
|
|
275
279
|
return N(n) ? await Promise.all(r) : await r[0];
|
|
276
280
|
}
|
|
277
281
|
async function Et(e) {
|
|
278
|
-
const n = new
|
|
282
|
+
const n = new Ne(), o = new Ve(n), a = (c) => new Promise((f, h) => {
|
|
279
283
|
o.load(
|
|
280
284
|
c,
|
|
281
|
-
(t) =>
|
|
285
|
+
(t) => f(t),
|
|
282
286
|
() => null,
|
|
283
287
|
() => {
|
|
284
|
-
|
|
288
|
+
h(new Error("[useTextures] - Failed to load texture"));
|
|
285
289
|
}
|
|
286
290
|
);
|
|
287
291
|
});
|
|
288
292
|
if (N(e)) {
|
|
289
|
-
const c = await Promise.all(e.map((
|
|
293
|
+
const c = await Promise.all(e.map((f) => a(f)));
|
|
290
294
|
return e.length > 1 ? c : c[0];
|
|
291
295
|
} else {
|
|
292
|
-
const { map: c, displacementMap:
|
|
296
|
+
const { map: c, displacementMap: f, normalMap: h, roughnessMap: t, metalnessMap: r, aoMap: l } = e;
|
|
293
297
|
return {
|
|
294
298
|
map: c ? await a(c) : null,
|
|
295
|
-
displacementMap:
|
|
296
|
-
normalMap:
|
|
299
|
+
displacementMap: f ? await a(f) : null,
|
|
300
|
+
normalMap: h ? await a(h) : null,
|
|
297
301
|
roughnessMap: t ? await a(t) : null,
|
|
298
302
|
metalnessMap: r ? await a(r) : null,
|
|
299
303
|
aoMap: l ? await a(l) : null
|
|
300
304
|
};
|
|
301
305
|
}
|
|
302
306
|
}
|
|
303
|
-
const U =
|
|
307
|
+
const U = Oe({
|
|
304
308
|
camera: void 0,
|
|
305
309
|
cameras: [],
|
|
306
310
|
aspectRatio: he(() => window.innerWidth / window.innerHeight)
|
|
307
311
|
});
|
|
308
|
-
function
|
|
312
|
+
function $() {
|
|
309
313
|
function e(o) {
|
|
310
314
|
return U[o];
|
|
311
315
|
}
|
|
@@ -319,109 +323,105 @@ function T() {
|
|
|
319
323
|
setState: n
|
|
320
324
|
};
|
|
321
325
|
}
|
|
322
|
-
const
|
|
326
|
+
const X = Q(new He()), B = L(new Ge()), me = L(null);
|
|
323
327
|
function mt() {
|
|
324
|
-
const { setState: e } =
|
|
325
|
-
e("raycaster",
|
|
328
|
+
const { setState: e } = $();
|
|
329
|
+
e("raycaster", X.value), e("pointer", B), e("currentInstance", me), T("raycaster", X), T("pointer", B), T("currentInstance", me);
|
|
326
330
|
function n(o) {
|
|
327
|
-
|
|
331
|
+
B.value.x = o.clientX / window.innerWidth * 2 - 1, B.value.y = -(o.clientY / window.innerHeight) * 2 + 1;
|
|
328
332
|
}
|
|
329
333
|
return window.addEventListener("pointermove", n), ge(() => {
|
|
330
334
|
window.removeEventListener("pointermove", n);
|
|
331
335
|
}), {
|
|
332
|
-
raycaster:
|
|
333
|
-
pointer:
|
|
336
|
+
raycaster: X,
|
|
337
|
+
pointer: B
|
|
334
338
|
};
|
|
335
339
|
}
|
|
336
|
-
const
|
|
340
|
+
const Y = {
|
|
337
341
|
realistic: {
|
|
338
|
-
outputEncoding:
|
|
339
|
-
toneMapping:
|
|
342
|
+
outputEncoding: Ke,
|
|
343
|
+
toneMapping: Xe,
|
|
340
344
|
toneMappingExposure: 3,
|
|
341
345
|
shadowMap: {
|
|
342
346
|
enabled: !0,
|
|
343
|
-
type:
|
|
347
|
+
type: Ye
|
|
344
348
|
}
|
|
345
349
|
}
|
|
346
|
-
},
|
|
350
|
+
}, Me = (e, n) => {
|
|
347
351
|
for (const o of Object.keys(n))
|
|
348
|
-
n[o] instanceof Object && Object.assign(n[o],
|
|
352
|
+
n[o] instanceof Object && Object.assign(n[o], Me(e[o], n[o]));
|
|
349
353
|
return Object.assign(e || {}, n), e;
|
|
350
|
-
},
|
|
354
|
+
}, y = Q(), J = L(!1);
|
|
351
355
|
function dt(e, n, o) {
|
|
352
356
|
const {
|
|
353
357
|
alpha: a = !0,
|
|
354
358
|
antialias: c = !0,
|
|
355
|
-
depth:
|
|
356
|
-
logarithmicDepthBuffer:
|
|
359
|
+
depth: f,
|
|
360
|
+
logarithmicDepthBuffer: h,
|
|
357
361
|
failIfMajorPerformanceCaveat: t,
|
|
358
362
|
precision: r,
|
|
359
363
|
premultipliedAlpha: l,
|
|
360
364
|
stencil: s,
|
|
361
365
|
shadows: i = !1,
|
|
362
|
-
shadowMapType:
|
|
363
|
-
physicallyCorrectLights:
|
|
364
|
-
useLegacyLights:
|
|
365
|
-
outputEncoding:
|
|
366
|
-
toneMapping:
|
|
367
|
-
toneMappingExposure:
|
|
368
|
-
context:
|
|
369
|
-
powerPreference:
|
|
370
|
-
preserveDrawingBuffer:
|
|
371
|
-
clearColor:
|
|
366
|
+
shadowMapType: u = Je,
|
|
367
|
+
physicallyCorrectLights: d = !1,
|
|
368
|
+
useLegacyLights: p = !1,
|
|
369
|
+
outputEncoding: m = le,
|
|
370
|
+
toneMapping: g = ue,
|
|
371
|
+
toneMappingExposure: R = 1,
|
|
372
|
+
context: v = void 0,
|
|
373
|
+
powerPreference: _ = "default",
|
|
374
|
+
preserveDrawingBuffer: S = !1,
|
|
375
|
+
clearColor: C,
|
|
372
376
|
windowSize: E = !1,
|
|
373
|
-
preset:
|
|
374
|
-
} = we(o), { width:
|
|
375
|
-
|
|
376
|
-
},
|
|
377
|
-
if (!
|
|
377
|
+
preset: O = void 0
|
|
378
|
+
} = we(o), { setState: x } = $(), { width: re, height: oe } = w(E) ? Fe() : _e(n), { logError: Le } = F(), { pixelRatio: ae } = Ue(), { pause: xe, resume: Pe } = ne(), V = he(() => re.value / oe.value), ie = () => {
|
|
379
|
+
y.value && (y.value.setSize(re.value, oe.value), y.value.setPixelRatio(Math.min(ae.value, 2)));
|
|
380
|
+
}, se = () => {
|
|
381
|
+
if (!y.value)
|
|
378
382
|
return;
|
|
379
|
-
const
|
|
380
|
-
if (
|
|
381
|
-
|
|
383
|
+
const P = w(O);
|
|
384
|
+
if (P) {
|
|
385
|
+
P in Y || Le("Renderer Preset must be one of these: " + Object.keys(Y).join(", ")), Me(y.value, Y[P]);
|
|
382
386
|
return;
|
|
383
387
|
}
|
|
384
|
-
|
|
388
|
+
y.value.shadowMap.enabled = w(i), y.value.shadowMap.type = w(u), y.value.toneMapping = w(g) || ue, y.value.toneMappingExposure = w(R), y.value.outputEncoding = w(m) || le, C != null && C.value && y.value.setClearColor(rt(w(C))), y.value.useLegacyLights = w(p);
|
|
389
|
+
}, be = () => {
|
|
390
|
+
const P = G(e);
|
|
391
|
+
y.value || !P || (y.value = new Ze({
|
|
392
|
+
canvas: P,
|
|
393
|
+
alpha: w(a),
|
|
394
|
+
antialias: w(c),
|
|
395
|
+
context: w(v),
|
|
396
|
+
depth: w(f),
|
|
397
|
+
failIfMajorPerformanceCaveat: w(t),
|
|
398
|
+
logarithmicDepthBuffer: w(h),
|
|
399
|
+
powerPreference: w(_),
|
|
400
|
+
precision: w(r),
|
|
401
|
+
stencil: w(s),
|
|
402
|
+
preserveDrawingBuffer: w(S),
|
|
403
|
+
premultipliedAlpha: w(l)
|
|
404
|
+
}), x("renderer", y.value), x("clock", new ve()), x("aspectRatio", V), se(), ie(), Pe(), J.value = !0);
|
|
385
405
|
}, Te = () => {
|
|
386
|
-
|
|
387
|
-
if (w.value || !x)
|
|
388
|
-
return;
|
|
389
|
-
w.value = new qe({
|
|
390
|
-
canvas: x,
|
|
391
|
-
alpha: h(a),
|
|
392
|
-
antialias: h(c),
|
|
393
|
-
context: h(S),
|
|
394
|
-
depth: h(d),
|
|
395
|
-
failIfMajorPerformanceCaveat: h(t),
|
|
396
|
-
logarithmicDepthBuffer: h(u),
|
|
397
|
-
powerPreference: h(F),
|
|
398
|
-
precision: h(r),
|
|
399
|
-
stencil: h(s),
|
|
400
|
-
preserveDrawingBuffer: h(V),
|
|
401
|
-
premultipliedAlpha: h(l)
|
|
402
|
-
});
|
|
403
|
-
const { setState: G } = T();
|
|
404
|
-
G("renderer", w.value), G("clock", new ve()), G("aspectRatio", H), ie(), ae(), be(), q.value = !0;
|
|
405
|
-
}, $e = () => {
|
|
406
|
-
w.value && (w.value.dispose(), w.value = void 0, q.value = !1, Pe());
|
|
406
|
+
y.value && (y.value.dispose(), y.value = void 0, J.value = !1, xe());
|
|
407
407
|
};
|
|
408
|
-
return j([
|
|
409
|
-
[i,
|
|
410
|
-
|
|
408
|
+
return j([V, ae], ie), j(
|
|
409
|
+
[i, u, m, p, g, R, C],
|
|
410
|
+
se
|
|
411
411
|
), j(
|
|
412
412
|
() => [e, n],
|
|
413
413
|
() => {
|
|
414
|
-
|
|
414
|
+
G(e) && G(n) && be();
|
|
415
415
|
},
|
|
416
416
|
{ immediate: !0, deep: !0 }
|
|
417
417
|
), {
|
|
418
|
-
renderer:
|
|
419
|
-
isReady:
|
|
420
|
-
dispose:
|
|
421
|
-
aspectRatio:
|
|
418
|
+
renderer: y,
|
|
419
|
+
isReady: J,
|
|
420
|
+
dispose: Te,
|
|
421
|
+
aspectRatio: V
|
|
422
422
|
};
|
|
423
423
|
}
|
|
424
|
-
const { logError:
|
|
424
|
+
const { logError: de, logWarning: gt } = F(), ht = q({
|
|
425
425
|
name: "TresCanvas",
|
|
426
426
|
props: {
|
|
427
427
|
shadows: Boolean,
|
|
@@ -443,13 +443,13 @@ const { logError: me, logWarning: gt } = I(), ht = Q({
|
|
|
443
443
|
preset: String
|
|
444
444
|
},
|
|
445
445
|
setup(e, { slots: n, attrs: o }) {
|
|
446
|
-
const a =
|
|
447
|
-
return
|
|
446
|
+
const a = L(), c = L(), { renderer: f, dispose: h, aspectRatio: t } = dt(a, c, e);
|
|
447
|
+
return T("aspect-ratio", t), T("renderer", f), n.default && !n.default().some((r) => r.type.name === "Scene") && de("TresCanvas must contain a Scene component."), n.default && !n.default().some((r) => {
|
|
448
448
|
var l;
|
|
449
449
|
return (l = r.type.name) == null ? void 0 : l.includes("Camera");
|
|
450
|
-
}) &&
|
|
450
|
+
}) && de("Scene must contain a Camera component."), Ae(() => h()), () => {
|
|
451
451
|
if (n.default)
|
|
452
|
-
return
|
|
452
|
+
return H(
|
|
453
453
|
"div",
|
|
454
454
|
{
|
|
455
455
|
ref: c,
|
|
@@ -464,7 +464,7 @@ const { logError: me, logWarning: gt } = I(), ht = Q({
|
|
|
464
464
|
}
|
|
465
465
|
},
|
|
466
466
|
[
|
|
467
|
-
|
|
467
|
+
H(
|
|
468
468
|
"div",
|
|
469
469
|
{
|
|
470
470
|
style: {
|
|
@@ -473,7 +473,7 @@ const { logError: me, logWarning: gt } = I(), ht = Q({
|
|
|
473
473
|
}
|
|
474
474
|
},
|
|
475
475
|
[
|
|
476
|
-
|
|
476
|
+
H("canvas", {
|
|
477
477
|
ref: a,
|
|
478
478
|
style: {
|
|
479
479
|
display: "block",
|
|
@@ -491,12 +491,12 @@ const { logError: me, logWarning: gt } = I(), ht = Q({
|
|
|
491
491
|
);
|
|
492
492
|
};
|
|
493
493
|
}
|
|
494
|
-
}), wt =
|
|
494
|
+
}), wt = q({
|
|
495
495
|
name: "Scene",
|
|
496
496
|
setup(e, { slots: n }) {
|
|
497
|
-
const { setState: o } =
|
|
498
|
-
return
|
|
499
|
-
|
|
497
|
+
const { setState: o } = $(), { scene: a } = ft(), c = Z("renderer"), { activeCamera: f } = Se(), { raycaster: h, pointer: t } = mt(), { onLoop: r } = ne();
|
|
498
|
+
return T("local-scene", a), o("scene", a.value), r(() => {
|
|
499
|
+
f.value && (h.value.setFromCamera(t.value, f.value), c != null && c.value && f && (a != null && a.value) && c.value.render(a == null ? void 0 : a.value, f.value));
|
|
500
500
|
}), () => {
|
|
501
501
|
if (n.default)
|
|
502
502
|
return n.default();
|
|
@@ -506,27 +506,27 @@ const { logError: me, logWarning: gt } = I(), ht = Q({
|
|
|
506
506
|
install(e, n) {
|
|
507
507
|
const o = (n == null ? void 0 : n.prefix) || "Tres";
|
|
508
508
|
e.component(`${o}Canvas`, ht), e.component(`${o}Scene`, wt);
|
|
509
|
-
const { catalogue: a, extend: c } =
|
|
510
|
-
e.provide("catalogue", a), e.provide("extend", c), e.provide("useTres",
|
|
511
|
-
const { createComponentInstances:
|
|
512
|
-
|
|
509
|
+
const { catalogue: a, extend: c } = et(e, o);
|
|
510
|
+
e.provide("catalogue", a), e.provide("extend", c), e.provide("useTres", $());
|
|
511
|
+
const { createComponentInstances: f } = Ce(o);
|
|
512
|
+
f(a).forEach(([t, r]) => {
|
|
513
513
|
e.component(t, r);
|
|
514
514
|
});
|
|
515
515
|
}
|
|
516
516
|
};
|
|
517
517
|
export {
|
|
518
|
-
|
|
518
|
+
qe as CameraType,
|
|
519
519
|
Rt as UseTresStateSymbol,
|
|
520
520
|
Mt as default,
|
|
521
521
|
pt as trasverseObjects,
|
|
522
522
|
Se as useCamera,
|
|
523
|
-
|
|
524
|
-
|
|
523
|
+
et as useCatalogue,
|
|
524
|
+
Ce as useInstanceCreator,
|
|
525
525
|
Ct as useLoader,
|
|
526
526
|
mt as useRaycaster,
|
|
527
|
-
|
|
527
|
+
ne as useRenderLoop,
|
|
528
528
|
dt as useRenderer,
|
|
529
529
|
ft as useScene,
|
|
530
530
|
Et as useTexture,
|
|
531
|
-
|
|
531
|
+
$ as useTres
|
|
532
532
|
};
|
package/dist/tres.umd.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* name: @tresjs/core
|
|
3
|
-
* version: v1.8.
|
|
3
|
+
* version: v1.8.1
|
|
4
4
|
* (c) 2023
|
|
5
5
|
* description: Declarative ThreeJS using Vue Components
|
|
6
6
|
* author: Alvaro Saburido <hola@alvarosaburido.dev> (https://github.com/alvarosabu/)
|
|
7
7
|
*/
|
|
8
|
-
(function(C,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue"),require("@vueuse/core"),require("three")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","three"],i):(C=typeof globalThis<"u"?globalThis:C||self,i(C.tres={},C.Vue,C.VueUseCore,C.Three))})(this,function(C,i,u,p){"use strict";function he(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:()=>e[r]})}}return t.default=e,Object.freeze(t)}const ee=he(p);var te=(e=>(e.Perspective="Perspective",e.Orthographic="Orthographic",e))(te||{});const we=45;let x;function G(){const{state:e,setState:t}=O();function r(d="Perspective",n){var a,f,c;if(d==="Perspective"){const{near:s,far:m,fov:y}=n||{near:.1,far:1e3,fov:we};x=new p.PerspectiveCamera(y,((a=e.aspectRatio)==null?void 0:a.value)||window.innerWidth/window.innerHeight,s,m),(f=e.cameras)==null||f.push(x)}else{const{left:s,right:m,top:y,bottom:M,near:g,far:h}=n||{left:-100,right:100,top:100,bottom:-100,near:.1,far:1e3};x=new p.OrthographicCamera(s,m,y,M,g,h),(c=e.cameras)==null||c.push(x)}return e.camera=x,t("camera",e.camera),x}function o(){var d;e.camera instanceof p.PerspectiveCamera&&e.aspectRatio&&(e.camera.aspect=e.aspectRatio.value),(d=e.camera)==null||d.updateProjectionMatrix()}function l(d){var n;(n=e.cameras)==null||n.push(d),d instanceof p.PerspectiveCamera&&e.aspectRatio&&(d.aspect=e.aspectRatio.value),d.updateProjectionMatrix(),t("camera",d)}function w(){e.cameras=[]}return e.aspectRatio&&i.watch(e.aspectRatio,o),{activeCamera:i.toRef(e,"camera"),createCamera:r,updateCamera:o,pushCamera:l,clearCameras:w}}const ne="[TresJS ▲ ■ ●] ";function j(){function e(o,l){console.error(`${ne} ${o}`,l||"")}function t(o){console.warn(`${ne} ${o}`)}function r(o,l){}return{logError:e,logWarning:t,logMessage:r}}const k=i.ref({...ee,uuid:ee.MathUtils.generateUUID()});delete k.value.Scene;let B;function K(e,t="Tres"){const{logError:r}=j();!B&&e&&(B=e);const{createComponentInstances:o}=q(t);return{extend:w=>{if(!w){r("No objects provided to extend catalogue");return}k.value=Object.assign(k.value,w);const d=o(i.ref(w));B&&d.forEach(([n,a])=>{B._context.components[n]||B.component(n,a)})},catalogue:k}}const U=e=>typeof e<"u",ye=e=>typeof e=="function",F=e=>!!e&&e.constructor===Array;function Ce(e){return typeof e=="number"?[e,e,e]:e instanceof p.Vector3?[e.x,e.y,e.z]:e}function ve(e){return e instanceof p.Color?e:Array.isArray(e)?new p.Color(...e):new p.Color(e)}const re=["rotation","scale","position"],Se=["X","Y","Z"],Me=["color"],Le=["r","g","b"];function q(e){const{logError:t}=j();function r(n,a){U(n)&&U(a)&&Object.entries(n).forEach(([f,c])=>{const s=f.replace(/(-\w)/g,m=>m[1].toUpperCase());a.setAttribute(s,new p.BufferAttribute(...c))})}function o(n,a){U(n)&&U(a)&&Object.entries(n).forEach(([f,c])=>{const s=f.replace(/(-\w)/g,h=>h[1].toUpperCase());let m,y,M,g;if(!(s==="args"||c===void 0)){re.includes(s)&&c?c=Ce(c):re.forEach(h=>{s.startsWith(h)&&s.length===h.length+1&&(m=h,y=s.substring(h.length),Se.includes(y)||t(`There was an error setting ${f} property, ${y} is not a valid axis for ${m}`))}),Me.forEach(h=>{s.startsWith(h)&&s.length===h.length+1&&(M=h,g=s.substring(h.length).toLowerCase(),Le.includes(g)||t(`There was an error setting ${f} property , ${g} is not a valid axis for ${M}`))}),n.ref&&(n.ref=a);try{if(a[s]&&U(a[s].set))a[s].set(...F(c)?c:[c]);else if(m&&a[m])U(a[m][`set${y}`])?a[m][`set${y}`](c):U(a[`rotate${y}`])&&a[`rotate${y}`](c);else if(M&&g&&a[M]&&a[M][g])a[M][g]=c;else{if(c===""&&(c=!0),ye(a[s])){if(f==="center"&&!c)return;a[s](...F(c)?c:[c]);return}a[s]=c}}catch(h){t(`There was an error setting ${s} property`,h)}}})}function l(n){var s,m;const a=/^Symbol\(Fragment\)$/g,f=/^Symbol\(Text\)$/g,c=/^Symbol\(Comment\)$/g;if(a.test(n.type.toString()))return n.children.map(y=>l(y));if(f.test(n.type.toString())||c.test(n.type.toString()))return;{const y=n.type.name.replace(e,""),{catalogue:M}=K(),g=i.inject("catalogue")||M;let h;if(g)if((s=n.children)!=null&&s.default){const $=n.children.default().map(L=>l(L));h=new g.value[y](...$.flat().filter(Boolean))}else(m=n==null?void 0:n.props)!=null&&m.args?g!=null&&g.value[y]?h=new g.value[y](...n.props.args):t(`There is no ${y} in the catalogue`,g==null?void 0:g.value.uuid):h=new g.value[y];return n!=null&&n.props&&(y==="BufferGeometry"?r(n.props,h):o(n.props,h)),h}}function w(n,a,f){if(f.default&&(f!=null&&f.default())){const c=f.default().map(s=>l(s));if(n.name==="Group"){const s=new n;return c.forEach(m=>{s.add(m)}),s}else return new n(...c.flat().filter(Boolean))}else return a.args?new n(...a.args):new n}function d(n){return Object.entries(n.value).filter(([a,f])=>{var c,s;return(s=(c=f==null?void 0:f.prototype)==null?void 0:c.constructor)==null?void 0:s.toString().includes("class")}).map(([a,f])=>{const c=`${e}${a}`,s=i.defineComponent({name:c,setup(m,{slots:y,attrs:M,...g}){const{state:h}=O(),{onLoop:$}=N(),L=h.scene,W=h.raycaster,{pushCamera:Q}=G();let S=w(f,M,y);o(M,S),(S instanceof p.PerspectiveCamera||S instanceof p.OrthographicCamera)&&Q(S),S.isObject3D&&(L==null||L.add(S));let P=null,b=null;if(S instanceof p.Mesh){$(()=>{if(S&&W&&(L!=null&&L.children)){const _=W.intersectObjects(L==null?void 0:L.children);_.length>0?(b=_[0],(P===null||P.object.uuid!==(b==null?void 0:b.object.uuid))&&g.emit("pointer-enter",b),g.emit("pointer-move",b)):(b=null,P!==null&&g.emit("pointer-leave",P)),P=b}});const T=u.useEventListener(window,"click",()=>{g.emit("click",P)});i.onUnmounted(()=>{T()})}return L&&S instanceof p.Fog&&(L.fog=S),g.expose(S),()=>{}}});return[c,s]})}return{createComponentInstances:d,processProps:o,createInstanceFromVNode:l}}const ae=u.createEventHook(),oe=u.createEventHook(),X=u.createEventHook(),I=new p.Clock;let D=0,z=0;const{pause:be,resume:Pe,isActive:Ue}=u.useRafFn(()=>{ae.trigger({delta:D,elapsed:z,clock:I}),oe.trigger({delta:D,elapsed:z,clock:I}),X.trigger({delta:D,elapsed:z,clock:I})},{immediate:!1});X.on(()=>{D=I.getDelta(),z=I.getElapsedTime()});function N(){return{onBeforeLoop:ae.on,onLoop:oe.on,onAfterLoop:X.on,pause:be,resume:Pe,isActive:Ue}}const Oe=i.shallowRef(new p.Scene);function ie(){return{scene:Oe}}function se(e){const t={nodes:{},materials:{}};return e&&e.traverse(r=>{r.name&&(t.nodes[r.name]=r),r.material&&!t.materials[r.material.name]&&(t.materials[r.material.name]=r.material)}),t}async function xe(e,t,r,o,l){const{logError:w}=j(),d=new e;l&&l(d),r&&r(d);const a=(Array.isArray(t)?t:[t]).map(f=>new Promise((c,s)=>{d.load(f,m=>{m.scene&&Object.assign(m,se(m.scene)),c(m)},o,m=>s(w("[useLoader] - Failed to load resource",m)))}));return F(t)?await Promise.all(a):await a[0]}async function $e(e){const t=new p.LoadingManager,r=new p.TextureLoader(t),o=l=>new Promise((w,d)=>{r.load(l,n=>w(n),()=>null,()=>{d(new Error("[useTextures] - Failed to load texture"))})});if(F(e)){const l=await Promise.all(e.map(w=>o(w)));return e.length>1?l:l[0]}else{const{map:l,displacementMap:w,normalMap:d,roughnessMap:n,metalnessMap:a,aoMap:f}=e;return{map:l?await o(l):null,displacementMap:w?await o(w):null,normalMap:d?await o(d):null,roughnessMap:n?await o(n):null,metalnessMap:a?await o(a):null,aoMap:f?await o(f):null}}}const V=i.shallowReactive({camera:void 0,cameras:[],aspectRatio:i.computed(()=>window.innerWidth/window.innerHeight)});function O(){function e(r){return V[r]}function t(r,o){V[r]=o}return{state:V,...i.toRefs(V),getState:e,setState:t}}const Y=i.shallowRef(new p.Raycaster),R=i.ref(new p.Vector2),ce=i.ref(null);function le(){const{setState:e}=O();e("raycaster",Y.value),e("pointer",R),e("currentInstance",ce),i.provide("raycaster",Y),i.provide("pointer",R),i.provide("currentInstance",ce);function t(r){R.value.x=r.clientX/window.innerWidth*2-1,R.value.y=-(r.clientY/window.innerHeight)*2+1}return window.addEventListener("pointermove",t),i.onUnmounted(()=>{window.removeEventListener("pointermove",t)}),{raycaster:Y,pointer:R}}const J={realistic:{outputEncoding:p.sRGBEncoding,toneMapping:p.ACESFilmicToneMapping,toneMappingExposure:3,shadowMap:{enabled:!0,type:p.PCFSoftShadowMap}}},fe=(e,t)=>{for(const r of Object.keys(t))t[r]instanceof Object&&Object.assign(t[r],fe(e[r],t[r]));return Object.assign(e||{},t),e},v=i.shallowRef(),Z=i.ref(!1);function ue(e,t,r){const{alpha:o=!0,antialias:l=!0,depth:w,logarithmicDepthBuffer:d,failIfMajorPerformanceCaveat:n,precision:a,premultipliedAlpha:f,stencil:c,shadows:s=!1,shadowMapType:m=p.PCFShadowMap,physicallyCorrectLights:y=!1,useLegacyLights:M=!1,outputEncoding:g=p.LinearEncoding,toneMapping:h=p.NoToneMapping,toneMappingExposure:$=1,context:L=void 0,powerPreference:W="default",preserveDrawingBuffer:Q=!1,clearColor:S,windowSize:P=!1,preset:b=void 0}=i.toRefs(r),{width:T,height:_}=u.resolveUnref(P)?u.useWindowSize():u.useElementSize(t),{logError:_e}=j(),{pixelRatio:de}=u.useDevicePixelRatio(),{pause:ke,resume:Fe}=N(),E=i.computed(()=>T.value/_.value),me=()=>{v.value&&(v.value.setSize(T.value,_.value),v.value.setPixelRatio(Math.min(de.value,2)))},ge=()=>{if(!v.value)return;const A=u.resolveUnref(b);if(A){A in J||_e("Renderer Preset must be one of these: "+Object.keys(J).join(", ")),fe(v.value,J[A]);return}v.value.shadowMap.enabled=u.resolveUnref(s),v.value.shadowMap.type=u.resolveUnref(m),v.value.toneMapping=u.resolveUnref(h)||p.NoToneMapping,v.value.toneMappingExposure=u.resolveUnref($),v.value.outputEncoding=u.resolveUnref(g)||p.LinearEncoding,S!=null&&S.value&&v.value.setClearColor(ve(u.resolveUnref(S))),v.value.useLegacyLights=u.resolveUnref(M)},De=()=>{const A=u.unrefElement(e);if(v.value||!A)return;v.value=new p.WebGLRenderer({canvas:A,alpha:u.resolveUnref(o),antialias:u.resolveUnref(l),context:u.resolveUnref(L),depth:u.resolveUnref(w),failIfMajorPerformanceCaveat:u.resolveUnref(n),logarithmicDepthBuffer:u.resolveUnref(d),powerPreference:u.resolveUnref(W),precision:u.resolveUnref(a),stencil:u.resolveUnref(c),preserveDrawingBuffer:u.resolveUnref(Q),premultipliedAlpha:u.resolveUnref(f)});const{setState:H}=O();H("renderer",v.value),H("clock",new p.Clock),H("aspectRatio",E),ge(),me(),Fe(),Z.value=!0},ze=()=>{v.value&&(v.value.dispose(),v.value=void 0,Z.value=!1,ke())};return i.watch([E,de],me),i.watch([s,m,g,M,h,$,S],ge),i.watch(()=>[e,t],()=>{u.unrefElement(e)&&u.unrefElement(t)&&De()},{immediate:!0,deep:!0}),{renderer:v,isReady:Z,dispose:ze,aspectRatio:E}}const{logError:pe,logWarning:Ae}=j(),je=i.defineComponent({name:"TresCanvas",props:{shadows:Boolean,shadowMapType:Number,physicallyCorrectLights:{type:Boolean,default:!1,validator:e=>(e&&Ae("physicallyCorrectLights is deprecated. Use useLegacyLights instead."),!0)},useLegacyLights:Boolean,outputEncoding:Number,toneMapping:Number,toneMappingExposure:Number,context:Object,powerPreference:String,preserveDrawingBuffer:Boolean,clearColor:String,windowSize:{type:Boolean,default:!1},preset:String},setup(e,{slots:t,attrs:r}){const o=i.ref(),l=i.ref(),{renderer:w,dispose:d,aspectRatio:n}=ue(o,l,e);return i.provide("aspect-ratio",n),i.provide("renderer",w),t.default&&!t.default().some(a=>a.type.name==="Scene")&&pe("TresCanvas must contain a Scene component."),t.default&&!t.default().some(a=>{var f;return(f=a.type.name)==null?void 0:f.includes("Camera")})&&pe("Scene must contain a Camera component."),i.onBeforeUnmount(()=>d()),()=>{if(t.default)return i.h("div",{ref:l,style:{position:"relative",width:"100%",height:"100%",overflow:"hidden",pointerEvents:"auto",touchAction:"none",...r.style}},[i.h("div",{style:{width:"100%",height:"100%"}},[i.h("canvas",{ref:o,style:{display:"block",width:"100%",height:"100%",position:e.windowSize?"fixed":"absolute",top:0,left:0}}),t.default()])])}}}),Be=i.defineComponent({name:"Scene",setup(e,{slots:t}){const{setState:r}=O(),{scene:o}=ie(),l=i.inject("renderer"),{activeCamera:w}=G(),{raycaster:d,pointer:n}=le(),{onLoop:a}=N();return i.provide("local-scene",o),r("scene",o.value),a(()=>{w.value&&(d.value.setFromCamera(n.value,w.value),l!=null&&l.value&&w&&(o!=null&&o.value)&&l.value.render(o==null?void 0:o.value,w.value))}),()=>{if(t.default)return t.default()}}}),Ie=Symbol("UseTresState"),Re={install(e,t){const r=(t==null?void 0:t.prefix)||"Tres";e.component(`${r}Canvas`,je),e.component(`${r}Scene`,Be);const{catalogue:o,extend:l}=K(e,r);e.provide("catalogue",o),e.provide("extend",l),e.provide("useTres",O());const{createComponentInstances:w}=q(r);w(o).forEach(([n,a])=>{e.component(n,a)})}};C.CameraType=te,C.UseTresStateSymbol=Ie,C.default=Re,C.trasverseObjects=se,C.useCamera=G,C.useCatalogue=K,C.useInstanceCreator=q,C.useLoader=xe,C.useRaycaster=le,C.useRenderLoop=N,C.useRenderer=ue,C.useScene=ie,C.useTexture=$e,C.useTres=O,Object.defineProperties(C,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
8
|
+
(function(C,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue"),require("@vueuse/core"),require("three")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","three"],i):(C=typeof globalThis<"u"?globalThis:C||self,i(C.tres={},C.Vue,C.VueUseCore,C.Three))})(this,function(C,i,u,p){"use strict";function he(e){const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const o in e)if(o!=="default"){const a=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(n,o,a.get?a:{enumerable:!0,get:()=>e[o]})}}return n.default=e,Object.freeze(n)}const Q=he(p);var E=(e=>(e.Perspective="Perspective",e.Orthographic="Orthographic",e))(E||{});const we=45;let $;function G(){const{state:e,setState:n}=x(),o=i.inject("aspect-ratio");function a(t="Perspective",r){var f,c,s;if(t==="Perspective"){const{near:d,far:w,fov:g}=r||{near:.1,far:1e3,fov:we};$=new p.PerspectiveCamera(g,((f=e.aspectRatio)==null?void 0:f.value)||window.innerWidth/window.innerHeight,d,w),(c=e.cameras)==null||c.push($)}else{const{left:d,right:w,top:g,bottom:h,near:y,far:U}=r||{left:-100,right:100,top:100,bottom:-100,near:.1,far:1e3};$=new p.OrthographicCamera(d,w,g,h,y,U),(s=e.cameras)==null||s.push($)}return e.camera=$,n("camera",e.camera),$}function l(){var t;e.camera instanceof p.PerspectiveCamera&&e.aspectRatio&&(e.camera.aspect=e.aspectRatio.value),(t=e.camera)==null||t.updateProjectionMatrix()}function m(t){var r;(r=e.cameras)==null||r.push(t),t instanceof p.PerspectiveCamera&&e.aspectRatio&&(t.aspect=e.aspectRatio.value),t.updateProjectionMatrix(),n("camera",t)}function v(){e.cameras=[]}return o&&i.watch(o,l),{activeCamera:i.toRef(e,"camera"),createCamera:a,updateCamera:l,pushCamera:m,clearCameras:v}}const H="[TresJS ▲ ■ ●] ";function B(){function e(a,l){console.error(`${H} ${a}`,l||"")}function n(a){console.warn(`${H} ${a}`)}function o(a,l){}return{logError:e,logWarning:n,logMessage:o}}const k=i.ref({...Q,uuid:Q.MathUtils.generateUUID()});delete k.value.Scene;let I;function ee(e,n="Tres"){const{logError:o}=B();!I&&e&&(I=e);const{createComponentInstances:a}=K(n);return{extend:m=>{if(!m){o("No objects provided to extend catalogue");return}k.value=Object.assign(k.value,m);const v=a(i.ref(m));I&&v.forEach(([t,r])=>{I._context.components[t]||I.component(t,r)})},catalogue:k}}const O=e=>typeof e<"u",ye=e=>typeof e=="function",D=e=>!!e&&e.constructor===Array;function Ce(e){return typeof e=="number"?[e,e,e]:e instanceof p.Vector3?[e.x,e.y,e.z]:e}function ve(e){return e instanceof p.Color?e:Array.isArray(e)?new p.Color(...e):new p.Color(e)}const te=["rotation","scale","position"],Se=["X","Y","Z"],Me=["color"],Le=["r","g","b"];function K(e){const{logError:n}=B();function o(t,r){O(t)&&O(r)&&Object.entries(t).forEach(([f,c])=>{const s=f.replace(/(-\w)/g,d=>d[1].toUpperCase());r.setAttribute(s,new p.BufferAttribute(...c))})}function a(t,r){O(t)&&O(r)&&Object.entries(t).forEach(([f,c])=>{const s=f.replace(/(-\w)/g,y=>y[1].toUpperCase());let d,w,g,h;if(!(s==="args"||c===void 0)){te.includes(s)&&c?c=Ce(c):te.forEach(y=>{s.startsWith(y)&&s.length===y.length+1&&(d=y,w=s.substring(y.length),Se.includes(w)||n(`There was an error setting ${f} property, ${w} is not a valid axis for ${d}`))}),Me.forEach(y=>{s.startsWith(y)&&s.length===y.length+1&&(g=y,h=s.substring(y.length).toLowerCase(),Le.includes(h)||n(`There was an error setting ${f} property , ${h} is not a valid axis for ${g}`))}),t.ref&&(t.ref=r);try{if(r[s]&&O(r[s].set))r[s].set(...D(c)?c:[c]);else if(d&&r[d])O(r[d][`set${w}`])?r[d][`set${w}`](c):O(r[`rotate${w}`])&&r[`rotate${w}`](c);else if(g&&h&&r[g]&&r[g][h])r[g][h]=c;else{if(c===""&&(c=!0),ye(r[s])){if(f==="center"&&!c)return;r[s](...D(c)?c:[c]);return}r[s]=c}}catch(y){n(`There was an error setting ${s} property`,y)}}})}function l(t){var s,d;const r=/^Symbol\(Fragment\)$/g,f=/^Symbol\(Text\)$/g,c=/^Symbol\(Comment\)$/g;if(r.test(t.type.toString()))return t.children.map(w=>l(w));if(f.test(t.type.toString())||c.test(t.type.toString()))return;{const w=t.type.name.replace(e,""),g=i.inject("catalogue");let h;if(g)if((s=t.children)!=null&&s.default){const y=t.children.default().map(U=>l(U));h=new g.value[w](...y.flat().filter(Boolean))}else(d=t==null?void 0:t.props)!=null&&d.args?g!=null&&g.value[w]?h=new g.value[w](...t.props.args):n(`There is no ${w} in the catalogue`,g==null?void 0:g.value.uuid):h=new g.value[w];return t!=null&&t.props&&(w==="BufferGeometry"?o(t.props,h):a(t.props,h)),h}}function m(t,r,f){if(f.default&&(f!=null&&f.default())){const c=f.default().map(s=>l(s));if(t.name==="Group"){const s=new t;return c.forEach(d=>{s.add(d)}),s}else return new t(...c.flat().filter(Boolean))}else return r.args?new t(...r.args):new t}function v(t){return Object.entries(t.value).filter(([r,f])=>{var c,s;return(s=(c=f==null?void 0:f.prototype)==null?void 0:c.constructor)==null?void 0:s.toString().includes("class")}).map(([r,f])=>{const c=`${e}${r}`,s=i.defineComponent({name:c,setup(d,{slots:w,attrs:g,...h}){const{state:y}=x(),{onLoop:U}=V(),M=y.scene,T=y.raycaster;let L=m(f,g,w);if(a(g,L),L instanceof p.PerspectiveCamera||L instanceof p.OrthographicCamera){const{pushCamera:F}=G();F(L)}L.isObject3D&&(M==null||M.add(L));let b=null,P=null;if(L instanceof p.Mesh){U(()=>{if(L&&T&&(M!=null&&M.children)){const j=T.intersectObjects(M==null?void 0:M.children);j.length>0?(P=j[0],(b===null||b.object.uuid!==(P==null?void 0:P.object.uuid))&&h.emit("pointer-enter",P),h.emit("pointer-move",P)):(P=null,b!==null&&h.emit("pointer-leave",b)),b=P}});const F=u.useEventListener(window,"click",()=>{h.emit("click",b)});i.onUnmounted(()=>{F()})}return M&&L instanceof p.Fog&&(M.fog=L),h.expose(L),()=>{}}});return[c,s]})}return{createComponentInstances:v,processProps:a,createInstanceFromVNode:l}}const ne=u.createEventHook(),re=u.createEventHook(),q=u.createEventHook(),_=new p.Clock;let z=0,N=0;const{pause:be,resume:Pe,isActive:Ue}=u.useRafFn(()=>{ne.trigger({delta:z,elapsed:N,clock:_}),re.trigger({delta:z,elapsed:N,clock:_}),q.trigger({delta:z,elapsed:N,clock:_})},{immediate:!1});q.on(()=>{z=_.getDelta(),N=_.getElapsedTime()});function V(){return{onBeforeLoop:ne.on,onLoop:re.on,onAfterLoop:q.on,pause:be,resume:Pe,isActive:Ue}}const Oe=i.shallowRef(new p.Scene);function oe(){return{scene:Oe}}function ae(e){const n={nodes:{},materials:{}};return e&&e.traverse(o=>{o.name&&(n.nodes[o.name]=o),o.material&&!n.materials[o.material.name]&&(n.materials[o.material.name]=o.material)}),n}async function xe(e,n,o,a,l){const{logError:m}=B(),v=new e;l&&l(v),o&&o(v);const r=(Array.isArray(n)?n:[n]).map(f=>new Promise((c,s)=>{v.load(f,d=>{d.scene&&Object.assign(d,ae(d.scene)),c(d)},a,d=>s(m("[useLoader] - Failed to load resource",d)))}));return D(n)?await Promise.all(r):await r[0]}async function $e(e){const n=new p.LoadingManager,o=new p.TextureLoader(n),a=l=>new Promise((m,v)=>{o.load(l,t=>m(t),()=>null,()=>{v(new Error("[useTextures] - Failed to load texture"))})});if(D(e)){const l=await Promise.all(e.map(m=>a(m)));return e.length>1?l:l[0]}else{const{map:l,displacementMap:m,normalMap:v,roughnessMap:t,metalnessMap:r,aoMap:f}=e;return{map:l?await a(l):null,displacementMap:m?await a(m):null,normalMap:v?await a(v):null,roughnessMap:t?await a(t):null,metalnessMap:r?await a(r):null,aoMap:f?await a(f):null}}}const W=i.shallowReactive({camera:void 0,cameras:[],aspectRatio:i.computed(()=>window.innerWidth/window.innerHeight)});function x(){function e(o){return W[o]}function n(o,a){W[o]=a}return{state:W,...i.toRefs(W),getState:e,setState:n}}const X=i.shallowRef(new p.Raycaster),R=i.ref(new p.Vector2),ie=i.ref(null);function se(){const{setState:e}=x();e("raycaster",X.value),e("pointer",R),e("currentInstance",ie),i.provide("raycaster",X),i.provide("pointer",R),i.provide("currentInstance",ie);function n(o){R.value.x=o.clientX/window.innerWidth*2-1,R.value.y=-(o.clientY/window.innerHeight)*2+1}return window.addEventListener("pointermove",n),i.onUnmounted(()=>{window.removeEventListener("pointermove",n)}),{raycaster:X,pointer:R}}const Y={realistic:{outputEncoding:p.sRGBEncoding,toneMapping:p.ACESFilmicToneMapping,toneMappingExposure:3,shadowMap:{enabled:!0,type:p.PCFSoftShadowMap}}},ce=(e,n)=>{for(const o of Object.keys(n))n[o]instanceof Object&&Object.assign(n[o],ce(e[o],n[o]));return Object.assign(e||{},n),e},S=i.shallowRef(),J=i.ref(!1);function le(e,n,o){const{alpha:a=!0,antialias:l=!0,depth:m,logarithmicDepthBuffer:v,failIfMajorPerformanceCaveat:t,precision:r,premultipliedAlpha:f,stencil:c,shadows:s=!1,shadowMapType:d=p.PCFShadowMap,physicallyCorrectLights:w=!1,useLegacyLights:g=!1,outputEncoding:h=p.LinearEncoding,toneMapping:y=p.NoToneMapping,toneMappingExposure:U=1,context:M=void 0,powerPreference:T="default",preserveDrawingBuffer:L=!1,clearColor:b,windowSize:P=!1,preset:F=void 0}=i.toRefs(o),{setState:j}=x(),{width:ue,height:pe}=u.resolveUnref(P)?u.useWindowSize():u.useElementSize(n),{logError:Re}=B(),{pixelRatio:de}=u.useDevicePixelRatio(),{pause:Fe,resume:ke}=V(),Z=i.computed(()=>ue.value/pe.value),me=()=>{S.value&&(S.value.setSize(ue.value,pe.value),S.value.setPixelRatio(Math.min(de.value,2)))},ge=()=>{if(!S.value)return;const A=u.resolveUnref(F);if(A){A in Y||Re("Renderer Preset must be one of these: "+Object.keys(Y).join(", ")),ce(S.value,Y[A]);return}S.value.shadowMap.enabled=u.resolveUnref(s),S.value.shadowMap.type=u.resolveUnref(d),S.value.toneMapping=u.resolveUnref(y)||p.NoToneMapping,S.value.toneMappingExposure=u.resolveUnref(U),S.value.outputEncoding=u.resolveUnref(h)||p.LinearEncoding,b!=null&&b.value&&S.value.setClearColor(ve(u.resolveUnref(b))),S.value.useLegacyLights=u.resolveUnref(g)},De=()=>{const A=u.unrefElement(e);S.value||!A||(S.value=new p.WebGLRenderer({canvas:A,alpha:u.resolveUnref(a),antialias:u.resolveUnref(l),context:u.resolveUnref(M),depth:u.resolveUnref(m),failIfMajorPerformanceCaveat:u.resolveUnref(t),logarithmicDepthBuffer:u.resolveUnref(v),powerPreference:u.resolveUnref(T),precision:u.resolveUnref(r),stencil:u.resolveUnref(c),preserveDrawingBuffer:u.resolveUnref(L),premultipliedAlpha:u.resolveUnref(f)}),j("renderer",S.value),j("clock",new p.Clock),j("aspectRatio",Z),ge(),me(),ke(),J.value=!0)},ze=()=>{S.value&&(S.value.dispose(),S.value=void 0,J.value=!1,Fe())};return i.watch([Z,de],me),i.watch([s,d,h,g,y,U,b],ge),i.watch(()=>[e,n],()=>{u.unrefElement(e)&&u.unrefElement(n)&&De()},{immediate:!0,deep:!0}),{renderer:S,isReady:J,dispose:ze,aspectRatio:Z}}const{logError:fe,logWarning:je}=B(),Ae=i.defineComponent({name:"TresCanvas",props:{shadows:Boolean,shadowMapType:Number,physicallyCorrectLights:{type:Boolean,default:!1,validator:e=>(e&&je("physicallyCorrectLights is deprecated. Use useLegacyLights instead."),!0)},useLegacyLights:Boolean,outputEncoding:Number,toneMapping:Number,toneMappingExposure:Number,context:Object,powerPreference:String,preserveDrawingBuffer:Boolean,clearColor:String,windowSize:{type:Boolean,default:!1},preset:String},setup(e,{slots:n,attrs:o}){const a=i.ref(),l=i.ref(),{renderer:m,dispose:v,aspectRatio:t}=le(a,l,e);return i.provide("aspect-ratio",t),i.provide("renderer",m),n.default&&!n.default().some(r=>r.type.name==="Scene")&&fe("TresCanvas must contain a Scene component."),n.default&&!n.default().some(r=>{var f;return(f=r.type.name)==null?void 0:f.includes("Camera")})&&fe("Scene must contain a Camera component."),i.onBeforeUnmount(()=>v()),()=>{if(n.default)return i.h("div",{ref:l,style:{position:"relative",width:"100%",height:"100%",overflow:"hidden",pointerEvents:"auto",touchAction:"none",...o.style}},[i.h("div",{style:{width:"100%",height:"100%"}},[i.h("canvas",{ref:a,style:{display:"block",width:"100%",height:"100%",position:e.windowSize?"fixed":"absolute",top:0,left:0}}),n.default()])])}}}),Be=i.defineComponent({name:"Scene",setup(e,{slots:n}){const{setState:o}=x(),{scene:a}=oe(),l=i.inject("renderer"),{activeCamera:m}=G(),{raycaster:v,pointer:t}=se(),{onLoop:r}=V();return i.provide("local-scene",a),o("scene",a.value),r(()=>{m.value&&(v.value.setFromCamera(t.value,m.value),l!=null&&l.value&&m&&(a!=null&&a.value)&&l.value.render(a==null?void 0:a.value,m.value))}),()=>{if(n.default)return n.default()}}}),Ie=Symbol("UseTresState"),_e={install(e,n){const o=(n==null?void 0:n.prefix)||"Tres";e.component(`${o}Canvas`,Ae),e.component(`${o}Scene`,Be);const{catalogue:a,extend:l}=ee(e,o);e.provide("catalogue",a),e.provide("extend",l),e.provide("useTres",x());const{createComponentInstances:m}=K(o);m(a).forEach(([t,r])=>{e.component(t,r)})}};C.CameraType=E,C.UseTresStateSymbol=Ie,C.default=_e,C.trasverseObjects=ae,C.useCamera=G,C.useCatalogue=ee,C.useInstanceCreator=K,C.useLoader=xe,C.useRaycaster=se,C.useRenderLoop=V,C.useRenderer=le,C.useScene=oe,C.useTexture=$e,C.useTres=x,Object.defineProperties(C,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED