-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 loc) · 2.06 KB
/
index.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
import * as THREE from "three";
import * as THREEx from "./node_modules/@ar-js-org/ar.js/three.js/build/ar-threex-location-only.js";
function main() {
const canvas = document.getElementById("canvas1");
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, 1.33, 0.1, 10000);
const renderer = new THREE.WebGLRenderer({ canvas: canvas });
const arjs = new THREEx.LocationBased(scene, camera);
const cam = new THREEx.WebcamRenderer(renderer);
const geom = new THREE.BoxGeometry(20, 20, 20);
const mtl = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const box = new THREE.Mesh(geom, mtl);
arjs.add(box, -0.72, 51.051);
arjs.fakeGps(-0.72, 51.05);
requestAnimationFrame(render);
function render() {
if (canvas.width != canvas.clientWidth || canvas.height != canvas.clientHeight) {
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
const aspect = canvas.clientWidth / canvas.clientHeight;
camera.aspect = aspect;
camera.updateProjectionMatrix();
}
cam.update();
const rotationStep = THREE.Math.degToRad(2);
let mousedown = false,
lastX = 0;
window.addEventListener("mousedown", (e) => {
mousedown = true;
});
window.addEventListener("mouseup", (e) => {
mousedown = false;
});
window.addEventListener("mousemove", (e) => {
if (!mousedown) return;
if (e.clientX < lastX) {
camera.rotation.y -= rotationStep;
if (camera.rotation.y < 0) {
camera.rotation.y += 2 * Math.PI;
}
} else if (e.clientX > lastX) {
camera.rotation.y += rotationStep;
if (camera.rotation.y > 2 * Math.PI) {
camera.rotation.y -= 2 * Math.PI;
}
}
lastX = e.clientX;
});
renderer.render(scene, camera);
requestAnimationFrame(render);
}
}
main();