three.html 1.78 KB
Newer Older
Alfakhori's avatar
Alfakhori committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>Cube Demo</title>

  <!-- three.js -->
  <script src="https://unpkg.com/three@0.126.0/build/three.js"></script>
</head>
<body>

<!-- Starting an immersive WebXR session requires user interaction.
    We start this one with a simple button. -->
<script>
Alfakhori's avatar
Alfakhori committed
17
18
 // create a Scene
const scene = new Scene();
Alfakhori's avatar
Alfakhori committed
19

Alfakhori's avatar
Alfakhori committed
20
21
// Set the background color
scene.background = new Color('skyblue');
Alfakhori's avatar
Alfakhori committed
22

Alfakhori's avatar
Alfakhori committed
23
24
25
26
27
// Create a camera
const fov = 35; // AKA Field of View
const aspect = container.clientWidth / container.clientHeight;
const near = 0.1; // the near clipping plane
const far = 100; // the far clipping plane
Alfakhori's avatar
Alfakhori committed
28

Alfakhori's avatar
Alfakhori committed
29
const camera = new PerspectiveCamera(fov, aspect, near, far);
Alfakhori's avatar
Alfakhori committed
30

Alfakhori's avatar
Alfakhori committed
31
32
33
// every object is initially created at ( 0, 0, 0 )
// move the camera back so we can view the scene
camera.position.set(0, 0, 10);
Alfakhori's avatar
Alfakhori committed
34

Alfakhori's avatar
Alfakhori committed
35
36
// create a geometry
const geometry = new BoxBufferGeometry(2, 2, 2);
Alfakhori's avatar
Alfakhori committed
37

Alfakhori's avatar
Alfakhori committed
38
39
// create a default (white) Basic material
const material = new MeshBasicMaterial();
Alfakhori's avatar
Alfakhori committed
40

Alfakhori's avatar
Alfakhori committed
41
42
// create a Mesh containing the geometry and material
const cube = new Mesh(geometry, material);
Alfakhori's avatar
Alfakhori committed
43

Alfakhori's avatar
Alfakhori committed
44
45
// add the mesh to the scene
scene.add(cube);
Alfakhori's avatar
Alfakhori committed
46

Alfakhori's avatar
Alfakhori committed
47
48
// create the renderer
const renderer = new WebGLRenderer();
Alfakhori's avatar
Alfakhori committed
49

Alfakhori's avatar
Alfakhori committed
50
51
// next, set the renderer to the same size as our container element
renderer.setSize(container.clientWidth, container.clientHeight);
Alfakhori's avatar
Alfakhori committed
52

Alfakhori's avatar
Alfakhori committed
53
54
// finally, set the pixel ratio so that our scene will look good on HiDPI displays
renderer.setPixelRatio(window.devicePixelRatio);
Alfakhori's avatar
Alfakhori committed
55

Alfakhori's avatar
Alfakhori committed
56
57
// add the automatically created <canvas> element to the page
container.append(renderer.domElement);
Alfakhori's avatar
Alfakhori committed
58

Alfakhori's avatar
Alfakhori committed
59
60
// render, or 'create a still image', of the scene
renderer.render(scene, camera);
Alfakhori's avatar
Alfakhori committed
61
62
63
64

</script>
</body>
</html>