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