-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThreeQuery.js
55 lines (46 loc) · 1.84 KB
/
ThreeQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// threeQuery.js
import * as THREE from 'three';
const defaultMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff });
class ThreeQuery {
constructor(selector) {
this.e = [];
if (typeof selector === "string") {
if (selector.startsWith("<") && selector.endsWith(">")) {
const primitive = selector.slice(1, -1).toLowerCase();
this.e = [createPrimitive(primitive)];
} else {
this.e = document.querySelectorAll(selector);
}
} else if (selector instanceof HTMLElement || selector instanceof THREE.Object3D) {
this.e = [selector];
}
this.length = this.e.length;
}
position(x, y, z) { this.e.forEach(e => e.position.set(x, y, z)); return this; }
scale(x, y, z) { this.e.forEach(e => e.scale.set(x, y, z)); return this; }
rotation(x, y, z) { this.e.forEach(e => e.rotation.set(x, y, z)); return this; }
addTo(parent) { this.e.forEach(e => parent.add(e)); return this; }
attachTo(parent) { this.e.forEach(e => parent.attach(e)); return this; }
// Add more methods here...
}
const createPrimitive = (primitive) => {
switch (primitive) {
case "sphere":
return new THREE.Mesh(new THREE.SphereGeometry(1, 32, 32), defaultMaterial);
case "plane":
return new THREE.Mesh(new THREE.PlaneGeometry(1, 1), defaultMaterial);
case "box":
return new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), defaultMaterial);
case "cylinder":
return new THREE.Mesh(new THREE.CylinderGeometry(1, 1, 1, 32), defaultMaterial);
case "directionallight":
return new THREE.DirectionalLight(0xffffff, 1);
case "pointlight":
return new THREE.PointLight(0xffffff, 1, 100);
// Add more cases for other primitives...
default:
throw new Error(`Unknown primitive: ${primitive}`);
}
};
const $3 = (selector) => new ThreeQuery(selector);
export default $3;