@tresjs/core 0.4.0 → 0.5.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/dist/tres.js CHANGED
@@ -1,31 +1,31 @@
1
1
  /**
2
2
  * name: @tresjs/core
3
- * version: v0.4.0
3
+ * version: v0.5.0
4
4
  * (c) 2022
5
5
  * description: Declarative ThreeJS using Vue Components
6
6
  * author: Alvaro Saburido <hola@alvarosaburido.dev> (https://github.com/alvarosabu/)
7
7
  */
8
- import { computed as F, watch as R, inject as Q, defineComponent as _, shallowRef as $, ref as O, reactive as le, toRefs as fe, provide as T, onBeforeUnmount as me, h as G } from "vue";
9
- import { useWindowSize as X, createEventHook as U, useRafFn as de, useElementSize as ge, useDevicePixelRatio as he, unrefElement as A, resolveUnref as v } from "@vueuse/core";
10
- import * as we from "three";
11
- import { PerspectiveCamera as I, OrthographicCamera as Ce, Color as z, Vector3 as ve, Clock as Me, Scene as Se, LoadingManager as Y, TextureLoader as ye, PCFShadowMap as xe, LinearEncoding as Ee, NoToneMapping as Pe, WebGLRenderer as Le } from "three";
12
- var Re = /* @__PURE__ */ ((e) => (e.Perspective = "Perspective", e.Orthographic = "Orthographic", e))(Re || {});
13
- const P = {
8
+ import { computed as j, watch as O, inject as Y, defineComponent as _, shallowRef as $, reactive as pe, toRefs as le, ref as D, provide as R, onBeforeUnmount as fe, h as q } from "vue";
9
+ import { useWindowSize as Z, createEventHook as U, useRafFn as me, useElementSize as de, useDevicePixelRatio as ge, unrefElement as B, resolveUnref as v } from "@vueuse/core";
10
+ import * as he from "three";
11
+ import { PerspectiveCamera as I, OrthographicCamera as we, Color as z, Vector3 as Ce, Clock as ve, Scene as ye, LoadingManager as Me, TextureLoader as Se, PCFShadowMap as Ee, LinearEncoding as Pe, NoToneMapping as xe, WebGLRenderer as Le } from "three";
12
+ var Oe = /* @__PURE__ */ ((e) => (e.Perspective = "Perspective", e.Orthographic = "Orthographic", e))(Oe || {});
13
+ const x = {
14
14
  cameras: []
15
- }, Te = 45;
16
- let y;
17
- function Z() {
18
- const { width: e, height: t } = X();
19
- function a(s = "Perspective", i) {
20
- if (s === "Perspective") {
21
- const { near: l, far: d, fov: m } = i || {
15
+ }, Re = 45;
16
+ let S;
17
+ function k() {
18
+ const { width: e, height: t } = Z();
19
+ function r(i = "Perspective", s) {
20
+ if (i === "Perspective") {
21
+ const { near: p, far: d, fov: f } = s || {
22
22
  near: 0.1,
23
23
  far: 1e3,
24
- fov: Te
24
+ fov: Re
25
25
  };
26
- y = new I(m, r.value, l, d), P.cameras.push(y);
26
+ S = new I(f, a.value, p, d), x.cameras.push(S);
27
27
  } else {
28
- const { left: l, right: d, top: m, bottom: h, near: M, far: S } = i || {
28
+ const { left: p, right: d, top: f, bottom: m, near: y, far: M } = s || {
29
29
  left: -100,
30
30
  right: 100,
31
31
  top: 100,
@@ -33,251 +33,270 @@ function Z() {
33
33
  near: 0.1,
34
34
  far: 1e3
35
35
  };
36
- y = new Ce(l, d, m, h, M, S), P.cameras.push(y);
36
+ S = new we(p, d, f, m, y, M), x.cameras.push(S);
37
37
  }
38
- return P.cameras.push(y), y;
38
+ return x.cameras.push(S), S;
39
39
  }
40
- const r = F(() => e.value / t.value), n = F(() => P.cameras[0]);
40
+ const a = j(() => e.value / t.value), n = j(() => x.cameras[0]);
41
41
  function o() {
42
- n.value instanceof I && (n.value.aspect = r.value), n.value.updateProjectionMatrix();
42
+ n.value instanceof I && (n.value.aspect = a.value), n.value.updateProjectionMatrix();
43
43
  }
44
- function p(s) {
45
- const i = Q("camera");
46
- s && i && (i.value = s), P.cameras.push(s), s instanceof I && (s.aspect = r.value), s.updateProjectionMatrix();
44
+ function u(i) {
45
+ const s = Y("camera");
46
+ i && s && (s.value = i), x.cameras.push(i), i instanceof I && (i.aspect = a.value), i.updateProjectionMatrix();
47
47
  }
48
- return R(r, o), {
48
+ return O(a, o), {
49
49
  activeCamera: n,
50
- createCamera: a,
50
+ createCamera: r,
51
51
  updateCamera: o,
52
- pushCamera: p
52
+ pushCamera: u
53
53
  };
54
54
  }
55
- let b = { ...we };
56
- delete b.Scene;
57
- function be() {
55
+ let T = { ...he };
56
+ delete T.Scene;
57
+ function Te() {
58
58
  return {
59
59
  extend: (t) => {
60
- b = Object.assign(b, t);
60
+ T = Object.assign(T, t);
61
61
  },
62
- catalogue: b
62
+ catalogue: T
63
63
  };
64
64
  }
65
- const Oe = (e) => typeof e < "u", k = (e) => !!e && e.constructor === Array;
66
- function je(e) {
67
- return typeof e == "number" ? [e, e, e] : e instanceof ve ? [e.x, e.y, e.z] : e;
65
+ const be = (e) => typeof e < "u", V = (e) => !!e && e.constructor === Array;
66
+ function Ae(e) {
67
+ return typeof e == "number" ? [e, e, e] : e instanceof Ce ? [e.x, e.y, e.z] : e;
68
68
  }
69
- function J(e) {
69
+ function K(e) {
70
70
  return e instanceof z ? e : Array.isArray(e) ? new z(...e) : new z(e);
71
71
  }
72
- const q = "[TresJS \u25B2 \u25A0 \u25CF] ";
73
- function ee() {
74
- function e(r, n) {
75
- console.error(`${q} ${r}`, n || "");
72
+ const Q = "[TresJS \u25B2 \u25A0 \u25CF] ";
73
+ function W() {
74
+ function e(a, n) {
75
+ console.error(`${Q} ${a}`, n || "");
76
76
  }
77
- function t(r) {
78
- console.warn(`${q} ${r}`);
77
+ function t(a) {
78
+ console.warn(`${Q} ${a}`);
79
79
  }
80
- function a(r, n) {
80
+ function r(a, n) {
81
81
  }
82
82
  return {
83
83
  logError: e,
84
84
  logWarning: t,
85
- logMessage: a
85
+ logMessage: r
86
86
  };
87
87
  }
88
88
  const Be = ["rotation", "scale", "position"];
89
- function Ae() {
90
- const { catalogue: e } = be(), { logMessage: t, logError: a } = ee();
91
- function r() {
89
+ function Ie() {
90
+ const { catalogue: e } = Te(), { logMessage: t, logError: r } = W();
91
+ function a() {
92
92
  return Object.entries(e).filter(([n, o]) => {
93
- var p, s;
94
- return (s = (p = o == null ? void 0 : o.prototype) == null ? void 0 : p.constructor) == null ? void 0 : s.toString().includes("class");
93
+ var u, i;
94
+ return (i = (u = o == null ? void 0 : o.prototype) == null ? void 0 : u.constructor) == null ? void 0 : i.toString().includes("class");
95
95
  }).map(([n, o]) => {
96
- const p = _({
96
+ const u = _({
97
97
  name: n,
98
- setup(s, { slots: i, attrs: l, ...d }) {
98
+ setup(i, { slots: s, attrs: p, ...d }) {
99
99
  t(n, {
100
- props: s,
101
- slots: i,
102
- attrs: l,
100
+ props: i,
101
+ slots: s,
102
+ attrs: p,
103
103
  ctx: d
104
104
  });
105
- let m;
106
- const { scene: h } = oe(), { pushCamera: M } = Z();
107
- function S() {
108
- if ((i == null ? void 0 : i.default) && (i == null ? void 0 : i.default())) {
109
- const C = i.default().map((c) => {
110
- var f;
105
+ let f;
106
+ const { scene: m } = ae(), { pushCamera: y } = k();
107
+ function M() {
108
+ if ((s == null ? void 0 : s.default) && (s == null ? void 0 : s.default())) {
109
+ const C = s.default().map((c) => {
110
+ var g;
111
111
  let w;
112
- return (f = c == null ? void 0 : c.props) != null && f.args ? w = new e[c.type.name](...c.props.args) : w = new e[c.type.name](), c != null && c.props && x(c.props, w), w;
112
+ return (g = c == null ? void 0 : c.props) != null && g.args ? w = new e[c.type.name](...c.props.args) : w = new e[c.type.name](), c != null && c.props && E(c.props, w), w;
113
113
  });
114
- m = new o(...C);
114
+ f = new o(...C);
115
115
  } else
116
- l.args ? m = new o(...l.args) : m = new o();
117
- x(l, m), n === "PerspectiveCamera" || n === "OrtographicCamera" ? M(m) : h == null || h.value.add(m), t("Instance added", h), d.expose(m);
116
+ p.args ? f = new o(...p.args) : f = new o();
117
+ E(p, f), n === "PerspectiveCamera" || n === "OrtographicCamera" ? y(f) : m == null || m.value.add(f), t("Instance added", m), d.expose(f);
118
118
  }
119
- function x(C, c) {
120
- Object.entries(C).forEach(([w, f]) => {
121
- const g = w.replace(/(-\w)/g, (E) => E[1].toUpperCase());
122
- if (!(g === "args" || f === void 0)) {
123
- Be.includes(w) && f && (f = je(f));
119
+ function E(C, c) {
120
+ Object.entries(C).forEach(([w, g]) => {
121
+ const h = w.replace(/(-\w)/g, (P) => P[1].toUpperCase());
122
+ if (!(h === "args" || g === void 0)) {
123
+ Be.includes(w) && g && (g = Ae(g));
124
124
  try {
125
- c[g] && Oe(c[g].set) ? k(f) ? c[g].set(...f) : c[g].set(f) : (f === "" && (f = !0), c[g] = f);
126
- } catch (E) {
127
- a(`There was an error setting ${g} property`, E);
125
+ c[h] && be(c[h].set) ? V(g) ? c[h].set(...g) : c[h].set(g) : (g === "" && (g = !0), c[h] = g);
126
+ } catch (P) {
127
+ r(`There was an error setting ${h} property`, P);
128
128
  }
129
129
  }
130
130
  }), C.ref && (C.ref = c);
131
131
  }
132
- return S(), () => {
132
+ return M(), () => {
133
133
  };
134
134
  }
135
135
  });
136
- return [n, p];
136
+ return [n, u];
137
137
  });
138
138
  }
139
139
  return {
140
- createComponentInstances: r
140
+ createComponentInstances: a
141
141
  };
142
142
  }
143
- const te = U(), ne = U(), re = U(), K = new Me(), { pause: Ie, resume: ze, isActive: De } = de(
143
+ const ee = U(), te = U(), ne = U(), X = new ve(), { pause: ze, resume: Fe, isActive: je } = me(
144
144
  () => {
145
- const e = K.getElapsedTime(), t = K.getDelta();
146
- te.trigger({ delta: t, elapsed: e }), ne.trigger({ delta: t, elapsed: e }), re.trigger({ delta: t, elapsed: e });
145
+ const e = X.getElapsedTime(), t = X.getDelta();
146
+ ee.trigger({ delta: t, elapsed: e }), te.trigger({ delta: t, elapsed: e }), ne.trigger({ delta: t, elapsed: e });
147
147
  },
148
148
  { immediate: !1 }
149
149
  );
150
- function ae() {
150
+ function re() {
151
151
  return {
152
- onBeforeLoop: te.on,
153
- onLoop: ne.on,
154
- onAfterLoop: re.on,
155
- pause: Ie,
156
- resume: ze,
157
- isActive: De
152
+ onBeforeLoop: ee.on,
153
+ onLoop: te.on,
154
+ onAfterLoop: ne.on,
155
+ pause: ze,
156
+ resume: Fe,
157
+ isActive: je
158
158
  };
159
159
  }
160
- const Fe = $(new Se());
161
- function oe() {
160
+ const De = $(new ye());
161
+ function ae() {
162
162
  return {
163
- scene: Fe
163
+ scene: De
164
164
  };
165
165
  }
166
- function Ne() {
167
- const e = new Y(), t = O(e);
168
- return { loadingManager: e, loadingManagerRef: t };
166
+ function _e(e) {
167
+ const t = { nodes: {}, materials: {} };
168
+ return e && e.traverse((r) => {
169
+ r.name && (t.nodes[r.name] = r), r.material && !t.materials[r.material.name] && (t.materials[r.material.name] = r.material);
170
+ }), t;
171
+ }
172
+ async function Ge(e, t, r, a, n) {
173
+ const { logError: o } = W(), u = new e();
174
+ n && n(u), r && r(e);
175
+ const s = (Array.isArray(t) ? t : [t]).map(
176
+ (p) => new Promise((d, f) => {
177
+ u.load(
178
+ p,
179
+ (m) => {
180
+ m.scene && Object.assign(m, _e(m.scene)), d(m);
181
+ },
182
+ a,
183
+ (m) => f(o("[useLoader] - Failed to load resource", m))
184
+ );
185
+ })
186
+ );
187
+ return V(t) ? await Promise.all(s) : await s[0];
169
188
  }
170
- async function Ge(e) {
171
- const t = new Y(), a = new ye(t), r = (n) => new Promise((o, p) => {
172
- a.load(
189
+ async function Je(e) {
190
+ const t = new Me(), r = new Se(t), a = (n) => new Promise((o, u) => {
191
+ r.load(
173
192
  n,
174
- (s) => o(s),
193
+ (i) => o(i),
175
194
  () => null,
176
195
  () => {
177
- p(new Error("[useTextures] - Failed to load texture"));
196
+ u(new Error("[useTextures] - Failed to load texture"));
178
197
  }
179
198
  );
180
199
  });
181
- if (k(e)) {
182
- const n = await Promise.all(e.map((o) => r(o)));
200
+ if (V(e)) {
201
+ const n = await Promise.all(e.map((o) => a(o)));
183
202
  return e.length > 1 ? n : n[0];
184
203
  } else {
185
- const { map: n, displacementMap: o, normalMap: p, roughnessMap: s, metalnessMap: i, aoMap: l } = e;
204
+ const { map: n, displacementMap: o, normalMap: u, roughnessMap: i, metalnessMap: s, aoMap: p } = e;
186
205
  return {
187
- map: n ? await r(n) : null,
188
- displacementMap: o ? await r(o) : null,
189
- normalMap: p ? await r(p) : null,
190
- roughnessMap: s ? await r(s) : null,
191
- metalnessMap: i ? await r(i) : null,
192
- aoMap: l ? await r(l) : null
206
+ map: n ? await a(n) : null,
207
+ displacementMap: o ? await a(o) : null,
208
+ normalMap: u ? await a(u) : null,
209
+ roughnessMap: i ? await a(i) : null,
210
+ metalnessMap: s ? await a(s) : null,
211
+ aoMap: p ? await a(p) : null
193
212
  };
194
213
  }
195
214
  }
196
- const L = le({});
197
- function Je() {
198
- function e(a) {
199
- return L[a];
215
+ const L = pe({});
216
+ function qe() {
217
+ function e(r) {
218
+ return L[r];
200
219
  }
201
- function t(a, r) {
202
- L[a] = r;
220
+ function t(r, a) {
221
+ L[r] = a;
203
222
  }
204
223
  return {
205
224
  state: L,
206
- ...fe(L),
225
+ ...le(L),
207
226
  getState: e,
208
227
  setState: t
209
228
  };
210
229
  }
211
- const u = $(), D = O(!1);
212
- function _e(e, t, a) {
230
+ const l = $(), F = D(!1);
231
+ function $e(e, t, r) {
213
232
  const {
214
- alpha: r = !1,
233
+ alpha: a = !1,
215
234
  antialias: n,
216
235
  depth: o,
217
- logarithmicDepthBuffer: p,
218
- failIfMajorPerformanceCaveat: s,
219
- precision: i,
220
- premultipliedAlpha: l,
236
+ logarithmicDepthBuffer: u,
237
+ failIfMajorPerformanceCaveat: i,
238
+ precision: s,
239
+ premultipliedAlpha: p,
221
240
  stencil: d,
222
- shadows: m = !1,
223
- shadowMapType: h = xe,
224
- physicallyCorrectLights: M = !1,
225
- outputEncoding: S = Ee,
226
- toneMapping: x = Pe,
241
+ shadows: f = !1,
242
+ shadowMapType: m = Ee,
243
+ physicallyCorrectLights: y = !1,
244
+ outputEncoding: M = Pe,
245
+ toneMapping: E = xe,
227
246
  toneMappingExposure: C = 1,
228
247
  context: c = void 0,
229
248
  powerPreference: w = "default",
230
- preserveDrawingBuffer: f = !1,
231
- clearColor: g = J("#000000"),
232
- windowSize: E = !1
233
- } = a, { width: j, height: B } = E ? X() : ge(t), { pixelRatio: V } = he(), { pause: se, resume: ie } = ae(), ce = F(() => j.value / B.value), W = () => {
234
- !u.value || (u.value.setSize(j.value, B.value), u.value.setPixelRatio(V.value));
235
- }, H = () => {
236
- !u.value || (u.value.shadowMap.enabled = v(m), u.value.outputEncoding = v(S), u.value.shadowMap.type = v(h), u.value.physicallyCorrectLights = v(M), u.value.toneMapping = v(x), u.value.toneMappingExposure = v(C), u.value.setClearColor(J(v(g))));
237
- }, ue = () => {
238
- const N = A(e);
239
- u.value || !N || (u.value = new Le({
240
- canvas: N,
241
- alpha: r,
249
+ preserveDrawingBuffer: g = !1,
250
+ clearColor: h = K("#000000"),
251
+ windowSize: P = !1
252
+ } = r, { width: b, height: A } = P ? Z() : de(t), { pixelRatio: H } = ge(), { pause: oe, resume: se } = re(), ie = j(() => b.value / A.value), N = () => {
253
+ !l.value || (l.value.setSize(b.value, A.value), l.value.setPixelRatio(H.value));
254
+ }, G = () => {
255
+ !l.value || (l.value.shadowMap.enabled = v(f), l.value.outputEncoding = v(M), l.value.shadowMap.type = v(m), l.value.physicallyCorrectLights = v(y), l.value.toneMapping = v(E), l.value.toneMappingExposure = v(C), l.value.setClearColor(K(v(h))));
256
+ }, ce = () => {
257
+ const J = B(e);
258
+ l.value || !J || (l.value = new Le({
259
+ canvas: J,
260
+ alpha: a,
242
261
  antialias: n,
243
262
  context: c,
244
263
  depth: o,
245
- failIfMajorPerformanceCaveat: s,
246
- logarithmicDepthBuffer: p,
264
+ failIfMajorPerformanceCaveat: i,
265
+ logarithmicDepthBuffer: u,
247
266
  powerPreference: w,
248
- precision: i,
267
+ precision: s,
249
268
  stencil: d,
250
- preserveDrawingBuffer: f,
251
- premultipliedAlpha: l
252
- }), H(), W(), ie(), D.value = !0);
253
- }, pe = () => {
254
- !u.value || (u.value.dispose(), u.value = void 0, D.value = !1, se());
269
+ preserveDrawingBuffer: g,
270
+ premultipliedAlpha: p
271
+ }), G(), N(), se(), F.value = !0);
272
+ }, ue = () => {
273
+ !l.value || (l.value.dispose(), l.value = void 0, F.value = !1, oe());
255
274
  };
256
- return R([j, B, V], W), R(
275
+ return O([b, A, H], N), O(
257
276
  () => [
277
+ f,
258
278
  m,
259
- h,
260
- S,
261
279
  M,
262
- x,
280
+ y,
281
+ E,
263
282
  C,
264
- g
283
+ h
265
284
  ],
266
- H
267
- ), R(
285
+ G
286
+ ), O(
268
287
  () => [e, t],
269
288
  () => {
270
- A(e) && A(t) && ue();
289
+ B(e) && B(t) && ce();
271
290
  },
272
291
  { immediate: !0, deep: !0 }
273
292
  ), {
274
- renderer: u,
275
- isReady: D,
276
- dispose: pe,
277
- aspectRatio: ce
293
+ renderer: l,
294
+ isReady: F,
295
+ dispose: ue,
296
+ aspectRatio: ie
278
297
  };
279
298
  }
280
- const $e = _({
299
+ const Ue = _({
281
300
  name: "TresCanvas",
282
301
  props: {
283
302
  shadows: Boolean,
@@ -292,13 +311,13 @@ const $e = _({
292
311
  clearColor: String,
293
312
  windowSize: Boolean
294
313
  },
295
- setup(e, { slots: t, attrs: a }) {
296
- const { logError: r } = ee(), n = O(), o = O(), { renderer: p, dispose: s, aspectRatio: i } = _e(n, o, e), l = $();
297
- return T("camera", l), T("renderer", p), T("aspect-ratio", i), t.default && !t.default().some((d) => d.type.name === "Scene") && r("TresCanvas must contain a Scene component."), t.default && !t.default().some(
314
+ setup(e, { slots: t, attrs: r }) {
315
+ const { logError: a } = W(), n = D(), o = D(), { renderer: u, dispose: i, aspectRatio: s } = $e(n, o, e), p = $();
316
+ return R("camera", p), R("renderer", u), R("aspect-ratio", s), t.default && !t.default().some((d) => d.type.name === "Scene") && a("TresCanvas must contain a Scene component."), t.default && !t.default().some(
298
317
  (d) => d.type.name === "PerspectiveCamera" || d.type.name === "OrtographicCamera"
299
- ) && r("Scene must contain a Camera component."), me(() => s()), () => {
318
+ ) && a("Scene must contain a Camera component."), fe(() => i()), () => {
300
319
  if (t.default)
301
- return G(
320
+ return q(
302
321
  "div",
303
322
  {
304
323
  ref: o,
@@ -306,11 +325,11 @@ const $e = _({
306
325
  position: "relative",
307
326
  width: "100%",
308
327
  height: "100vh",
309
- ...a.style
328
+ ...r.style
310
329
  }
311
330
  },
312
331
  [
313
- G("canvas", {
332
+ q("canvas", {
314
333
  ref: n,
315
334
  style: { width: "100%", height: "100%", position: "absolute", top: 0, left: 0 }
316
335
  }),
@@ -319,37 +338,38 @@ const $e = _({
319
338
  );
320
339
  };
321
340
  }
322
- }), Ue = _({
341
+ }), Ve = _({
323
342
  name: "Scene",
324
343
  setup(e, { slots: t }) {
325
- const { scene: a } = oe(), r = Q("renderer"), { activeCamera: n } = Z(), { onLoop: o } = ae();
326
- return T("local-scene", a), o(() => {
327
- (r == null ? void 0 : r.value) && (n == null ? void 0 : n.value) && (a == null ? void 0 : a.value) && r.value.render(a == null ? void 0 : a.value, n.value);
344
+ const { scene: r } = ae(), a = Y("renderer"), { activeCamera: n } = k(), { onLoop: o } = re();
345
+ return R("local-scene", r), o(() => {
346
+ (a == null ? void 0 : a.value) && (n == null ? void 0 : n.value) && (r == null ? void 0 : r.value) && a.value.render(r == null ? void 0 : r.value, n.value);
328
347
  }), () => {
329
348
  if (t.default)
330
349
  return t.default();
331
350
  };
332
351
  }
333
- }), qe = Symbol("UseTresState"), Ke = {
352
+ }), Ke = Symbol("UseTresState"), Qe = {
334
353
  install(e) {
335
- e.component("TresCanvas", $e), e.component("Scene", Ue);
336
- const { createComponentInstances: t } = Ae();
337
- t().forEach(([r, n]) => {
338
- e.component(r, n);
354
+ e.component("TresCanvas", Ue), e.component("Scene", Ve);
355
+ const { createComponentInstances: t } = Ie();
356
+ t().forEach(([a, n]) => {
357
+ e.component(a, n);
339
358
  });
340
359
  }
341
360
  };
342
361
  export {
343
- Re as CameraType,
344
- qe as UseTresStateSymbol,
345
- Ke as default,
346
- Z as useCamera,
347
- be as useCatalog,
348
- Ae as useInstanceCreator,
349
- Ne as useLoadingManager,
350
- ae as useRenderLoop,
351
- _e as useRenderer,
352
- oe as useScene,
353
- Ge as useTexture,
354
- Je as useTres
362
+ Oe as CameraType,
363
+ Ke as UseTresStateSymbol,
364
+ Qe as default,
365
+ _e as trasverseObjects,
366
+ k as useCamera,
367
+ Te as useCatalog,
368
+ Ie as useInstanceCreator,
369
+ Ge as useLoader,
370
+ re as useRenderLoop,
371
+ $e as useRenderer,
372
+ ae as useScene,
373
+ Je as useTexture,
374
+ qe as useTres
355
375
  };
package/dist/tres.umd.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * name: @tresjs/core
3
- * version: v0.4.0
3
+ * version: v0.5.0
4
4
  * (c) 2022
5
5
  * description: Declarative ThreeJS using Vue Components
6
6
  * author: Alvaro Saburido <hola@alvarosaburido.dev> (https://github.com/alvarosabu/)
7
7
  */
8
- (function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("vue"),require("@vueuse/core"),require("three")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","three"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u.tres={},u.Vue,u.VueUseCore,u.Three))})(this,function(u,a,p,f){"use strict";function H(e){if(e&&e.__esModule)return e;const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const o in e)if(o!=="default"){const t=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(n,o,t.get?t:{enumerable:!0,get:()=>e[o]})}}return n.default=e,Object.freeze(n)}const ee=H(f);var F=(e=>(e.Perspective="Perspective",e.Orthographic="Orthographic",e))(F||{});const O={cameras:[]},ne=45;let b;function z(){const{width:e,height:n}=p.useWindowSize();function o(i="Perspective",c){if(i==="Perspective"){const{near:g,far:C,fov:w}=c||{near:.1,far:1e3,fov:ne};b=new f.PerspectiveCamera(w,t.value,g,C),O.cameras.push(b)}else{const{left:g,right:C,top:w,bottom:y,near:v,far:L}=c||{left:-100,right:100,top:100,bottom:-100,near:.1,far:1e3};b=new f.OrthographicCamera(g,C,w,y,v,L),O.cameras.push(b)}return O.cameras.push(b),b}const t=a.computed(()=>e.value/n.value),r=a.computed(()=>O.cameras[0]);function s(){r.value instanceof f.PerspectiveCamera&&(r.value.aspect=t.value),r.value.updateProjectionMatrix()}function m(i){const c=a.inject("camera");i&&c&&(c.value=i),O.cameras.push(i),i instanceof f.PerspectiveCamera&&(i.aspect=t.value),i.updateProjectionMatrix()}return a.watch(t,s),{activeCamera:r,createCamera:o,updateCamera:s,pushCamera:m}}let _={...ee};delete _.Scene;function R(){return{extend:n=>{_=Object.assign(_,n)},catalogue:_}}const te=e=>typeof e<"u",V=e=>!!e&&e.constructor===Array;function re(e){return typeof e=="number"?[e,e,e]:e instanceof f.Vector3?[e.x,e.y,e.z]:e}function $(e){return e instanceof f.Color?e:Array.isArray(e)?new f.Color(...e):new f.Color(e)}const W="[TresJS \u25B2 \u25A0 \u25CF] ";function q(){function e(t,r){console.error(`${W} ${t}`,r||"")}function n(t){console.warn(`${W} ${t}`)}function o(t,r){}return{logError:e,logWarning:n,logMessage:o}}const ae=["rotation","scale","position"];function N(){const{catalogue:e}=R(),{logMessage:n,logError:o}=q();function t(){return Object.entries(e).filter(([r,s])=>{var m,i;return(i=(m=s==null?void 0:s.prototype)==null?void 0:m.constructor)==null?void 0:i.toString().includes("class")}).map(([r,s])=>{const m=a.defineComponent({name:r,setup(i,{slots:c,attrs:g,...C}){n(r,{props:i,slots:c,attrs:g,ctx:C});let w;const{scene:y}=I(),{pushCamera:v}=z();function L(){if((c==null?void 0:c.default)&&(c==null?void 0:c.default())){const P=c.default().map(l=>{var h;let S;return(h=l==null?void 0:l.props)!=null&&h.args?S=new e[l.type.name](...l.props.args):S=new e[l.type.name],l!=null&&l.props&&j(l.props,S),S});w=new s(...P)}else g.args?w=new s(...g.args):w=new s;j(g,w),r==="PerspectiveCamera"||r==="OrtographicCamera"?v(w):y==null||y.value.add(w),n("Instance added",y),C.expose(w)}function j(P,l){Object.entries(P).forEach(([S,h])=>{const M=S.replace(/(-\w)/g,x=>x[1].toUpperCase());if(!(M==="args"||h===void 0)){ae.includes(S)&&h&&(h=re(h));try{l[M]&&te(l[M].set)?V(h)?l[M].set(...h):l[M].set(h):(h===""&&(h=!0),l[M]=h)}catch(x){o(`There was an error setting ${M} property`,x)}}}),P.ref&&(P.ref=l)}return L(),()=>{}}});return[r,m]})}return{createComponentInstances:t}}const k=p.createEventHook(),G=p.createEventHook(),J=p.createEventHook(),K=new f.Clock,{pause:oe,resume:se,isActive:ie}=p.useRafFn(()=>{const e=K.getElapsedTime(),n=K.getDelta();k.trigger({delta:n,elapsed:e}),G.trigger({delta:n,elapsed:e}),J.trigger({delta:n,elapsed:e})},{immediate:!1});function B(){return{onBeforeLoop:k.on,onLoop:G.on,onAfterLoop:J.on,pause:oe,resume:se,isActive:ie}}const ce=a.shallowRef(new f.Scene);function I(){return{scene:ce}}function ue(){const e=new f.LoadingManager,n=a.ref(e);return{loadingManager:e,loadingManagerRef:n}}async function le(e){const n=new f.LoadingManager,o=new f.TextureLoader(n),t=r=>new Promise((s,m)=>{o.load(r,i=>s(i),()=>null,()=>{m(new Error("[useTextures] - Failed to load texture"))})});if(V(e)){const r=await Promise.all(e.map(s=>t(s)));return e.length>1?r:r[0]}else{const{map:r,displacementMap:s,normalMap:m,roughnessMap:i,metalnessMap:c,aoMap:g}=e;return{map:r?await t(r):null,displacementMap:s?await t(s):null,normalMap:m?await t(m):null,roughnessMap:i?await t(i):null,metalnessMap:c?await t(c):null,aoMap:g?await t(g):null}}}const U=a.reactive({});function pe(){function e(o){return U[o]}function n(o,t){U[o]=t}return{state:U,...a.toRefs(U),getState:e,setState:n}}const d=a.shallowRef(),T=a.ref(!1);function Q(e,n,o){const{alpha:t=!1,antialias:r,depth:s,logarithmicDepthBuffer:m,failIfMajorPerformanceCaveat:i,precision:c,premultipliedAlpha:g,stencil:C,shadows:w=!1,shadowMapType:y=f.PCFShadowMap,physicallyCorrectLights:v=!1,outputEncoding:L=f.LinearEncoding,toneMapping:j=f.NoToneMapping,toneMappingExposure:P=1,context:l=void 0,powerPreference:S="default",preserveDrawingBuffer:h=!1,clearColor:M=$("#000000"),windowSize:x=!1}=o,{width:A,height:D}=x?p.useWindowSize():p.useElementSize(n),{pixelRatio:X}=p.useDevicePixelRatio(),{pause:he,resume:we}=B(),Ce=a.computed(()=>A.value/D.value),Y=()=>{!d.value||(d.value.setSize(A.value,D.value),d.value.setPixelRatio(X.value))},Z=()=>{!d.value||(d.value.shadowMap.enabled=p.resolveUnref(w),d.value.outputEncoding=p.resolveUnref(L),d.value.shadowMap.type=p.resolveUnref(y),d.value.physicallyCorrectLights=p.resolveUnref(v),d.value.toneMapping=p.resolveUnref(j),d.value.toneMappingExposure=p.resolveUnref(P),d.value.setClearColor($(p.resolveUnref(M))))},Me=()=>{const E=p.unrefElement(e);d.value||!E||(d.value=new f.WebGLRenderer({canvas:E,alpha:t,antialias:r,context:l,depth:s,failIfMajorPerformanceCaveat:i,logarithmicDepthBuffer:m,powerPreference:S,precision:c,stencil:C,preserveDrawingBuffer:h,premultipliedAlpha:g}),Z(),Y(),we(),T.value=!0)},ye=()=>{!d.value||(d.value.dispose(),d.value=void 0,T.value=!1,he())};return a.watch([A,D,X],Y),a.watch(()=>[w,y,L,v,j,P,M],Z),a.watch(()=>[e,n],()=>{p.unrefElement(e)&&p.unrefElement(n)&&Me()},{immediate:!0,deep:!0}),{renderer:d,isReady:T,dispose:ye,aspectRatio:Ce}}const fe=a.defineComponent({name:"TresCanvas",props:{shadows:Boolean,shadowMapType:Object,physicallyCorrectLights:Boolean,outputEncoding:Object,toneMapping:Object,toneMappingExposure:Number,context:Object,powerPreference:Object,preserveDrawingBuffer:Boolean,clearColor:String,windowSize:Boolean},setup(e,{slots:n,attrs:o}){const{logError:t}=q(),r=a.ref(),s=a.ref(),{renderer:m,dispose:i,aspectRatio:c}=Q(r,s,e),g=a.shallowRef();return a.provide("camera",g),a.provide("renderer",m),a.provide("aspect-ratio",c),n.default&&!n.default().some(C=>C.type.name==="Scene")&&t("TresCanvas must contain a Scene component."),n.default&&!n.default().some(C=>C.type.name==="PerspectiveCamera"||C.type.name==="OrtographicCamera")&&t("Scene must contain a Camera component."),a.onBeforeUnmount(()=>i()),()=>{if(n.default)return a.h("div",{ref:s,style:{position:"relative",width:"100%",height:"100vh",...o.style}},[a.h("canvas",{ref:r,style:{width:"100%",height:"100%",position:"absolute",top:0,left:0}}),n.default()])}}}),de=a.defineComponent({name:"Scene",setup(e,{slots:n}){const{scene:o}=I(),t=a.inject("renderer"),{activeCamera:r}=z(),{onLoop:s}=B();return a.provide("local-scene",o),s(()=>{(t==null?void 0:t.value)&&(r==null?void 0:r.value)&&(o==null?void 0:o.value)&&t.value.render(o==null?void 0:o.value,r.value)}),()=>{if(n.default)return n.default()}}}),me=Symbol("UseTresState"),ge={install(e){e.component("TresCanvas",fe),e.component("Scene",de);const{createComponentInstances:n}=N();n().forEach(([t,r])=>{e.component(t,r)})}};u.CameraType=F,u.UseTresStateSymbol=me,u.default=ge,u.useCamera=z,u.useCatalog=R,u.useInstanceCreator=N,u.useLoadingManager=ue,u.useRenderLoop=B,u.useRenderer=Q,u.useScene=I,u.useTexture=le,u.useTres=pe,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
8
+ (function(u,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("vue"),require("@vueuse/core"),require("three")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","three"],o):(u=typeof globalThis<"u"?globalThis:u||self,o(u.tres={},u.Vue,u.VueUseCore,u.Three))})(this,function(u,o,f,m){"use strict";function ee(e){if(e&&e.__esModule)return e;const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const te=ee(m);var V=(e=>(e.Perspective="Perspective",e.Orthographic="Orthographic",e))(V||{});const b={cameras:[]},ne=45;let P;function A(){const{width:e,height:t}=f.useWindowSize();function n(c="Perspective",i){if(c==="Perspective"){const{near:g,far:C,fov:h}=i||{near:.1,far:1e3,fov:ne};P=new m.PerspectiveCamera(h,r.value,g,C),b.cameras.push(P)}else{const{left:g,right:C,top:h,bottom:w,near:O,far:L}=i||{left:-100,right:100,top:100,bottom:-100,near:.1,far:1e3};P=new m.OrthographicCamera(g,C,h,w,O,L),b.cameras.push(P)}return b.cameras.push(P),P}const r=o.computed(()=>e.value/t.value),a=o.computed(()=>b.cameras[0]);function s(){a.value instanceof m.PerspectiveCamera&&(a.value.aspect=r.value),a.value.updateProjectionMatrix()}function p(c){const i=o.inject("camera");c&&i&&(i.value=c),b.cameras.push(c),c instanceof m.PerspectiveCamera&&(c.aspect=r.value),c.updateProjectionMatrix()}return o.watch(r,s),{activeCamera:a,createCamera:n,updateCamera:s,pushCamera:p}}let U={...te};delete U.Scene;function $(){return{extend:t=>{U=Object.assign(U,t)},catalogue:U}}const re=e=>typeof e<"u",T=e=>!!e&&e.constructor===Array;function ae(e){return typeof e=="number"?[e,e,e]:e instanceof m.Vector3?[e.x,e.y,e.z]:e}function W(e){return e instanceof m.Color?e:Array.isArray(e)?new m.Color(...e):new m.Color(e)}const q="[TresJS \u25B2 \u25A0 \u25CF] ";function z(){function e(r,a){console.error(`${q} ${r}`,a||"")}function t(r){console.warn(`${q} ${r}`)}function n(r,a){}return{logError:e,logWarning:t,logMessage:n}}const oe=["rotation","scale","position"];function N(){const{catalogue:e}=$(),{logMessage:t,logError:n}=z();function r(){return Object.entries(e).filter(([a,s])=>{var p,c;return(c=(p=s==null?void 0:s.prototype)==null?void 0:p.constructor)==null?void 0:c.toString().includes("class")}).map(([a,s])=>{const p=o.defineComponent({name:a,setup(c,{slots:i,attrs:g,...C}){t(a,{props:c,slots:i,attrs:g,ctx:C});let h;const{scene:w}=I(),{pushCamera:O}=A();function L(){if((i==null?void 0:i.default)&&(i==null?void 0:i.default())){const v=i.default().map(l=>{var y;let M;return(y=l==null?void 0:l.props)!=null&&y.args?M=new e[l.type.name](...l.props.args):M=new e[l.type.name],l!=null&&l.props&&j(l.props,M),M});h=new s(...v)}else g.args?h=new s(...g.args):h=new s;j(g,h),a==="PerspectiveCamera"||a==="OrtographicCamera"?O(h):w==null||w.value.add(h),t("Instance added",w),C.expose(h)}function j(v,l){Object.entries(v).forEach(([M,y])=>{const S=M.replace(/(-\w)/g,_=>_[1].toUpperCase());if(!(S==="args"||y===void 0)){oe.includes(M)&&y&&(y=ae(y));try{l[S]&&re(l[S].set)?T(y)?l[S].set(...y):l[S].set(y):(y===""&&(y=!0),l[S]=y)}catch(_){n(`There was an error setting ${S} property`,_)}}}),v.ref&&(v.ref=l)}return L(),()=>{}}});return[a,p]})}return{createComponentInstances:r}}const k=f.createEventHook(),G=f.createEventHook(),J=f.createEventHook(),K=new m.Clock,{pause:se,resume:ie,isActive:ce}=f.useRafFn(()=>{const e=K.getElapsedTime(),t=K.getDelta();k.trigger({delta:t,elapsed:e}),G.trigger({delta:t,elapsed:e}),J.trigger({delta:t,elapsed:e})},{immediate:!1});function B(){return{onBeforeLoop:k.on,onLoop:G.on,onAfterLoop:J.on,pause:se,resume:ie,isActive:ce}}const ue=o.shallowRef(new m.Scene);function I(){return{scene:ue}}function Q(e){const t={nodes:{},materials:{}};return e&&e.traverse(n=>{n.name&&(t.nodes[n.name]=n),n.material&&!t.materials[n.material.name]&&(t.materials[n.material.name]=n.material)}),t}async function le(e,t,n,r,a){const{logError:s}=z(),p=new e;a&&a(p),n&&n(e);const i=(Array.isArray(t)?t:[t]).map(g=>new Promise((C,h)=>{p.load(g,w=>{w.scene&&Object.assign(w,Q(w.scene)),C(w)},r,w=>h(s("[useLoader] - Failed to load resource",w)))}));return T(t)?await Promise.all(i):await i[0]}async function fe(e){const t=new m.LoadingManager,n=new m.TextureLoader(t),r=a=>new Promise((s,p)=>{n.load(a,c=>s(c),()=>null,()=>{p(new Error("[useTextures] - Failed to load texture"))})});if(T(e)){const a=await Promise.all(e.map(s=>r(s)));return e.length>1?a:a[0]}else{const{map:a,displacementMap:s,normalMap:p,roughnessMap:c,metalnessMap:i,aoMap:g}=e;return{map:a?await r(a):null,displacementMap:s?await r(s):null,normalMap:p?await r(p):null,roughnessMap:c?await r(c):null,metalnessMap:i?await r(i):null,aoMap:g?await r(g):null}}}const x=o.reactive({});function pe(){function e(n){return x[n]}function t(n,r){x[n]=r}return{state:x,...o.toRefs(x),getState:e,setState:t}}const d=o.shallowRef(),D=o.ref(!1);function X(e,t,n){const{alpha:r=!1,antialias:a,depth:s,logarithmicDepthBuffer:p,failIfMajorPerformanceCaveat:c,precision:i,premultipliedAlpha:g,stencil:C,shadows:h=!1,shadowMapType:w=m.PCFShadowMap,physicallyCorrectLights:O=!1,outputEncoding:L=m.LinearEncoding,toneMapping:j=m.NoToneMapping,toneMappingExposure:v=1,context:l=void 0,powerPreference:M="default",preserveDrawingBuffer:y=!1,clearColor:S=W("#000000"),windowSize:_=!1}=n,{width:F,height:R}=_?f.useWindowSize():f.useElementSize(t),{pixelRatio:Y}=f.useDevicePixelRatio(),{pause:we,resume:Ce}=B(),ye=o.computed(()=>F.value/R.value),Z=()=>{!d.value||(d.value.setSize(F.value,R.value),d.value.setPixelRatio(Y.value))},E=()=>{!d.value||(d.value.shadowMap.enabled=f.resolveUnref(h),d.value.outputEncoding=f.resolveUnref(L),d.value.shadowMap.type=f.resolveUnref(w),d.value.physicallyCorrectLights=f.resolveUnref(O),d.value.toneMapping=f.resolveUnref(j),d.value.toneMappingExposure=f.resolveUnref(v),d.value.setClearColor(W(f.resolveUnref(S))))},Se=()=>{const H=f.unrefElement(e);d.value||!H||(d.value=new m.WebGLRenderer({canvas:H,alpha:r,antialias:a,context:l,depth:s,failIfMajorPerformanceCaveat:c,logarithmicDepthBuffer:p,powerPreference:M,precision:i,stencil:C,preserveDrawingBuffer:y,premultipliedAlpha:g}),E(),Z(),Ce(),D.value=!0)},Me=()=>{!d.value||(d.value.dispose(),d.value=void 0,D.value=!1,we())};return o.watch([F,R,Y],Z),o.watch(()=>[h,w,L,O,j,v,S],E),o.watch(()=>[e,t],()=>{f.unrefElement(e)&&f.unrefElement(t)&&Se()},{immediate:!0,deep:!0}),{renderer:d,isReady:D,dispose:Me,aspectRatio:ye}}const me=o.defineComponent({name:"TresCanvas",props:{shadows:Boolean,shadowMapType:Object,physicallyCorrectLights:Boolean,outputEncoding:Object,toneMapping:Object,toneMappingExposure:Number,context:Object,powerPreference:Object,preserveDrawingBuffer:Boolean,clearColor:String,windowSize:Boolean},setup(e,{slots:t,attrs:n}){const{logError:r}=z(),a=o.ref(),s=o.ref(),{renderer:p,dispose:c,aspectRatio:i}=X(a,s,e),g=o.shallowRef();return o.provide("camera",g),o.provide("renderer",p),o.provide("aspect-ratio",i),t.default&&!t.default().some(C=>C.type.name==="Scene")&&r("TresCanvas must contain a Scene component."),t.default&&!t.default().some(C=>C.type.name==="PerspectiveCamera"||C.type.name==="OrtographicCamera")&&r("Scene must contain a Camera component."),o.onBeforeUnmount(()=>c()),()=>{if(t.default)return o.h("div",{ref:s,style:{position:"relative",width:"100%",height:"100vh",...n.style}},[o.h("canvas",{ref:a,style:{width:"100%",height:"100%",position:"absolute",top:0,left:0}}),t.default()])}}}),de=o.defineComponent({name:"Scene",setup(e,{slots:t}){const{scene:n}=I(),r=o.inject("renderer"),{activeCamera:a}=A(),{onLoop:s}=B();return o.provide("local-scene",n),s(()=>{(r==null?void 0:r.value)&&(a==null?void 0:a.value)&&(n==null?void 0:n.value)&&r.value.render(n==null?void 0:n.value,a.value)}),()=>{if(t.default)return t.default()}}}),ge=Symbol("UseTresState"),he={install(e){e.component("TresCanvas",me),e.component("Scene",de);const{createComponentInstances:t}=N();t().forEach(([r,a])=>{e.component(r,a)})}};u.CameraType=V,u.UseTresStateSymbol=ge,u.default=he,u.trasverseObjects=Q,u.useCamera=A,u.useCatalog=$,u.useInstanceCreator=N,u.useLoader=le,u.useRenderLoop=B,u.useRenderer=X,u.useScene=I,u.useTexture=fe,u.useTres=pe,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tresjs/core",
3
3
  "description": "Declarative ThreeJS using Vue Components",
4
- "version": "0.4.0",
4
+ "version": "0.5.0",
5
5
  "type": "module",
6
6
  "author": "Alvaro Saburido <hola@alvarosaburido.dev> (https://github.com/alvarosabu/)",
7
7
  "files": [
@@ -38,7 +38,7 @@
38
38
  "devDependencies": {
39
39
  "@alvarosabu/utils": "^2.2.0",
40
40
  "@histoire/plugin-vue": "0.11.7",
41
- "@tresjs/cientos": "^0.3.0",
41
+ "@tresjs/cientos": "^0.4.0",
42
42
  "@vitejs/plugin-vue": "^3.2.0",
43
43
  "@vitest/coverage-c8": "^0.25.1",
44
44
  "@vitest/ui": "^0.25.1",