-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneWithACubeAR.html
74 lines (62 loc) · 3.39 KB
/
SceneWithACubeAR.html
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
<!DOCTYPE html>
<html>
<head>
<title>Create your first Scene with a Cube Moving inside with AR</title>
<meta charset="utf-8" />
<script type="module">
// Find the latest version by visiting https://cdn.skypack.dev/three.
import * as THREE from "https://cdn.skypack.dev/three";
// We are adding the import of VRButton here. For more information: https://threejs.org/docs/#manual/en/introduction/How-to-create-VR-content
import { ARButton } from "https://cdn.rawgit.com/mrdoob/three.js/r117/examples/jsm/webxr/ARButton.js";
//////////// CREATING THE SCENE ////////////
const scene = new THREE.Scene();
//////////// CREATING THE CAMERA ////////////
// More info about the camera here: https://threejsfundamentals.org/threejs/lessons/threejs-cameras.html //
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.01,
1000
);
// By default, when we call scene.add(), the thing we add will be added to the coordinates (0,0,0).
//This would cause both the camera and the cube to be inside each other. To avoid this, we simply move the camera out a bit.
camera.position.z = 5;
//////////// CREATING THE RENDERER ////////////
// More info about the renderer here: https://threejs.org/docs/#api/en/renderers/WebGLRenderer //
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// We tell to WebGLRenderer to enable XR rendering. To know more about WebXR here: https://github.com/immersive-web/webxr/blob/master/explainer.md
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
// We are adding in the body the AR button.
document.body.appendChild(ARButton.createButton(renderer));
//////////// CREATING THE CUBE ////////////
// A BoxGeometry is the container for all the points(vertices) and fill(faces) of the cube.
const geometry = new THREE.BoxGeometry().rotateX(Math.PI / 2);
// To color the cube, we will need a material. Here, we are in a MeshBasicMaterial and we specify the color of the cube.
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
// Adding a controller when it's in AR, to put the cube when we clic on the screen.
//phone
var controller = renderer.xr.getController(0);
controller.addEventListener("select", onSelect);
scene.add(controller);
function onSelect() {
// Mesh is an object who take the geometry and applies to the geometry the material on it.
var cube = new THREE.Mesh(geometry, material);
cube.position.set(0, 0, -0.3).applyMatrix4(controller.matrixWorld);
cube.quaternion.setFromRotationMatrix(controller.matrixWorld);
scene.add(cube);
}
//////////// CREATING ANIMATE FUNCTION ////////////
// To see something, we will need to render it.
// So we are creating a function that will be launch the rendering and recall the function directly.
function animate() {
// We are asking the renderer to render the scene and the camera.
renderer.render(scene, camera);
}
// Here, we will use this function to control the animation to make it more efficient for VR.
renderer.setAnimationLoop(animate);
</script>
</head>
<body></body>
</html>