-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
197 lines (167 loc) · 5.35 KB
/
script.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { ARButton } from "https://unpkg.com/three@0.126.0/examples/jsm/webxr/ARButton.js";
let camera, scene, renderer;
let target;
let placedObjects = [];
let hitTestSource = null;
let localSpace = null;
let hitTestSourceInitialized = false;
let artisticSphere
let hue = 0;
initConfiguration();
initScene();
addTargetObject();
addInteraction();
addTorusFigure();
addArtisticSphere();
function initConfiguration() {
const container = document.createElement("div");
document.body.appendChild(container);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
70,
window.innerWidth / window.innerHeight,
0.01,
40
);
camera.position.set(0, 3, 3);
camera.rotation.set(-0.4, 0, 0);
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
container.appendChild(renderer.domElement);
document.body.appendChild(
ARButton.createButton(renderer, {
requiredFeatures: ["hit-test"],
})
);
renderer.domElement.style.display = "none";
renderer.setAnimationLoop(render);
}
function initScene() {
var light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
light.position.set(0.5, 1, 0.25);
scene.add(light);
AddInitialObjects();
function AddInitialObjects() {
const material = new THREE.MeshPhongMaterial({
shininess: 6,
flatShading: true,
transparent: 1,
opacity: 0.8,
});
SetObject(
new THREE.CylinderGeometry(0.2, 0.2, 1, 32),
material.clone(),
[-0.5, 1.5, 0],
new THREE.Color("rgb(226,35,0)")
);
SetObject(
new THREE.BoxGeometry(1, 1, 1),
material.clone(),
[0.5, 1.5, 0],
new THREE.Color("rgb(100,100,255)")
);
SetObject(
new THREE.SphereGeometry(0.6, 20, 20),
material.clone(),
[0, 1.5, -0.5],
new THREE.Color("rgb(0,255,0)")
);
function SetObject(geometry, mat, position, color) {
mat.color = color;
let mesh = new THREE.Mesh(geometry, mat.clone());
mesh.position.set(position[0], position[1], position[2]);
mesh.scale.set(0.1, 0.1, 0.1);
scene.add(mesh);
}
}
}
function addTorusFigure() {
const geometry = new THREE.TorusKnotGeometry(0.3, 0.1, 100, 16);
const material = new THREE.MeshPhongMaterial({ color: 0xff69b4 });
const torusKnot = new THREE.Mesh(geometry, material);
torusKnot.position.set(1.5, 1.0, -0.5);
scene.add(torusKnot);
}
function addArtisticSphere() {
const geometry = new THREE.SphereGeometry(0.2, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
artisticSphere = new THREE.Mesh(geometry, material);
// Position it above and in front of the initial camera viewpoint
artisticSphere.position.set(0, 2, -1);
scene.add(artisticSphere);
}
async function initializeHitTestSource() {
const session = renderer.xr.getSession();
const viewerSpace = await session.requestReferenceSpace("viewer");
hitTestSource = await session.requestHitTestSource({ space: viewerSpace });
localSpace = await session.requestReferenceSpace("local");
hitTestSourceInitialized = true;
session.addEventListener("end", () => {
hitTestSourceInitialized = false;
hitTestSource = null;
});
}
function addTargetObject() {
const geometry = new THREE.RingBufferGeometry(0.15, 0.2, 32).rotateX(
-Math.PI / 2
);
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
target = new THREE.Mesh(geometry, material);
target.matrixAutoUpdate = false;
target.visible = false;
scene.add(target);
target.add(new THREE.AxesHelper(1));
}
function addInteraction() {
var controller = renderer.xr.getController(0);
controller.addEventListener("select", onSelect);
scene.add(controller);
}
function onSelect() {
if (target.visible) {
const geometry = new THREE.CylinderBufferGeometry(0, 0.05, 0.2, 32);
const material = new THREE.MeshPhongMaterial({
color: 0xffffff * Math.random(),
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.setFromMatrixPosition(target.matrix);
mesh.quaternion.setFromRotationMatrix(target.matrix);
mesh.userData.rotateSpeedX = Math.random() * 0.05 + 0.01;
mesh.userData.rotateSpeedZ = Math.random() * 0.05 + 0.01;
placedObjects.push(mesh);
scene.add(mesh);
}
}
function render(timestamp, frame) {
if (frame) {
if (!hitTestSourceInitialized) {
initializeHitTestSource();
} else if (hitTestSourceInitialized) {
const hitTestResults = frame.getHitTestResults(hitTestSource);
if (hitTestResults.length > 0) {
const hit = hitTestResults[0];
const pose = hit.getPose(localSpace);
target.visible = true;
target.matrix.fromArray(pose.transform.matrix);
} else {
target.visible = false;
}
}
// Update placed objects rotation
placedObjects.forEach((obj) => {
obj.rotation.x += obj.userData.rotateSpeedX;
obj.rotation.z += obj.userData.rotateSpeedZ;
});
// Animate the artistic sphere
// Pulsate scale
const scale = 0.2 + 0.05 * Math.sin(timestamp / 500);
artisticSphere.scale.set(scale, scale, scale);
// Shift color hue over time
hue += 0.001;
if (hue > 1) hue = 0;
artisticSphere.material.color.setHSL(hue, 1.0, 0.5);
renderer.render(scene, camera);
}
}